branch: externals/orderless
commit d1c0cbf87ba7d927ec64c161ec3aac65b9643b61
Author: Omar Antolín <[email protected]>
Commit: Omar Antolín <[email protected]>
Add matching style and dispatcher per category overrides (fix #31)
---
README.org | 25 +++++++++++++++++++++++++
orderless.el | 54 +++++++++++++++++++++++++++++++++++++++++++-----------
orderless.texi | 24 ++++++++++++++++++++++++
3 files changed, 92 insertions(+), 11 deletions(-)
diff --git a/README.org b/README.org
index b3c912737f..b66786c636 100644
--- a/README.org
+++ b/README.org
@@ -157,6 +157,16 @@ The variable =orderless-matching-styles= can be set to a
list of the
desired matching styles to use. By default it enables the regexp and
initialism styles.
+If you want to use certain matching styles only for completion of
+certain categories, you can add an entry to the built-in variable
+=completion-category-overrides=. For example, to use =orderless-flex= to
+complete buffer names, you could use:
+
+#+begin_src emacs-lisp
+ (add-to-list 'completion-category-overrides
+ '(buffer (orderless-matching-styles orderless-flex)))
+#+end_src
+
*** Style dispatchers
For more fine-grained control on which matching styles to use for
@@ -205,6 +215,21 @@ initialism styles.
without-if-bang))
#+end_src
+As with matching styles, you can set which style dispatchers to use
+for specific completion catgories using the
+=completion-category-overrides= variable. For example, if you wanted to
+match buffer using the =orderless-flex= matching style except for
+components starting with =!=, which you want to treat as excluded terms,
+you could use the following configuration (which relies on the above
+=without-if-bang= dispatcher):
+
+#+begin_src emacs-lisp
+ (add-to-list 'completion-category-overrides
+ '(buffer (orderless-matching-styles orderless-flex)
+ (orderless-style-dispatchers without-if-bang)))
+#+end_src
+
+
** Component separator regexp
The pattern components are space-separated by default: this is
diff --git a/orderless.el b/orderless.el
index d4fe98a1e1..b92e6dd5d4 100644
--- a/orderless.el
+++ b/orderless.el
@@ -415,22 +415,54 @@ DISPATCHERS decline to handle the component, then the
list of
matching STYLES is used. See `orderless-dispatch' for details on
dispatchers.
-The STYLES default to `orderless-matching-styles', and the
-DISPATCHERS default to `orderless-dipatchers'. Since nil gets you
-the default, if want to no dispatchers to be run, use '(ignore)
-as the value of DISPATCHERS.
+If the variable `orderless-transient-component-separator' is
+non-nil, it is used in place of `orderless-component-separator'.
-The `orderless-transient-*' variables, when non-nil, override the
-corresponding value among `orderless-component-separator', STYLES
-and DISPATCHERS.
+When STYLES is nil, it defaults to a list computed as follows:
+
+- if the value of `orderless-transient-matching-styles' is
+ non-nil, this value is used;
+
+- next, the category of the current minibuffer completion session
+ is looked up in `completion-category-overrides' and if the
+ alist associated to it has an `orderless-matching-styles' key,
+ the corresponding value is used;
+
+- otherwise, STYLES defaults to the value of the variable
+ `orderless-matching-styles'.
+
+The analogous process is used if DISPATCHERS is nil. Since nil
+gets you this default, if want to no dispatchers to be run, use
+'(ignore) as the value of DISPATCHERS.
This function is the default for `orderless-pattern-compiler' and
might come in handy as a subroutine to implement other pattern
compilers."
- (unless styles (setq styles orderless-matching-styles))
- (setq styles (or orderless-transient-matching-styles styles))
- (unless dispatchers (setq dispatchers orderless-style-dispatchers))
- (setq dispatchers (or orderless-transient-style-dispatchers dispatchers))
+
+ ;; figure out defaults for styles and dispatchers
+ (let ((overrides
+ (unless (or (not minibuffer-completion-table)
+ (and (or styles orderless-transient-matching-styles)
+ (or dispatchers
orderless-transient-style-dispatchers)))
+ ;; we are in minibuffer completion and at least one out of
+ ;; styles and dispachers might be overridden in
+ ;; completion-category-overrides
+ (let* ((metadata (completion-metadata
+ (buffer-substring-no-properties
+ (field-beginning) (point))
+ minibuffer-completion-table
+ minibuffer-completion-predicate))
+ (category (completion-metadata-get metadata 'category)))
+ (cdr (assq category completion-category-overrides))))))
+ (setq styles (or styles
+ orderless-transient-matching-styles
+ (cdr (assq 'orderless-matching-styles overrides))
+ orderless-matching-styles))
+ (setq dispatchers (or dispatchers
+ orderless-transient-style-dispatchers
+ (cdr (assq 'orderless-style-dispatchers overrides))
+ orderless-style-dispatchers)))
+
(cl-loop
with splitter = (or orderless-transient-component-separator
orderless-component-separator)
diff --git a/orderless.texi b/orderless.texi
index d5361c7ac3..0aa29c6b02 100644
--- a/orderless.texi
+++ b/orderless.texi
@@ -197,6 +197,16 @@ The variable @samp{orderless-matching-styles} can be set
to a list of the
desired matching styles to use. By default it enables the regexp and
initialism styles.
+If you want to use certain matching styles only for completion of
+certain categories, you can add an entry to the built-in variable
+@samp{completion-category-overrides}. For example, to use
@samp{orderless-flex} to
+complete buffer names, you could use:
+
+@lisp
+(add-to-list 'completion-category-overrides
+ '(buffer (orderless-matching-styles orderless-flex)))
+@end lisp
+
@menu
* Style dispatchers::
@end menu
@@ -256,6 +266,20 @@ You can achieve this with the following configuration:
without-if-bang))
@end lisp
+As with matching styles, you can set which style dispatchers to use
+for specific completion catgories using the
+@samp{completion-category-overrides} variable. For example, if you wanted to
+match buffer using the @samp{orderless-flex} matching style except for
+components starting with @samp{!}, which you want to treat as excluded terms,
+you could use the following configuration (which relies on the above
+@samp{without-if-bang} dispatcher):
+
+@lisp
+(add-to-list 'completion-category-overrides
+ '(buffer (orderless-matching-styles orderless-flex)
+ (orderless-style-dispatchers without-if-bang)))
+@end lisp
+
@node Component separator regexp
@section Component separator regexp