Orderless is a robust improvement to the Emacs completion system, offering high adaptability through the configuration of dispatchers.
I often utilize the following completion style:
- For strings that contain
`
(regardless of position), treat them as\`regex
:(defun sloth/orderless-circumflex-dispatcher (pattern _index _total) (let ((indicator "`")) (when (string-search indicator pattern) `(orderless-regexp . ,(concat "\\" indicator (string-replace indicator "" pattern))))))
- For strings that contain
'
(regardless of position), treat them asregex\'
:(defun sloth/orderless-dollar-dispatcher (pattern _index _total) "Make `'' work with consult." ;; https://www.reddit.com/r/emacs/comments/n43mdo/comment/i3iu2yq (let ((indicator "'") (tofu-chars (if (and (minibufferp) (featurep 'consult)) (format "[%c-%c]" consult--tofu-char (+ consult--tofu-char consult--tofu-range -1)) ""))) (when (string-search indicator pattern) `(orderless-regexp . ,(concat (string-replace indicator "" pattern) tofu-chars "*\\" indicator)))))
- For strings that contain
*
, complete with flex:(defun sloth/orderless-flex-dispatcher (pattern _index _total) (let ((indicator "*")) (when (string-search indicator pattern) `(orderless-flex . ,(string-replace indicator "" pattern)))))
- For strings that contain ===, complete with literal:
(defun sloth/orderless-literal-dispatcher (pattern _index _total) (let ((indicator "=")) (when (string-search indicator pattern) `(orderless-literal . ,(string-replace indicator "" pattern)))))
- For strings that contain
,
, complete with initialism:(defun sloth/orderless-initialism-dispatcher (pattern _index _total) (let ((indicator ",")) (when (string-search indicator pattern) `(orderless-initialism . ,(string-replace indicator "" pattern)))))
- For strings that contain
!
, complete with excluding:(defun sloth/orderless-exclude-dispatcher (pattern _index _total) (let ((indicator "!")) (when (string-search indicator pattern) `(orderless-without-literal . ,(string-replace indicator "" pattern)))))
Now, let’s configure Orderless to accommodate the dispatchers mentioned above:
(use-package orderless
:demand t
:custom
(orderless-component-separator #'split-string-and-unquote)
(completion-styles '(orderless basic))
(orderless-matching-styles '(orderless-regexp))
(orderless-style-dispatchers '(sloth/orderless-flex-dispatcher
sloth/orderless-literal-dispatcher
sloth/orderless-initialism-dispatcher
sloth/orderless-dollar-dispatcher
sloth/orderless-circumflex-dispatcher
sloth/orderless-exclude-dispatcher))
:bind (:map minibuffer-local-completion-map
("SPC" . self-insert-command)))