orderless.info (25629B)
1 This is orderless.info, produced by makeinfo version 6.7 from 2 orderless.texi. 3 4 INFO-DIR-SECTION Emacs misc features 5 START-INFO-DIR-ENTRY 6 * Orderless: (orderless). Completion style for matching regexps in any order. 7 END-INFO-DIR-ENTRY 8 9 10 File: orderless.info, Node: Top, Next: Overview, Up: (dir) 11 12 Orderless 13 ********* 14 15 * Menu: 16 17 * Overview:: 18 * Customization:: 19 * Integration with other completion UIs:: 20 * Related packages:: 21 22 — The Detailed Node Listing — 23 24 Customization 25 26 * Component matching styles:: 27 * Component separator regexp:: 28 * Defining custom orderless styles:: 29 * Faces for component matches:: 30 * Pattern compiler:: 31 * Interactively changing the configuration:: 32 33 Component matching styles 34 35 * Style modifiers:: 36 * Style dispatchers:: 37 38 Integration with other completion UIs 39 40 * Ivy:: 41 * Helm:: 42 * Company:: 43 44 Related packages 45 46 * Ivy and Helm:: 47 * Prescient:: 48 * Restricting to current matches in Icicles, Ido and Ivy: Restricting to current matches in Icicles Ido and Ivy. 49 50 51 52 File: orderless.info, Node: Overview, Next: Customization, Prev: Top, Up: Top 53 54 1 Overview 55 ********** 56 57 This package provides an ‘orderless’ _completion style_ that divides the 58 pattern into space-separated components, and matches candidates that 59 match all of the components in any order. Each component can match in 60 any one of several ways: literally, as a regexp, as an initialism, in 61 the flex style, or as multiple word prefixes. By default, regexp and 62 literal matches are enabled. 63 64 A completion style is a back-end for completion and is used from a 65 front-end that provides a completion UI. Any completion style can be 66 used with the default Emacs completion UI (sometimes called minibuffer 67 tab completion), with the built-in Icomplete package (which is similar 68 to the more well-known Ido Mode), the icomplete-vertical variant from 69 Emacs 28 (see the external icomplete-vertical 70 (https://github.com/oantolin/icomplete-vertical) package to get that 71 functionality on earlier versions of Emacs), or with some third party 72 minibuffer completion frameworks such as Mct 73 (https://gitlab.com/protesilaos/mct) or Vertico 74 (https://github.com/minad/vertico). 75 76 All the completion UIs just mentioned are for minibuffer completion, 77 used when Emacs commands prompt the user in the minibuffer for some 78 input, but there is also completion at point in normal buffers, 79 typically used for identifiers in programming languages. Completion 80 styles can also be used for that purpose by completion at point UIs such 81 as Corfu (https://github.com/minad/corfu), Company 82 (https://company-mode.github.io/) or the function 83 ‘consult-completion-in-region’ from Consult 84 (https://github.com/minad/consult). 85 86 To use a completion style with any of the above mentioned completion 87 UIs simply add it as an entry in the variables ‘completion-styles’ and 88 ‘completion-category-overrides’ and ‘completion-category-defaults’ (see 89 their documentation). 90 91 The ‘completion-category-defaults’ variable serves as a default value 92 for ‘completion-category-overrides’. If you want to use ‘orderless’ 93 exclusively, set both variables to ‘nil’, but be aware that 94 ‘completion-category-defaults’ is modified by packages at load time. 95 96 With a bit of effort, it might still be possible to use ‘orderless’ 97 with other completion UIs, even if those UIs don’t support the standard 98 Emacs completion styles. Currently there is support for Ivy 99 (https://github.com/abo-abo/swiper) (see below). Also, while Company 100 does support completion styles directly, pressing ‘SPC’ takes you out of 101 completion, so comfortably using ‘orderless’ with it takes a bit of 102 configuration (see below). 103 104 If you use ELPA or MELPA, the easiest way to install ‘orderless’ is 105 via ‘package-install’. If you use ‘use-package’, you can use: 106 107 (use-package orderless 108 :ensure t 109 :custom 110 (completion-styles '(orderless basic)) 111 (completion-category-overrides '((file (styles basic partial-completion))))) 112 113 Alternatively, put ‘orderless.el’ somewhere on your ‘load-path’, and 114 use the following configuration: 115 116 (require 'orderless) 117 (setq completion-styles '(orderless basic) 118 completion-category-overrides '((file (styles basic partial-completion)))) 119 120 The ‘basic’ completion style is specified as fallback in addition to 121 ‘orderless’ in order to ensure that completion commands which rely on 122 dynamic completion tables, e.g., ‘completion-table-dynamic’ or 123 ‘completion-table-in-turn’, work correctly. Furthermore the ‘basic’ 124 completion style needs to be tried _first_ (not as a fallback) for TRAMP 125 hostname completion to work. In order to achieve that, we add an entry 126 for the ‘file’ completion category in the 127 ‘completion-category-overrides’ variable. In addition, the 128 ‘partial-completion’ style allows you to use wildcards for file 129 completion and partial paths, e.g., ‘/u/s/l’ for ‘/usr/share/local’. 130 131 Bug reports are highly welcome and appreciated! 132 133 134 File: orderless.info, Node: Customization, Next: Integration with other completion UIs, Prev: Overview, Up: Top 135 136 2 Customization 137 *************** 138 139 * Menu: 140 141 * Component matching styles:: 142 * Component separator regexp:: 143 * Defining custom orderless styles:: 144 * Faces for component matches:: 145 * Pattern compiler:: 146 * Interactively changing the configuration:: 147 148 149 File: orderless.info, Node: Component matching styles, Next: Component separator regexp, Up: Customization 150 151 2.1 Component matching styles 152 ============================= 153 154 Each component of a pattern can match in any of several matching styles. 155 A matching style is a function from strings to regexps or predicates, so 156 it is easy to define new matching styles. The value returned by a 157 matching style can be either a regexp as a string, an s-expression in 158 ‘rx’ syntax or a predicate function. The predefined matching styles 159 are: 160 161 orderless-regexp 162 the component is treated as a regexp that must match somewhere in 163 the candidate. 164 165 If the component is not a valid regexp, it is ignored. 166 167 orderless-literal 168 the component is treated as a literal string that must occur in the 169 candidate. 170 171 orderless-literal-prefix 172 the component is treated as a literal string that must occur as a 173 prefix of a candidate. 174 175 orderless-prefixes 176 the component is split at word endings and each piece must match at 177 a word boundary in the candidate, occurring in that order. 178 179 This is similar to the built-in ‘partial-completion’ 180 completion-style. For example, ‘re-re’ matches 181 ‘query-replace-regexp’, ‘recode-region’ and 182 ‘magit-remote-list-refs’; ‘f-d.t’ matches ‘final-draft.txt’. 183 184 orderless-initialism 185 each character of the component should appear as the beginning of a 186 word in the candidate, in order. 187 188 This maps ‘abc’ to ‘\<a.*\<b.*\c’. 189 190 orderless-flex 191 the characters of the component should appear in that order in the 192 candidate, but not necessarily consecutively. 193 194 This maps ‘abc’ to ‘a.*b.*c’. 195 196 *orderless-without-literal* 197 the component is a treated as a literal string that must *not* 198 occur in the candidate. 199 200 Nothing is highlighted by this style. This style should not be 201 used directly in ‘orderless-matching-styles’ but with a style 202 dispatcher instead. See also the more general style modifier 203 ‘orderless-not’. 204 205 The variable ‘orderless-matching-styles’ can be set to a list of the 206 desired matching styles to use. By default it enables the literal and 207 regexp styles. 208 209 * Menu: 210 211 * Style modifiers:: 212 * Style dispatchers:: 213 214 215 File: orderless.info, Node: Style modifiers, Next: Style dispatchers, Up: Component matching styles 216 217 2.1.1 Style modifiers 218 --------------------- 219 220 Style modifiers are functions which take a predicate function and a 221 regular expression as a string and return a new predicate function. 222 Style modifiers should not be used directly in 223 ‘orderless-matching-styles’ but with a style dispatcher instead. 224 225 orderless-annotation 226 this style modifier matches the pattern against the annotation 227 string of the candidate, instead of against the candidate string. 228 229 orderless-not 230 this style modifier inverts the pattern, such that candidates pass 231 which do not match the pattern. 232 233 234 File: orderless.info, Node: Style dispatchers, Prev: Style modifiers, Up: Component matching styles 235 236 2.1.2 Style dispatchers 237 ----------------------- 238 239 For more fine-grained control on which matching styles to use for each 240 component of the input string, you can customize the variable 241 ‘orderless-style-dispatchers’. You can use this feature to define your 242 own “query syntax”. For example, the default value of 243 ‘orderless-style-dispatchers’ lists a single dispatcher called 244 ‘orderless-affix-dispatch’ which enables a simple syntax based on 245 special characters used as either a prefix or suffix: 246 247 • ! modifies the component with ‘orderless-not’. Both ‘!bad’ and 248 ‘bad!’ will match strings that do _not_ contain the pattern ‘bad’. 249 • & modifies the component with ‘orderless-annotation’. The pattern 250 will match against the candidate’s annotation (cheesy mnemonic: 251 andnotation!). 252 • , uses ‘orderless-initialism’. 253 • = uses ‘orderless-literal’. 254 • ^ uses ‘orderless-literal-prefix’. 255 • ~ uses ‘orderless-flex’. 256 • % makes the string match ignoring diacritics and similar 257 inflections on characters (it uses the function 258 ‘char-fold-to-regexp’ to do this). 259 260 You can add, remove or change this mapping between affix characters 261 and matching styles by customizing the user option 262 ‘orderless-affix-dispatch-alist’. Most users will probably find this 263 type of customization sufficient for their query syntax needs, but for 264 those desiring further control the rest of this section explains how to 265 implement your own style dispatchers. 266 267 Style dispatchers are functions which take a component, its index in 268 the list of components (starting from 0), and the total number of 269 components, and are used to determine the matching styles used for that 270 specific component, overriding the default matching styles. 271 272 A style dispatcher can either decline to handle the input string or 273 component, or it can return which matching styles to use. It can also, 274 if desired, additionally return a new string to use in place of the 275 given one. Consult the documentation of ‘orderless--dispatch’ for full 276 details. 277 278 As an example of writing your own dispatchers, say you wanted the 279 following setup: 280 281 • you normally want components to match as regexps, 282 • except for the first component, which should always match as an 283 initialism —this is pretty useful for, say, 284 ‘execute-extended-command’ (‘M-x’) or ‘describe-function’ (‘C-h 285 f’), 286 • later components ending in ‘~’ should match (the characters other 287 than the final ‘~’) in the flex style, and 288 • later components starting with ‘!’ should indicate the rest of the 289 component is a literal string not contained in the candidate (this 290 is part of the functionality of the default configuration). 291 292 You can achieve this with the following configuration: 293 294 (defun flex-if-twiddle (pattern _index _total) 295 (when (string-suffix-p "~" pattern) 296 `(orderless-flex . ,(substring pattern 0 -1)))) 297 298 (defun first-initialism (pattern index _total) 299 (if (= index 0) 'orderless-initialism)) 300 301 (defun not-if-bang (pattern _index _total) 302 (cond 303 ((equal "!" pattern) 304 #'ignore) 305 ((string-prefix-p "!" pattern) 306 `(orderless-not . ,(substring pattern 1))))) 307 308 (setq orderless-matching-styles '(orderless-regexp) 309 orderless-style-dispatchers '(first-initialism 310 flex-if-twiddle 311 not-if-bang)) 312 313 314 File: orderless.info, Node: Component separator regexp, Next: Defining custom orderless styles, Prev: Component matching styles, Up: Customization 315 316 2.2 Component separator regexp 317 ============================== 318 319 The pattern components are space-separated by default: this is 320 controlled by the variable ‘orderless-component-separator’, which should 321 be set either to a regexp that matches the desired component separator, 322 or to a function that takes a string and returns the list of components. 323 The default value is a regexp matches a non-empty sequence of spaces. 324 It may be useful to add hyphens or slashes (or both), to match symbols 325 or file paths, respectively. 326 327 Even if you want to split on spaces you might want to be able to 328 escape those spaces or to enclose space in double quotes (as in shell 329 argument parsing). For backslash-escaped spaces set 330 ‘orderless-component-separator’ to the function 331 ‘orderless-escapable-split-on-space’; for shell-like double-quotable 332 space, set it to the standard Emacs function ‘split-string-and-unquote’. 333 334 If you are implementing a command for which you know you want a 335 different separator for the components, bind 336 ‘orderless-component-separator’ in a ‘let’ form. 337 338 339 File: orderless.info, Node: Defining custom orderless styles, Next: Faces for component matches, Prev: Component separator regexp, Up: Customization 340 341 2.3 Defining custom orderless styles 342 ==================================== 343 344 Orderless allows the definition of custom completion styles using the 345 ‘orderless-define-completion-style’ macro. Any Orderless configuration 346 variable can be adjusted locally for the new style, e.g., 347 ‘orderless-matching-styles’. 348 349 By default Orderless only enables the regexp and literal matching 350 styles. In the following example an ‘orderless+initialism’ style is 351 defined, which additionally enables initialism matching. This 352 completion style can then used when matching candidates of the symbol or 353 command completion category. 354 355 (orderless-define-completion-style orderless+initialism 356 (orderless-matching-styles '(orderless-initialism 357 orderless-literal 358 orderless-regexp))) 359 (setq completion-category-overrides 360 '((command (styles orderless+initialism)) 361 (symbol (styles orderless+initialism)) 362 (variable (styles orderless+initialism)))) 363 364 Note that in order for the ‘orderless+initialism’ style to kick-in 365 with the above configuration, you’d need to use commands whose metadata 366 indicates that the completion candidates are commands or symbols. In 367 Emacs 28, ‘execute-extended-command’ has metadata indicating you are 368 selecting a command, but earlier versions of Emacs lack this metadata. 369 Activating ‘marginalia-mode’ from the Marginalia 370 (https://github.com/minad/marginalia) package provides this metadata 371 automatically for many built-in commands and is recommended if you use 372 the above example configuration, or other similarly fine-grained control 373 of completion styles according to completion category. 374 375 376 File: orderless.info, Node: Faces for component matches, Next: Pattern compiler, Prev: Defining custom orderless styles, Up: Customization 377 378 2.4 Faces for component matches 379 =============================== 380 381 The portions of a candidate matching each component get highlighted in 382 one of four faces, ‘orderless-match-face-?’ where ‘?’ is a number from 0 383 to 3. If the pattern has more than four components, the faces get 384 reused cyclically. 385 386 If your ‘completion-styles’ (or ‘completion-category-overrides’ for 387 some particular category) has more than one entry, remember than Emacs 388 tries each completion style in turn and uses the first one returning 389 matches. You will only see these particular faces when the ‘orderless’ 390 completion is the one that ends up being used, of course. 391 392 393 File: orderless.info, Node: Pattern compiler, Next: Interactively changing the configuration, Prev: Faces for component matches, Up: Customization 394 395 2.5 Pattern compiler 396 ==================== 397 398 The default mechanism for turning an input string into a predicate and a 399 list of regexps to match against, configured using 400 ‘orderless-matching-styles’, is probably flexible enough for the vast 401 majority of users. The patterns are compiled by ‘orderless-compile’. 402 Under special circumstances it may be useful to implement a custom 403 pattern compiler by advising ‘orderless-compile’. 404 405 406 File: orderless.info, Node: Interactively changing the configuration, Prev: Pattern compiler, Up: Customization 407 408 2.6 Interactively changing the configuration 409 ============================================ 410 411 You might want to change the separator or the matching style 412 configuration on the fly while matching. There many possible user 413 interfaces for this: you could toggle between two chosen configurations, 414 cycle among several, have a keymap where each key sets a different 415 configurations, have a set of named configurations and be prompted (with 416 completion) for one of them, popup a hydra 417 (https://github.com/abo-abo/hydra) to choose a configuration, etc. 418 Since there are so many possible UIs and which to use is mostly a matter 419 of taste, ‘orderless’ does not provide any such commands. But it’s easy 420 to write your own! 421 422 For example, say you want to use the keybinding ‘C-l’ to make all 423 components match literally. You could use the following code: 424 425 (defun my/match-components-literally () 426 "Components match literally for the rest of the session." 427 (interactive) 428 (setq-local orderless-matching-styles '(orderless-literal) 429 orderless-style-dispatchers nil)) 430 431 (define-key minibuffer-local-completion-map (kbd "C-l") 432 #'my/match-components-literally) 433 434 Using ‘setq-local’ to assign to the configuration variables ensures 435 the values are only used for that minibuffer completion session. 436 437 438 File: orderless.info, Node: Integration with other completion UIs, Next: Related packages, Prev: Customization, Up: Top 439 440 3 Integration with other completion UIs 441 *************************************** 442 443 Several excellent completion UIs exist for Emacs in third party 444 packages. They do have a tendency to forsake standard Emacs APIs, so 445 integration with them must be done on a case by case basis. 446 447 If you manage to use ‘orderless’ with a completion UI not listed 448 here, please file an issue or make a pull request so others can benefit 449 from your effort. The functions ‘orderless-filter’, 450 ‘orderless-highlight-matches’, ‘orderless--highlight’ and 451 ‘orderless--component-regexps’ are likely to help with the integration. 452 453 * Menu: 454 455 * Ivy:: 456 * Helm:: 457 * Company:: 458 459 460 File: orderless.info, Node: Ivy, Next: Helm, Up: Integration with other completion UIs 461 462 3.1 Ivy 463 ======= 464 465 To use ‘orderless’ from Ivy add this to your Ivy configuration: 466 467 (setq ivy-re-builders-alist '((t . orderless-ivy-re-builder))) 468 (add-to-list 'ivy-highlight-functions-alist '(orderless-ivy-re-builder . orderless-ivy-highlight)) 469 470 471 File: orderless.info, Node: Helm, Next: Company, Prev: Ivy, Up: Integration with other completion UIs 472 473 3.2 Helm 474 ======== 475 476 To use ‘orderless’ from Helm, simply configure ‘orderless’ as you would 477 for completion UIs that use Emacs completion styles and add this to your 478 Helm configuration: 479 480 (setq helm-completion-style 'emacs) 481 482 483 File: orderless.info, Node: Company, Prev: Helm, Up: Integration with other completion UIs 484 485 3.3 Company 486 =========== 487 488 Company comes with a ‘company-capf’ backend that uses the 489 completion-at-point functions, which in turn use completion styles. 490 This means that the ‘company-capf’ backend will automatically use 491 ‘orderless’, no configuration necessary! 492 493 But there are a couple of points of discomfort: 494 495 1. Pressing SPC takes you out of completion, so with the default 496 separator you are limited to one component, which is no fun. To 497 fix this add a separator that is allowed to occur in identifiers, 498 for example, for Emacs Lisp code you could use an ampersand: 499 500 (setq orderless-component-separator "[ &]") 501 502 2. The matching portions of candidates aren’t highlighted. That’s 503 because ‘company-capf’ is hard-coded to look for the 504 ‘completions-common-part’ face, and it only use one face, 505 ‘company-echo-common’ to highlight candidates. 506 507 So, while you can’t get different faces for different components, 508 you can at least get the matches highlighted in the sole available 509 face with this configuration: 510 511 (defun just-one-face (fn &rest args) 512 (let ((orderless-match-faces [completions-common-part])) 513 (apply fn args))) 514 515 (advice-add 'company-capf--candidates :around #'just-one-face) 516 517 (Aren’t dynamically scoped variables and the advice system nifty?) 518 519 If you would like to use different ‘completion-styles’ with 520 ‘company-capf’ instead, you can add this to your configuration: 521 522 ;; We follow a suggestion by company maintainer u/hvis: 523 ;; https://www.reddit.com/r/emacs/comments/nichkl/comment/gz1jr3s/ 524 (defun company-completion-styles (capf-fn &rest args) 525 (let ((completion-styles '(basic partial-completion))) 526 (apply capf-fn args)) 527 528 (advice-add 'company-capf :around #'company-completion-styles) 529 530 531 File: orderless.info, Node: Related packages, Prev: Integration with other completion UIs, Up: Top 532 533 4 Related packages 534 ****************** 535 536 * Menu: 537 538 * Ivy and Helm:: 539 * Prescient:: 540 * Restricting to current matches in Icicles, Ido and Ivy: Restricting to current matches in Icicles Ido and Ivy. 541 542 543 File: orderless.info, Node: Ivy and Helm, Next: Prescient, Up: Related packages 544 545 4.1 Ivy and Helm 546 ================ 547 548 The well-known and hugely powerful completion frameworks Ivy 549 (https://github.com/abo-abo/swiper) and Helm 550 (https://github.com/emacs-helm/helm) also provide for matching 551 space-separated component regexps in any order. In Ivy, this is done 552 with the ‘ivy--regex-ignore-order’ matcher. In Helm, it is the default, 553 called “multi pattern matching”. 554 555 This package is significantly smaller than either of those because it 556 solely defines a completion style, meant to be used with any completion 557 UI supporting completion styles while both of those provide their own 558 completion UI (and many other cool features!). 559 560 It is worth pointing out that Helm does provide its multi pattern 561 matching as a completion style which could be used with default tab 562 completion, Icomplete or other UIs supporting completion styles! (Ivy 563 does not provide a completion style to my knowledge.) So, for example, 564 Icomplete users could, instead of using this package, install Helm and 565 configure Icomplete to use it as follows: 566 567 (require 'helm) 568 (setq completion-styles '(helm basic)) 569 (icomplete-mode) 570 571 (Of course, if you install Helm, you might as well use the Helm UI in 572 ‘helm-mode’ rather than Icomplete.) 573 574 575 File: orderless.info, Node: Prescient, Next: Restricting to current matches in Icicles Ido and Ivy, Prev: Ivy and Helm, Up: Related packages 576 577 4.2 Prescient 578 ============= 579 580 The prescient.el (https://github.com/radian-software/prescient.el) 581 library also provides matching of space-separated components in any 582 order. It offers a completion-style that can be used with Emacs’ 583 default completion UI, Mct, Vertico or with Icomplete. Furthermore Ivy 584 is supported. The components can be matched literally, as regexps, as 585 initialisms or in the flex style (called “fuzzy” in prescient). 586 Prescient does not offer the same flexibility as Orderless with its 587 style dispatchers. However in addition to matching, Prescient supports 588 sorting of candidates, while Orderless leaves that up to the candidate 589 source and the completion UI. 590 591 592 File: orderless.info, Node: Restricting to current matches in Icicles Ido and Ivy, Prev: Prescient, Up: Related packages 593 594 4.3 Restricting to current matches in Icicles, Ido and Ivy 595 ========================================================== 596 597 An effect equivalent to matching multiple components in any order can be 598 achieved in completion frameworks that provide a way to restrict further 599 matching to the current list of candidates. If you use the keybinding 600 for restriction instead of ‘SPC’ to separate your components, you get 601 out of order matching! 602 603 • Icicles (https://www.emacswiki.org/emacs/Icicles) calls this 604 _progressive completion_ and uses the 605 ‘icicle-apropos-complete-and-narrow’ command, bound to ‘S-SPC’, to 606 do it. 607 608 • Ido has ‘ido-restrict-to-matches’ and binds it to ‘C-SPC’. 609 610 • Ivy has ‘ivy-restrict-to-matches’, bound to ‘S-SPC’, so you can get 611 the effect of out of order matching without using 612 ‘ivy--regex-ignore-order’. 613 614 615 616 Tag Table: 617 Node: Top235 618 Node: Overview975 619 Node: Customization5028 620 Node: Component matching styles5386 621 Node: Style modifiers7677 622 Node: Style dispatchers8366 623 Node: Component separator regexp12030 624 Node: Defining custom orderless styles13267 625 Node: Faces for component matches15159 626 Node: Pattern compiler15963 627 Node: Interactively changing the configuration16554 628 Node: Integration with other completion UIs18017 629 Node: Ivy18803 630 Node: Helm19155 631 Node: Company19499 632 Node: Related packages21478 633 Node: Ivy and Helm21776 634 Node: Prescient23110 635 Node: Restricting to current matches in Icicles Ido and Ivy23943 636 637 End Tag Table 638 639 640 Local Variables: 641 coding: utf-8 642 End: