Skip to content

Instantly share code, notes, and snippets.

@cxa
Created November 7, 2023 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cxa/560e1756217be30133f124eb788983d7 to your computer and use it in GitHub Desktop.
Save cxa/560e1756217be30133f124eb788983d7 to your computer and use it in GitHub Desktop.
My orderless config

Orderless

Orderless is a robust improvement to the Emacs completion system, offering high adaptability through the configuration of dispatchers.

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 as ~regex'~:
    (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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment