oantolin / orderless

Emacs completion style that matches multiple regexps in any order

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to disable `orderless` for some specific regions or type of hints?

hongyi-zhao opened this issue · comments

On Ubuntu 20.04.3 LTS, I use the latest self-compiled git master version of Emacs and the following orderless configuration:

(use-package orderless
  :config
  (setq
   ;; https://github.com/oantolin/orderless#ivy
   ivy-re-builders-alist '((t . orderless-ivy-re-builder))

   ;; Configure a custom style dispatcher (see the Consult wiki)
   ;; orderless-style-dispatchers '(+orderless-dispatch)
   ;; orderless-component-separator #'orderless-escapable-split-on-space

   completion-styles '(orderless)
   completion-category-defaults nil
   completion-category-overrides '((file (styles . (partial-completion)))))
  )

This produces some undesirable results, such as the following candidate list when I want to enter a use-package:

image

As you can see, this will make my selection of target candidates more difficult than convenient. Any hints for disabling the orderless for some specific regions/hints, say the above one?

Regards,
HZ

You can just advise company-calculate-candidates to use any completion styles you prefer:

(defun without-orderless (fn &rest args)
  (let ((completion-styles '(partial-completion)))
    (apply fn args)))

(advice-add 'company-calculate-candidates :around #'without-orderless)

Instead of disabling orderless you could alternatively add a style dispatcher that automatically anchors the first component using ^. If you don't want to use it all the time, you can set things up so that it only is used for company:

(defun anchor-first-component (pattern index _total)
  (when (= index 0)
    (cons 'orderless-regexp (concat "^" pattern))))

(defun with-anchored-first-component (fn &rest args)
  (let ((orderless-style-dispatchers
         (cons #'anchor-first-component orderless-style-dispatchers)))
    (apply fn args)))

(advice-add 'company-calculate-candidates
            :around #'with-anchored-first-component)

Any hints for disabling the orderless for some specific regions/hints, say the above one?

I just realized I may not be understanding your question: what do you mean by "specific region/hints"?

Also, I just realized that probably the easiest possible solution here is something like: (setq completion-styles '(partial-completion orderless)). That should in practice always use partial-completion for company while still letting you use orderless in the minibuffer.

Please let me know if none of these approaches does what you want, and feel free to reopen the issue.

I just realized I may not be understanding your question: what do you mean by "specific region/hints"?

Since I was only faced with a situation that confuses me while using orderless, I have no way to describe my needs in general terms. Basically, I want to have a method to let me switch on/off orderless on-the-fly. For example, as you mentioned, when a ^ is type as an anchor, enable it, otherwise, disable it.

Instead of disabling orderless you could alternatively add a style dispatcher that automatically anchors the first component using ^. If you don't want to use it all the time, you can set things up so that it only is used for company:

(use-package orderless
 :config
 (setq
  ;; https://github.com/oantolin/orderless#ivy
  ivy-re-builders-alist '((t . orderless-ivy-re-builder))

  ;; Configure a custom style dispatcher (see the Consult wiki)
  ;; orderless-style-dispatchers '(+orderless-dispatch)
  ;; orderless-component-separator #'orderless-escapable-split-on-space

  completion-styles '(orderless)
  completion-category-defaults nil
  completion-category-overrides '((file (styles . (partial-completion)))))

 (defun anchor-first-component (pattern index _total)
   (when (= index 0)
     (cons 'orderless-regexp (concat "^" pattern))))

 (defun with-anchored-first-component (fn &rest args)
   (let ((orderless-style-dispatchers
          (cons #'anchor-first-component orderless-style-dispatchers)))
     (apply fn args)))

 (advice-add 'company-calculate-candidates
             :around #'with-anchored-first-component)
 )

It seems that the above configuration will always use partial-completion style when I'm not in minibuffer:

image

I mean, without typing ^ an anchor, the partial-completion style is always enabled, which prevents me from choosing to use the orderless style of completion when necessary.

(setq completion-styles '(partial-completion orderless))

I tried the following configuration according to your above comment:

 (use-package orderless
  :config
  (setq
   ;; https://github.com/oantolin/orderless#ivy
   ivy-re-builders-alist '((t . orderless-ivy-re-builder))

   ;; Configure a custom style dispatcher (see the Consult wiki)
   ;; orderless-style-dispatchers '(+orderless-dispatch)
   ;; orderless-component-separator #'orderless-escapable-split-on-space

   completion-styles '(partial-completion orderless)
   completion-category-defaults nil
   completion-category-overrides '((file (styles . (partial-completion))))))

Just as you have mentioned, this configuration can always use partial-completion for company while still letting me use orderless in the minibuffer. But, as I’ve explained above, it is not flexible enough to allow me to easily switch between using orderless and not using orderless at any time on-the-fly conveniently.