oantolin / orderless

Emacs completion style that matches multiple regexps in any order

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Escapable split on spaces, hyphen, or slash

rudolf-adamkovic opened this issue · comments

I would like to get both escaping and splitting on spaces/hyphens/slashes.

When I set orderless-component-separator to 'orderless-escapable-split-on-space, I get splitting on spaces and escaping but not splitting on hyphens and slashes. When I set orderless-component-separator to "Spaces, hyphen or slash" ( +\\|[-/]), I get splitting on spaces/hyphens/slashes but not escaping.

Did you try writing your own component separator derived from orderless-escapable-split-on-space which handles spaces/hyphens/slashes?

I think this is a pretty niche request, so I agree with @minad that the best way to deal with it is to write in your personal configuration a custom separator based on orderless-escapable-split-on-space. Let me know if you get stuck adapting the source code of orderless-escapable-split-on-space to your needs.

Also, just in case this is an XY problem, why do you want to split on hyphens and slashes?

@oantolin

Also, just in case this is an XY problem, why do you want to split on hyphens and slashes?

It might be! TL;DR Escapable spaces work well in the mini-buffer, but when writing Scheme with Corfu, I would like to split on hyphens. I tried corfu-quit-at-boundary but it annoys me how the completion popup lingers around.

For anyone reading in the future, I came up with the following solution:

(defun my-orderless-escapable-split-on-space-and-dash (string)
  (orderless-escapable-split-on-space (string-replace "-" " " string)))

(setq orderless-component-separator
      'my-orderless-escapable-split-on-space-and-dash)

Now, Orderless splits (escapably) on dashes (fantastic for Lisp) and spaces (fantastic for everything else). I left out slashes for now. 😄

Now, Orderless splits [...]

Correction: 20 days later, and I found that my original one-liner above has a problem. While it splits on both dashes and spaces, it does not interpret escaped dashes correctly. For instance, C\-d would not match C-d. So, I wrote the version below to fix the problem.

(defun my-orderless-escapable-split-on-space-and-dash (string)
  "Split STRING on spaces and dash, if not escaped with backslash."
  (let ((encode
         (lambda (string)
           (let* ((step-1 (string-replace "\\ " "&space;" string))
                  (step-2 (string-replace "\\-" "−" step-1)))
             step-2)))
        (decode
         (lambda (string)
           (let* ((step-1 (string-replace "&space;" " " string))
                  (step-2 (string-replace "−" "-" step-1)))
             step-2))))
    (let* ((string-encoded (funcall encode string))
           (components-encoded (split-string string-encoded " +\\|-"))
           (components (mapcar decode components-encoded)))
      components)))