Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jul 16, 2024

Conversation

masmu
Copy link
Contributor

@masmu masmu commented Jun 8, 2024

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:

  • Initiation of a search:

    • Ctrl-f (to open the search query box in the command bar)
    • Enter the search term
    • Press Enter to start the search
      Expectation: The first occurrence of the search term is being highlighted
  • Continuation of a previously initiated search:

    • Just press Enter to continue the search
      Expectation: The next occurrence of the search term is being highlighted.
      You can repeat this step as often as you like. Each Enter highlights the next occurrence.
  • Exit the search operation when you found what you are looking for

    • Press Esc to exit
      Expectation: The search term found is still highlighted and you can continue with normal text editing.

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
Copy link
Collaborator

dmaluka commented Jun 9, 2024

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 #2755 (comment) (and I still think it would be good to improve all those actions as well).

@JoeKar
Copy link
Collaborator

JoeKar commented Jun 10, 2024

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
Copy link
Collaborator

dmaluka commented Jun 10, 2024

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

Absolutely.

@masmu
Copy link
Contributor Author

masmu commented Jun 12, 2024

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
Copy link
Collaborator

JoeKar commented Jun 12, 2024

No worries, we're not in hurry.

@dmaluka
Copy link
Collaborator

dmaluka commented Jun 13, 2024

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

@dmaluka
Copy link
Collaborator

dmaluka commented Jun 15, 2024

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
Copy link
Collaborator

dmaluka commented Jun 15, 2024

See #3352.

@@ -815,6 +815,7 @@ var BufKeyActions = map[string]BufKeyAction{
"ToggleRuler": (*BufPane).ToggleRuler,
"ToggleHighlightSearch": (*BufPane).ToggleHighlightSearch,
"UnhighlightSearch": (*BufPane).UnhighlightSearch,
"ResetSearch": (*BufPane).ResetSearch,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for my late response.
As a last small change request: ResetSearch should be added to keybindings.md#bindable-actions-and-bindable-keys.

If this is added it can be merged. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. I was quite busy as well.

Good catch!
I noticed that there are other actions missing in the keybindings.md. Such as ToggleHighlightSearch, UnhighlightSearch and probably others. Isn't there are better way to keep that part of keybindings.md in sync with the BufKeyActions map?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there are better way to keep that part of keybindings.md in sync with the BufKeyActions map?

Currently I can only imagine to generate some parts of the docs via e.g. make doc.

Added ResetSearch to the list of bindable actions in keybindings.md
@masmu masmu force-pushed the feature/reset-search branch from 30cb84b to bbf6ec2 Compare July 4, 2024 13:46
@JoeKar JoeKar merged commit 762e31f into zyedidia:master Jul 16, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants