reddit / emacs

Magic orderless

romulosno · 2022-10-01 22:56
Thanks to u/oantolin work my completion inside emacs has come to another level of comfort. I've been using orderless with some custom dispatchers for a while now, but I've always felt like something was wrong or too complex when using the OR operator (regex: `\(pattern1\|pattern2\)`). So today I invested half a hour into learn basics of Elisp string operations and have created my own dispatcher for that. (defun or-regexp (pattern index _total) (cond ((string-suffix-p "|" pattern) `(orderless-regexp . ,(concat "\\(" (concat (s-replace "|" "\\|" (substring pattern 0 -1)) "\\)")))) ((string-match-p "|" pattern) `(orderless-regexp . ,(concat "\\(" (concat (s-replace "|" "\\|" pattern) "\\)")))))) I assume it's not the prettiest code, but it's enough for me
quokka70 · 2022-10-03 13:22
This looks useful. Thanks! It looks like the two patterns are "foo|bar|" and "foo|bar". Why have the trailing-| version?
romulosno · 2022-10-04 00:50
oh, glad you liked it. The first case is useful when I'm writing a query with more than two args in the or operator: "\\(foo\\|bar\\|baz\\)". Before I write "baz", i expect that the minibuffer only search for the first two terms (in this case: foo|bar). If I don't remove the last pipeline the regex would become "\\(foo\\|bar\\|\\)"
quokka70 · 2022-10-04 00:54
Oh! Incremental search. Great idea!