Skip to content

Instantly share code, notes, and snippets.

@sonota88
Last active June 10, 2023 04:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonota88/1101202 to your computer and use it in GitHub Desktop.
Save sonota88/1101202 to your computer and use it in GitHub Desktop.
volatile-highlight.el
;; (c) 2011 sonota486 <yosiot8753@gmail.com>
;; license: GPL
;; ;; .emacs.el example
;; (require 'volatile-highlight)
;; (set-face-background 'volatile-highlight-face "#ffff00") ; optional
(copy-face 'highlight 'volatile-highlight-face)
(defvar volatile-highlight:timer nil)
(defvar volatile-highlight:default-duration-sec 0.1)
;; オーバーレイ管理用
;; expire-time: オーバーレイが消えるべき時刻、unix time
;; ((expire-time1 . overlay1)
;; (expire-time2 . overlay2)
;; ;; ...
;; )
(defvar volatile-highlight:overlay-list '())
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun volatile-highlight:add-to-list (overlay duration-sec)
(setq volatile-highlight:overlay-list
(cons (cons (+ (time-to-seconds (current-time)) duration-sec)
overlay)
volatile-highlight:overlay-list)))
(defun volatile-highlight:remove-from-list (pair-to-remove)
(setq volatile-highlight:overlay-list
(seq-remove (lambda (pair)
(equal (car pair)
(car pair-to-remove)))
volatile-highlight:overlay-list)))
(defun volatile-highlight:delete-overlay (pair-to-remove)
(let (overlay)
(setq overlay (cdr pair-to-remove))
;; オーバーレイを削除
(delete-overlay overlay)
(setq overlay nil)
;; リストから除去
(volatile-highlight:remove-from-list pair-to-remove)))
(defun volatile-highlight:expired-p (pair)
(let ((now (time-to-seconds (current-time))))
(<= (car pair) now)))
(defun volatile-highlight:delete-expired ()
(let (now
expired-pairs)
(setq expired-pairs
(seq-filter
(lambda (pair)
(volatile-highlight:expired-p pair))
volatile-highlight:overlay-list))
(dolist (pair expired-pairs)
(volatile-highlight:delete-overlay pair))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun volatile-highlight (beg end &optional duration-sec)
(let (overlay)
(setq duration-sec
(or duration-sec
volatile-highlight:default-duration-sec))
(setq overlay
(make-overlay beg end))
(overlay-put overlay
'font-lock-face 'volatile-highlight-face)
;; リストに追加
(volatile-highlight:add-to-list overlay duration-sec)
(run-with-timer duration-sec
nil
'volatile-highlight:delete-expired)))
(provide 'volatile-highlight)
;; for test
;; (setq volatile-highlight:overlay-list '())
;; (progn (volatile-highlight 10 20 2.5)
;; (sit-for 0.51) (volatile-highlight 30 40 1.5)
;; (sit-for 0.51) (volatile-highlight 50 60 0.5)
;; )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment