[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/which-key 3642c11 45/51: Speed up which-key--maybe-repl
From: |
Stefan Monnier |
Subject: |
[elpa] externals/which-key 3642c11 45/51: Speed up which-key--maybe-replace |
Date: |
Tue, 8 Sep 2020 10:26:21 -0400 (EDT) |
branch: externals/which-key
commit 3642c11d5ef9be3c6fb9edb8fd5ec3c370abd889
Author: Rudi Grinberg <me@rgrinberg.com>
Commit: Justin Burkett <justin@burkett.cc>
Speed up which-key--maybe-replace
The first optimization is avoiding the intermediate list created by
append. Instead we just iterate over the two lists in turn
The second opimitization occurs where
which-key-allow-multiple-replacements is nil, in which case we can stop
the iteration once we've found a replacement
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
---
which-key.el | 67 +++++++++++++++++++++++++++++++++++++-----------------------
1 file changed, 41 insertions(+), 26 deletions(-)
diff --git a/which-key.el b/which-key.el
index 475772f..143540f 100644
--- a/which-key.el
+++ b/which-key.el
@@ -1476,39 +1476,54 @@ local bindings coming first. Within these categories
order using
(eq pseudo-def real-def))
(cons (car key-binding) pseudo-desc))))
+(defsubst which-key--replace-in-binding (key-binding repl)
+ (cond ((or (not (consp repl)) (null (cdr repl)))
+ key-binding)
+ ((functionp (cdr repl))
+ (funcall (cdr repl) key-binding))
+ ((consp (cdr repl))
+ (cons
+ (cond ((and (caar repl) (cadr repl))
+ (replace-regexp-in-string
+ (caar repl) (cadr repl) (car key-binding) t))
+ ((cadr repl) (cadr repl))
+ (t (car key-binding)))
+ (cond ((and (cdar repl) (cddr repl))
+ (replace-regexp-in-string
+ (cdar repl) (cddr repl) (cdr key-binding) t))
+ ((cddr repl) (cddr repl))
+ (t (cdr key-binding)))))))
+
+(defun which-key--replace-in-repl-list-once (key-binding repls)
+ (cl-dolist (repl repls)
+ (when (which-key--match-replacement key-binding repl)
+ (cl-return (which-key--replace-in-binding key-binding repl)))))
+
+(defun which-key--replace-in-repl-list-many (key-binding repls)
+ (dolist (repl repls key-binding)
+ (when (which-key--match-replacement key-binding repl)
+ (setq key-binding (which-key--replace-in-binding key-binding repl)))))
+
(defun which-key--maybe-replace (key-binding &optional prefix)
"Use `which-key--replacement-alist' to maybe replace KEY-BINDING.
KEY-BINDING is a cons cell of the form \(KEY . BINDING\) each of
which are strings. KEY is of the form produced by `key-binding'."
(let* ((pseudo-binding (which-key--get-pseudo-binding key-binding prefix))
- one-match)
+ replaced-key-binding)
(if pseudo-binding
pseudo-binding
- (let* ((all-repls
- (append (cdr-safe (assq major-mode which-key-replacement-alist))
- which-key-replacement-alist)))
- (dolist (repl all-repls key-binding)
- (when (and (or which-key-allow-multiple-replacements
- (not one-match))
- (which-key--match-replacement key-binding repl))
- (setq one-match t)
- (setq key-binding
- (cond ((or (not (consp repl)) (null (cdr repl)))
- key-binding)
- ((functionp (cdr repl))
- (funcall (cdr repl) key-binding))
- ((consp (cdr repl))
- (cons
- (cond ((and (caar repl) (cadr repl))
- (replace-regexp-in-string
- (caar repl) (cadr repl) (car key-binding) t))
- ((cadr repl) (cadr repl))
- (t (car key-binding)))
- (cond ((and (cdar repl) (cddr repl))
- (replace-regexp-in-string
- (cdar repl) (cddr repl) (cdr key-binding) t))
- ((cddr repl) (cddr repl))
- (t (cdr key-binding)))))))))))))
+ (let* ((replacer (if which-key-allow-multiple-replacements
+ #'which-key--replace-in-repl-list-many
+ #'which-key--replace-in-repl-list-once)))
+ (setq replaced-key-binding
+ (apply replacer
+ (list key-binding
+ (cdr-safe (assq major-mode
which-key-replacement-alist)))))
+ ;; terminate early if we're only looking for one replacement and we
found it
+ (if (and replaced-key-binding (not
which-key-allow-multiple-replacements))
+ replaced-key-binding
+ (setq key-binding (or replaced-key-binding key-binding))
+ (or (apply replacer (list key-binding which-key-replacement-alist))
key-binding))))))
(defsubst which-key--current-key-list (&optional key-str)
(append (listify-key-sequence (which-key--current-prefix))
- [elpa] externals/which-key cf8572a 07/51: which-key-persistent-popup-p -> which-key-persistent-popup, (continued)
- [elpa] externals/which-key cf8572a 07/51: which-key-persistent-popup-p -> which-key-persistent-popup, Stefan Monnier, 2020/09/08
- [elpa] externals/which-key 8554a9f 17/51: Generalize C-h commands to use help-char, Stefan Monnier, 2020/09/08
- [elpa] externals/which-key 5cec113 10/51: Announce which-key-manual-update, Stefan Monnier, 2020/09/08
- [elpa] externals/which-key dcb9820 16/51: Default to imprecise window fitting on terminal, Stefan Monnier, 2020/09/08
- [elpa] externals/which-key e554326 49/51: Remove testing badge from README, Stefan Monnier, 2020/09/08
- [elpa] externals/which-key db3d003 21/51: Add full keymap versions of show-{major, minor}-mode, Stefan Monnier, 2020/09/08
- [elpa] externals/which-key 8b49ae9 42/51: Fix last commit, Stefan Monnier, 2020/09/08
- [elpa] externals/which-key b11227b 23/51: Strip out advice in command docstrings, Stefan Monnier, 2020/09/08
- [elpa] externals/which-key 9ff54ff 37/51: Fix short windows being resized, Stefan Monnier, 2020/09/08
- [elpa] externals/which-key 9550707 40/51: Merge remote-tracking branch 'hlissner/patch-1', Stefan Monnier, 2020/09/08
- [elpa] externals/which-key 3642c11 45/51: Speed up which-key--maybe-replace,
Stefan Monnier <=
- [elpa] externals/which-key da1abb4 27/51: Fix previous commit, Stefan Monnier, 2020/09/08
- [elpa] externals/which-key 2b10b8e 50/51: Remove use of focus-{in, out}-hook, Stefan Monnier, 2020/09/08
- [elpa] externals/which-key 9d1de2f 20/51: Refactor regexp & fix incompatibility + error, Stefan Monnier, 2020/09/08
- [elpa] externals/which-key c4b7aae 32/51: Revive Cask file, Stefan Monnier, 2020/09/08
- [elpa] externals/which-key f9d8d49 39/51: Fix prefix argument for which-key-show-major-mode (#239), Stefan Monnier, 2020/09/08
- [elpa] externals/which-key 8f2427a 43/51: Fix which-key--propertize-description use make-text-button return value (#249), Stefan Monnier, 2020/09/08
- [elpa] externals/which-key 90d10a8 33/51: Fix sorting of keys and add test, Stefan Monnier, 2020/09/08
- [elpa] externals/which-key 42a2505 14/51: Rework implementation of manual updating, Stefan Monnier, 2020/09/08
- [elpa] externals/which-key 0d0af8a 19/51: Strip out advice in command docstrings, Stefan Monnier, 2020/09/08
- [elpa] externals/which-key 190310d 30/51: Disable travis and cask, Stefan Monnier, 2020/09/08