├── .elpaignore ├── .github ├── FUNDING.yml └── ISSUE_TEMPLATE │ ├── bug_report.md │ └── config.yml ├── .gitignore ├── CHANGELOG.org ├── LICENSE ├── README.org └── marginalia.el /.elpaignore: -------------------------------------------------------------------------------- 1 | LICENSE 2 | .elpaignore 3 | .github -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [minad, oantolin] 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 3 | name: 🐞 Bug report 4 | about: Report a bug. Do not use this for questions, support or feature requests. 5 | --- 6 | Thank you for reporting a bug. 7 | 8 | Please use the latest stable release of Emacs 30.1 and start with `emacs -Q` or 9 | `package-isolate` in order to only load a minimal set of packages. This way your 10 | Emacs configuration is not loaded. 11 | 12 | Please provide precise information and the exact steps to reproduce the issue. 13 | This is important to ensure that your problem can be reproduced on a different 14 | machine. 15 | 16 | If you are not really sure if your issue is a bug, please open a discussion 17 | instead. 18 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: "🙏 Please support @minad" 4 | url: https://github.com/sponsors/oantolin 5 | about: See https://github.com/sponsors/minad for sponsoring information. 6 | - name: "🙏 Please support @oantolin" 7 | url: https://github.com/sponsors/oantolin 8 | about: See https://github.com/sponsors/oantolin for sponsoring information. 9 | - name: "💡 Suggest a feature ➞ Please create a discussion" 10 | url: https://github.com/minad/marginalia/discussions/categories/ideas 11 | about: Start a new discussion suggesting an improvement or a feature. 12 | - name: "🧑‍🤝‍🧑 Ask the community for support" 13 | url: https://www.reddit.com/r/emacs 14 | about: Please be kind and support others. 15 | - name: "🤓 Ask the maintainer for support ➞ Please create a discussion" 16 | url: https://github.com/minad/marginalia/discussions/categories/q-a 17 | about: Please keep in mind that our bandwidth is limited. 18 | - name: "🔍 Search through old issues or discussions" 19 | url: https://github.com/search?q=repo%3Aminad%2Fmarginalia&type=issues 20 | about: The same question may have been asked before. 21 | # - name: "📝 Marginalia wiki" 22 | # url: https://github.com/minad/marginalia/wiki 23 | # about: Additional configuration tips are covered there. Feel free to edit! 24 | - name: "📖 Marginalia manual" 25 | url: https://github.com/minad/marginalia/blob/main/README.org 26 | about: The manual covers the basic setup and workflow. 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *-autoloads.el 2 | *-pkg.el 3 | *.elc 4 | *.info 5 | *.texi 6 | *~ 7 | \#*\# 8 | /README-elpa 9 | -------------------------------------------------------------------------------- /CHANGELOG.org: -------------------------------------------------------------------------------- 1 | #+title: marginalia.el - Changelog 2 | #+author: Omar Antolín Camarena, Daniel Mendler 3 | #+language: en 4 | 5 | * Version 2.1 (2025-06-04) 6 | 7 | - Rename =marginalia-annotator-registry= to =marginalia-annotators= for consistency 8 | with =marginalia-classifiers=. 9 | - Add context menu in minibuffer. Enable =context-menu-mode= to try. 10 | 11 | * Version 2.0 (2025-03-17) 12 | 13 | - =marginalia--variable-value=: Add tool tip for integers with hexadecimal, octal 14 | and character representation. 15 | 16 | * Version 1.8 (2024-12-22) 17 | 18 | - Require Emacs 28.1. 19 | - Add G symbol class for customization groups. 20 | 21 | * Version 1.7 (2024-07-26) 22 | 23 | - Bump Compat dependency to Compat 30. 24 | - Advise both ~completion-metadata-get~ and ~(compat-function 25 | completion-metadata-get)~ in order to provide completion categories and 26 | annotation functions. 27 | 28 | * Version 1.6 (2024-04-04) 29 | 30 | - ~marginalia-annotate-buffer~: Handle dead buffers, which can occur when 31 | annotating the candidates of ~consult-buffer~, which maintains the actual buffer 32 | objects during completion. 33 | 34 | * Version 1.5 (2023-12-27) 35 | 36 | - ~marginalia-annotate-bookmark~: Show location and more context. 37 | - ~marginalia-annotate-tab~: Show tab index starting from one. 38 | 39 | * Version 1.4 (2023-12-01) 40 | 41 | - =marginalia-annotate-theme=: New annotator based on =marginalia-annotate-library=. 42 | - =marginalia-remote-file-regexps=: New customization variable set to a list of 43 | regexps matching remote paths, which should be excluded from file annotations. 44 | 45 | * Version 1.3 (2023-07-02) 46 | 47 | - =marginalia-classify-by-prompt=: Use case-insensitive matching. 48 | - =marginalia-annotate-symbol=: Additional symbol classes. Use =M= for module 49 | functions, =P= for primitives and =S= for special forms. 50 | - =marginalia-annotate-symbol=: Add =symbol-file= column. 51 | - =marginalia-cycle=: Add =completion-predicate= to display command only in 52 | recursive minibuffers. 53 | 54 | * Version 1.2 (2023-04-17) 55 | 56 | - =marginalia-classify-by-command-name=: Resolve function aliases and use the name 57 | of the original command to determine the completion category. 58 | 59 | * Version 1.1 (2023-02-17) 60 | 61 | - Require the =compat= library. 62 | - Fix =marginalia-classify-by-prompt= such that it handles multiple brackets in 63 | the prompt gracefully. 64 | - Add =help-echo= properties to truncated annotations. The full string is shown on 65 | mouse hover. 66 | - Add =help-echo= to the symbol classes of =marginalia-annotate-symbol=. 67 | - Add =help-echo= to file sizes showing the exact size in bytes. 68 | - Add =help-echo= to file dates showing the exact date. 69 | 70 | * Version 1.0 (2022-12-22) 71 | 72 | - Start of changelog. 73 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 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 misc features 6 | #+texinfo_dir_title: Marginalia: (marginalia). 7 | #+texinfo_dir_desc: Marginalia in the minibuffer 8 | 9 | #+html: GNU Emacs 10 | #+html: GNU ELPA 11 | #+html: GNU-devel ELPA 12 | #+html: MELPA 13 | #+html: MELPA Stable 14 | 15 | #+html: 16 | 17 | This package provides =marginalia-mode= which adds marginalia to the minibuffer 18 | completions. [[https://en.wikipedia.org/wiki/Marginalia][Marginalia]] are marks or annotations placed at the margin of the 19 | page of a book or in this case helpful colorful annotations placed at the margin 20 | of the minibuffer for your completion candidates. Marginalia can only add 21 | annotations to the completion candidates. It cannot modify the appearance of the 22 | candidates themselves, which are shown unaltered as supplied by the original 23 | command. 24 | 25 | The annotations are added based on the completion category. For example 26 | =find-file= reports the =file= category and =M-x= reports the =command= category. You 27 | can cycle between more or less detailed annotators or even disable the annotator 28 | with command =marginalia-cycle=. 29 | 30 | #+html: 31 | 32 | #+toc: headlines 8 33 | 34 | * Configuration 35 | 36 | It is recommended to use Marginalia together with either the [[https://github.com/minad/vertico][Vertico]], [[https://github.com/protesilaos/mct][Mct]], 37 | [[https://www.gnu.org/software/emacs/manual/html_node/emacs/Icomplete.html][Icomplete]] or the default completion UI. Furthermore Marginalia can be combined 38 | with [[https://github.com/oantolin/embark][Embark]] for action support and [[https://github.com/minad/consult][Consult]], which provides many useful commands. 39 | 40 | #+begin_src emacs-lisp 41 | ;; Enable rich annotations using the Marginalia package 42 | (use-package marginalia 43 | ;; Bind `marginalia-cycle' locally in the minibuffer. To make the binding 44 | ;; available in the *Completions* buffer, add it to the 45 | ;; `completion-list-mode-map'. 46 | :bind (:map minibuffer-local-map 47 | ("M-A" . marginalia-cycle)) 48 | 49 | ;; The :init section is always executed. 50 | :init 51 | 52 | ;; Marginalia must be activated in the :init section of use-package such that 53 | ;; the mode gets enabled right away. Note that this forces loading the 54 | ;; package. 55 | (marginalia-mode)) 56 | #+end_src 57 | 58 | * Information shown by the annotators 59 | 60 | In general, to learn more about what different annotations mean, a good starting 61 | point is to look at ~marginalia-annotators~, and follow up to the annotation 62 | function of the category you are interested in. 63 | 64 | For example the annotations for Elisp symbols include their symbol class - =v= for 65 | variable, =f= for function, =c= for command, etc. For more information on what the 66 | different classifications mean, see the docstring of ~marginalia--symbol-class~. 67 | 68 | * Adding custom annotators or classifiers 69 | 70 | *IMPORTANT NOTICE FOR PACKAGE AUTHORS*: The intention of the Marginalia package is 71 | to give the user means to overwrite completion categories and to add custom 72 | annotators for existing commands in their user configuration. *Marginalia is a 73 | user facing package and is not intended to be used as a library*. Therefore 74 | Marginalia does not expose library functions as part of its public API. If you 75 | add your own completion commands to your package we recommend to specify an 76 | =annotation-function= or an =affixation-function=, avoiding the Marginalia 77 | dependency this way. The =annotation-function= and =affixation-function= are 78 | documented in the [[https://www.gnu.org/software/emacs/manual/html_node/elisp/Completion.html][Elisp manual]]. If you use =consult--read=, you can specify an 79 | =:annotate= keyword argument. 80 | 81 | There is an exception to our recommendation: If you want to implement 82 | annotations for an existing package =hypothetic.el=, which does not have 83 | annotations and where annotations cannot be added, then the creation of a 84 | =marginalia-hypothetic.el= package is a good idea, since Marginalia provides the 85 | facilities to enhance existing commands from the outside. 86 | 87 | Commands that support minibuffer completion use a completion table of all the 88 | available candidates. Candidates are associated with a *category* such as =command=, 89 | =file=, =face=, or =variable= depending on what the candidates are. Based on the 90 | category of the candidates, Marginalia selects an *annotator* to generate 91 | annotations for display for each candidate. 92 | 93 | Unfortunately, not all commands (including Emacs' builtin ones) specify the 94 | category of their candidates. To compensate for this shortcoming, Marginalia 95 | hooks into the Emacs completion framework and runs the *classifiers* listed in the 96 | variable =marginalia-classifiers=, which use the command's prompt or other 97 | properties of the candidates to specify the completion category. 98 | 99 | For example, the =marginalia-classify-by-prompt= classifier checks the minibuffer 100 | prompt against regexps listed in the =marginalia-prompt-categories= alist to 101 | determine a category. The following is already included but would be a way to 102 | assign the category =face= to all candidates from commands with prompts that 103 | include the word "face". 104 | 105 | #+begin_src emacs-lisp 106 | (add-to-list 'marginalia-prompt-categories '("\\" . face)) 107 | #+end_src 108 | 109 | The =marginalia-classify-by-command-name= classifier uses the alist 110 | =marginalia-command-categories= to specify the completion category based on the 111 | command name. This is particularly useful if the prompt classifier yields a 112 | false positive. 113 | 114 | Completion categories are also important for [[https://github.com/oantolin/embark][Embark]], which associates actions 115 | based on the completion category and benefits from Marginalia's classifiers. 116 | 117 | Once the category of the candidates is known, Marginalia looks in the 118 | =marginalia-annotators= to find the associated annotator to use. An annotator is a 119 | function that takes a completion candidate string as an argument and returns an 120 | annotation string to be displayed after the candidate in the minibuffer. More 121 | than one annotator can be assigned to each each category, displaying more, less 122 | or different information. Use the =marginalia-cycle= command to cycle between the 123 | annotations of different annotators defined for the current 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 | After defining a new annotator, associate it with a category in the annotator 135 | registry as follows: 136 | 137 | #+begin_src emacs-lisp 138 | (add-to-list 'marginalia-annotators 139 | '(face my-face-annotator marginalia-annotate-face builtin none)) 140 | #+end_src 141 | 142 | This makes the =my-face-annotator= the first of four annotators for the face 143 | category. The others are the annotator provided by Marginalia 144 | (=marginalia-annotate-face=), the =builtin= annotator as defined by Emacs and the 145 | =none= annotator, which disables the annotations. With this setting, after 146 | invoking =M-x describe-face RET= you can cycle between all of these annotators 147 | using =marginalia-cycle=. 148 | 149 | * Disabling annotators, builtin or lightweight annotators 150 | 151 | Marginalia activates rich annotators by default. Depending on your preference 152 | you may want to use the builtin annotators or even no annotators by default and 153 | only activate the annotators on demand by invoking ~marginalia-cycle~. 154 | 155 | In order to disable an annotator permanently, the ~marginalia-annotators~ can be 156 | modified. For example if you prefer to never see file annotations, you can 157 | delete all file annotators from the registry. 158 | 159 | #+begin_src emacs-lisp 160 | (setq marginalia-annotators 161 | (assq-delete-all 'file marginalia-annotators)) 162 | #+end_src 163 | 164 | To use the builtin annotators by default, you can run the following code. 165 | Replace =builtin= by =none= to disable annotators by default. 166 | 167 | #+begin_src emacs-lisp 168 | (mapc (lambda (x) 169 | (setcdr x (cons 'builtin (remq 'builtin (cdr x))))) 170 | marginalia-annotators) 171 | #+end_src 172 | 173 | As an alternative to ~marginalia-cycle~, if a completion category supports two 174 | annotators, you can toggle between them using the following command. 175 | 176 | #+begin_src emacs-lisp 177 | (defun marginalia-toggle () 178 | (interactive) 179 | (mapc 180 | (lambda (x) 181 | (setcdr x (append (reverse (remq 'none 182 | (remq 'builtin (cdr x)))) 183 | '(builtin none)))) 184 | marginalia-annotators)) 185 | #+end_src 186 | 187 | After cycling the annotators you may want to automatically save the 188 | configuration. This can be achieved using an advice which calls 189 | ~customize-save-variable~. 190 | 191 | #+begin_src emacs-lisp 192 | (advice-add #'marginalia-cycle :after 193 | (lambda () 194 | (let ((inhibit-message t)) 195 | (customize-save-variable 'marginalia-annotators 196 | marginalia-annotators)))) 197 | #+end_src 198 | 199 | * Icons in the minibuffer 200 | 201 | Marginalia focuses on text annotations. The [[https://github.com/rainstormstudio/nerd-icons-completion][nerd-icons-completion]] package is 202 | compatible with Marginalia and uses the special NerdFonts to add icons in front 203 | of minibuffer completion candidates. There exist related packages to enhance 204 | Dired, Ibuffer, Corfu and other modes with icons consistently. 205 | 206 | * Contributions 207 | 208 | Since this package is part of [[https://elpa.gnu.org/packages/marginalia.html][GNU ELPA]] contributions require a copyright 209 | assignment to the FSF. 210 | -------------------------------------------------------------------------------- /marginalia.el: -------------------------------------------------------------------------------- 1 | ;;; marginalia.el --- Enrich existing commands with completion annotations -*- lexical-binding: t -*- 2 | 3 | ;; Copyright (C) 2021-2025 Free Software Foundation, Inc. 4 | 5 | ;; Author: Omar Antolín Camarena , Daniel Mendler 6 | ;; Maintainer: Omar Antolín Camarena , Daniel Mendler 7 | ;; Created: 2020 8 | ;; Version: 2.1 9 | ;; Package-Requires: ((emacs "28.1") (compat "30")) 10 | ;; URL: https://github.com/minad/marginalia 11 | ;; Keywords: docs, help, matching, completion 12 | 13 | ;; This file is part of GNU Emacs. 14 | 15 | ;; This program is free software: you can redistribute it and/or modify 16 | ;; it under the terms of the GNU General Public License as published by 17 | ;; the Free Software Foundation, either version 3 of the License, or 18 | ;; (at your option) any later version. 19 | 20 | ;; This program is distributed in the hope that it will be useful, 21 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 22 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 | ;; GNU General Public License for more details. 24 | 25 | ;; You should have received a copy of the GNU General Public License 26 | ;; along with this program. If not, see . 27 | 28 | ;;; Commentary: 29 | 30 | ;; Enrich existing commands with completion annotations 31 | 32 | ;;; Code: 33 | 34 | (require 'compat) 35 | (eval-when-compile 36 | (require 'subr-x) 37 | (require 'cl-lib)) 38 | 39 | ;;;; Customization 40 | 41 | (defgroup marginalia nil 42 | "Enrich existing commands with completion annotations." 43 | :link '(info-link :tag "Info Manual" "(marginalia)") 44 | :link '(url-link :tag "Website" "https://github.com/minad/marginalia") 45 | :link '(emacs-library-link :tag "Library Source" "marginalia.el") 46 | :group 'help 47 | :group 'docs 48 | :group 'minibuffer 49 | :prefix "marginalia-") 50 | 51 | (defcustom marginalia-field-width 80 52 | "Maximum truncation width of annotation fields. 53 | 54 | This value is adjusted depending on the `window-width'." 55 | :type 'natnum) 56 | 57 | (defcustom marginalia-separator " " 58 | "Annotation field separator." 59 | :type 'string) 60 | 61 | (defcustom marginalia-align 'left 62 | "Alignment of the annotations." 63 | :type '(choice (const :tag "Left" left) 64 | (const :tag "Center" center) 65 | (const :tag "Right" right))) 66 | 67 | (defcustom marginalia-align-offset 0 68 | "Additional offset added to the alignment." 69 | :type 'natnum) 70 | 71 | (defcustom marginalia-max-relative-age (* 60 60 24 14) 72 | "Maximum relative age in seconds displayed by the file annotator. 73 | 74 | Set to `most-positive-fixnum' to always use a relative age, or 0 to never show 75 | a relative age." 76 | :type 'natnum) 77 | 78 | (defcustom marginalia-remote-file-regexps 79 | '("\\`/\\([^/|:]+\\):") ;; Tramp path 80 | "List of remote file regexps where the files should not be annotated. 81 | 82 | The first match group is displayed instead of the detailed file 83 | attribute information. For Tramp paths, the protocol is 84 | displayed instead." 85 | :type '(repeat regexp)) 86 | 87 | (define-obsolete-variable-alias 'marginalia-annotator-registry 88 | 'marginalia-annotators "2.0") 89 | 90 | (defcustom marginalia-annotators 91 | (mapcar 92 | (lambda (x) (append x (list 'builtin 'none))) 93 | `((command ,#'marginalia-annotate-command ,#'marginalia-annotate-binding) 94 | (embark-keybinding ,#'marginalia-annotate-embark-keybinding) 95 | (customize-group ,#'marginalia-annotate-customize-group) 96 | (variable ,#'marginalia-annotate-variable) 97 | (function ,#'marginalia-annotate-function) 98 | (face ,#'marginalia-annotate-face) 99 | (color ,#'marginalia-annotate-color) 100 | (unicode-name ,#'marginalia-annotate-char) 101 | (minor-mode ,#'marginalia-annotate-minor-mode) 102 | (symbol ,#'marginalia-annotate-symbol) 103 | (environment-variable ,#'marginalia-annotate-environment-variable) 104 | (input-method ,#'marginalia-annotate-input-method) 105 | (coding-system ,#'marginalia-annotate-coding-system) 106 | (charset ,#'marginalia-annotate-charset) 107 | (package ,#'marginalia-annotate-package) 108 | (imenu ,#'marginalia-annotate-imenu) 109 | (bookmark ,#'marginalia-annotate-bookmark) 110 | (file ,#'marginalia-annotate-file) 111 | (project-file ,#'marginalia-annotate-project-file) 112 | (buffer ,#'marginalia-annotate-buffer) 113 | (library ,#'marginalia-annotate-library) 114 | (theme ,#'marginalia-annotate-theme) 115 | (tab ,#'marginalia-annotate-tab) 116 | (multi-category ,#'marginalia-annotate-multi-category))) 117 | "Annotator function registry. 118 | Associates completion categories with annotation functions. 119 | Each annotation function must return a string, 120 | which is appended to the completion candidate." 121 | :type '(alist :key-type symbol :value-type (repeat symbol))) 122 | 123 | (defcustom marginalia-classifiers 124 | (list #'marginalia-classify-by-command-name 125 | #'marginalia-classify-original-category 126 | #'marginalia-classify-by-prompt 127 | #'marginalia-classify-symbol) 128 | "List of functions to determine current completion category. 129 | Each function should take no arguments and return a symbol 130 | indicating the category, or nil to indicate it could not 131 | determine it." 132 | :type 'hook) 133 | 134 | (defcustom marginalia-prompt-categories 135 | '(("\\" . customize-group) 136 | ("\\" . command) 137 | ("\\" . package) 138 | ("\\" . bookmark) 139 | ("\\" . color) 140 | ("\\" . face) 141 | ("\\" . environment-variable) 142 | ("\\" . function) 143 | ("\\" . variable) 144 | ("\\" . input-method) 145 | ("\\" . charset) 146 | ("\\" . coding-system) 147 | ("\\" . minor-mode) 148 | ("\\" . kill-ring) 149 | ("\\" . tab) 150 | ("\\" . library) 151 | ("\\" . theme)) 152 | "Associates regexps to match against minibuffer prompts with categories. 153 | The prompts are matched case-insensitively." 154 | :type '(alist :key-type regexp :value-type symbol)) 155 | 156 | (defcustom marginalia-censor-variables 157 | '("pass\\|auth-source-netrc-cache\\|auth-source-.*-nonce\\|api-?key") 158 | "The value of variables matching any of these regular expressions is not shown. 159 | This configuration variable is useful to hide variables which may 160 | hold sensitive data, e.g., passwords. The variable names are 161 | matched case-sensitively." 162 | :type '(repeat (choice symbol regexp))) 163 | 164 | (defcustom marginalia-command-categories 165 | `((,#'imenu . imenu) 166 | (,'recentf-open . file) ;; Available only on Emacs 29. 167 | (,#'where-is . command)) 168 | "Associate commands with a completion category. 169 | The value of `this-command' is used as key for the lookup." 170 | :type '(alist :key-type symbol :value-type symbol)) 171 | 172 | (defgroup marginalia-faces nil 173 | "Faces used by `marginalia-mode'." 174 | :group 'marginalia 175 | :group 'faces) 176 | 177 | (defface marginalia-key 178 | '((t :inherit font-lock-keyword-face)) 179 | "Face used to highlight keys.") 180 | 181 | (defface marginalia-type 182 | '((t :inherit marginalia-key)) 183 | "Face used to highlight types.") 184 | 185 | (defface marginalia-char 186 | '((t :inherit marginalia-key)) 187 | "Face used to highlight character annotations.") 188 | 189 | (defface marginalia-lighter 190 | '((t :inherit marginalia-size)) 191 | "Face used to highlight minor mode lighters.") 192 | 193 | (defface marginalia-on 194 | '((t :inherit success)) 195 | "Face used to signal enabled modes.") 196 | 197 | (defface marginalia-off 198 | '((t :inherit error)) 199 | "Face used to signal disabled modes.") 200 | 201 | (defface marginalia-documentation 202 | '((t :inherit completions-annotations)) 203 | "Face used to highlight documentation strings.") 204 | 205 | (defface marginalia-value 206 | '((t :inherit marginalia-key)) 207 | "Face used to highlight general variable values.") 208 | 209 | (defface marginalia-null 210 | '((t :inherit font-lock-comment-face)) 211 | "Face used to highlight null or unbound variable values.") 212 | 213 | (defface marginalia-true 214 | '((t :inherit font-lock-builtin-face)) 215 | "Face used to highlight true variable values.") 216 | 217 | (defface marginalia-function 218 | '((t :inherit font-lock-function-name-face)) 219 | "Face used to highlight function symbols.") 220 | 221 | (defface marginalia-symbol 222 | '((t :inherit font-lock-type-face)) 223 | "Face used to highlight general symbols.") 224 | 225 | (defface marginalia-list 226 | '((t :inherit font-lock-constant-face)) 227 | "Face used to highlight list expressions.") 228 | 229 | (defface marginalia-mode 230 | '((t :inherit marginalia-key)) 231 | "Face used to highlight buffer major modes.") 232 | 233 | (defface marginalia-date 234 | '((t :inherit marginalia-key)) 235 | "Face used to highlight dates.") 236 | 237 | (defface marginalia-version 238 | '((t :inherit marginalia-number)) 239 | "Face used to highlight package versions.") 240 | 241 | (defface marginalia-archive 242 | '((t :inherit warning)) 243 | "Face used to highlight package archives.") 244 | 245 | (defface marginalia-installed 246 | '((t :inherit success)) 247 | "Face used to highlight the status of packages.") 248 | 249 | (defface marginalia-size 250 | '((t :inherit marginalia-number)) 251 | "Face used to highlight sizes.") 252 | 253 | (defface marginalia-number 254 | '((t :inherit font-lock-constant-face)) 255 | "Face used to highlight numeric values.") 256 | 257 | (defface marginalia-string 258 | '((t :inherit font-lock-string-face)) 259 | "Face used to highlight string values.") 260 | 261 | (defface marginalia-modified 262 | '((t :inherit font-lock-negation-char-face)) 263 | "Face used to highlight buffer modification indicators.") 264 | 265 | (defface marginalia-file-name 266 | '((t :inherit marginalia-documentation)) 267 | "Face used to highlight file names.") 268 | 269 | (defface marginalia-file-owner 270 | '((t :inherit font-lock-preprocessor-face)) 271 | "Face used to highlight file owner and group names.") 272 | 273 | (defface marginalia-file-priv-no 274 | '((t :inherit shadow)) 275 | "Face used to highlight the no file privilege attribute.") 276 | 277 | (defface marginalia-file-priv-dir 278 | '((t :inherit font-lock-keyword-face)) 279 | "Face used to highlight the dir file privilege attribute.") 280 | 281 | (defface marginalia-file-priv-link 282 | '((t :inherit font-lock-keyword-face)) 283 | "Face used to highlight the link file privilege attribute.") 284 | 285 | (defface marginalia-file-priv-read 286 | '((t :inherit font-lock-type-face)) 287 | "Face used to highlight the read file privilege attribute.") 288 | 289 | (defface marginalia-file-priv-write 290 | '((t :inherit font-lock-builtin-face)) 291 | "Face used to highlight the write file privilege attribute.") 292 | 293 | (defface marginalia-file-priv-exec 294 | '((t :inherit font-lock-function-name-face)) 295 | "Face used to highlight the exec file privilege attribute.") 296 | 297 | (defface marginalia-file-priv-other 298 | '((t :inherit font-lock-constant-face)) 299 | "Face used to highlight some other file privilege attribute.") 300 | 301 | (defface marginalia-file-priv-rare 302 | '((t :inherit font-lock-variable-name-face)) 303 | "Face used to highlight a rare file privilege attribute.") 304 | 305 | ;;;; Pre-declarations for external packages 306 | 307 | (declare-function bookmark-prop-get "bookmark") 308 | 309 | (declare-function project-current "project") 310 | (declare-function project-root "project") 311 | 312 | (defvar package--builtins) 313 | (defvar package-archive-contents) 314 | (declare-function package--from-builtin "package") 315 | (declare-function package-desc-archive "package") 316 | (declare-function package-desc-status "package") 317 | (declare-function package-desc-summary "package") 318 | (declare-function package-desc-version "package") 319 | (declare-function package-version-join "package") 320 | 321 | (declare-function color-rgb-to-hex "color") 322 | (declare-function color-rgb-to-hsl "color") 323 | (declare-function color-hsl-to-rgb "color") 324 | 325 | ;;;; Marginalia mode 326 | 327 | (defalias 'marginalia--orig-completion-metadata-get 328 | (symbol-function 329 | (if (fboundp 'marginalia--orig-completion-metadata-get) 330 | 'marginalia--orig-completion-metadata-get 331 | (compat-function completion-metadata-get))) 332 | "Original `completion-metadata-get' function.") 333 | 334 | (defvar marginalia--pangram "Cwm fjord bank glyphs vext quiz.") 335 | 336 | (defvar marginalia--bookmark-type-transforms 337 | (let ((words (regexp-opt '("handle" "handler" "jump" "bookmark")))) 338 | `((,(format "-+%s-+" words) . "-") 339 | (,(format "\\`%s-+" words) . "") 340 | (,(format "-%s\\'" words) . "") 341 | ("\\`default\\'" . "File") 342 | (".*" . ,#'capitalize))) 343 | "List of bookmark type transformers. 344 | Relying on this mechanism is discouraged in favor of the 345 | `bookmark-handler-type' property. The function names are matched 346 | case-sensitively.") 347 | 348 | (defvar marginalia--cand-width-step 10 349 | "Round candidate width.") 350 | 351 | (defvar-local marginalia--cand-width-max 20 352 | "Maximum width of candidates.") 353 | 354 | (defvar marginalia--fontified-file-modes nil 355 | "List of fontified file modes.") 356 | 357 | (defvar-local marginalia--cache nil 358 | "The cache, pair of list and hashtable.") 359 | 360 | (defvar marginalia--cache-size 100 361 | "Size of the cache, set to 0 to disable the cache. 362 | Disabling the cache is useful on non-incremental UIs like default completion or 363 | for performance profiling of the annotators.") 364 | 365 | (defvar-local marginalia--command nil 366 | "Last command symbol saved in order to allow annotations.") 367 | 368 | (defvar-local marginalia--base-position 0 369 | "Last completion base position saved to get full file paths.") 370 | 371 | (defvar marginalia--metadata nil 372 | "Completion metadata from the current completion.") 373 | 374 | (defvar marginalia--ellipsis nil) 375 | (defun marginalia--ellipsis () 376 | "Return ellipsis." 377 | (with-memoization marginalia--ellipsis 378 | (cond 379 | ((bound-and-true-p truncate-string-ellipsis)) 380 | ((char-displayable-p ?…) "…") 381 | ("...")))) 382 | 383 | (defun marginalia--truncate (str width) 384 | "Truncate string STR to WIDTH." 385 | (when (floatp width) (setq width (round (* width marginalia-field-width)))) 386 | (when-let (pos (string-search "\n" str)) 387 | (setq str (substring str 0 pos))) 388 | (let* ((face (and (not (equal str "")) 389 | (get-text-property (1- (length str)) 'face str))) 390 | (ell (if face 391 | (propertize (marginalia--ellipsis) 'face face) 392 | (marginalia--ellipsis))) 393 | (trunc 394 | (if (< width 0) 395 | (nreverse (truncate-string-to-width (reverse str) (- width) 0 ?\s ell)) 396 | (truncate-string-to-width str width 0 ?\s ell)))) 397 | (unless (string-prefix-p str trunc) 398 | (put-text-property 0 (length trunc) 'help-echo str trunc)) 399 | trunc)) 400 | 401 | (cl-defmacro marginalia--field (field &key truncate face width format) 402 | "Format FIELD as a string according to some options. 403 | TRUNCATE is the truncation width. 404 | WIDTH is the field width. 405 | FORMAT is a format string. 406 | FACE is the name of the face, with which the field should be propertized." 407 | (setq field (if format `(format ,format ,field) `(or ,field ""))) 408 | (when width (setq field `(format ,(format "%%%ds" (- width)) ,field))) 409 | (when truncate (setq field `(marginalia--truncate ,field ,truncate))) 410 | (when face 411 | (setq field (if (or format width truncate) 412 | (cl-with-gensyms (f) 413 | `(let ((,f ,field)) 414 | (put-text-property 0 (length ,f) 'face ,face ,f) 415 | ,f)) 416 | `(propertize ,field 'face ,face)))) 417 | field) 418 | 419 | (defmacro marginalia--fields (&rest fields) 420 | "Format annotation FIELDS as a string with separators in between." 421 | (let ((left t)) 422 | (cons 'concat 423 | (mapcan 424 | (lambda (field) 425 | (if (not (eq (car field) :left)) 426 | `(,@(when left (setq left nil) `(#(" " 0 1 (marginalia--align t)))) 427 | marginalia-separator (marginalia--field ,@field)) 428 | (unless left (error "Left fields must come first")) 429 | `((marginalia--field ,@(cdr field))))) 430 | fields)))) 431 | 432 | (defmacro marginalia--in-minibuffer (&rest body) 433 | "Run BODY inside minibuffer if minibuffer is active. 434 | Otherwise stay within current buffer." 435 | (declare (indent 0)) 436 | `(with-current-buffer (if-let (win (active-minibuffer-window)) 437 | (window-buffer win) 438 | (current-buffer)) 439 | ,@body)) 440 | 441 | (defun marginalia--documentation (str) 442 | "Format documentation string STR." 443 | (when str 444 | (marginalia--fields 445 | (str :truncate 1.0 :face 'marginalia-documentation)))) 446 | 447 | (defun marginalia-annotate-binding (cand) 448 | "Annotate command CAND with keybinding." 449 | (when-let ((sym (intern-soft cand)) 450 | (key (and (commandp sym) (where-is-internal sym nil 'first-only)))) 451 | (format #(" (%s)" 1 5 (face marginalia-key)) (key-description key)))) 452 | 453 | (defun marginalia--annotator (cat) 454 | "Return annotation function for category CAT." 455 | (pcase (car (alist-get cat marginalia-annotators)) 456 | ('none #'ignore) 457 | ('builtin nil) 458 | (fun fun))) 459 | 460 | (defun marginalia-annotate-multi-category (cand) 461 | "Annotate multi-category CAND, dispatching to the appropriate annotator." 462 | (if-let ((multi (get-text-property 0 'multi-category cand)) 463 | (annotate (marginalia--annotator (car multi)))) 464 | ;; Use the Marginalia annotator corresponding to the multi category. 465 | (funcall annotate (cdr multi)) 466 | ;; Apply the original annotation function on the original candidate. Bypass 467 | ;; our `marginalia--completion-metadata-get' advice. 468 | (when-let (annotate (marginalia--orig-completion-metadata-get 469 | marginalia--metadata 'annotation-function)) 470 | (funcall annotate cand)))) 471 | 472 | (defconst marginalia--advice-regexp 473 | (rx bos 474 | (1+ (seq (? "This function has ") 475 | (or ":before" ":after" ":around" ":override" 476 | ":before-while" ":before-until" ":after-while" 477 | ":after-until" ":filter-args" ":filter-return") 478 | " advice: " (0+ nonl) "\n")) 479 | "\n") 480 | "Regexp to match lines about advice in function documentation strings.") 481 | 482 | ;; Taken from advice--make-docstring, is this robust? 483 | (defun marginalia--advised (fun) 484 | "Return t if function FUN is advised." 485 | (let ((flist (indirect-function fun))) 486 | (advice--p (if (eq 'macro (car-safe flist)) (cdr flist) flist)))) 487 | 488 | (defun marginalia--symbol-class (s) 489 | "Return symbol class characters for symbol S. 490 | 491 | This function is an extension of `help--symbol-class'. It returns 492 | more fine-grained and more detailed symbol information. 493 | 494 | Function: 495 | f function 496 | c command 497 | C interactive-only command 498 | m macro 499 | F special-form 500 | M module function 501 | P primitive 502 | g cl-generic 503 | p pure 504 | s side-effect-free 505 | @ autoloaded 506 | ! advised 507 | - obsolete 508 | & alias 509 | 510 | Variable: 511 | u custom (U modified compared to global value) 512 | v variable 513 | l local (L modified compared to default value) 514 | - obsolete 515 | & alias 516 | 517 | Other: 518 | G custom group 519 | a face 520 | t cl-type" 521 | (let ((class 522 | (append 523 | (when (fboundp s) 524 | (list 525 | (cond 526 | ((get s 'pure) '("p" . "pure")) 527 | ((get s 'side-effect-free) '("s" . "side-effect-free"))) 528 | (cond 529 | ((commandp s) 530 | (if (get s 'interactive-only) 531 | '("C" . "interactive-only command") 532 | '("c" . "command"))) 533 | ((cl-generic-p s) '("g" . "cl-generic")) 534 | ((macrop (symbol-function s)) '("m" . "macro")) 535 | ((special-form-p (symbol-function s)) '("F" . "special-form")) 536 | ((subr-primitive-p (symbol-function s)) '("P" . "primitive")) 537 | ((module-function-p (symbol-function s)) '("M" . "module function")) 538 | (t '("f" . "function"))) 539 | (and (autoloadp (symbol-function s)) '("@" . "autoload")) 540 | (and (marginalia--advised s) '("!" . "advised")) 541 | (and (symbolp (symbol-function s)) 542 | (cons "&" (format "alias for `%s'" (symbol-function s)))) 543 | (and (get s 'byte-obsolete-info) '("-" . "obsolete")))) 544 | (when (boundp s) 545 | (list 546 | (when (local-variable-if-set-p s) 547 | (if (ignore-errors 548 | (not (equal (symbol-value s) 549 | (default-value s)))) 550 | '("L" . "local, modified from global") 551 | '("l" . "local, unmodified"))) 552 | (if (get s 'standard-value) 553 | (if (ignore-errors 554 | (not (equal (symbol-value s) 555 | (eval (car (get s 'standard-value)))))) 556 | '("U" . "custom, modified from standard") 557 | '("u" . "custom, unmodified")) 558 | '("v" . "variable")) 559 | (and (not (eq (ignore-errors (indirect-variable s)) s)) 560 | (cons "&" (format "alias for `%s'" (ignore-errors (indirect-variable s))))) 561 | (and (get s 'byte-obsolete-variable) '("-" . "obsolete")))) 562 | (list 563 | (and (get s 'group-documentation) '("G" . "custom group")) 564 | (and (facep s) '("a" . "face")) 565 | (and (get s 'cl--class) '("t" . "cl-type")))))) ;; cl-find-class, cl--find-class 566 | (setq class (delq nil class)) 567 | (propertize 568 | (format " %-6s" (mapconcat #'car class "")) 569 | 'help-echo 570 | (mapconcat (pcase-lambda (`(,x . ,y)) (concat x " " y)) class "\n")))) 571 | 572 | (defun marginalia--function-doc (sym) 573 | "Documentation string of function SYM." 574 | (when-let (str (ignore-errors (documentation sym))) 575 | (save-match-data 576 | (if (string-match marginalia--advice-regexp str) 577 | (substring str (match-end 0)) 578 | str)))) 579 | 580 | ;; Derived from elisp-get-fnsym-args-string 581 | (defun marginalia--function-args (sym) 582 | "Return function arguments for SYM." 583 | (let (tmp) 584 | (elisp-function-argstring 585 | (cond 586 | ((listp (setq tmp (gethash (indirect-function sym) 587 | advertised-signature-table t))) 588 | tmp) 589 | ((setq tmp (help-split-fundoc 590 | (ignore-errors (documentation sym t)) 591 | sym)) 592 | (car tmp)) 593 | ((setq tmp (help-function-arglist sym)) 594 | (if (and (stringp tmp) (string-search "not available" tmp)) 595 | ;; A shorter text fits better into the limited Marginalia space. 596 | "[autoload]" 597 | tmp)))))) 598 | 599 | (defun marginalia-annotate-symbol (cand) 600 | "Annotate symbol CAND with its documentation string." 601 | (when-let (sym (intern-soft cand)) 602 | (marginalia--fields 603 | (:left (marginalia-annotate-binding cand)) 604 | ((marginalia--symbol-class sym) :face 'marginalia-type) 605 | ((if (fboundp sym) (marginalia--function-doc sym) 606 | (cl-loop 607 | for doc in '(variable-documentation 608 | face-documentation 609 | group-documentation) 610 | thereis (ignore-errors (documentation-property sym doc)))) 611 | :truncate 1.0 :face 'marginalia-documentation) 612 | ((abbreviate-file-name (or (symbol-file sym) "")) 613 | :truncate -0.5 :face 'marginalia-file-name)))) 614 | 615 | (defun marginalia-annotate-command (cand) 616 | "Annotate command CAND with its documentation string. 617 | Similar to `marginalia-annotate-symbol', but does not show symbol class." 618 | (when-let (sym (intern-soft cand)) 619 | (concat 620 | (marginalia-annotate-binding cand) 621 | (marginalia--documentation (marginalia--function-doc sym))))) 622 | 623 | (defun marginalia-annotate-embark-keybinding (cand) 624 | "Annotate Embark keybinding CAND with its documentation string. 625 | Similar to `marginalia-annotate-command', but does not show the 626 | keybinding since CAND includes it." 627 | (when-let (cmd (get-text-property 0 'embark-command cand)) 628 | (marginalia--documentation (marginalia--function-doc cmd)))) 629 | 630 | (defun marginalia-annotate-imenu (cand) 631 | "Annotate imenu CAND with its documentation string." 632 | (when (derived-mode-p 'emacs-lisp-mode) 633 | ;; Strip until the last whitespace in order to support flat imenu 634 | (marginalia-annotate-symbol (replace-regexp-in-string "\\`.* " "" cand)))) 635 | 636 | (defun marginalia-annotate-function (cand) 637 | "Annotate function CAND with its documentation string." 638 | (when-let (sym (intern-soft cand)) 639 | (when (fboundp sym) 640 | (marginalia--fields 641 | (:left (marginalia-annotate-binding cand)) 642 | ((marginalia--symbol-class sym) :face 'marginalia-type) 643 | ((marginalia--function-args sym) :face 'marginalia-value 644 | :truncate 0.5) 645 | ((marginalia--function-doc sym) :truncate 1.0 646 | :face 'marginalia-documentation))))) 647 | 648 | (defun marginalia--variable-value (sym) 649 | "Return the variable value of SYM as string." 650 | (cond 651 | ((not (boundp sym)) 652 | (propertize "#" 'face 'marginalia-null)) 653 | ((and marginalia-censor-variables 654 | (let ((name (symbol-name sym)) 655 | case-fold-search) 656 | (cl-loop for r in marginalia-censor-variables 657 | thereis (if (symbolp r) 658 | (eq r sym) 659 | (string-match-p r name))))) 660 | (propertize "*****" 661 | 'face 'marginalia-null 662 | 'help-echo "Hidden due to `marginalia-censor-variables'")) 663 | (t 664 | (let ((val (symbol-value sym))) 665 | (pcase val 666 | ('nil (propertize "nil" 'face 'marginalia-null)) 667 | ('t (propertize "t" 'face 'marginalia-true)) 668 | ((pred keymapp) (propertize "#" 'face 'marginalia-value)) 669 | ((pred bool-vector-p) (propertize "#" 'face 'marginalia-value)) 670 | ((pred hash-table-p) (propertize "#" 'face 'marginalia-value)) 671 | ((pred syntax-table-p) (propertize "#" 'face 'marginalia-value)) 672 | ;; Emacs bug#53988: abbrev-table-p throws an error 673 | ((guard (static-if (< emacs-major-version 30) 674 | (and (vectorp val) (ignore-errors (abbrev-table-p val))) 675 | (abbrev-table-p val))) 676 | (propertize "#" 'face 'marginalia-value)) 677 | ((pred char-table-p) (propertize "#" 'face 'marginalia-value)) 678 | ;; Emacs 29 comes with callable objects or object closures (OClosures) 679 | ((guard (and (fboundp 'oclosure-type) (oclosure-type val))) 680 | (format (propertize "#" 'face 'marginalia-function) 681 | (and (fboundp 'oclosure-type) (oclosure-type val)))) 682 | ((pred byte-code-function-p) (propertize "#" 'face 'marginalia-function)) 683 | ((and (pred functionp) (pred symbolp)) 684 | ;; We are not consistent here, values are generally printed 685 | ;; unquoted. But we make an exception for function symbols to visually 686 | ;; distinguish them from symbols. I am not entirely happy with this, 687 | ;; but we should not add quotation to every type. 688 | (format (propertize "#'%s" 'face 'marginalia-function) val)) 689 | ((pred recordp) (format (propertize "#" 'face 'marginalia-value) (type-of val))) 690 | ((pred symbolp) (propertize (symbol-name val) 'face 'marginalia-symbol)) 691 | ((pred numberp) 692 | (propertize (number-to-string val) 693 | 'face 'marginalia-number 694 | 'help-echo (and (integerp val) 695 | (format "%d, #o%o, #x%x%s" val val val 696 | (if (characterp val) (format ", ?%c" val) ""))))) 697 | (_ (let ((print-escape-newlines t) 698 | (print-escape-control-characters t) 699 | ;;(print-escape-multibyte t) 700 | (print-level 3) 701 | (print-length marginalia-field-width)) 702 | (propertize 703 | (replace-regexp-in-string 704 | ;; `print-escape-control-characters' does not escape Unicode control characters. 705 | "[\x0-\x1F\x7f-\x9f\x061c\x200e\x200f\x202a-\x202e\x2066-\x2069]" 706 | (lambda (x) (format "\\x%x" (string-to-char x))) 707 | (prin1-to-string 708 | (if (stringp val) 709 | ;; Get rid of string properties to save some of the precious space 710 | (substring-no-properties 711 | val 0 712 | (min (length val) marginalia-field-width)) 713 | val)) 714 | 'fixedcase 'literal) 715 | 'face 716 | (cond 717 | ((listp val) 'marginalia-list) 718 | ((stringp val) 'marginalia-string) 719 | (t 'marginalia-value)))))))))) 720 | 721 | (defun marginalia-annotate-variable (cand) 722 | "Annotate variable CAND with its documentation string." 723 | (when-let (sym (intern-soft cand)) 724 | (marginalia--fields 725 | ((marginalia--symbol-class sym) :face 'marginalia-type) 726 | ((marginalia--variable-value sym) :truncate 0.5) 727 | ((documentation-property sym 'variable-documentation) 728 | :truncate 1.0 :face 'marginalia-documentation)))) 729 | 730 | (defun marginalia-annotate-environment-variable (cand) 731 | "Annotate environment variable CAND with its current value." 732 | (when-let (val (getenv cand)) 733 | (marginalia--fields 734 | (val :truncate 1.0 :face 'marginalia-value)))) 735 | 736 | (defun marginalia-annotate-face (cand) 737 | "Annotate face CAND with its documentation string and face example." 738 | (when-let (sym (intern-soft cand)) 739 | (marginalia--fields 740 | ;; HACK: Manual alignment to fix misalignment due to face 741 | ((concat marginalia--pangram #(" " 0 1 (display (space :align-to center)))) 742 | :face sym) 743 | ((documentation-property sym 'face-documentation) 744 | :truncate 1.0 :face 'marginalia-documentation)))) 745 | 746 | (defun marginalia-annotate-color (cand) 747 | "Annotate face CAND with its documentation string and face example." 748 | (when-let (rgb (color-name-to-rgb cand)) 749 | (pcase-let* ((`(,r ,g ,b) rgb) 750 | (`(,h ,s ,l) (apply #'color-rgb-to-hsl rgb)) 751 | (cr (color-rgb-to-hex r 0 0)) 752 | (cg (color-rgb-to-hex 0 g 0)) 753 | (cb (color-rgb-to-hex 0 0 b)) 754 | (ch (apply #'color-rgb-to-hex (color-hsl-to-rgb h 1 0.5))) 755 | (cs (apply #'color-rgb-to-hex (color-hsl-to-rgb h s 0.5))) 756 | (cl (apply #'color-rgb-to-hex (color-hsl-to-rgb 0 0 l)))) 757 | (marginalia--fields 758 | (" " :face `(:background ,(apply #'color-rgb-to-hex rgb))) 759 | ((format 760 | "%s%s%s %s" 761 | (propertize "r" 'face `(:background ,cr :foreground ,(readable-foreground-color cr))) 762 | (propertize "g" 'face `(:background ,cg :foreground ,(readable-foreground-color cg))) 763 | (propertize "b" 'face `(:background ,cb :foreground ,(readable-foreground-color cb))) 764 | (color-rgb-to-hex r g b 2))) 765 | ((format 766 | "%s%s%s %3s° %3s%% %3s%%" 767 | (propertize "h" 'face `(:background ,ch :foreground ,(readable-foreground-color ch))) 768 | (propertize "s" 'face `(:background ,cs :foreground ,(readable-foreground-color cs))) 769 | (propertize "l" 'face `(:background ,cl :foreground ,(readable-foreground-color cl))) 770 | (round (* 360 h)) 771 | (round (* 100 s)) 772 | (round (* 100 l)))))))) 773 | 774 | (defun marginalia-annotate-char (cand) 775 | "Annotate character CAND with its general character category and character code." 776 | (when-let (char (char-from-name cand t)) 777 | (marginalia--fields 778 | (:left char :format" (%c)" :face 'marginalia-char) 779 | (char :format "%06X" :face 'marginalia-number) 780 | ((char-code-property-description 781 | 'general-category 782 | (get-char-code-property char 'general-category)) 783 | :width 30 :face 'marginalia-documentation)))) 784 | 785 | (defun marginalia-annotate-minor-mode (cand) 786 | "Annotate minor-mode CAND with status and documentation string." 787 | (let* ((sym (intern-soft cand)) 788 | (message-log-max nil) 789 | (mode (if (and sym (boundp sym)) 790 | sym 791 | (lookup-minor-mode-from-indicator cand))) 792 | (lighter (cdr (assq mode minor-mode-alist))) 793 | (lighter-str (and lighter (string-trim (format-mode-line (cons t lighter)))))) 794 | (marginalia--fields 795 | ((if (and (boundp mode) (symbol-value mode)) 796 | (propertize "On" 'face 'marginalia-on) 797 | (propertize "Off" 'face 'marginalia-off)) :width 3) 798 | ((if (local-variable-if-set-p mode) "Local" "Global") :width 6 :face 'marginalia-type) 799 | (lighter-str :width 20 :face 'marginalia-lighter) 800 | ((marginalia--function-doc mode) 801 | :truncate 1.0 :face 'marginalia-documentation)))) 802 | 803 | (defun marginalia-annotate-package (cand) 804 | "Annotate package CAND with its description summary." 805 | (when-let ((pkg-alist (bound-and-true-p package-alist)) 806 | (name (replace-regexp-in-string "-[0-9\\.-]+\\'" "" cand)) 807 | (pkg (intern-soft name)) 808 | (desc (or (unless (equal name cand) 809 | (cl-loop with version = (substring cand (1+ (length name))) 810 | for d in (alist-get pkg pkg-alist) 811 | if (equal (package-version-join (package-desc-version d)) version) 812 | return d)) 813 | ;; taken from `describe-package-1' 814 | (car (alist-get pkg pkg-alist)) 815 | (if-let (built-in (assq pkg package--builtins)) 816 | (package--from-builtin built-in) 817 | (car (alist-get pkg package-archive-contents)))))) 818 | (marginalia--fields 819 | ((package-version-join (package-desc-version desc)) :truncate 16 :face 'marginalia-version) 820 | ((cond 821 | ((package-desc-archive desc) (propertize (package-desc-archive desc) 'face 'marginalia-archive)) 822 | (t (propertize (or (package-desc-status desc) "orphan") 'face 'marginalia-installed))) :truncate 12) 823 | ((package-desc-summary desc) :truncate 1.0 :face 'marginalia-documentation)))) 824 | 825 | (defun marginalia--bookmark-type (bm) 826 | "Return bookmark type string of BM. 827 | The string is transformed according to `marginalia--bookmark-type-transforms'." 828 | (let ((handler (or (bookmark-prop-get bm 'handler) 'bookmark-default-handler))) 829 | (and 830 | ;; Some libraries use lambda handlers instead of symbols. For 831 | ;; example the function `xwidget-webkit-bookmark-make-record' is 832 | ;; affected. I consider this bad style since then the lambda is 833 | ;; persisted. 834 | (symbolp handler) 835 | (or (get handler 'bookmark-handler-type) 836 | (let ((str (symbol-name handler)) 837 | case-fold-search) 838 | (dolist (transformer marginalia--bookmark-type-transforms str) 839 | (when (string-match-p (car transformer) str) 840 | (setq str 841 | (if (stringp (cdr transformer)) 842 | (replace-regexp-in-string (car transformer) (cdr transformer) str) 843 | (funcall (cdr transformer) str)))))))))) 844 | 845 | (defun marginalia-annotate-bookmark (cand) 846 | "Annotate bookmark CAND with its file name and front context string." 847 | (when-let ((bm (assoc cand (bound-and-true-p bookmark-alist)))) 848 | (marginalia--fields 849 | ((marginalia--bookmark-type bm) :width 10 :face 'marginalia-type) 850 | ((or (bookmark-prop-get bm 'filename) 851 | (bookmark-prop-get bm 'location)) 852 | :truncate (if (bookmark-prop-get bm 'filename) -0.5 0.5) 853 | :face 'marginalia-file-name) 854 | ((let ((front (or (bookmark-prop-get bm 'front-context-string) "")) 855 | (rear (or (bookmark-prop-get bm 'rear-context-string) ""))) 856 | (unless (and (string-blank-p front) (string-blank-p rear)) 857 | (string-clean-whitespace 858 | (concat front (marginalia--ellipsis) rear)))) 859 | :truncate 0.5 :face 'marginalia-documentation)))) 860 | 861 | (defun marginalia-annotate-customize-group (cand) 862 | "Annotate customization group CAND with its documentation string." 863 | (marginalia--documentation (documentation-property (intern cand) 'group-documentation))) 864 | 865 | (defun marginalia-annotate-input-method (cand) 866 | "Annotate input method CAND with its description." 867 | (marginalia--documentation (nth 4 (assoc cand input-method-alist)))) 868 | 869 | (defun marginalia-annotate-charset (cand) 870 | "Annotate charset CAND with its description." 871 | (marginalia--documentation (charset-description (intern cand)))) 872 | 873 | (defun marginalia-annotate-coding-system (cand) 874 | "Annotate coding system CAND with its description." 875 | (marginalia--documentation (coding-system-doc-string (intern cand)))) 876 | 877 | (defun marginalia--buffer-status (buffer) 878 | "Return the status of BUFFER as a string." 879 | (format-mode-line '((:propertize "%1*%1+%1@" face marginalia-modified) 880 | marginalia-separator 881 | (7 (:propertize "%I" face marginalia-size)) 882 | marginalia-separator 883 | ;; InactiveMinibuffer has 18 letters, but there are longer names. 884 | ;; For example Org-Agenda produces very long mode names. 885 | ;; Therefore we have to truncate. 886 | (20 (-20 (:propertize mode-name face marginalia-mode)))) 887 | nil nil buffer)) 888 | 889 | (defun marginalia--buffer-file (buffer) 890 | "Return the file or process name of BUFFER." 891 | (if-let (proc (get-buffer-process buffer)) 892 | (format "(%s %s) %s" 893 | proc (process-status proc) 894 | (abbreviate-file-name (buffer-local-value 'default-directory buffer))) 895 | (abbreviate-file-name 896 | (or (cond 897 | ;; see ibuffer-buffer-file-name 898 | ((buffer-file-name buffer)) 899 | ((when-let (dir (and (local-variable-p 'dired-directory buffer) 900 | (buffer-local-value 'dired-directory buffer))) 901 | (expand-file-name (if (stringp dir) dir (car dir)) 902 | (buffer-local-value 'default-directory buffer)))) 903 | ((local-variable-p 'list-buffers-directory buffer) 904 | (buffer-local-value 'list-buffers-directory buffer))) 905 | "")))) 906 | 907 | (defun marginalia-annotate-buffer (cand) 908 | "Annotate buffer CAND with modification status, file name and major mode." 909 | (when-let ((buffer (get-buffer cand))) 910 | (if (buffer-live-p buffer) 911 | (marginalia--fields 912 | ((marginalia--buffer-status buffer)) 913 | ((marginalia--buffer-file buffer) 914 | :truncate -0.5 :face 'marginalia-file-name)) 915 | (marginalia--fields ("(dead buffer)" :face 'error))))) 916 | 917 | (defun marginalia--full-candidate (cand) 918 | "Return completion candidate CAND in full. 919 | For some completion tables, the completion candidates offered are 920 | meant to be only a part of the full minibuffer contents. For 921 | example, during file name completion the candidates are one path 922 | component of a full file path." 923 | (if-let (win (active-minibuffer-window)) 924 | (with-current-buffer (window-buffer win) 925 | (concat (let ((end (minibuffer-prompt-end))) 926 | (buffer-substring-no-properties 927 | end (+ end marginalia--base-position))) 928 | cand)) 929 | ;; no minibuffer is active, trust that cand already conveys all 930 | ;; necessary information (there's not much else we can do) 931 | cand)) 932 | 933 | (defun marginalia--remote-file-p (file) 934 | "Return non-nil if FILE is remote. 935 | The return value is a string describing the remote location, 936 | e.g., the protocol." 937 | (save-match-data 938 | (setq file (substitute-in-file-name file)) 939 | (cl-loop for r in marginalia-remote-file-regexps 940 | if (string-match r file) 941 | return (or (match-string 1 file) "remote")))) 942 | 943 | (defun marginalia--annotate-local-file (cand) 944 | "Annotate local file CAND." 945 | (marginalia--in-minibuffer 946 | (when-let (attrs (ignore-errors 947 | ;; may throw permission denied errors 948 | (file-attributes (substitute-in-file-name 949 | (marginalia--full-candidate cand)) 950 | 'integer))) 951 | ;; HACK: Format differently accordingly to alignment, since the file owner 952 | ;; is usually not displayed. Otherwise we will see an excessive amount of 953 | ;; whitespace in front of the file permissions. Furthermore the alignment 954 | ;; in `consult-buffer' will look ugly. Find a better solution! 955 | (if (eq marginalia-align 'right) 956 | (marginalia--fields 957 | ;; File owner at the left 958 | ((marginalia--file-owner attrs) :face 'marginalia-file-owner) 959 | ((marginalia--file-modes attrs)) 960 | ((marginalia--file-size attrs) :face 'marginalia-size :width -7) 961 | ((marginalia--time (file-attribute-modification-time attrs)) 962 | :face 'marginalia-date :width -12)) 963 | (marginalia--fields 964 | ((marginalia--file-modes attrs)) 965 | ((marginalia--file-size attrs) :face 'marginalia-size :width -7) 966 | ((marginalia--time (file-attribute-modification-time attrs)) 967 | :face 'marginalia-date :width -12) 968 | ;; File owner at the right 969 | ((marginalia--file-owner attrs) :face 'marginalia-file-owner)))))) 970 | 971 | (defun marginalia-annotate-file (cand) 972 | "Annotate file CAND with its size, modification time and other attributes. 973 | These annotations are skipped for remote paths." 974 | (if-let (remote (or (marginalia--remote-file-p cand) 975 | (when-let (win (active-minibuffer-window)) 976 | (with-current-buffer (window-buffer win) 977 | (marginalia--remote-file-p (minibuffer-contents-no-properties)))))) 978 | (marginalia--fields (remote :format "*%s*" :face 'marginalia-documentation)) 979 | (marginalia--annotate-local-file cand))) 980 | 981 | (defun marginalia--file-owner (attrs) 982 | "Return file owner given ATTRS." 983 | (let ((uid (file-attribute-user-id attrs)) 984 | (gid (file-attribute-group-id attrs))) 985 | (when (or (/= (user-uid) uid) (/= (group-gid) gid)) 986 | (format "%s:%s" 987 | (or (user-login-name uid) uid) 988 | (or (group-name gid) gid))))) 989 | 990 | (defun marginalia--file-size (attrs) 991 | "Return formatted file size given ATTRS." 992 | (propertize (file-size-human-readable (file-attribute-size attrs)) 993 | 'help-echo (number-to-string (file-attribute-size attrs)))) 994 | 995 | (defun marginalia--file-modes (attrs) 996 | "Return fontified file modes given the ATTRS." 997 | ;; Without caching this can a be significant portion of the time 998 | ;; `marginalia-annotate-file' takes to execute. Caching improves performance 999 | ;; by about a factor of 20. 1000 | (setq attrs (file-attribute-modes attrs)) 1001 | (or (car (member attrs marginalia--fontified-file-modes)) 1002 | (progn 1003 | (setq attrs (substring attrs)) ;; copy because attrs is about to be modified 1004 | (dotimes (i (length attrs)) 1005 | (put-text-property 1006 | i (1+ i) 'face 1007 | (pcase (aref attrs i) 1008 | (?- 'marginalia-file-priv-no) 1009 | (?d 'marginalia-file-priv-dir) 1010 | (?l 'marginalia-file-priv-link) 1011 | (?r 'marginalia-file-priv-read) 1012 | (?w 'marginalia-file-priv-write) 1013 | (?x 'marginalia-file-priv-exec) 1014 | ((or ?s ?S ?t ?T) 'marginalia-file-priv-other) 1015 | (_ 'marginalia-file-priv-rare)) 1016 | attrs)) 1017 | (push attrs marginalia--fontified-file-modes) 1018 | attrs))) 1019 | 1020 | (defconst marginalia--time-relative 1021 | `((100 "sec" 1) 1022 | (,(* 60 100) "min" 60.0) 1023 | (,(* 3600 30) "hour" 3600.0) 1024 | (,(* 3600 24 400) "day" ,(* 3600.0 24.0)) 1025 | (nil "year" ,(* 365.25 24 3600))) 1026 | "Formatting used by the function `marginalia--time-relative'.") 1027 | 1028 | ;; Taken from `seconds-to-string'. 1029 | (defun marginalia--time-relative (time) 1030 | "Format TIME as a relative age." 1031 | (setq time (max 0 (float-time (time-since time)))) 1032 | (let ((sts marginalia--time-relative) here) 1033 | (while (and (car (setq here (pop sts))) (<= (car here) time))) 1034 | (setq time (round time (caddr here))) 1035 | (format "%s %s%s ago" time (cadr here) (if (= time 1) "" "s")))) 1036 | 1037 | (defun marginalia--time-absolute (time) 1038 | "Format TIME as an absolute age." 1039 | (let ((system-time-locale "C")) 1040 | (format-time-string 1041 | (if (> (decoded-time-year (decode-time (current-time))) 1042 | (decoded-time-year (decode-time time))) 1043 | " %Y %b %d" 1044 | "%b %d %H:%M") 1045 | time))) 1046 | 1047 | (defun marginalia--time (time) 1048 | "Format file age TIME, suitably for use in annotations." 1049 | (propertize 1050 | (if (< (float-time (time-since time)) marginalia-max-relative-age) 1051 | (marginalia--time-relative time) 1052 | (marginalia--time-absolute time)) 1053 | 'help-echo (format-time-string "%Y-%m-%d %T" time))) 1054 | 1055 | (defvar-local marginalia--project-root 'unset) 1056 | (defun marginalia--project-root () 1057 | "Return project root." 1058 | (marginalia--in-minibuffer 1059 | (when (eq marginalia--project-root 'unset) 1060 | (setq marginalia--project-root 1061 | (or (let ((prompt (minibuffer-prompt)) 1062 | case-fold-search) 1063 | (and (string-match 1064 | "\\`\\(?:Dired\\|Find file\\) in \\(.*\\): \\'" 1065 | prompt) 1066 | (match-string 1 prompt))) 1067 | (when-let (proj (project-current)) 1068 | (project-root proj))))) 1069 | marginalia--project-root)) 1070 | 1071 | (defun marginalia-annotate-project-file (cand) 1072 | "Annotate file CAND with its size, modification time and other attributes." 1073 | ;; Absolute project directories also report project-file category 1074 | (if (file-name-absolute-p cand) 1075 | (marginalia-annotate-file cand) 1076 | (when-let (root (marginalia--project-root)) 1077 | (marginalia-annotate-file (expand-file-name cand root))))) 1078 | 1079 | (defvar-local marginalia--library-cache nil) 1080 | (defun marginalia--library-cache () 1081 | "Return hash table from library name to library file." 1082 | (marginalia--in-minibuffer 1083 | ;; `locate-file' and `locate-library' are bottlenecks for the 1084 | ;; annotator. Therefore we compute all the library paths first. 1085 | (unless marginalia--library-cache 1086 | (setq marginalia--library-cache (make-hash-table :test #'equal)) 1087 | (dolist (dir (delete-dups 1088 | (reverse ;; Reverse because of shadowing 1089 | (append load-path (custom-theme--load-path))))) ;; Include themes 1090 | (dolist (file (ignore-errors 1091 | (directory-files dir 'full 1092 | "\\.el\\(?:\\.gz\\)?\\'"))) 1093 | (puthash (marginalia--library-name file) 1094 | file marginalia--library-cache)))) 1095 | marginalia--library-cache)) 1096 | 1097 | (defun marginalia--library-name (file) 1098 | "Get name of library FILE." 1099 | (replace-regexp-in-string "\\(\\.gz\\|\\.elc?\\)+\\'" "" 1100 | (file-name-nondirectory file))) 1101 | 1102 | (defun marginalia--library-doc (file) 1103 | "Return library documentation string for FILE." 1104 | (let ((doc (get-text-property 0 'marginalia--library-doc file))) 1105 | (unless doc 1106 | ;; Extract documentation string. We cannot use `lm-summary' here, 1107 | ;; since it decompresses the whole file, which is slower. 1108 | (setq doc (or (ignore-errors 1109 | (let ((shell-file-name "sh") 1110 | (shell-command-switch "-c")) 1111 | (shell-command-to-string 1112 | (format (if (string-suffix-p ".gz" file) 1113 | "gzip -c -q -d %s | head -n1" 1114 | "head -n1 %s") 1115 | (shell-quote-argument file))))) 1116 | "")) 1117 | (cond 1118 | ((string-match "\\`(define-package\\s-+\"\\([^\"]+\\)\"" doc) 1119 | (setq doc (format "Generated package description from %s.el" 1120 | (match-string 1 doc)))) 1121 | ((string-match "\\`;+\\s-*" doc) 1122 | (setq doc (substring doc (match-end 0))) 1123 | (when (string-match "\\`[^ \t]+\\s-+-+\\s-+" doc) 1124 | (setq doc (substring doc (match-end 0)))) 1125 | (when (string-match "\\s-*-\\*-" doc) 1126 | (setq doc (substring doc 0 (match-beginning 0))))) 1127 | (t (setq doc ""))) 1128 | ;; Add the documentation string to the cache 1129 | (put-text-property 0 1 'marginalia--library-doc doc file)) 1130 | doc)) 1131 | 1132 | (defun marginalia-annotate-theme (cand) 1133 | "Annotate theme CAND with documentation and path." 1134 | (marginalia-annotate-library (concat cand "-theme"))) 1135 | 1136 | (defun marginalia-annotate-library (cand) 1137 | "Annotate library CAND with documentation and path." 1138 | (setq cand (marginalia--library-name cand)) 1139 | (when-let (file (gethash cand (marginalia--library-cache))) 1140 | (marginalia--fields 1141 | ;; Display if the corresponding feature is loaded. 1142 | ;; feature/=library file, but better than nothing. 1143 | ((when-let (sym (intern-soft cand)) 1144 | (when (memq sym features) 1145 | (propertize "Loaded" 'face 'marginalia-on))) 1146 | :width 8) 1147 | ((marginalia--library-doc file) 1148 | :truncate 1.0 :face 'marginalia-documentation) 1149 | ((abbreviate-file-name (file-name-directory file)) 1150 | :truncate -0.5 :face 'marginalia-file-name)))) 1151 | 1152 | (defun marginalia-annotate-tab (cand) 1153 | "Annotate named tab CAND with tab index, window and buffer information." 1154 | (when-let ((tabs (funcall tab-bar-tabs-function)) 1155 | (index (seq-position 1156 | tabs nil 1157 | (lambda (tab _) (equal (alist-get 'name tab) cand))))) 1158 | (let* ((tab (nth index tabs)) 1159 | (ws (alist-get 'ws tab)) 1160 | (bufs (window-state-buffers ws))) 1161 | ;; When the buffer key is present in the window state it is added in front 1162 | ;; of the window buffer list and gets duplicated. 1163 | (when (cadr (assq 'buffer ws)) (pop bufs)) 1164 | (marginalia--fields 1165 | (:left (1+ index) :format " (%s)" :face 'marginalia-key) 1166 | ((if (eq (car tab) 'current-tab) 1167 | (length (window-list nil 'no-minibuf)) 1168 | (length bufs)) 1169 | :format "win:%s" :face 'marginalia-size) 1170 | ((or (alist-get 'group tab) 'none) 1171 | :format "group:%s" :face 'marginalia-type :truncate 20) 1172 | ((if (eq (car tab) 'current-tab) 1173 | "(current tab)" 1174 | (string-join bufs " ")) 1175 | :face 'marginalia-documentation))))) 1176 | 1177 | (defun marginalia-classify-by-command-name () 1178 | "Lookup category for current command." 1179 | (and marginalia--command 1180 | (or (alist-get marginalia--command marginalia-command-categories) 1181 | ;; The command can be an alias, e.g., `recentf' -> `recentf-open'. 1182 | (when-let ((chain (function-alias-p marginalia--command))) 1183 | (alist-get (car (last chain)) marginalia-command-categories))))) 1184 | 1185 | (defun marginalia-classify-original-category () 1186 | "Return original category reported by completion metadata." 1187 | ;; Bypass our `marginalia--completion-metadata-get' advice. 1188 | (when-let (cat (marginalia--orig-completion-metadata-get marginalia--metadata 'category)) 1189 | ;; Ignore `symbol-help' category in order to ensure that the categories are 1190 | ;; refined to our categories function and variable. 1191 | (and (not (eq cat 'symbol-help)) cat))) 1192 | 1193 | (defun marginalia-classify-symbol () 1194 | "Determine if currently completing symbols." 1195 | (when-let (mct minibuffer-completion-table) 1196 | (when (or (eq mct 'help--symbol-completion-table) 1197 | (obarrayp mct) 1198 | (and (not (functionp mct)) (consp mct) (symbolp (car mct)))) ; assume list of symbols 1199 | 'symbol))) 1200 | 1201 | (defun marginalia-classify-by-prompt () 1202 | "Determine category by matching regexps against the minibuffer prompt. 1203 | This runs through the `marginalia-prompt-categories' alist 1204 | looking for a regexp that matches the prompt." 1205 | (when-let (prompt (minibuffer-prompt)) 1206 | (setq prompt 1207 | (replace-regexp-in-string "(.*?default.*?)\\|\\[.*?\\]" "" prompt)) 1208 | (cl-loop with case-fold-search = t 1209 | for (regexp . category) in marginalia-prompt-categories 1210 | when (string-match-p regexp prompt) 1211 | return category))) 1212 | 1213 | (defun marginalia--cache-reset (&rest _) 1214 | "Reset the cache." 1215 | (setq marginalia--cache (and marginalia--cache (> marginalia--cache-size 0) 1216 | (cons nil (make-hash-table :test #'equal 1217 | :size marginalia--cache-size))))) 1218 | 1219 | (defun marginalia--cached (cache fun key) 1220 | "Cached application of function FUN with KEY. 1221 | The CACHE keeps around the last `marginalia--cache-size' computed 1222 | annotations. The cache is mainly useful when scrolling in 1223 | completion UIs like Vertico or Icomplete." 1224 | (if cache 1225 | (let ((ht (cdr cache))) 1226 | (or (gethash key ht) 1227 | (let ((val (funcall fun key))) 1228 | (push key (car cache)) 1229 | (puthash key val ht) 1230 | (when (>= (hash-table-count ht) marginalia--cache-size) 1231 | (let ((end (last (car cache) 2))) 1232 | (remhash (cadr end) ht) 1233 | (setcdr end nil))) 1234 | val))) 1235 | (funcall fun key))) 1236 | 1237 | (defun marginalia--align (cands) 1238 | "Align annotations of CANDS according to `marginalia-align'." 1239 | (cl-loop 1240 | for (cand . ann) in cands do 1241 | (when-let (align (text-property-any 0 (length ann) 'marginalia--align t ann)) 1242 | (setq marginalia--cand-width-max 1243 | (max marginalia--cand-width-max 1244 | (* (ceiling (+ (string-width cand) (string-width ann 0 align)) 1245 | marginalia--cand-width-step) 1246 | marginalia--cand-width-step))))) 1247 | (cl-loop 1248 | for (cand . ann) in cands collect 1249 | (progn 1250 | (when-let (align (text-property-any 0 (length ann) 'marginalia--align t ann)) 1251 | (put-text-property 1252 | align (1+ align) 'display 1253 | `(space :align-to 1254 | ,(pcase-exhaustive marginalia-align 1255 | ('center `(+ center ,marginalia-align-offset)) 1256 | ('left `(+ left ,(+ marginalia-align-offset marginalia--cand-width-max))) 1257 | ('right `(+ right ,(+ marginalia-align-offset 1 1258 | (- (string-width ann 0 align) 1259 | (string-width ann))))))) 1260 | ann)) 1261 | (list cand "" ann)))) 1262 | 1263 | (defun marginalia--affixate (metadata annotator cands) 1264 | "Affixate CANDS given METADATA and Marginalia ANNOTATOR." 1265 | ;; Compute minimum width of windows, which display the minibuffer, including 1266 | ;; the miniwindow. In general the computed width corresponds to the full 1267 | ;; frame width, since the miniwindow spans the full frame. For example 1268 | ;; `vertico-buffer' displays the minibuffer in a separate window. Similarly, 1269 | ;; we could detect other types of completion buffers, e.g., Embark Collect or 1270 | ;; the default completion buffer, and compute smaller widths. 1271 | (let* ((width (cl-loop for win in (get-buffer-window-list) minimize (window-width win))) 1272 | (marginalia-field-width (min (/ width 2) marginalia-field-width)) 1273 | (marginalia--metadata metadata) 1274 | (cache marginalia--cache)) 1275 | (marginalia--align 1276 | ;; Run the annotators in the original window. `with-selected-window' 1277 | ;; is necessary because of `lookup-minor-mode-from-indicator'. 1278 | ;; Otherwise it would suffice to only change the current buffer. We 1279 | ;; need the `selected-window' fallback for Embark Occur. 1280 | (with-selected-window (or (minibuffer-selected-window) (selected-window)) 1281 | (cl-loop for cand in cands collect 1282 | (let ((ann (or (marginalia--cached cache annotator cand) ""))) 1283 | (cons cand (if (string-blank-p ann) "" ann)))))))) 1284 | 1285 | (defun marginalia--completion-metadata-get (metadata prop) 1286 | "Meant as :before-until advice for `completion-metadata-get'. 1287 | METADATA is the metadata. 1288 | PROP is the property which is looked up." 1289 | (pcase prop 1290 | ('annotation-function 1291 | ;; We do want the advice triggered for `completion-metadata-get'. 1292 | (when-let ((cat (completion-metadata-get metadata 'category)) 1293 | (annotator (marginalia--annotator cat))) 1294 | (lambda (cand) 1295 | (let ((ann (caddar (marginalia--affixate metadata annotator (list cand))))) 1296 | (and (not (equal ann "")) ann))))) 1297 | ('affixation-function 1298 | ;; We do want the advice triggered for `completion-metadata-get'. 1299 | (when-let ((cat (completion-metadata-get metadata 'category)) 1300 | (annotator (marginalia--annotator cat))) 1301 | (apply-partially #'marginalia--affixate metadata annotator))) 1302 | ('category 1303 | ;; Find the completion category by trying each of our classifiers. 1304 | ;; Store the metadata for `marginalia-classify-original-category'. 1305 | (let ((marginalia--metadata metadata)) 1306 | (run-hook-with-args-until-success 'marginalia-classifiers))))) 1307 | 1308 | (defun marginalia--minibuffer-setup () 1309 | "Setup the minibuffer for Marginalia. 1310 | Remember `this-command' for `marginalia-classify-by-command-name'." 1311 | (setq marginalia--cache t marginalia--command this-command) 1312 | ;; Reset cache if window size changes, recompute alignment 1313 | (add-hook 'window-state-change-hook #'marginalia--cache-reset nil 'local) 1314 | (add-hook 'context-menu-functions #'marginalia--context-menu nil t) 1315 | (marginalia--cache-reset)) 1316 | 1317 | (defun marginalia--base-position (completions) 1318 | "Record the base position of COMPLETIONS." 1319 | ;; As a small optimization we track the base position only for file 1320 | ;; completions, since `marginalia--full-candidate' is currently used only by 1321 | ;; the file annotation function. 1322 | ;; bug#75910: category instead of `minibuffer-completing-file-name' 1323 | (when minibuffer-completing-file-name 1324 | (let ((base (or (cdr (last completions)) 0))) 1325 | (unless (= marginalia--base-position base) 1326 | (marginalia--cache-reset) 1327 | (setq marginalia--base-position base 1328 | marginalia--cand-width-max (default-value 'marginalia--cand-width-max))))) 1329 | completions) 1330 | 1331 | ;;;###autoload 1332 | (define-minor-mode marginalia-mode 1333 | "Annotate completion candidates with richer information." 1334 | :global t :group 'marginalia 1335 | (if marginalia-mode 1336 | (progn 1337 | ;; Remember `this-command' in order to select the annotation function. 1338 | (add-hook 'minibuffer-setup-hook #'marginalia--minibuffer-setup) 1339 | ;; Replace the metadata function. 1340 | (advice-add (compat-function completion-metadata-get) :before-until #'marginalia--completion-metadata-get) 1341 | (advice-add #'completion-metadata-get :before-until #'marginalia--completion-metadata-get) 1342 | ;; Record completion base position, for `marginalia--full-candidate' 1343 | (advice-add #'completion-all-completions :filter-return #'marginalia--base-position)) 1344 | (advice-remove #'completion-all-completions #'marginalia--base-position) 1345 | (advice-remove (compat-function completion-metadata-get) #'marginalia--completion-metadata-get) 1346 | (advice-remove #'completion-metadata-get #'marginalia--completion-metadata-get) 1347 | (remove-hook 'minibuffer-setup-hook #'marginalia--minibuffer-setup))) 1348 | 1349 | (defun marginalia--completion-metadata () 1350 | "Get completion metadata." 1351 | (let* ((end (minibuffer-prompt-end)) 1352 | (pt (max 0 (- (point) end)))) 1353 | (completion-metadata (buffer-substring-no-properties end (+ end pt)) 1354 | minibuffer-completion-table 1355 | minibuffer-completion-predicate))) 1356 | 1357 | (defun marginalia--builtin-annotator-p (md) 1358 | "Builtin annotator available in metadata MD?" 1359 | (or (marginalia--orig-completion-metadata-get md 'annotation-function) 1360 | (marginalia--orig-completion-metadata-get md 'affixation-function))) 1361 | 1362 | ;;;###autoload 1363 | (defun marginalia-cycle () 1364 | "Cycle between annotators in `marginalia-annotators'." 1365 | ;; Only show `marginalia-cycle' in M-x in recursive minibuffers 1366 | (declare (completion (lambda (&rest _) (> (minibuffer-depth) 1)))) 1367 | (interactive) 1368 | (with-current-buffer (window-buffer 1369 | (or (active-minibuffer-window) 1370 | (user-error "Marginalia: No active minibuffer"))) 1371 | (let* ((md (marginalia--completion-metadata)) 1372 | (cat (or (completion-metadata-get md 'category) 1373 | (user-error "Marginalia: Unknown completion category"))) 1374 | (ann (or (assq cat marginalia-annotators) 1375 | (user-error "Marginalia: No annotators found for category `%s'" cat)))) 1376 | (setcdr ann (append (cddr ann) (list (cadr ann)))) 1377 | ;; When the builtin annotator is selected and no builtin function is 1378 | ;; available, skip to the next annotator. Bypass the 1379 | ;; `marginalia--completion-metadata-get' advice. 1380 | (when (and (eq (cadr ann) 'builtin) (not (marginalia--builtin-annotator-p md))) 1381 | (setcdr ann (append (cddr ann) (list (cadr ann))))) 1382 | (marginalia--cache-reset) 1383 | (message "Marginalia: Use annotator `%s' for category `%s'" (cadr ann) cat)))) 1384 | 1385 | (defun marginalia--context-menu (menu _event) 1386 | "Add Marginalia commands to context MENU." 1387 | (when-let ((md (marginalia--completion-metadata)) 1388 | (cat (completion-metadata-get md 'category)) 1389 | (ann (assq cat marginalia-annotators)) 1390 | (items (cl-loop 1391 | for fun in (cdr ann) for i from 0 1392 | if (or (not (eq fun 'builtin)) (marginalia--builtin-annotator-p md)) 1393 | collect 1394 | (vector 1395 | (thread-last (symbol-name fun) 1396 | (replace-regexp-in-string ".*?-+annotate-+" "") 1397 | (replace-regexp-in-string "-+" " ") 1398 | capitalize) 1399 | (let ((i i)) 1400 | (lambda () 1401 | (interactive) 1402 | (setcdr ann (append (drop i (cdr ann)) (take i (cdr ann)))) 1403 | (marginalia--cache-reset) 1404 | (message "Marginalia: Use annotator `%s' for category `%s'" (cadr ann) cat))) 1405 | :style 'radio :selected (eq fun (cadr ann)))))) 1406 | (define-key menu [marginalia] 1407 | `("Marginalia" . ,(easy-menu-create-menu 1408 | "" `(["Cycle" marginalia-cycle] "---" ,@items))))) 1409 | menu) 1410 | 1411 | (provide 'marginalia) 1412 | ;;; marginalia.el ends here 1413 | --------------------------------------------------------------------------------