mirror of
https://github.com/magnars/multiple-cursors.el.git
synced 2026-02-04 14:22:24 +00:00
Support commands with multiple read-chars
This change fixes commands that read-chars multiple times. Previously, two stage commands like embrace-change would read the same char twice immediately and avy-goto-char-timer would never stop reading input as a cached value was always provided during the timer. Instead, the read-char prompt is included in the cache key so that multiple different calls are cached separately and accessible by the fake cursors.
This commit is contained in:
@@ -28,8 +28,6 @@
|
||||
(require 'cl-lib)
|
||||
(require 'rect)
|
||||
|
||||
(defvar mc--read-char)
|
||||
|
||||
(defface mc/cursor-face
|
||||
'((t (:inverse-video t)))
|
||||
"The face used for fake cursors"
|
||||
@@ -52,11 +50,6 @@ rendered or shift text."
|
||||
:type '(boolean)
|
||||
:group 'multiple-cursors)
|
||||
|
||||
(defcustom mc--reset-read-variables '()
|
||||
"A list of cache variable names to reset by multiple-cursors."
|
||||
:type '(list symbol)
|
||||
:group 'multiple-cursors)
|
||||
|
||||
(defface mc/region-face
|
||||
'((t :inherit region))
|
||||
"The face used for fake regions"
|
||||
@@ -326,34 +319,35 @@ cursor with updated info."
|
||||
;; Intercept some reading commands so you won't have to
|
||||
;; answer them for every single cursor
|
||||
|
||||
(defvar multiple-cursors-mode nil)
|
||||
(defvar mc--input-function-cache nil)
|
||||
|
||||
(defun mc--reset-read-prompts ()
|
||||
(mapc (lambda (var) (set var nil))
|
||||
mc--reset-read-variables))
|
||||
(setq mc--input-function-cache nil))
|
||||
|
||||
(defmacro mc--cache-input-function (fn-name)
|
||||
(defmacro mc--cache-input-function (fn-name args-cache-key-fn)
|
||||
"Advise FN-NAME to cache its value in a private variable. Cache
|
||||
is to be used by mc/execute-command-for-all-fake-cursors and
|
||||
caches will be reset by mc--reset-read-prompts."
|
||||
caches will be reset by mc--reset-read-prompts. ARGS-CACHE-KEY-FN
|
||||
should transform FN-NAME's args to a unique cache-key so that
|
||||
different calls to FN-NAME during a command can return multiple
|
||||
values."
|
||||
(let ((mc-name (intern (concat "mc--" (symbol-name fn-name)))))
|
||||
`(progn
|
||||
(defvar ,mc-name nil)
|
||||
(defun ,mc-name (orig-fun &rest args)
|
||||
(if (not multiple-cursors-mode)
|
||||
(apply orig-fun args)
|
||||
(unless ,mc-name
|
||||
(setq ,mc-name (apply orig-fun args)))
|
||||
,mc-name))
|
||||
(advice-add ',fn-name :around #',mc-name)
|
||||
(add-to-list 'mc--reset-read-variables ',mc-name))))
|
||||
(let* ((cache-key (cons ,(symbol-name fn-name) (,args-cache-key-fn args)))
|
||||
(cached-value (assoc cache-key mc--input-function-cache))
|
||||
(return-value (if cached-value (cdr cached-value) (apply orig-fun args))))
|
||||
(unless cached-value
|
||||
(push (cons cache-key return-value) mc--input-function-cache))
|
||||
return-value)))
|
||||
(advice-add ',fn-name :around #',mc-name))))
|
||||
|
||||
(mc--cache-input-function read-char)
|
||||
(mc--cache-input-function read-quoted-char)
|
||||
(mc--cache-input-function register-read-with-preview) ; used by insert-register
|
||||
(mc--cache-input-function read-char-from-minibuffer) ; used by zap-to-char
|
||||
|
||||
(mc--reset-read-prompts)
|
||||
(mc--cache-input-function read-char car)
|
||||
(mc--cache-input-function read-quoted-char car)
|
||||
(mc--cache-input-function register-read-with-preview car) ; used by insert-register
|
||||
(mc--cache-input-function read-char-from-minibuffer car) ; used by zap-to-char
|
||||
|
||||
(defun mc/fake-cursor-p (o)
|
||||
"Predicate to check if an overlay is a fake cursor"
|
||||
|
||||
Reference in New Issue
Block a user