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

PushMode and PopMode hooks for fully-general mode-tracking. #2513

Open
Screwtapello opened this issue Oct 22, 2018 · 2 comments
Open

PushMode and PopMode hooks for fully-general mode-tracking. #2513

Screwtapello opened this issue Oct 22, 2018 · 2 comments

Comments

@Screwtapello
Copy link
Contributor

Originally, Kakoune had four hooks for mode-change events: InsertBegin, InsertEnd, NormalBegin and NormalEnd. However, Kakoune does have other modes, and adding "begin" and "end" hooks for all of them would be prohibitive. In fact, it would be impossible, since Kakoune has user-defined modes as well.

In #1772, a new ModeChange hook was introduced, aiming to be a general replacement for the previous mode-change hooks. For example, InsertEnd could be replaced with a ModeChange insert:.* hook. Unfortunately, that didn't quite work out because Kakoune maintains a stack of current modes. For example, from normal mode i pushes an insert mode, and from the insert mode <a-;> pushes a second normal mode. That transition from insert mode to the second normal mode counts as a ModeChange insert:normal because the active mode changes, but it does not count as an InsertEnd because the insert mode doesn't end, it just gets temporarily replaced. See also #2122

To fully generally replace the original events, we should have PushMode and PopMode events with the same parameter string as ModeChange. That is:

  • InsertBegin can be replaced with PushMode .*:insert
  • InsertEnd can be replaced with PopMode insert:.*
  • NormalBegin can be replaced with PushMode .*:normal
  • NormalEnd can be replaced with PopMode normal:.*
  • ModeChange X:Y can be replaced with hooking both PushMode X:Y and PopMode X:Y

In the style of the existing hooks documentation:

PushMode X:Y: triggered when a command in mode X starts a new Y mode; for example, when <a-;> temporarily enters normal mode from insert mode, PushMode insert:normal will trigger

PopMode X:Y: triggered when a command in mode X exits that mode, returning to mode Y; for example, when <esc> exits insert mode, PopMode insert:normal will trigger

@Delapouite
Copy link
Contributor

Yes, mawww stated multiple times that the ModeChange hook was not that good in the end. PushMode and PopMode are indeed nice improvements! Thanks.

But I feel that the same ambiguity remain:

normal → PushMode → insert

Will the commands done during the PushMode hook be treated as normal or insert commands?
The hook seats right in the middle and there's no way to know to which context it belongs.

So maybe we need 4 hooks:

normal → PushModePre → PushModePost → insert → PopModePre → PopModePost → normal

PushModePre and PopModePre are executed in the mode we're leaving, while PushModePost and PopModePost in the mode we're entering.

@Screwtapello
Copy link
Contributor Author

By "commands" you specifically mean an evaluate-keys command in the hook "commands" block, or in a function called from that block, yes?

The "pre" hooks seem dangerous: what if somebody does:

hook global PushModePre normal:view %{execute keys i}

After the hook executes, will Kakoune be in "view" or "insert" modes? Or how about:

hook global PushModePre normal:insert %{execute keys i}

...will that cause an infinite loop?

Unless there's an important use-case for *ModePre hooks, I'd rather just declare that PushMode and PopMode always execute after the mode change has occurred.

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

No branches or pull requests

2 participants