dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

README.org (10052B)


      1 #+title: marginalia.el - Marginalia in the minibuffer
      2 #+author: Omar AntolĂ­n Camarena, Daniel Mendler
      3 #+language: en
      4 #+export_file_name: marginalia.texi
      5 #+texinfo_dir_category: Emacs
      6 #+texinfo_dir_title: Marginalia: (marginalia).
      7 #+texinfo_dir_desc: Marginalia in the minibuffer
      8 
      9 #+html: <a href="https://www.gnu.org/software/emacs/"><img alt="GNU Emacs" src="https://github.com/minad/corfu/blob/screenshots/emacs.svg?raw=true"/></a>
     10 #+html: <a href="http://elpa.gnu.org/packages/marginalia.html"><img alt="GNU ELPA" src="https://elpa.gnu.org/packages/marginalia.svg"/></a>
     11 #+html: <a href="http://elpa.gnu.org/devel/marginalia.html"><img alt="GNU-devel ELPA" src="https://elpa.gnu.org/devel/marginalia.svg"/></a>
     12 #+html: <a href="https://melpa.org/#/marginalia"><img alt="MELPA" src="https://melpa.org/packages/marginalia-badge.svg"/></a>
     13 #+html: <a href="https://stable.melpa.org/#/marginalia"><img alt="MELPA Stable" src="https://stable.melpa.org/packages/marginalia-badge.svg"/></a>
     14 
     15 * Introduction
     16 
     17 #+html: <img src="https://upload.wikimedia.org/wikipedia/commons/4/4f/Marginalia_%285095211566%29.jpg" align="right" width="30%">
     18 
     19 This package provides =marginalia-mode= which adds marginalia to the
     20 minibuffer completions.
     21 [[https://en.wikipedia.org/wiki/Marginalia][Marginalia]] are marks or
     22 annotations placed at the margin of the page of a book or in this case
     23 helpful colorful annotations placed at the margin of the minibuffer for
     24 your completion candidates. Marginalia can only add annotations to be
     25 displayed with the completion candidates. It cannot modify the
     26 appearance of the candidates themselves, which are shown as supplied by
     27 the original commands.
     28 
     29 The annotations are added based on the completion category. For example
     30 =find-file= reports the =file= category and =M-x= reports the =command= category. You
     31 can cycle between more or less detailed annotators or even disable the annotator
     32 with command =marginalia-cycle=.
     33 
     34 #+html: <img src="https://github.com/minad/marginalia/blob/main/marginalia-mode.png?raw=true">
     35 
     36 * Configuration
     37 
     38 It is recommended to use Marginalia together with either the [[https://github.com/raxod502/selectrum][Selectrum]], [[https://github.com/minad/vertico][Vertico]]
     39 or the [[https://github.com/oantolin/icomplete-vertical][Icomplete-vertical]] completion system. Furthermore Marginalia can be
     40 combined with [[https://github.com/oantolin/embark][Embark]] for action support and [[https://github.com/minad/consult][Consult]], which provides many useful
     41 commands.
     42 
     43 #+begin_src emacs-lisp
     44 ;; Enable richer annotations using the Marginalia package
     45 (use-package marginalia
     46   ;; Either bind `marginalia-cycle` globally or only in the minibuffer
     47   :bind (("M-A" . marginalia-cycle)
     48          :map minibuffer-local-map
     49          ("M-A" . marginalia-cycle))
     50 
     51   ;; The :init configuration is always executed (Not lazy!)
     52   :init
     53 
     54   ;; Must be in the :init section of use-package such that the mode gets
     55   ;; enabled right away. Note that this forces loading the package.
     56   (marginalia-mode))
     57 #+end_src
     58 
     59 * Information shown by the annotators
     60 
     61 In general, to learn more about what different annotations mean, a good starting
     62 point is to look at ~marginalia-annotator-registry~, and follow up to the
     63 annotation function of the category you are interested in.
     64 
     65 For example the annotations for elisp symbols include their symbol class - v for
     66 variable, f for function, c for command, etc. For more information on what the
     67 different classifications mean, see the docstring of ~marginalia--symbol-class~.
     68 
     69 * Adding custom annotators or classifiers
     70 
     71 *IMPORTANT NOTICE FOR PACKAGE AUTHORS*: The intention of the Marginalia package is
     72 to give the user means to overwrite completion categories and to add custom
     73 annotators for existing commands in their user configuration. Marginalia is a
     74 user facing package and is not intended to be used as a library. Therefore
     75 Marginalia does not expose library functions as part of its public API. If you
     76 add your own completion commands to your package we recommend to specify an
     77 =annotation-function= or an =affixation-function=, avoiding the Marginalia
     78 dependency this way. The =annotation-function= is documented in the [[https://www.gnu.org/software/emacs/manual/html_node/elisp/Completion.html][Elisp manual]].
     79 There is an exception to our recommendation: If you want to implement
     80 annotations for an existing package =hypothetical.el=, which does not have
     81 annotations and where annotations cannot be added, then the creation of a
     82 =marginalia-hypothetical.el= package is a good idea, since Marginalia provides the
     83 facilities to enhance existing commands from the outside. If you have questions
     84 feel free to ask on the Marginalia issue tracker!
     85 
     86 Commands that support minibuffer completion use a completion table of all the
     87 available candidates. Candidates are associated with a *category* such as =command=,
     88 =file=, =face=, or =variable= depending on what the candidates are. Based on the
     89 category of the candidates, Marginalia selects an *annotator* to generate
     90 annotations for display for each candidate.
     91 
     92 Unfortunately, not all commands (including Emacs' builtin ones) specify the
     93 category of their candidates. To compensate for this shortcoming, Marginalia
     94 hooks into the emacs completion framework and runs the *classifiers* listed in the
     95 variable =marginalia-classifiers=, which use the command's prompt or other
     96 properties of the candidates to specify the completion category.
     97 
     98 For example, the =marginalia-classify-by-prompt= classifier checks the minibuffer
     99 prompt against regexps listed in the =marginalia-prompt-categories= alist to
    100 determine a category. The following is already included but would be a way to
    101 assign the category =face= to all candidates from commands with prompts that
    102 include the word "face".
    103 
    104 #+begin_src emacs-lisp
    105   (add-to-list 'marginalia-prompt-categories '("\\<face\\>" . face))
    106 #+end_src
    107 
    108 The =marginalia-classify-by-command-name= classifier uses the alist
    109 =marginalia-command-categories= to specify the completion category based on the
    110 command name. This is particularily useful if the prompt classifier yields a
    111 false positive.
    112 
    113 Completion categories are also important for [[https://github.com/oantolin/embark][Embark]], which associates actions
    114 based on the completion category and benefits from Marginalia's classifiers.
    115 
    116 Once the category of the candidates is known, Marginalia looks in the
    117 =marginalia-annotator-registry= to find the associated annotator to use. An
    118 annotator is a function that takes a completion candidate string as an argument
    119 and returns an annotation string to be displayed after the candidate in the
    120 minibuffer. More than one annotator can be assigned to each each category,
    121 displaying more, less or different information. Use the =marginalia-cycle= command
    122 to cycle between the annotations of different annotators defined for the current
    123 category.
    124 
    125 Here's an example of a basic face annotator:
    126 
    127 #+begin_src emacs-lisp
    128   (defun my-face-annotator (cand)
    129     (when-let (sym (intern-soft cand))
    130       (concat (propertize " " 'display '(space :align-to center))
    131               (propertize "The quick brown fox jumps over the lazy dog" 'face sym))))
    132 #+end_src
    133 
    134 Look at Marginalia's various annotators for examples of formating annotations.
    135 In particular, the helper function =marginalia--fields= can be used to format
    136 information into columns.
    137 
    138 After defining a new annotator, associate it with a category in the annotator
    139 registry as follows:
    140 
    141 #+begin_src emacs-lisp
    142   (add-to-list 'marginalia-annotator-registry
    143                '(face my-face-annotator marginalia-annotate-face builtin none))
    144 #+end_src
    145 
    146 This makes the =my-face-annotator= the first of four annotators for the face
    147 category. The others are the annotator provided by Marginalia
    148 (=marginalia-annotate-face=), the =builtin= annotator as defined by Emacs and the
    149 =none= annotator, which disables the annotations. With this setting, after
    150 invoking =M-x describe-face RET= you can cycle between all of these annotators
    151 using =marginalia-cycle=.
    152 
    153 * Disabling annotators, builtin or lightweight annotators
    154 
    155 Marginalia activates rich annotators by default. Depending on your preference
    156 you may want to use the builtin annotators or even no annotators by default and
    157 only activate the annotators on demand by invoking ~marginalia-cycle~.
    158 
    159 In order to use the builtin annotators by default, you can use the following
    160 command. Replace =builtin= by =none= to disable annotators by default.
    161 
    162 #+begin_src emacs-lisp
    163   (defun marginalia-use-builtin ()
    164     (interactive)
    165     (mapc
    166      (lambda (x)
    167        (setcdr x (cons 'builtin (remq 'builtin (cdr x)))))
    168      marginalia-annotator-registry))
    169 #+end_src
    170 
    171 If a completion category supports two annotators, you can toggle between
    172 those using this command.
    173 
    174 #+begin_src emacs-lisp
    175   (defun marginalia-toggle ()
    176     (interactive)
    177     (mapc
    178      (lambda (x)
    179        (setcdr x (append (reverse (remq 'none
    180                                         (remq 'builtin (cdr x))))
    181                          '(builtin none))))
    182      marginalia-annotator-registry))
    183 #+end_src
    184 
    185 After cycling the annotators you may want to automatically save the
    186 configuration. This can be achieved using an advice which calls
    187 ~customize-save-variable~.
    188 
    189 #+begin_src emacs-lisp
    190   (advice-add #'marginalia-cycle :after
    191               (lambda ()
    192                 (let ((inhibit-message t))
    193                   (customize-save-variable 'marginalia-annotator-registry
    194                                            marginalia-annotator-registry))))
    195 #+end_src
    196 
    197 In order to disable an annotator permanently, the ~marginalia-annotator-registry~
    198 can be modified. For example if you prefer to never see file annotations, you
    199 can delete all file annotators from the registry.
    200 
    201 #+begin_src emacs-lisp
    202   (setq marginalia-annotator-registry
    203         (assq-delete-all 'file marginalia-annotator-registry))
    204 #+end_src
    205 
    206 * Contributions
    207 
    208 Since this package is part of [[http://elpa.gnu.org/packages/marginalia.html][GNU ELPA]] contributions require a copyright
    209 assignment to the FSF.