2021-05-24 Ivy and ignoring the order of tokens

Emacs function naming is strange. I rediscovered that fact a few weeks ago when I tried to find a function giving me the position of the beginning of the current line. I tried to look for a function whose name would match begin.*line, and there was none. I concluded that there is no such function (which was strange, but I learned not to be surprised that often), and proceeded to write my own.

After some time I found out that such a function obviously exists, and that it is called… line-beginning-position. Oh well.

Of course, being who I am, I immediately got an idea to write an apropos-type command which would accept a number of words/tokens separated by spaces, construct a regex matching any string having those “tokens” in any order and feeding it to apropos. Fortunately, I decided to check first if anything like that existed already.

And of course it did. I’ve been using the great Ivy package as my completion engine for a long time now, and from time to time I skim through the manual and find some hidden gems there. This time it was ivy--regex-ignore-order, one of the “completion styles” of Ivy. This “completion style” means that the input of the form begin line will match anything containing both begin and line, in whichever order. Of course, I don’t want every my search to ignore the token order, but searching for Emacs functions and variables should. That’s why I added this to my init.el:

(add-to-list 'ivy-re-builders-alist '(counsel-describe-function . ivy--regex-ignore-order))
(add-to-list 'ivy-re-builders-alist '(counsel-describe-variable . ivy--regex-ignore-order))

(As you might expect, I have also bound C-h f and C-h v to the counsel-... versions, coming from the Counsel package, which defines Ivy-aware counterparts to many Emacs functions and more.)

CategoryEnglish, CategoryBlog, CategoryEmacs