[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/marginalia cf9f411 125/241: marginalia: always run anno
From: |
Stefan Monnier |
Subject: |
[elpa] externals/marginalia cf9f411 125/241: marginalia: always run annotators in the original window context |
Date: |
Fri, 28 May 2021 20:49:11 -0400 (EDT) |
branch: externals/marginalia
commit cf9f411fe71ce2aa0b23fc758bdd002435142885
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>
marginalia: always run annotators in the original window context
---
marginalia.el | 97 ++++++++++++++++++++++++++++++++---------------------------
1 file changed, 53 insertions(+), 44 deletions(-)
diff --git a/marginalia.el b/marginalia.el
index 6751c4c..a937567 100644
--- a/marginalia.el
+++ b/marginalia.el
@@ -303,19 +303,18 @@ This hash table is needed to speed up
`marginalia-annotate-binding'.")
(defun marginalia-annotate-binding (cand)
"Annotate command CAND with keybinding."
- (with-current-buffer (window-buffer (minibuffer-selected-window))
- ;; Precomputing the keybinding of every command is faster than looking it
up every time using
- ;; `where-is-internal'. `where-is-internal' generates a lot of garbage,
leading to garbage
- ;; collecting pauses when interacting with the minibuffer. See
- ;; https://github.com/minad/marginalia/issues/16.
- (unless marginalia--annotate-binding-hash
- (setq marginalia--annotate-binding-hash (make-hash-table :size 1025))
- (mapatoms (lambda (sym)
- (when-let (key (and (commandp sym) (where-is-internal sym
nil t)))
- (puthash sym key marginalia--annotate-binding-hash)))))
- (when-let* ((sym (intern-soft cand))
- (binding (gethash sym marginalia--annotate-binding-hash)))
- (propertize (format " (%s)" (key-description binding)) 'face
'marginalia-key))))
+ ;; Precomputing the keybinding of every command is faster than looking it up
every time using
+ ;; `where-is-internal'. `where-is-internal' generates a lot of garbage,
leading to garbage
+ ;; collecting pauses when interacting with the minibuffer. See
+ ;; https://github.com/minad/marginalia/issues/16.
+ (unless marginalia--annotate-binding-hash
+ (setq marginalia--annotate-binding-hash (make-hash-table :size 1025))
+ (mapatoms (lambda (sym)
+ (when-let (key (and (commandp sym) (where-is-internal sym nil
t)))
+ (puthash sym key marginalia--annotate-binding-hash)))))
+ (when-let* ((sym (intern-soft cand))
+ (binding (gethash sym marginalia--annotate-binding-hash)))
+ (propertize (format " (%s)" (key-description binding)) 'face
'marginalia-key)))
;; This annotator is consult-specific, it will annotate the `consult-buffer'
command.
(defun marginalia-annotate-virtual-buffer-class (cand)
@@ -423,9 +422,7 @@ Similar to `marginalia-annotate-symbol', but does not show
symbol class."
(defun marginalia-annotate-imenu (cand)
"Annotate imenu CAND with its documentation string."
- (when (provided-mode-derived-p (buffer-local-value 'major-mode
- (window-buffer
(minibuffer-selected-window)))
- 'emacs-lisp-mode)
+ (when (derived-mode-p 'emacs-lisp-mode)
(marginalia-annotate-symbol (replace-regexp-in-string "^.* " "" cand))))
(defun marginalia-annotate-variable (cand)
@@ -469,24 +466,22 @@ Similar to `marginalia-annotate-symbol', but does not
show symbol class."
(defun marginalia-annotate-minor-mode (cand)
"Annotate minor-mode CAND with status and documentation string."
- ;; `with-selected-window' is necessary because of
`lookup-minor-mode-from-indicator'
- (with-selected-window (minibuffer-selected-window)
- (let* ((cand (replace-regexp-in-string "^\\(.[[:nonascii:]] \\)+" ""
cand)) ;; Strip consult narrowing prefix
- (sym (intern-soft cand))
- (mode (if (and sym (boundp sym))
- sym
- (lookup-minor-mode-from-indicator cand)))
- (lighter (cdr (assq mode minor-mode-alist)))
- (lighter-str (and lighter (string-trim (format-mode-line (cons t
lighter))))))
- (concat
- (marginalia--fields
- ((if (and (boundp mode) (symbol-value mode))
- (propertize "On" 'face 'marginalia-on)
- (propertize "Off" 'face 'marginalia-off)) :width 3)
- ((if (local-variable-if-set-p mode) "Local" "Global") :width 6 :face
'marginalia-modified)
- (lighter-str :width 14 :face 'marginalia-lighter)
- ((marginalia--function-doc mode)
- :truncate marginalia-truncate-width :face
'marginalia-documentation))))))
+ (let* ((cand (replace-regexp-in-string "^\\(.[[:nonascii:]] \\)+" "" cand))
;; Strip consult narrowing prefix
+ (sym (intern-soft cand))
+ (mode (if (and sym (boundp sym))
+ sym
+ (lookup-minor-mode-from-indicator cand)))
+ (lighter (cdr (assq mode minor-mode-alist)))
+ (lighter-str (and lighter (string-trim (format-mode-line (cons t
lighter))))))
+ (concat
+ (marginalia--fields
+ ((if (and (boundp mode) (symbol-value mode))
+ (propertize "On" 'face 'marginalia-on)
+ (propertize "Off" 'face 'marginalia-off)) :width 3)
+ ((if (local-variable-if-set-p mode) "Local" "Global") :width 6 :face
'marginalia-modified)
+ (lighter-str :width 14 :face 'marginalia-lighter)
+ ((marginalia--function-doc mode)
+ :truncate marginalia-truncate-width :face 'marginalia-documentation)))))
(defun marginalia-annotate-package (cand)
"Annotate package CAND with its description summary."
@@ -622,6 +617,20 @@ looking for a regexp that matches the prompt."
when (string-match-p regexp prompt)
return category)))
+;; We generally run the annotators in the original window.
+;; `with-selected-window' is necessary because of
`lookup-minor-mode-from-indicator'.
+;; Otherwise it would probably suffice to only change the current buffer.
+(defmacro marginalia--context (&rest body)
+ "Setup annotator context around BODY."
+ (let ((w (make-symbol "w")))
+ `(let ((,w (window-width)))
+ (with-selected-window (minibuffer-selected-window)
+ (let ((marginalia-truncate-width (min (/ ,w 2)
marginalia-truncate-width))
+ (marginalia--separator (if (>= ,w
marginalia-separator-threshold) " " " "))
+ (marginalia--margin (when (>= ,w (+ marginalia-margin-min
marginalia-margin-threshold))
+ (make-string (- ,w
marginalia-margin-threshold) 32))))
+ ,@body)))))
+
(defun marginalia--completion-metadata-get (metadata prop)
"Meant as :before-until advice for `completion-metadata-get'.
METADATA is the metadata.
@@ -629,14 +638,19 @@ PROP is the property which is looked up."
(pcase prop
('annotation-function
;; we do want the advice triggered for completion-metadata-get
- (when-let (cat (completion-metadata-get metadata 'category))
- (alist-get cat (symbol-value (car marginalia-annotators)))))
+ (when-let* ((cat (completion-metadata-get metadata 'category))
+ (annotate (alist-get cat (symbol-value (car
marginalia-annotators)))))
+ (lambda (cand)
+ (marginalia--context
+ (funcall annotate cand)))))
('affixation-function
;; We do want the advice triggered for `completion-metadata-get'.
;; Return wrapper around `annotation-function'.
(when-let* ((cat (completion-metadata-get metadata 'category))
(annotate (alist-get cat (symbol-value (car
marginalia-annotators)))))
- (lambda (cands) (mapcar (lambda (x) (list x (funcall annotate x)))
cands))))
+ (lambda (cands)
+ (marginalia--context
+ (mapcar (lambda (x) (list x (funcall annotate x))) cands)))))
('category
;; using alist-get bypasses any advice on completion-metadata-get
;; to avoid infinite recursion
@@ -645,13 +659,8 @@ PROP is the property which is looked up."
(defun marginalia--minibuffer-setup ()
"Setup minibuffer for `marginalia-mode'.
-Remember `this-command' for annotation."
- (let ((w (window-width)))
- (setq-local marginalia-truncate-width (min (/ w 2)
marginalia-truncate-width))
- (setq-local marginalia--separator (if (>= w
marginalia-separator-threshold) " " " "))
- (setq-local marginalia--margin (when (>= w (+ marginalia-margin-min
marginalia-margin-threshold))
- (make-string (- w
marginalia-margin-threshold) 32)))
- (setq-local marginalia--this-command this-command)))
+Remember `this-command' for `marginalia-classify-by-command-name'."
+ (setq-local marginalia--this-command this-command))
;;;###autoload
(define-minor-mode marginalia-mode
- [elpa] externals/marginalia ed02f22 099/241: marginalia--align: only align text if non-empty, (continued)
- [elpa] externals/marginalia ed02f22 099/241: marginalia--align: only align text if non-empty, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 88faa59 089/241: marginalia-annotate-variable: ensure that control characters are escaped, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 333ff80 104/241: fix #32, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 531fb6e 107/241: fix #27, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia ef4fabf 106/241: add preliminary support for affixation-function, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 69332ba 110/241: fix hex code formatting, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia d0c4e41 111/241: marginalia--field: fix nil handling, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 7d9c6aa 117/241: binding hash: specify larger initial size, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia b69be18 119/241: marginalia-annotate-variable: add local indicator, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 5802a5d 118/241: readme: configuration example for selectrum refresh after marginalia-cycle, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia cf9f411 125/241: marginalia: always run annotators in the original window context,
Stefan Monnier <=
- [elpa] externals/marginalia c574a26 054/241: more colors!, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 2b85b73 130/241: fix marginalia--full-candidate, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 3ec73cd 138/241: Merry Christmas, MELPA!, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 5129917 150/241: fix readme, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 2f849d8 013/241: Simplify marginalia--completion-metadata-get, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia c97911b 021/241: All classifiers from Embark have been imported, remove TODO, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 0cb98a0 023/241: Remove default value from prompt before matching, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 7fe8f71 027/241: improve buffer annotation formatting, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 4ba9804 031/241: Fix bug: file annotations were only shown for current directory, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 78023eb 038/241: update readme, Stefan Monnier, 2021/05/28