orderless icon indicating copy to clipboard operation
orderless copied to clipboard

Request/suggestion: support ^-prefix and $-suffix to STRICT leading and ending initialisms

Open dalanicolai opened this issue 4 years ago • 13 comments

First of all, thanks for your work. Your project(s) look very interesting.

I am an Ivy and Spacemacs user. In Spacemacs, Ivy opens with the ^ as initial-input by default. It means that we want to match from the beginning of some expression. If we don't want to match the beginning, we simply remove the ^ with backspace or, as a better alternative, just insert a SPC first. Also in Spacemacs M-x is mapped to SPC SPC so if we do not want to match from beginning we just type triple space.

Anyway, I would suggest that orderless offers the option to insert a ^ by default, and furthermore, the option to indicate when using initialisms, by pre-/suf-fixing the first and last characters that they should match the initials of the first and last words (if it should not match the first initial than we could insert a SPC first).

I hope my explanation is clear enough, and that you find it a useful suggestion (of course this is just taken from Ivy).

dalanicolai avatar Oct 05 '21 13:10 dalanicolai

Here is a video

  • I first type SPC twice
  • then I type SPC three times
  • subsequently I ad pe
  • and finally I add the $ to tell 'pe' should match the final part (and after that I stop the gif-screencast) output-2021-10-05-15:16:39

dalanicolai avatar Oct 05 '21 13:10 dalanicolai

I am not sure I understand you fully here, but orderless style dispatchers allow at least to prefix the first component with "^". In Orderless almost nothing is hard coded, the user should write their own orderless style dispatchers and configure the desired orderless matching styles (initialism etc).

minad avatar Oct 05 '21 19:10 minad

Maybe the style dispatchers I am using are helpful for your use case: https://github.com/minad/consult/wiki#orderless-style-dispatchers-ensure-that-the--regexp-works-with-consult-buffer. My configuration has a bit of special casing for Consult and Corfu completion-in-region and is a bit primitive. Omar has a more sophisticated configuration in his https://github.com/oantolin/emacs-config.

minad avatar Oct 05 '21 19:10 minad

If you really want to insert initial input by default you can do this:

(advice-add #'vertico--setup :after #'insert-circumflex)
(defvar insert-circumflex-exclude nil
   "List of commands where circumflex should not be inserted.")
(defun insert-circumflex () 
  (unless (memq this-command insert-circumflex-exclude)
    (insert "^")))

But I am very hesitant to recommend something like this. It will break commands and it will break most completion styles, which don't understand regular expressions.

minad avatar Oct 06 '21 19:10 minad

@minad Haha, I am also not sure if I understand you correctly, but what I mean is, I think, really quite straightforward. It seems that initialism 'patterns' don't recognize ^ and $, but instead orderless offers different completion styles for it, namely orderless-initialism, orderless-strict-leading-initialism and orderless-strict-full-initialism. So my request is for support of going from orderless-initialism to orderless-strict-leading-initialism by prepending the pattern with a ^, and going from strict-leading to strict-full by also appending a $. So that we do not need to configure the different completion styles. I hope this is more clearly explained now. If there is support already for this, then my request is to document it more prominently. So I am not sure if this is covered by your suggestions and advice, I only quickly skimmed them, but I could not immediately recognize the 'fix' for this request.

Anyway, still very useful info of course. I will take a closer look when I find some time again soon. Thanks again of course!

dalanicolai avatar Oct 06 '21 20:10 dalanicolai

I'm a little confused with all this talk about initialisms. In your GIF you show ^cur matching cursor-sensor-mode. That is what orderless calls regexp matching (which it not only supports but is on by default), not initialism matching. Initialism matching means each letter is matched at a word boundary (more precisely at the start of a word), for example as an initialism cur would match comment-or-uncomment-region because the 1st, 3rd and 4th words start with c, u, r.

So maybe what you want is just regexp matching? Out of the box ^cur and the version with an extra space do match as in your GIF.

oantolin avatar Oct 06 '21 21:10 oantolin

@oantolin I am specifically talking about initialisms (I explicitly explained how this syntax can 'replace' those three 'initialism completion styles' that I mentioned in my previous comment). I know the regexp matching already supports what I want, and for that I only requested to provide the option to insert the ^ by default, but it looks like @minad provided the solution for that already. So now it is only about 'extending' the syntax for initialisms. The screencast was just to show you/explain the behavior in Ivy which indeed only supports regexps (I think).

dalanicolai avatar Oct 06 '21 22:10 dalanicolai

Oh, I see. Well ^ and $ can't really express whether an intialism matches strictly or not. "Strictly" means you are not allowed to skip any words. For example, I said cur matches comment-or-uncomment-region as an initialism but it does not match as a strict initialism because we skipped the word or. So you can't replace the strict styles by expanding orderless-initialism to allow anchors.

Of course, you may want the anchor extension anyway, even if it does not replace the strict initialism matching styles.

oantolin avatar Oct 06 '21 22:10 oantolin

Okay, I misunderstood/not well considered the strict part, but it is more about the meaning of the 'leading' and `full' in those styles. I guess this should be perfectly possible with the proposed syntax...

As far as I did my experiments correctly, I found that it is not possible with current initialism syntax, to indicate that the first letter should only match with the beginning of the 'symbol` by prepending it with ^ (and similarly for $)

dalanicolai avatar Oct 06 '21 22:10 dalanicolai

Yes, and it is fairly easy to do. A matching style is a function that takes a string and returns a regexp. Here you can leverage orderless-initialism for the middle part and just handle any initial ^ or final $:

(defun orderless-anchorable-initialism (pattern)
  (concat
   (if (string-prefix-p "^" pattern) "^" "")
   (orderless-initialism
    (string-remove-prefix "^" (string-remove-suffix "$" pattern)))
   (if (string-suffix-p "$" pattern) "[A-Za-z0-9]*$" "")))

oantolin avatar Oct 06 '21 22:10 oantolin

Note that for $ I don't just tack on a $, because the last letter matches at the start of the last word, so instead we say "the rest of string is letters and numbers".

oantolin avatar Oct 06 '21 22:10 oantolin

You may also want to similarly define an "anchorable prefixes" matching style.

oantolin avatar Oct 06 '21 22:10 oantolin

Ah, I don't understand this immediately (as I do not fully understand dispatchers yet/did not use them yet), but I guess this must be a perfect solution to my request.

So, I would suggest adding this info to a more prominent place in the documentation (just suggestion of course).

So this sounds great! Many thanks for your advice and examples (also @minad)...

And your great work of course... the packages and their 'philosophy' look and sound really nice!

dalanicolai avatar Oct 06 '21 22:10 dalanicolai