zyedidia / micro

A modern and intuitive terminal-based text editor
https://micro-editor.github.io
MIT License
24.9k stars 1.17k forks source link

Implemented `ResetSearch` and allow action chaining of `FindNext` and `FindPrevious` #3333

Closed masmu closed 2 months ago

masmu commented 3 months ago

This is a small feature to reset previously initiated searches and to use FindNext and FindPrevious in chained actions.

Personally I never could get used to use different shortcuts for initiating and continuing a search. (First Ctrl-f, then Ctrl-n or Ctrl-p)

Assuming that the option hlsearch is set to false, my preferred workflow is:

To achieve this chaining actions seems like the way to go. Like:

"Ctrl-f": "Find",
"Enter": "FindNext|InsertNewline",

But this didn't work, since BufPane.FindNext() and BufPane.FindPrevious() always return true, which prevents all subsequent actions from being executed. I'm not sure if this is intentional for some reason. It seems more likely that this case was omitted or forgotten because it generally excludes the possibility of chaining actions. Buffer.FindNext() handles the case of a search term being empty. But it would also reset the selection when adding a return false there. So adding a guard clause at the top of the function may be the best solution and should not break any potential user bindings out there.

The missing piece left was to add ResetSearch() which allows to exit a previously initiated search. Like:

"Esc": "ResetSearch|RemoveAllMultiCursors|Escape",

Just as a bonus: The following snippet adds a visual representation for being in search mode to the status line, which is IMHO quite nice.


micro.SetStatusInfoFn("initlua.tabSearch")

function tabSearch(buf)
    if buf.LastSearch == '' then
        return ''
    end
    return string.format(' [search=%s]', buf.LastSearch)
end
dmaluka commented 3 months ago

Looks good to me.

But this didn't work, since BufPane.FindNext() and BufPane.FindPrevious() always return true, which prevents all subsequent actions from being executed. I'm not sure if this is intentional for some reason. It seems more likely that this case was omitted or forgotten because it generally excludes the possibility of chaining actions.

Just omitted, I believe. Like in the vast majority of actions. I wrote about it a long time ago in https://github.com/zyedidia/micro/issues/2755#issuecomment-1436000051 (and I still think it would be good to improve all those actions as well).

JoeKar commented 3 months ago

Just omitted, I believe. Like in the vast majority of actions. I wrote about it a long time ago in #2755 (comment) (and I still think it would be good to improve all those actions as well).

How about now and here? Otherwise it will be a good idea/intention, but not more and we forget about it again.

dmaluka commented 3 months ago

How about now and here? Otherwise it will be a good idea/intention, but not more and we forget about it again.

Absolutely.

masmu commented 3 months ago

I would be happy to have a look. But due to time constraints, I hope it's okay if this can happen at the end of next week at the earliest.

JoeKar commented 3 months ago

No worries, we're not in hurry.

dmaluka commented 3 months ago

I didn't mean that it must be @masmu who has to implement it. :)

dmaluka commented 3 months ago

How about now and here? Otherwise it will be a good idea/intention, but not more and we forget about it again.

FYI I'm gonna push a PR soon.

dmaluka commented 3 months ago

See #3352.