VimGolf In Emacs: A Numbered List

The conventional wisdom is that Vim is much more efficient—at least in keystrokes—than Emacs. In working the various VimGolf challenges and reading (or watching) other peoples’ solutions I’ve found that that’s often true but not always. A non-trivial part of the time Emacs can do much better than Vim. This challenge is an example of that.

This VimGolf challenge is to convert a pandoc unordered list to a numbered list. That’s extraordinarily simple if we’re using Org mode but even treating the file as just text we can still do better. The challenge is to take the starting file

My cursor happens to be on this line

## Here is my unordered list
   * item 1
      + it has some sub-items
         - sometimes it has sub-sub-items
      - i list some information about it
   * here is another item
   * and another
      - with some random sub-item
      - maybe with something in **bold** to make asterisks suck
   * woohoo I'm listing stuff like mad
   * I'm a mad pandoc'ing fool
      - long-live **John MacFarlane**!
      + reasons why pandoc is awesome
         - there's just too many list
         - but i'll put some more sub-sub-items for example
         - superfluous - and + to make matching them a pain
   * woo look at these sexy bullet points
   * you almost don't want to turn them into integers
   * but then you do because you know it's the right thing to do

## Here's some other stuff in the file
   * blah blah blah
   * maybe I should go with #. but then it's not readable in txt format

and make part of it numbered.

My cursor happens to be on this line

## Here is my unordered list
   1. item 1
      + it has some sub-items
         - sometimes it has sub-sub-items
      - i list some information about it
   2. here is another item
   3. and another
      - with some random sub-item
      - maybe with something in **bold** to make asterisks suck
   4. woohoo I'm listing stuff like mad
   5. I'm a mad pandoc'ing fool
      - long-live **John MacFarlane**!
      + reasons why pandoc is awesome
         - there's just too many list
         - but i'll put some more sub-sub-items for example
         - superfluous - and + to make matching them a pain
   6. woo look at these sexy bullet points
   7. you almost don't want to turn them into integers
   8. but then you do because you know it's the right thing to do

## Here's some other stuff in the file
   * blah blah blah
   * maybe I should go with #. but then it's not readable in txt format

The best Vim solution is 20 keystrokes. Here’s my solution that does it in 12.

Ctrl+1 F3 Start macro with counter set to 1
Ctrl+s␠␠* Search for leading star
Return Exit incremental search
Backspace Delete star
F3. Insert counter and period
Ctrl+8 F4 Exit macro definition and execute it 8 times

Of course, if we treat the starting file as an Org buffer we can do it in 4.

Ctrl+3 Ctrl+n Move down to line of first star
Ctrl+c - Change to numbered
Posted in General | Tagged | 4 Comments

Some Common Lisp Resources

Here’s a collection of some useful Common Lisp resources that I’ve come across recently.

Posted in Programming | Tagged , | 2 Comments

A First Emacs Challenge

Xah Lee has contributed a challenge that serves as an excellent example of one of the types of problems I think we should concentrate on if we start an EmacsGolf site. I like this problem because it is a real problem of the type that Emacs users encounter every day.

Lee has a minor mode, xmsi-mode, that is useful for mathematical input (it’s very handy if you do that sort of thing; check it out). Recently, he was refactoring the code and wanted to change lines like

(puthash "■" "□" xmsi-abrvs)
(puthash "□" "■" xmsi-abrvs)
(puthash "●" "○" xmsi-abrvs)
(puthash "○" "●" xmsi-abrvs)
(puthash "◆" "◇" xmsi-abrvs)
(puthash "◇" "◆" xmsi-abrvs)
(puthash "▲" "△" xmsi-abrvs)
(puthash "△" "▲" xmsi-abrvs)
(puthash "◀" "◁" xmsi-abrvs)
(puthash "◁" "◀" xmsi-abrvs)
(puthash "▶" "▷" xmsi-abrvs)
(puthash "▷" "▶" xmsi-abrvs)
(puthash "▼" "▽" xmsi-abrvs)
(puthash "▽" "▼" xmsi-abrvs)
(puthash "★" "☆" xmsi-abrvs)
(puthash "☆" "★" xmsi-abrvs)
(puthash "♠" "♤" xmsi-abrvs)
(puthash "♤" "♠" xmsi-abrvs)
(puthash "♣" "♧" xmsi-abrvs)
(puthash "♧" "♣" xmsi-abrvs)
(puthash "♥" "♡" xmsi-abrvs)
(puthash "♡" "♥" xmsi-abrvs)
(puthash "♦" "♢" xmsi-abrvs)
(puthash "♢" "♦" xmsi-abrvs)

