You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 6, 2018. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 6, 2018. It is now read-only.
Multiple keypress bindings are incorrect/limited #129
The current keybinding system in Atom waits for additional keypresses if they could match, this works great for multiple keypresses. However it breaks down when you have keybindings as shown below.
In the current implementation g g and g t shadow g, which makes it impossible to use g. We could use a timeout system to determine that a g without a following character within a certain amount of time indicates a single g but I'm not sure that this the ideal solution here.
A timeout seems like the only clear solution to me in the event of a conflict like this. Either that or some kind of universal binding terminator that says "I'm done". But what would that be? And then why not just bake that into the original g binding?
So I'm 👍 on a timeout in the keymap core code. Still seems like a pretty annoying experience to shadow like this.
The timeout sounds imperfect, but honestly vim has been using this for ages and I never even realized it. I had no idea and I always thought it was incredible, and fast. If someone would have first approached me with some sort of timeout concept without knowing vim already does it, I would have been like "that sounds awful" and I would just have vivid nightmares of hitting some keypresses and then waiting a few seconds for a timeout to expire so my command would execute.
In practice though, I've never had this problem in vim. Maybe others have a different experience.
That being said though, faced with the situation you mentioned, to play devil's advocate, g in the context of go to line is really <prefix>g where prefix is a number. g by itself does nothing. Is there something to be taken advantage of with that fact?
An idea occurred to me earlier - can we represent keypresses as nodes in an AST like programming languages are parsed into? And in that example, could we define terminal and non-terminal nodes? Maybe that's the holy grail solution?
@jroes The reason that Vim isn't maddening to use all the time is that Vim doesn't wait for a second key unless there is a multi-key binding that starts with the first one. That's why I can't understand why anyone would bind j j, lol.
👍 from me for the timeout. Too many important key bindings won't work without it. Let's just be mindful that we don't pause on every keystroke, just the ones with multi-key bindings.
An idea occurred to me earlier - can we represent keypresses as nodes in an AST like programming languages are parsed into? And in that example, could we define terminal and non-terminal nodes?
That's essentially what we're trying to achieve with the opStack and compositional operators. We haven't defined a context-free grammar, but since context-free languages can be expressed with pushdown automata, this should essentially offer a similar level of power.
@zenhob haha, I actually have always bound jk in vim now that I think of it. I may have tried jj first and realized the err of my ways.
@nathansobo Ah nice! When I was working on operator-pending mode I started to realize that the mode abstraction may not even entirely be necessary given opStack. So I guess it wasn't a crazy idea after all. I think it's getting tldr'ed and/or lost in the mix, but you can see some progress over on #58.
My question for going full opStack is, would we then just want to handle all keypresses ourselves? I guess that would mean we'd have to sacrifice configurability/remapping, unless we use our own format.
Yeah, I was envisioning using the keymap to describe the building blocks and then allowing the op-stack to compose them. So you'd bind w to vim:word-motion or something. I haven't actually looked at this package since the initial experimentation I did with it.
Yeah, so on further thinking, @mcolyer, it seems like the composition of multiple bindings should really be something that's expressed in vim mode. That would leave single bindings to emit commands based on the components of a fully-composed command.
That's probably not quite right but sort of a sketch. The idea is that d i w wouldn't be expressed in the keymap but instead in the composition of multiple discrete commands within vim-mode.
Does this ring true for you? Does composing multiple commands in vim rather than relying on multi-stroke bindings remove the need for a timeout in core's Keymap?
@nathansobo I think from a purity of implementation standpoint that would be nearly ideal (I'm not even sure we'd need to restrict to .command-mode even). However, I think what we're going to end up with is a keybinding for every keypress, and potentially a lot of branching in each implementation. I don't necessarily think that's a problem.
But, I think this goes back to what I was mentioning earlier -- if we end up with a structure like that, does that mean that users can no longer map things themselves? They could certainly remap some individual key not in use by vim, but they wouldn't be able to bind very much on their own. A good example is the jk as escape thing. They'd have to fork vim-mode to implement jk because we'd take over handling j before they had the chance. Now, that doesn't mean they can't map things themselves, it just means they'd have to map things in some special vim-mode format that we'd have to expose a configuration for. I think.
Yeah. It's even worse with j k because that's in insert mode and gets handled by the editor.
I feel like we're missing something here. I'm thinking there are multiple stroke bindings that still run a single command like escape, but there are also individual bindings that compose together. Is that true? Does vim offer a rebinding syntax for compositions like "change to next word", or is that emergent from the individual bindings?
I wish I had time to do a deeper dive on this right now. We really need to carve these ideas at their joints and come up with a proposal that cleanly integrates with the default keymap with potentially a few key features added, without trying to cram all of vim into multi-stroke bindings.
I'm not a vim expert, but I think vim only lets you map keys to other keys:
For example, the way I map jk is map jk <Esc>. You can also do something like map ab :echo 'hello'<CR>
I agree, I don't think we want to put ourselves in a position where we are implementing vim entirely through keymaps. And we certainly can't in core's current state. So if we don't implement vim that way, what are our options?
The first option that comes to mind is to simply overtake key mapping entirely within vim-mode's code. If we handle all keystrokes, we should be able to push things into the opStack and attempt to evaluate after each press. The downside will be that we need to provide a new keymap format in vim-mode if we want users to be able to change any mappings.
Is there prior art in some other editor (Sublime Text) that might give us clues to another implementation option?
If we type 'v' and wait N ms, it puts us in visual mode.
If we type 'v i w' we enter visual mode and select inside the current word.
If we type 'v i c e' we never enter visual mode and instead play the miami vice episode. If we wait too long to type the 'e' we enter visual mode.
If we type 'v i a' we enter visual mode.
Yes.
I think this algorithm is very close.
What if we were to type v i c (and pretend i c was a motion in visual mode)? I think given the described it would wait until it receives another keystroke and it would not enter visual mode nor select the word which could be unintuitive. Maybe this won't happen in practice but I thought it would be worth bringing up now. Maybe we should have a maximum timeout for any single command and then follow the replay steps?
I added a clarifying point about what to do when we time out of a pending state. I think the case you're describing would play out as follows:
v is pressed. We go into a pending state and don't go into visual mode.
i and c are pressed, putting us in a new pending mode due to the ambiguity with v i c e
We time out, disabling the longest binding (v i c e) and replay the queued keystrokes.
v is replayed. It is now unambiguous so we go into visual mode.
i and c are replayed, this time in visual mode, causing us to select your pretend text object.
True, we don't enter visual mode right away when there's ambiguity, but this is exactly how vim behaves. For quick keystrokes i think it will barely be noticeable.
I'm going to implement this in the keymap. Started working on some cleanup yesterday and will continue today. Can't make any precise promises, but we should have this cleared up fairly soon.
This should be fixed with the next release of Atom now that the keymap has been rewritten. It's also been open sourced for those who are interested or find issues with it.
This still doesn't quite work for mapping something like jk to activate command mode. Sure it will work. But now in insert mode I have to press j twice to register one j character inside an insert buffer.
@nathansobo From what I have seen (say i have jk mapped to esc) vim won't place a j when insert mode until either 1. A timeout occurs, 2. A key other than k is pressed after j (then j and the next key will both appear at the same time) or 3. k is pressed after j triggering the escape binding
That makes sense. We're really close with the replay behavior, but we're messed up by the fact that input events are handled separately from keydown events by the browser. So we need to be able to replay a keystroke in a way that also produces an input event. I'm not even sure that possible in a straightforward way, but we can probably figure out a workaround.
What do you have in mind? One possibility is to explore making the keymap manager more knowledgable about handling textual input. It's actually fairly nuanced, and other components may want to benefit from this facility. But handling the textinput events is somewhat entangled with the editor's update lifecycle, so I'm not 100% sure how easy it will be to extract. Worth exploring for sure.
I'm going to wave my hands a bit since I don't know this area of the code super well. I was thinking whatever grabs the events ends up forwarding the request to a publicly visible method in the keymap manager.
That way other code could manipulate the keymap manager without having to create fake input events. This approach might be totally naive to the complexities involved here so please correct me.
Something like that might work, but there would be more to the story. Let me sketch out my mental model for handling an input event with a text editor focused:
The editor has a hidden input element that receives focus whenever the editor is focused.
When the user types j in the input, it first emits a keydown event that bubbles up the DOM.
The workspace captures the keydown event and sends it to the keymap manager with atom.keymaps.handleKeyboardEvent.
In insert mode with j k bound to escape, the keymap manager finds a partial binding for j.
Today's Behavior
Here's where things get problematic. Today, when we find any sort of match, we call preventDefault on the event. This short-circuits any further action by the browser, halting insertion of input into the hidden text box.
When the keymap manager exits the pending state by seeing another character or hitting the timeout, it replays the keydown event associated with the j keystroke internally, but this does not result in a textinput event on the input box. The event is essentially discarded once it is matched in the keymap with respect to textual input.
Hypothetical Future Behavior
Here's one idea for a solution. There may be better ones.
The user expresses their intent to have the keymap handle textual input by setting a data- attribute on the input field, like data-manage-textinput.
When the keymap detects a non-modified keystroke on a field that opts into textual input handling, it does not match it and instead waits for a subsequent textinput event.
When handling a textinput event, the keymap matches the keystroke against plain-character bindings just as it did for keydown events.
When a textinput event fails to match any bindings, whether initially or during replay, we emit a custom managed-textinput event on the input field (perhaps there's a better name).
This would enable plain keystrokes to be replayed that correctly result in input in the editor. All the logic that currently exists in the editor for handling the accented character menu, IME input, etc would need to be moved from the editor into the keymap manager for us to get away with this. There's a full implementation in the current editor view, and a partial but cleaner implementation in the new React-based editor component. We would need to be careful to cleanly slot in with the editor's DOM update when clearing the contents input field between events.
If anyone is interested in taking a stab at implementing this, I could definitely use help. Needless to say, any implementation would need to be test-driven to be merged. Otherwise I'll try to get to it soonish.
internally, but this does not result in a textinput event on the input box. The event is essentially discarded once it is matched in the keymap with respect to textual input.
That was the key part I was missing.
I wonder if a better approach isn't to throw the textinput event from inside the keymap manager using something like textinput.dispatchEvent(...). Or are there issues with that approach as well?
Well, we need to allow the textinput event to occur "naturally" because the OS and Chromium do a lot of work to interpret the input corresponding to a given keystroke. I suppose we could manually hand the event to the keymap manager in the editor and then only continue to process it if the default isn't prevented...
Editor receives a text input event for a j
We call atom.keymaps.handleTextInputEvent with the event.
The keymap has an ambiguous binding, it prevents default on the event.
We note the default prevention in the editor and abort processing of the event.
The timeout occours, and the keymap manager "replays" the input event on the text field.
The editor calls handleTextInputEventagain, but this time the default is not prevented and we proceed as normal.
Might be nice because we don't have to introduce the data- attribute and the special event name.
@schmitzjake I have observed different behavior in vim on ubuntu 12.04. In your case of "jk", when I type j the j appears immediately, but the cursor doesn't advance until either the timeout of one second is reached or I type another key that doesn't have a mapping starting with j.
If I type k before the timeout, the j is removed from the screen and the cursor moves one character to the left (unless already at column 0).
Activity
nathansobo commentedon Mar 5, 2014
A timeout seems like the only clear solution to me in the event of a conflict like this. Either that or some kind of universal binding terminator that says "I'm done". But what would that be? And then why not just bake that into the original
gbinding?So I'm 👍 on a timeout in the keymap core code. Still seems like a pretty annoying experience to shadow like this.
jroes commentedon Mar 5, 2014
The timeout sounds imperfect, but honestly vim has been using this for ages and I never even realized it. I had no idea and I always thought it was incredible, and fast. If someone would have first approached me with some sort of timeout concept without knowing vim already does it, I would have been like "that sounds awful" and I would just have vivid nightmares of hitting some keypresses and then waiting a few seconds for a timeout to expire so my command would execute.
In practice though, I've never had this problem in vim. Maybe others have a different experience.
That being said though, faced with the situation you mentioned, to play devil's advocate,
gin the context of go to line is really<prefix>gwhere prefix is a number.gby itself does nothing. Is there something to be taken advantage of with that fact?An idea occurred to me earlier - can we represent keypresses as nodes in an AST like programming languages are parsed into? And in that example, could we define terminal and non-terminal nodes? Maybe that's the holy grail solution?
zenhob commentedon Mar 5, 2014
@jroes The reason that Vim isn't maddening to use all the time is that Vim doesn't wait for a second key unless there is a multi-key binding that starts with the first one. That's why I can't understand why anyone would bind
j j, lol.👍 from me for the timeout. Too many important key bindings won't work without it. Let's just be mindful that we don't pause on every keystroke, just the ones with multi-key bindings.
nathansobo commentedon Mar 5, 2014
That's essentially what we're trying to achieve with the
opStackand compositional operators. We haven't defined a context-free grammar, but since context-free languages can be expressed with pushdown automata, this should essentially offer a similar level of power.jroes commentedon Mar 5, 2014
@zenhob haha, I actually have always bound
jkin vim now that I think of it. I may have triedjjfirst and realized the err of my ways.@nathansobo Ah nice! When I was working on operator-pending mode I started to realize that the mode abstraction may not even entirely be necessary given
opStack. So I guess it wasn't a crazy idea after all. I think it's getting tldr'ed and/or lost in the mix, but you can see some progress over on #58.My question for going full
opStackis, would we then just want to handle all keypresses ourselves? I guess that would mean we'd have to sacrifice configurability/remapping, unless we use our own format.nathansobo commentedon Mar 5, 2014
Yeah, I was envisioning using the keymap to describe the building blocks and then allowing the op-stack to compose them. So you'd bind
wtovim:word-motionor something. I haven't actually looked at this package since the initial experimentation I did with it.nathansobo commentedon Mar 5, 2014
Yeah, so on further thinking, @mcolyer, it seems like the composition of multiple bindings should really be something that's expressed in vim mode. That would leave single bindings to emit commands based on the components of a fully-composed command.
That's probably not quite right but sort of a sketch. The idea is that
d i wwouldn't be expressed in the keymap but instead in the composition of multiple discrete commands within vim-mode.Does this ring true for you? Does composing multiple commands in vim rather than relying on multi-stroke bindings remove the need for a timeout in core's
Keymap?jroes commentedon Mar 5, 2014
@nathansobo I think from a purity of implementation standpoint that would be nearly ideal (I'm not even sure we'd need to restrict to .command-mode even). However, I think what we're going to end up with is a keybinding for every keypress, and potentially a lot of branching in each implementation. I don't necessarily think that's a problem.
But, I think this goes back to what I was mentioning earlier -- if we end up with a structure like that, does that mean that users can no longer map things themselves? They could certainly remap some individual key not in use by vim, but they wouldn't be able to bind very much on their own. A good example is the
jkas escape thing. They'd have to fork vim-mode to implementjkbecause we'd take over handlingjbefore they had the chance. Now, that doesn't mean they can't map things themselves, it just means they'd have to map things in some special vim-mode format that we'd have to expose a configuration for. I think.nathansobo commentedon Mar 5, 2014
Yeah. It's even worse with
j kbecause that's in insert mode and gets handled by the editor.I feel like we're missing something here. I'm thinking there are multiple stroke bindings that still run a single command like escape, but there are also individual bindings that compose together. Is that true? Does vim offer a rebinding syntax for compositions like "change to next word", or is that emergent from the individual bindings?
I wish I had time to do a deeper dive on this right now. We really need to carve these ideas at their joints and come up with a proposal that cleanly integrates with the default keymap with potentially a few key features added, without trying to cram all of vim into multi-stroke bindings.
Thanks for your thoughts!
jroes commentedon Mar 7, 2014
I'm not a vim expert, but I think vim only lets you map keys to other keys:
For example, the way I map
jkismap jk <Esc>. You can also do something likemap ab :echo 'hello'<CR>I agree, I don't think we want to put ourselves in a position where we are implementing vim entirely through keymaps. And we certainly can't in core's current state. So if we don't implement vim that way, what are our options?
The first option that comes to mind is to simply overtake key mapping entirely within vim-mode's code. If we handle all keystrokes, we should be able to push things into the
opStackand attempt to evaluate after each press. The downside will be that we need to provide a new keymap format in vim-mode if we want users to be able to change any mappings.Is there prior art in some other editor (Sublime Text) that might give us clues to another implementation option?
nathansobo commentedon Mar 11, 2014
In thinking about this problem more deeply today, I think I've come up with a solution. Imagine that we had commands bound to the following bindings:
We want a solution that supports the following behavior:
Here is a rough outline of how the algorithm could work in the keymap:
mcolyer commentedon Mar 11, 2014
@nathansobo great writeup!
Yes.
I think this algorithm is very close.
What if we were to type
v i c(and pretendi cwas a motion in visual mode)? I think given the described it would wait until it receives another keystroke and it would not enter visual mode nor select the word which could be unintuitive. Maybe this won't happen in practice but I thought it would be worth bringing up now. Maybe we should have a maximum timeout for any single command and then follow the replay steps?nathansobo commentedon Mar 11, 2014
I added a clarifying point about what to do when we time out of a pending state. I think the case you're describing would play out as follows:
vis pressed. We go into a pending state and don't go into visual mode.iandcare pressed, putting us in a new pending mode due to the ambiguity withv i c ev i c e) and replay the queued keystrokes.vis replayed. It is now unambiguous so we go into visual mode.iandcare replayed, this time in visual mode, causing us to select your pretend text object.True, we don't enter visual mode right away when there's ambiguity, but this is exactly how vim behaves. For quick keystrokes i think it will barely be noticeable.
bhuga commentedon Mar 11, 2014
Is there a default vim keybinding that does this?
5 remaining items
nathansobo commentedon Mar 12, 2014
I'm going to implement this in the keymap. Started working on some cleanup yesterday and will continue today. Can't make any precise promises, but we should have this cleared up fairly soon.
nathansobo commentedon Mar 19, 2014
This should be fixed with the next release of Atom now that the keymap has been rewritten. It's also been open sourced for those who are interested or find issues with it.
schmty commentedon Apr 19, 2014
This still doesn't quite work for mapping something like
jkto activate command mode. Sure it will work. But now in insert mode I have to pressjtwice to register one j character inside an insert buffer.nathansobo commentedon Apr 24, 2014
You're right. This is going to take further thought. Does vim basically undo the insertion of the
jwhen it figures out you're triggering a binding?schmty commentedon Apr 24, 2014
@nathansobo From what I have seen (say i have
jkmapped to esc) vim won't place a j when insert mode until either 1. A timeout occurs, 2. A key other than k is pressed afterj(then j and the next key will both appear at the same time) or 3.kis pressed afterjtriggering the escape bindingnathansobo commentedon Apr 25, 2014
That makes sense. We're really close with the replay behavior, but we're messed up by the fact that input events are handled separately from keydown events by the browser. So we need to be able to replay a keystroke in a way that also produces an input event. I'm not even sure that possible in a straightforward way, but we can probably figure out a workaround.
mcolyer commentedon May 5, 2014
@nathansobo maybe another layer of abstraction?
nathansobo commentedon May 5, 2014
What do you have in mind? One possibility is to explore making the keymap manager more knowledgable about handling textual input. It's actually fairly nuanced, and other components may want to benefit from this facility. But handling the textinput events is somewhat entangled with the editor's update lifecycle, so I'm not 100% sure how easy it will be to extract. Worth exploring for sure.
mcolyer commentedon May 6, 2014
I'm going to wave my hands a bit since I don't know this area of the code super well. I was thinking whatever grabs the events ends up forwarding the request to a publicly visible method in the keymap manager.
That way other code could manipulate the keymap manager without having to create fake input events. This approach might be totally naive to the complexities involved here so please correct me.
nathansobo commentedon May 7, 2014
Something like that might work, but there would be more to the story. Let me sketch out my mental model for handling an input event with a text editor focused:
jin the input, it first emits a keydown event that bubbles up the DOM.atom.keymaps.handleKeyboardEvent.j kbound to escape, the keymap manager finds a partial binding forj.Today's Behavior
Here's where things get problematic. Today, when we find any sort of match, we call
preventDefaulton the event. This short-circuits any further action by the browser, halting insertion of input into the hidden text box.When the keymap manager exits the pending state by seeing another character or hitting the timeout, it replays the keydown event associated with the
jkeystroke internally, but this does not result in atextinputevent on the input box. The event is essentially discarded once it is matched in the keymap with respect to textual input.Hypothetical Future Behavior
Here's one idea for a solution. There may be better ones.
data-attribute on the input field, likedata-manage-textinput.textinputevent.textinputevent, the keymap matches the keystroke against plain-character bindings just as it did for keydown events.textinputevent fails to match any bindings, whether initially or during replay, we emit a custommanaged-textinputevent on the input field (perhaps there's a better name).This would enable plain keystrokes to be replayed that correctly result in input in the editor. All the logic that currently exists in the editor for handling the accented character menu, IME input, etc would need to be moved from the editor into the keymap manager for us to get away with this. There's a full implementation in the current editor view, and a partial but cleaner implementation in the new React-based editor component. We would need to be careful to cleanly slot in with the editor's DOM update when clearing the contents input field between events.
If anyone is interested in taking a stab at implementing this, I could definitely use help. Needless to say, any implementation would need to be test-driven to be merged. Otherwise I'll try to get to it soonish.
mcolyer commentedon May 7, 2014
That was the key part I was missing.
I wonder if a better approach isn't to throw the textinput event from inside the keymap manager using something like
textinput.dispatchEvent(...). Or are there issues with that approach as well?nathansobo commentedon May 7, 2014
Well, we need to allow the
textinputevent to occur "naturally" because the OS and Chromium do a lot of work to interpret the input corresponding to a given keystroke. I suppose we could manually hand the event to the keymap manager in the editor and then only continue to process it if the default isn't prevented...jatom.keymaps.handleTextInputEventwith the event.handleTextInputEventagain, but this time the default is not prevented and we proceed as normal.Might be nice because we don't have to introduce the
data-attribute and the special event name.mcolyer commentedon May 7, 2014
👍, makes sense to me.
drbraden commentedon May 9, 2014
@schmitzjake I have observed different behavior in vim on ubuntu 12.04. In your case of "jk", when I type j the j appears immediately, but the cursor doesn't advance until either the timeout of one second is reached or I type another key that doesn't have a mapping starting with j.
If I type k before the timeout, the j is removed from the screen and the cursor moves one character to the left (unless already at column 0).