Skip to content

Instantly share code, notes, and snippets.

@yuhan0
Created March 19, 2019 14:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yuhan0/7a3c70fbd6d582eba804fea0a4960d45 to your computer and use it in GitHub Desktop.
Save yuhan0/7a3c70fbd6d582eba804fea0a4960d45 to your computer and use it in GitHub Desktop.
rate limit evil-refresh-cursor
(defvar evil--refresh-cursor-last-executed 0
"Unix time of when the evil cursor was last refreshed")
(defvar evil-refresh-cursor-delay 0.1
"Max delay in seconds between each cursor refresh")
(defvar evil--refresh-cursor-current-timer nil
"Current timer keeping track of cursor refresh changes")
(defvar evil--refresh-cursor-delayed-specs nil
"Cons pair of cursor (state . buffer) to set after the delay")
(defun evil--delayed-refresh-cursor ()
(evil-refresh-cursor
(car evil--refresh-cursor-delayed-specs)
(cdr evil--refresh-cursor-delayed-specs))
(setq evil--refresh-cursor-current-timer nil))
;; overrides original
(defun evil-refresh-cursor (&optional state buffer)
"Refresh the cursor for STATE in BUFFER.
BUFFER defaults to the current buffer. If STATE is nil the
cursor type is either `evil-force-cursor' or the current state.
Since this function can be called very frequently, rate limit it to only run
every `evil-refresh-cursor-delay' seconds. "
(when (and (boundp 'evil-local-mode) evil-local-mode)
(if (> (- (float-time)
;; (nth 0 evil--refresh-cursor-delayed-specs)
evil--refresh-cursor-last-executed)
evil-refresh-cursor-delay)
(let* ((state (or state evil-force-cursor evil-state 'normal))
(default (or evil-default-cursor t))
(cursor (evil-state-property state :cursor t))
(color (or (and (stringp cursor) cursor)
(and (listp cursor)
(evil-member-if #'stringp cursor))
(frame-parameter nil 'cursor-color))))
(with-current-buffer (or buffer (current-buffer))
;; if both STATE and `evil-default-cursor'
;; specify a color, don't set it twice
(when (and color (listp default))
(setq default (evil-filter-list #'stringp default)))
(evil-set-cursor default)
(evil-set-cursor cursor))
(setq evil--refresh-cursor-last-executed (float-time)))
;; delay refreshing till later
(setq evil--refresh-cursor-delayed-specs (cons state buffer))
(unless (timerp evil--refresh-cursor-current-timer)
(setq evil--refresh-cursor-current-timer
(run-at-time evil-refresh-cursor-delay
nil #'evil--delayed-refresh-cursor))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment