Kate Vim Mode: Papercuts (+ bonus emscripten-qt stuff!)

I was a long time Vim holdout and often wondered to myself, “just why do those nutheads use vim?” until I googled and read the blog I just linked. After trying Vim out for a few days, I was completely hooked, but didn’t want to leave KDevelop with its awesome Intellisense/ code completion/ navigation features. Thankfully, Kate has a Vim mode, so I could get the best of both worlds ๐Ÿ™‚

However, I’d often run into small but jarring inconsistencies with Vim, or sometimes find missing features, and so I set about trying to fix as many as I could. Since these were usually very small and unimportant things, I’ll just go ahead and list them, Graesslin-style ๐Ÿ™‚

Vim mode is one of those rare and happy pieces of code which you can very easily write tests for: just add the starting text; the list of keypresses; and the expected, final text. Kuzmich Svyatoslav wrote a big battery of them a while back, and my first ever patch was an incredibly trivial little one that fixed a hole in the test framework and a bug that the hole obscured.

After that, I:

  • Improved the test framework to enable us to test Insert Mode features;
  • Fixed the inner curly bracket text object to be more like Vim (rather than deleting everything between the curly braces on a diB, Vim usually attempts to leave the closing curly bracket on its own line);
  • Added { and } motions to Visual Mode;
  • Fix a few bugs with Erlend’s very handy “comma text object” extension;
  • Fixed numerous bugs with t/T and , & ; involving them getting “stuck” behind occurrences of the character to search for, and also made them “counted” so that one can do e.g. 3ta, and also mirroring Vim’s behaviour where if we can’t find the required count of characters on the line, we don’t move at all;
  • Stopped the transition from Visual Mode to Normal mode from resetting the cursor position if we exited via ESC or Ctrl-c;
  • Fixed a bug with the nifty “block append” feature;
  • Fixed a nice bug with replaying the last command via . when the last command was an insertion that had a Ctrl-o in the middle of it (all the characters after we exited the Ctrl-o were treated as commands, with predictably zany results!)
  • Ensured that the bracket text object worked in Visual Mode;
  • Made the very handy Key Mapping feature “counted”, so you could perform your chosen Mapping several times in a row, and also made them count as one edit that could be reverted with just one request to Undo;
  • Made p & P move the cursor to the end of the pasted text instead of leaving it at the beginning (whoops!), and similarly for gp/gP which places the cursor one past the end, hopefully respecting Vim’s slightly obscure exceptions and corner cases (e.g. p and P act as gp and gp when in Temporary Normal Mode);
  • When doing Ctrl-o in Insert Mode, move the cursor back one step if we are at the end of the line;
  • Try to keep the length of the number under the cursor unchanged on Ctrl-x and Ctrl-a and also don’t let the cursor stray off the number;
  • Respect Vim’s rules for counting with Ctrl-x and Ctrl-a, especially with repeats;
  • Fix a lovely bug with some of the key parsing where the repeat last change command did not work after a Ctrl-a;
  • Made inserts (i, I, a, A, o, O and block append) counted, so you can do e.g. 100oHello, World!<ESC>, although I now see that the old implementation of counted o and O was “by design”. Oops – I’ll have to sort that out at some point :/;
  • Make Ctrl-c reset the parser so that e.g. d<Ctrl-c> aborts the deletion. This one was cool as it remained undetected for ages by a sheer fluke: the only visible signs were that after doing e.g. d<Ctrl-c>, the cursor would stay at half size, and also another bug that I had been promising myself I’d investigate for a while: typing r<Ctrl-c> would replace the current character with a nonsense character, which turned out to be the representation of the keypress Ctrl-c!;
  • Fixed a few minor flaws where after e.g. osometext<ESC>, we would have to request an Undo *twice* before getting back to where we issued the command;
  • Arranged for a return to Normal Mode if we Ctrl-c or ESC when in Temporary Normal Mode. Vim is weird, here: its behaviour is just as you would expect if you were in Normal Mode, but the status bar still claims we are in Insert Mode!
  • Fixed a bug where issuing y% would actually move the cursor as well as yanking the text;
  • Added the last edit markers, [,] and .;
  • Fixed a few bugs with inner bracket text objects being treated as invalid if there were less than three characters long;
  • Respected Vim’s rules for the [[, ][ etc motions when used with commands: they behave slightly differently than when they are used as cursor motions;

and that’s about it! Kate Vim’s testability make it very pleasant to develop features/ fixes Test First, so all of these are buttressed by a decent amount of automated tests.

The Future

I’m nearing the end of the papercuts stuff, and will shortly move onto slightly larger scale things. This will consist of implementing a Vim-style search and replace bar, with all the usual Vim shortcuts: Ctrl-c to dismiss; Ctrl-r to insert contents of register; Ctrl-rCtrl-w to insert the word under the cursor; etc, and with proper Vim style searching e.g. positioning the cursor on the first letter of the current match; return to where we were if cancelled; interactive search and replace with Y/N/Q/A; etc. I think I’ll use “smartcase” for deciding case sensitivity, but might make this configurable through a hidden config option if there is any demand for it.

There’s currently quite a lot of Vim-specific code that has leaked into Kate’s own Find/ Replace dialog, and I’d really like to be able to delete that and just have Vim use this Vim-specific version. What do people think? I’d have to get it at least as functional as Kate’s own one before I’d consider this, so we’d need e.g. some way of going through the Find and the Replace history separately (I don’t think stock Vim can do this?) and easily toggling whether this is a regex or a literal search, etc. I’d really like some feedback on this, and a suggestion for shortcuts etc!

Done properly, this would give us a powerful, keyboard-friendly Find (and Command Line) dialog where we can easily track the keypresses, which would set us up nicely for the next major task – Vim Macro support!

Before all that, though, I just have a couple of smaller odds and ends to sort out: I need to get auto-completion properly working with . (currently, if auto-completion is automatically invoked, pressing . won’t include the completed text), and it would be nice if Ctrl-p and Ctrl-n could wrap around in the list of completions as they do in Vim (need to sort out a Kate bug with completion first). I also have an another idea which I’d like some feedback on; it would be nice if people would try it out and let me know if they find it helpful, annoying, etc. If only there was a way of letting people test out a patch that hasn’t been committed yet … ! Which leads nicely on to:

emscripten-kate ๐Ÿ™‚

You may have seen my blogs on emscripten-qt, a port of Qt to Emscripten, an awesome technology that allows one to compile C/C++ to Javascript. This caught the eye of Mozilla, who were looking for an example of a “serious” app to go along with the games and utilities in their current list of Emscripten demos, and they kindly offered to sponsor me to work on this. After looking through a list of candidate apps, they eventually suggested porting Kate: not exactly an easy task, as Kate obviously requires not only a big chunk of kdelibs (with its reliance on DBUS and external processes like kded, etc) but also QtScript for its extensibility (the automatic code indentation for various programming languages, for example, is implemented in JS) meaning I’d need to get a C++-based Javascript interpreter ported to Javascript ๐Ÿ˜‰

This ended up being not as hard as I feared, and now we have a decent (though very cut-down; there are no plugins, so it’s more like KWrite than Kate ;)) version of Kate ported to Javascript. I’ve merged in the patch I’d like feedback on as mentioned above so you can easily try it out as long as you have a modern browser, plenty of bandwidth (even after server-side compression, you’re still looking at a tens of MB download :/) and also plenty of free RAM and CPU. There’s an unfortunate bug in Chrome which means you’ll need to use the no icons version, alas. Konqueror and reKonq also do not work, to my knowledge. Let me know what you think, and if anyone has any thoughts on shortcuts for the extended Find/ Command Line bar, do let me know!

10 thoughts on “Kate Vim Mode: Papercuts (+ bonus emscripten-qt stuff!)”

  1. Nice to hear that you’ll be working on the find&replace stuff.
    About the highlighting: I already left a comment at RB that I like it, so only thing left to say: If anyone wants to try it out, just download the diff from the review board and apply it with patch -p1 -i against the current master of Kate.

  2. Great work! I’d really love to see vim macro support in vim. Though the feature I miss most in vim mode is pipe through command using ‘!’. The most missing search feature for me is reverse search with ‘?’ and ‘N’.

    1. “Though the feature I miss most in vim mode is pipe through command using โ€˜!โ€™”

      Interesting – this might be something that could be useful to have in Kate proper, rather than just the Vim mode. What do people think?

      “The most missing search feature for me is reverse search with โ€˜?โ€™ and โ€˜Nโ€™.”

      This is definitely something I’ll be implementing along with the Find/ Replace dialogs stuff – the only reason I haven’t done it so far was that I was uncomfortable with adding more Vim-specific stuff to Kate’s own Find/ Replace.

      1. pipe in the command line: certainly. Maybe discuss on kwrite-devel? The KTextEditor::Command doesn’t provide any way to concatenate commands, maybe this needs work?

        N ? is ctrl+h and ctrl+shift+h, so the code is already there. Maybe it can be reused by the vi mode.

        1. “ipe in the command line: certainly. Maybe discuss on kwrite-devel? The KTextEditor::Command doesnโ€™t provide any way to concatenate commands, maybe this needs work?”

          Cool – will look into this at some point in the future ๐Ÿ™‚

          “N ? is ctrl+h and ctrl+shift+h, so the code is already there. Maybe it can be reused by the vi mode.”

          Ah, yes, it’s pretty easily added, it’s just that “?” will need yet more Vim-specific code added to Kate’s Find/ Replace dialog, which I’d prefer to avoid.

          1. โ€œpipe in the command line: certainly. Maybe discuss on kwrite-devel? The KTextEditor::Command doesnโ€™t provide any way to concatenate commands, maybe this needs work?โ€

            Maybe kate could use the konsolepart plugin to do this, so the output is shown, unless suppressed.

            โ€œN ? is ctrl+h and ctrl+shift+h, so the code is already there. Maybe it can be reused by the vi mode.โ€

            I have the generic experience that, when I am in vim mode, reverse search does not work, even if I press the buttons.

  3. I like emscripten-kate. It’s amazing and really seems to works. The only problem I have at the moment is, that I can’t enter curly braces and so on, since it appears all ALT+ and ALT-GR+ characters are eaten by the browser (firefox 19.0.2/german)

    1. This is what happens when a developer only uses a UK keyboard ๐Ÿ˜‰ Now that I come to think of it, I think I’ve neglected to add “ALT” support at all – although there is always the chance, as you say, that when do it will be eaten by the browser anyway (I did a bit of work to stop Chrome for eating keypresses, but it still gobbles up Ctrl-N despite my best efforts – I’d imagine it would be even more greedy when it comes to the ‘Alt’ key) :/

  4. I wish there was someway to hook up the vim plugins in Kate. I really miss Command-T and vim surround. Also, good support for buffers would be nice.

    Anyway, really nice work! Thanks a lot for all these fixes.

    1. “I wish there was someway to hook up the vim plugins in Kate. I really miss Command-T and vim surround.”

      Once my plans for the Command Line dialog are complete, you’ll be able to use the Mappings feature to call arbitrary Javascript using the shortcut of your choice. Hopefully a clone of “vim surround” at least will be implementable in Javascript ๐Ÿ™‚

Leave a Reply