into lines like this

(xmsi-add-cycle ["■" "□"])
(xmsi-add-cycle ["●" "○"])
(xmsi-add-cycle ["◆" "◇"])
(xmsi-add-cycle ["▲" "△"])
(xmsi-add-cycle ["◀" "◁"])
(xmsi-add-cycle ["▶" "▷"])
(xmsi-add-cycle ["▼" "▽"])
(xmsi-add-cycle ["★" "☆"])
(xmsi-add-cycle ["♠" "♤"])
(xmsi-add-cycle ["♣" "♧"])
(xmsi-add-cycle ["♥" "♡"])
(xmsi-add-cycle ["♦" "♢"])

This isn’t a hard problem but how efficiently (in terms of keystrokes) can you solve it? You can use anything in core Emacs, ELPA, or any external tool accessed in the usual way from Emacs. As a baseline, I did it in 29 keystrokes without trying too hard. I’m sure many of you can do better. I’ll be trying too.

Thanks to Xah Lee for generously donating this challenge rather than posting it himself.

Posted in General | Tagged , | 8 Comments

An Emacs Minor Mode Tutorial

Over at the Google G+ Emacs Community, Agu Monkey has a pointer to an excellent tutorial on writing Emacs minor modes by Christopher Wellons. Most of us probably have no interest in writing a full-fledged minor mode (although Wellons’ tutorial is complete enough for that) but it’s often an easy way to provide some custom key bindings for a major mode or otherwise provide additional functionality.

Wellons takes us through the development of a trivial minor mode that merely inserts the word “foo” when called with a 【Ctrl+c f】 key sequence and keeps a count of how many foos are inserted. He shows how to make a buffer-local variable to count the insertions, how to instantiate a hook-function variable that users can insert hook functions into to run their own code when the minor mode is toggled, how to define a keymap for the minor mode, and how to activate the minor mode when a major mode is activated.

This is a really useful tutorial that you should read and then bookmark for the day that you want to add a little extra functionality to some mode or when you want to write a full blown minor mode. Highly recommended.

Posted in Programming | Tagged | 2 Comments

Stross on Ebooks

Charlie Stross has a very interesting post on the history of ebooks. Stross, of course, is a well known Science Fiction author who earns his living writing books so he’s very familiar with the publishing industry and its history. The post is mainly the text of a speech he gave at the CREATe conference, a UK research council made up of academics from 7 universities intended to help the UK “creative” industries adapt and thrive in the digital age.

Stross traces the history of ebooks, the reaction to them by the publishing industry, and their effect on authors. Most Irreal readers will be familiar with some of the story but Stross fills in the gaps and brings all the pieces together. It’s an informative post and well worth a read.

Posted in General | Leave a comment

Emacs Challenge?

Some of the most popular posts here at Irreal are those in which I attempt bring the power of Emacs to bear on VimGolf problems. I like those posts too because I always learn some new tricks both by working on the problem myself and from the (usually better) solutions that readers contribute. Commenters on other blogs have lamented that there’s no EmacsGolf to challenge and entertain users of the one true editor. I’ve been wondering if there’s really any interest in that.

To me the value of such a site would be the opportunity to try some challenges working the way the typical Emacs user works and thus learn new techniques both self-discovered and from others. That means that the rules should be a bit different from VimGolf. Here’s some ideas off the top of my head

  • Any ELPA package from the standard sites could be used because, after all, that’s how we actually do our work.
  • External tools accessed through mechanisms such as 【Ctrl+u Meta+|】 are allowed for the same reason.
  • Solutions would be held for a week, say, and then posted all at once.
  • Maybe favor real-world problems so that the solutions are more generally applicable.

Is there any interest in such a thing? If so, we can start off easy by posting the challenges and solutions here at Irreal. If there’s enough participation, it would probably be worth moving it to its own site. I am definitely not a front-end person so if I do all the heavy lifting, even the new site would be WordPress (or something) based. If someone with the requisite skills wants to get involved we could make it more like the VimGolf site or whatever seems best.

