1

I am using command completions, and would like to 'filter' out certain built-in packages. For example, typing "text";

Example completions

In these completions, I would like to filter out/hide the entries that start with html2text, mail-, bibtex- and eww-, as those are built-in packages I do not use.

How can I best do this? I am using Emacs 29.1, vertico and orderless with their default/most basic configurations;

    (use-package orderless
  :defer 1
  :config
  (orderless-define-completion-style orderless+initialism
    (orderless-matching-styles
      '(orderless-initialism orderless-literal orderless-regexp)))
  (setq
    completion-styles '(orderless basic)
    completion-category-defaults nil
    completion-category-overrides '(
      (file (styles partial-completion))
      (command (styles orderless+initialism))
      (symbol (styles orderless+initialism))
      (variable (styles orderless+initialism)))
    read-file-name-completion-ignore-case t
    read-buffer-completion-ignore-case t
    completion-ignore-case t))

2 Answers 2

0

(I assume you mean filter interactively, and not just call `completing-read' applying a filter in the call.)

I can't answer for Vertico or Orderless, I'm afraid.


But with Icicles what you want is trivial to do.

M-x text S-SPC ^\(html2\|mail\|bibtex\|eww\) S-TAB C-~

Breaking that down:

  1. M-x text S-SPC: Apropos-match text, which amounts to matching text as a substring.

  2. ^\(html2\|mail\|bibtex\|eww\) S-TAB: Apropos-match that regexp, which matches those alternatives at the start of each completion candidate. That is, restrict the matches to candidates (all of which have substring text) that have those prefixes.

  3. C-~: Take the complement of those candidates, within the previous set of candidates. That is, remove, from the set of candidates matching text, all those that match the prefixes html2, mail, bibtex, and eww.

Such progressive filtering, applying different filters one-by-one, like interactive shell piping, I call "progressive completion".

Removing a set of matches I call "chipping away the non-elephant".

These are the most commonly used operations in Icicles.

0

With vertico and orderless you're almost there, all you need is to add a style dispatcher to your configuration, also mentioned in the orderless README.

Now you can prefix a term with ! to exclude matching candidates.

(defun without-if-bang (pattern _index _total)
    (cond
     ((equal "!" pattern)
      '(orderless-literal . ""))
     ((string-prefix-p "!" pattern)
      `(orderless-without-literal . ,(substring pattern 1)))))

(setq orderless-style-dispatchers '(without-if-bang))
1
  • Hi Bram, thanks for the orderless example. This works as expected; it filters out any keyword typed into the buffer that is prefixed with !. However I am hoping to have a pre-existing list of keywords that I could 'hard-code', so that don't have to explicitly !exclude them. How would you go ahead with that? I figure I could assign a style-dispatcher to a unique character that inserts the without-literal style, but that still requires explicit action in the search pattern... Commented Sep 13, 2023 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.