Skip to content
This repository was archived by the owner on Apr 6, 2018. It is now read-only.
This repository was archived by the owner on Apr 6, 2018. It is now read-only.

Multiple keypress bindings are incorrect/limited #129

Closed

Description

@mcolyer

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.

'.vim-mode.command-mode:not(.mini)':
  'g': 'vim-mode:move-to-line'
  'g g': 'vim-mode:move-to-start-of-file'
  'g t': 'pane:show-next-item'

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.

Activity

  1. nathansobo commented on Mar 5, 2014

    @nathansobo
    Contributor

    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.

  2. jroes commented on Mar 5, 2014

    @jroes
    Contributor

    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?

  3. zenhob commented on Mar 5, 2014

    @zenhob
    Contributor

    @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.

  4. nathansobo commented on Mar 5, 2014

    @nathansobo
    Contributor

    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.

  5. jroes commented on Mar 5, 2014

    @jroes
    Contributor

    @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.

  6. nathansobo commented on Mar 5, 2014

    @nathansobo
    Contributor

    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.

  7. nathansobo commented on Mar 5, 2014

    @nathansobo
    Contributor

    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.

    '.vim-mode.command-mode:not(.mini)':
      'd': 'vim-mode:delete'
      'i': 'vim-mode:inside-text-object'
      'w': 'pane:word'

    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?

  8. jroes commented on Mar 5, 2014

    @jroes
    Contributor

    @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.

  9. nathansobo commented on Mar 5, 2014

    @nathansobo
    Contributor

    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.

    Thanks for your thoughts!

  10. jroes commented on Mar 7, 2014

    @jroes
    Contributor

    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?

  11. nathansobo commented on Mar 11, 2014

    @nathansobo
    Contributor

    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:

    '.command-mode':
      'v': "vim:visual-mode"
      'v i c e': "vim:play-favorite-miami-vice-episode"
    
    '.visual-mode':
      'i w': "vim:select-inside-word"

    We want a solution that supports the following behavior:

    • 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.

    Here is a rough outline of how the algorithm could work in the keymap:

    • When we get a keystroke, we push it onto a queue.
    • Then we check what commands have a prefix matching the currently enqueued keystrokes.
    • When the queue unambiguously matches a command, we execute it and clear the queue.
    • When there is ambiguity between multiple commands for the current prefix, we put the keymap into a pending state.
      • If the pending state times out, we temporarily disable the longest ambiguous bindings and replay the queue. See below for more info about the replay.
    • If the set of ambiguous bindings includes an exact match, we set a timeout for the pending state.
    • Otherwise, the next keystroke terminates the pending state. To terminate the state:
      • We push the newest keystroke to the queue and look for matches again.
      • If we are again ambiguous, we put the keymap back into a pending state.
      • If we have a single match, we clear the queue and execute it.
      • If we match nothing, we temporarily disable the longest of the ambiguous bindings that led to the current pending state, then replay the keystrokes
      • If during the replay we again get into an ambiguous state, we disable the next-longest ambiguous bindings and replay again.
      • If during the replay we don't find a matching binding, we abort the replay.
  12. mcolyer commented on Mar 11, 2014

    @mcolyer
    ContributorAuthor

    @nathansobo great writeup!

    • 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?

  13. nathansobo commented on Mar 11, 2014

    @nathansobo
    Contributor

    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.

  14. bhuga commented on Mar 11, 2014

    @bhuga
    Contributor

    but this is exactly how vim behaves.

    Is there a default vim keybinding that does this?

  15. 5 remaining items

  16. nathansobo commented on Mar 12, 2014

    @nathansobo
    Contributor

    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.

  17. nathansobo commented on Mar 19, 2014

    @nathansobo
    Contributor

    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.

  18. schmty commented on Apr 19, 2014

    @schmty

    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.

  19. nathansobo commented on Apr 24, 2014

    @nathansobo
    Contributor

    You're right. This is going to take further thought. Does vim basically undo the insertion of the j when it figures out you're triggering a binding?

  20. schmty commented on Apr 24, 2014

    @schmty

    @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

  21. nathansobo commented on Apr 25, 2014

    @nathansobo
    Contributor

    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.

  22. mcolyer commented on May 5, 2014

    @mcolyer
    ContributorAuthor

    @nathansobo maybe another layer of abstraction?

  23. nathansobo commented on May 5, 2014

    @nathansobo
    Contributor

    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.

  24. mcolyer commented on May 6, 2014

    @mcolyer
    ContributorAuthor

    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.

    document.addEventListener("input", function(e){
    KeymapManager.receiveEvent(e)
    })
    

    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.

  25. nathansobo commented on May 7, 2014

    @nathansobo
    Contributor

    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.

  26. mcolyer commented on May 7, 2014

    @mcolyer
    ContributorAuthor

    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?

  27. nathansobo commented on May 7, 2014

    @nathansobo
    Contributor

    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 handleTextInputEvent again, 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.

  28. mcolyer commented on May 7, 2014

    @mcolyer
    ContributorAuthor

    👍, makes sense to me.

  29. drbraden commented on May 9, 2014

    @drbraden

    @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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions