[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/marginalia 59d65af 018/241: Add basic classifiers: orgi
From: |
Stefan Monnier |
Subject: |
[elpa] externals/marginalia 59d65af 018/241: Add basic classifiers: orginal category, symbols, by words in prompt |
Date: |
Fri, 28 May 2021 20:48:48 -0400 (EDT) |
branch: externals/marginalia
commit 59d65afcb4d8c898a6ddbf611103d6e74f14a782
Author: Omar Antolín <omar.antolin@gmail.com>
Commit: Omar Antolín <omar.antolin@gmail.com>
Add basic classifiers: orginal category, symbols, by words in prompt
---
marginalia.el | 84 +++++++++++++++++++++++++++++++++++------------------------
1 file changed, 50 insertions(+), 34 deletions(-)
diff --git a/marginalia.el b/marginalia.el
index f7695a1..1770403 100644
--- a/marginalia.el
+++ b/marginalia.el
@@ -30,6 +30,7 @@
;;; Code:
(require 'subr-x)
+(eval-when-compile (require 'cl-lib))
;;;; Customization
@@ -74,7 +75,10 @@ Annotations are only shown if `marginalia-mode' is enabled."
:group 'marginalia)
(defcustom marginalia-classifiers
- '(marginalia-classify-by-command-name)
+ '(marginalia-classify-by-command-name
+ marginalia-classify-original-category
+ marginalia-classify-by-prompt
+ marginalia-classify-symbol)
"List of functions to determine current completion category.
Each function should take no arguments and return a symbol
indicating the category, or nil to indicate it could not
@@ -82,31 +86,16 @@ determine it."
:type 'hook
:group 'marginalia)
-(defcustom marginalia-command-category-alist
- '((execute-extended-command . command)
- (customize-face . face)
- (customize-face-other-window . face)
- (customize-group . customize-group)
- (customize-group-other-window . customize-group)
- (customize-option . variable)
- (customize-option-other-window . variable)
- (customize-set-variable . variable)
- (customize-variable . variable)
- (customize-variable-other-window . variable)
- (describe-function . symbol)
- (describe-variable . variable)
- (describe-face . face)
- (describe-symbol . symbol)
- (helpful-callable . symbol)
- (helpful-command . symbol)
- (helpful-function . symbol)
- (helpful-macro . symbol)
- (helpful-symbol . symbol)
- (helpful-variable . variable)
- (describe-package . package)
- (package-install . package)
- (package-delete . package)
- (package-reinstall . package))
+(defcustom marginalia-prompt-categories
+ '((group . customize-group) (M-x . command) package face variable)
+ "Words whose presence in a minibuffer prompt determins the category.
+The words should be given either as a symbol which if found in
+the prompt is the category name, or as a dotted pair of symbols,
+the presence of the first indicating the second is the category."
+ :type '(repeat (choice symbol (cons symbol symbol)))
+ :group 'marginalia)
+
+(defcustom marginalia-command-category-alist nil
"Associate commands with a completion category."
:type '(alist :key-type symbol :value-type symbol)
:group 'marginalia)
@@ -124,6 +113,9 @@ determine it."
(defvar marginalia--this-command nil
"Last command symbol saved in order to allow annotations.")
+(defvar marginalia--original-category nil
+ "Original category reported by completion metadata.")
+
(defun marginalia--truncate (str width)
"Truncate string STR to WIDTH."
(truncate-string-to-width (car (split-string str "\n")) width 0 32 "…"))
@@ -214,19 +206,47 @@ determine it."
(and marginalia--this-command
(alist-get marginalia--this-command marginalia-command-category-alist)))
-(defun marginalia--completion-metadata-get (_metadata prop)
+(defun marginalia-classify-original-category ()
+ "Return original category reported by completion metadata."
+ marginalia--original-category)
+
+(defun marginalia-classify-symbol ()
+ "Determine if currently completing symbols."
+ (when-let ((mct minibuffer-completion-table))
+ (when (or (eq mct 'help--symbol-completion-table)
+ (obarrayp mct)
+ (and (consp mct) (symbolp (car mct))) ; assume list of symbols
+ ;; imenu from an Emacs Lisp buffer produces symbols
+ (and (eq marginalia--this-command 'imenu)
+ (with-current-buffer
+ (window-buffer (minibuffer-selected-window))
+ (derived-mode-p 'emacs-lisp-mode))))
+ 'symbol)))
+
+(defun marginalia-classify-by-prompt ()
+ "Determine category by a special word in prompt."
+ (when-let ((prompt (minibuffer-prompt)))
+ (cl-loop for spec in marginalia-prompt-categories
+ for (word . category) = (if (consp spec) spec (cons spec spec))
+ when (string-match-p (format "\\<%s\\>" word) prompt)
+ return category)))
+
+(defun marginalia--completion-metadata-get (metadata prop)
"Advice for `completion-metadata-get'.
Replaces the category and annotation function.
-FUN is the original function.
METADATA is the metadata.
PROP is the property which is looked up."
;; TODO add more category classifiers from Embark
(pcase prop
('annotation-function
- (when-let (cat (marginalia--category-type))
+ (when-let (cat (completion-metadata-get metadata 'category))
+ ;; we do want the advice triggered for completion-metadata-get
(alist-get cat marginalia-annotator-alist)))
('category
- (run-hook-with-args-until-success 'marginalia-classifiers))))
+ (let ((marginalia--original-category (alist-get 'category metadata)))
+ ;; using alist-get in the line above bypasses any advice on
+ ;; completion-metadata-get to avoid infinite recursion
+ (run-hook-with-args-until-success 'marginalia-classifiers)))))
(defun marginalia--minibuffer-setup ()
"Setup minibuffer for `marginalia-mode'.
@@ -240,10 +260,6 @@ Remember `this-command' for annotation."
minibuffer-completion-table
minibuffer-completion-predicate))
-(defun marginalia--category-type ()
- "Return minibuffer completion category per metadata."
- (completion-metadata-get (marginalia--metadata) 'category))
-
;;;###autoload
(define-minor-mode marginalia-mode
"Annotate completion candidates with richer information."
- [elpa] externals/marginalia 655f03b 035/241: introduce marginalia-annotators-(light|heavy), remove marginalia-set-command-annotator, (continued)
- [elpa] externals/marginalia 655f03b 035/241: introduce marginalia-annotators-(light|heavy), remove marginalia-set-command-annotator, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 4cb6d2b 039/241: marginalia-annotate-file: add modes, user and group, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia ed74ca9 040/241: add marginalia-toggle-annotators, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 57b1176 051/241: marginalia-annotate-command-binding: only store the binding in the hash table, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 6537c37 015/241: Shorten first line of docstring using our "annotator" term, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 69ec4d7 017/241: Remove unused function, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 28defd6 014/241: Introduce new name "annotator" for annotation functions, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia e2af1d3 019/241: Make marginalia-prompt-categories an alist of regexps and categories, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 1c8ef02 020/241: Remove unused marginalia--metadata function, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia c05face 042/241: Improve marginalia-toggle-annotators if invoked from minibuffer, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 59d65af 018/241: Add basic classifiers: orginal category, symbols, by words in prompt,
Stefan Monnier <=
- [elpa] externals/marginalia 0f1e763 025/241: rename some variables for consistency, update readme, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia cd95c65 028/241: buffer annotation: add read-only status, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 675e680 036/241: improve annotator defcustoms, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 8be2daf 046/241: marginalia-annotate-file: remove unnecessary offset which only costs space for no reason, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia c8c3f87 045/241: Rename toggle -> cycle, improve documenation, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 1269445 049/241: marginalia-annotate-command-binding: cache key binding, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 376ccc5 048/241: Revert "fix marginalia--full-candidate for selectrum", Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia ccefc57 052/241: marginalia-annotate-command-binding: add comment, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 597186d 065/241: add marginalia-annotate-minor-mode, Stefan Monnier, 2021/05/28
- [elpa] externals/marginalia 14dce57 062/241: scale field widths such that annotations look better on smaller windows, Stefan Monnier, 2021/05/28