Created
March 19, 2019 14:18
-
-
Save yuhan0/7a3c70fbd6d582eba804fea0a4960d45 to your computer and use it in GitHub Desktop.
rate limit evil-refresh-cursor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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