My Solution to EmacsGolf 3

Here’s my solution to the EmacsGolf 3 challenge. When I first saw Saul’s post, the first thing that popped into my mind was that this is the classic use case for a keyboard macro. The only problem is that each line is slightly different. Fortunately, Tim Visher showed us the way early in his VimGolf in Emacs series.

We start by beginning a keyboard macro with 【F3】 and then just start typing the first line until we get to Route. Next we enter recursive edit with 【Ctrl+u Ctrl+x q】, type in Route, and exit the recursive edit with 【Ctrl+Meta+c】. Now we backwards kill the last word (textViewRoute), yank it back again, and type in the rest of the line until we get to the second textViewRoute where we yank it back in again. Finally we finish the line, type 【Return】 and end the macro.

We complete the problem by running the macro again for each line but type in the unique word for that line. Here’s the keystrokes for the macro definition:

TextView                ;; self-insert-command * 8
SPC                     ;; self-insert-command
textView                ;; self-insert-command * 8
C-u C-x q               ;; kbd-macro-query
<C-backspace>           ;; backward-kill-word
C-y                     ;; yank
SPC                     ;; self-insert-command
=                       ;; self-insert-command
SPC                     ;; self-insert-command
(TextView)convertView.findView  ;; self-insert-command * 30
ById(R.id.              ;; self-insert-command * 10
C-y                     ;; yank
);                      ;; self-insert-command * 2
RET                     ;; newline

That’s 69 keystrokes for the macro, two for the start and stop macro, 42 for the unique words, and 7 for the macro invocations. That’s a total of 120. It’s hard to see how you can do much better since almost all the keystrokes are just text that must appear1. If you have a better solution, by all means leave a comment.

Update: Oops. We need 8 more keystrokes for the 【Ctrl+Meta+c】 after each recursive edit.

Footnotes:

1 You could, I guess, save another 5 keystrokes by repeating the macro invocation 7 times with 【Meta+7 F4】.

This entry was posted in General and tagged . Bookmark the permalink.