If you would be interested in this leave a comment. Feel free to suggest additional or alternative rules. If enough people respond positively, I’ll make up some challenges (and solicit them from readers) and start posting them here at Irreal. If we get enough traction, I’ll mount a separate site.

I thought about calling this thing EmacsGolf or something similar but that seems a bit derivative so I’m thinking along the lines of The Emacs Challenge or something like that. Suggestions welcome.

Update: 【Ctrl+|】 → 【Meta+|

Posted in Blogging | Tagged | 9 Comments

VimGolf in Emacs: Enumerate Words

Here’s a pretty hard challenge—at least I found it challenging. Given this Lorem Ipsum

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
no sea takimata sanctus est Lorem ipsum dolor sit amet.

list the unique, capitalized words in alphabetical order:

Accusam
Aliquyam
Amet
At
Clita
Consetetur
Diam
Dolor
Dolore
Dolores
Duo
Ea
Eirmod
Elitr
Eos
Erat
Est
Et
Gubergren
Invidunt
Ipsum
Justo
Kasd
Labore
Lorem
Magna
No
Nonumy
Rebum
Sadipscing
Sanctus
Sea
Sed
Sit
Stet
Takimata
Tempor
Ut
Vero
Voluptua

The winning Vim solutions use 23 keystrokes but the best I can do is 32. My solution:

Ctrl+Meta+% Invoke query-replace-regexp
space Return Replace spaces
Ctrl+q Ctrl+j Return With carriage returns
! In the entire buffer
Meta+< Back to top-of-buffer
Ctrl+Meta+% Invoke query-replace-regexp
[.,]Return Replace periods and commas
Return With nothing
! In the entire buffer
Return Down one line
Ctrl+Return Invoke rectangle mode
Meta+< Extend rectangle to top-of-buffer
Meta+u Upcase rectangle
Ctrl+x h Mark buffer
Ctrl+u Meta+| Pipe to shell and replace
sort -uReturn Sort and delete duplicates

Most of the work involved the two query-replace-regexp calls but I couldn’t figure out a way to do it in one without causing other problems. The call to the external sort may be a technical violation but I’m claiming that we’re grandfathered in by Tim on that.

I think the Vimers did so well because Vim has an internal sort that’s easy to invoke. I’m sure many of you can beat my pathetic 32 so leave a comment with your better solutions.

Posted in General | Tagged | 14 Comments

Xah’s Emacs Tutorial

I’ve commented many times on Xah Lee’s Emacs and Emacs Lisp tutorials. I’ve learned a lot from them and the Elisp tutorials helped me transfer my Common Lisp and Scheme knowledge to Emacs.

The tutorials are on the Web and available for free at the links above but Lee also makes them available as ad-free zip files for those who’d like a local copy. Lee charges $5 for the files but makes updates available for free. Lee has just released another archive of the tutorials so this is a good time to get a copy if you’re interested. Buying the tutorials is a good way to support Lee’s effort so if, like me, you find them useful contribute the $5 if you can.

Posted in General | Tagged | 1 Comment

Sony: The Bill So Far

Back in 2011 I wrote about the Sony break in and subsequent disclosure of 100 million log on credentials. At the time I remarked that it would be years before the final cost of the exploit would be known. Now we know that the answer—at least for monetary costs—is $172,000,000. It’s almost enough to make you feel sorry for Sony until you remember the details.

One would think that this object lesson would get other high profile sites to clean up their act but of course it hasn’t. We still have sites storing passwords in plain text, not keeping their systems patched, misusing SSL and all manners of other transgressions. The article at the link says that the average attack costs a company $5,500,000. You’d think that would be enough to get their attention but apparently not. At least it provides a never ending source of blog posts.

Posted in General | Tagged | Leave a comment

Another Emacs Quote

Vivkek Haldar appears to be the go to guy for Emacs quotes. Magnar Sveen’s excellent What the Emacs.d!? begins and end with a Haldar quote. Now Haldar brings us a quote from Kieran Healy on the enduring excellence of Emacs. Take a look. If you’re an Emacasien1, it will resonate.

Footnotes:

1 Hey, it’s my blog; I get to make up words.

Posted in General | Tagged | Leave a comment