Wednesday, May 24, 2017

The surprisingly simple path to a foot powered emacs

I finally got around to soldering the components that power my USB foot pedal:

While it's hardly a polished build, it's currently working quite well. I've found that it's an essential accessory for navigating macOS. To be able to jump from monitor to monitor without moving my fingers from the home row is a joy.

On the Windows side of things, I'd planned to use the pedal to enhance emacs. There were two barriers to this: first, I needed a good key stroke related problem to solve. I've been using emacs for so long that even inefficient key-strokes feel right. The second challenge was figuring out how to integrate the pedal into emacs's keymaps and such. I imagined having to wade through thousands of lines of elisp to figure out an esoteric solution.

Turns out, I was way overthinking things. This article explained more than I needed to know about emacs key handling. It was this snippet that ultimately paved the way for a trivial implementation:

In order to actually bind a key you must first tell Emacs what key you intend to use. Unfortunately there’s more than one way of representing keys in Emacs: as a string, or as a vector. We won’t concern ourselves with either, as we will use a handy macro built in to Emacs called kbd.

The kbd macro translates a human-readable key into a format Emacs can understand.

I had previously coded the foot pedal to send F8 whenever it was pressed. I hesitantly popped into the *scratch* buffer and evaluated the following:

(global-set-key (kbd "<f8>") 'goto-line)

I pressed the foot pedal and emacs prompted me for a line to jump to.

OK, fair enough. But surely setting the foot pedal up to act as a sort of prefix key would require some elisp-fu. After invoking global-unset-key on F8 I tried the following:

(global-set-key (kbd "<f8> r") 'rgrep)
(global-set-key (kbd "<f8> g") 'goto-line)

I pushed the pedal down and typed the letter 'r'. And just like that, emacs asked me what I wanted to search for. I tried the same with pushing the pedal followed by g. That worked to.

Apparently kbd and global-set-key just do the right thing. So much for having to read mountains of elisp code.

I was then left with my initial challenge: how did I actually want to use the pedal to be efficient? For now, I've decided that any command I regularly type M-x, I'll bind to the pedal plus another key. As you can guess, M-x rgrep and M-x goto-line are two common examples of this. I'll definitely be on the lookout for more interesting challenges to solve, but at least it's a start.

Have any suggestions for how I can get put the foot pedal to use? Please drop me a comment below.

6 comments:

  1. Using this to replace the CTRL key could be righteous.

    ReplyDelete
  2. I would be tempted to bind it to ctrl or meta, but that's just me. I could also imagine binding it to a hydra.

    I'd be interested in this project, but am concerned about its interop with Linux. Has anyone successfully gotten foot pedals to work with Emacs on Linux?

    ReplyDelete
  3. Using this to replace the CTRL key could be righteous.


    โกลเด้นสล็อต


    goldenslot

    ReplyDelete
  4. > I'd be interested in this project, but am concerned about its interop with Linux. Has anyone successfully gotten foot pedals to work with Emacs on Linux?

    From the OS's perspective, the device is a USB keyboard, and nothing more. It's a boring keyboard, as it sends exactly one key, but it's a keyboard none the less.

    I see no reason why this wouldn't work with Linux.

    > Using this to replace the CTRL key could be righteous.

    This would be interesting; but for my purposes probably not very practical. My fingers are so used to finding control that anything else would probably just slow me down.

    Just today I added these bindings:

    (global-set-key (kbd " b s") 'bookmark-set)
    (global-set-key (kbd " b j") 'bookmark-jump)
    (global-set-key (kbd " b d") 'bookmark-delete)

    which I'm liking so far.

    ReplyDelete
  5. I'm curious why people call these devices "foot pedals" rather than "pedals."

    ReplyDelete
  6. Pat - that's a good question. I think you're right, from now on, it's just a pedal. :-)

    ReplyDelete