Sacha on Emacs Micro-Habits

Sacha Chua, as everyone here knows, is relentless about improving her work flow and procedures. Happily for us, she's also diligent about sharing the results of her efforts with the rest of us. Her recent outstanding post on cultivating Emacs micro-habits is an excellent example of this.

She's thought more deeply than most of us about which habits make sense and are worth the effort to burn into our muscle memory. The post represents some of her conclusions so far. Even if you're an experienced Emacs user, you are likely to find something useful in the post that you didn't know.

For example, she has a nice idiom for dealing with the problem of writing commands that work on the entire buffer unless a region is defined. I've long used a macro for that but Chua has another way. Suppose we want to extract the contents of a buffer into a string unless a region is defined, in which case we want the contents of just the region. Chua uses conditon-case like this:

(setq contents (condition-case nil (buffer-substring beg end) (mark-inactive (buffer-string))))

That's not a micro-optimization in itself but is part of a function that copies code into a blog post, posts it as a Gist, and adds a link to the Gist in the blog post. That's something useful too if you post code as a Gist.

Another nice trick I learned is how to programmatically retrieve properties from an Org mode buffer. That's also very useful if you want to write code to automatically process an Org buffer.

The whole post is full of goodness and you really should give it a read. If you're interested in improving your work flow, you'll probably find something useful.

This entry was posted in General and tagged , . Bookmark the permalink.
  • Joost Kremers

    TBH, I don't find the use of `condititon-case` here a nice idiom. When I see `condition-case`, I expect a form that needs to be protected against certain errors. Here it's used as a kind of if-statement. `use-region-p` or your macro indicate much more clearly what they're about...

    • abo-abo

      Somehow I get the association of using `try`, `throw` and `catch` in C++, which would be a very inefficient way to get an `if` statement. Also, `(condition-case nil foo)` has a standard macro shortcut `(ignore-errors foo)`.

      • jcs

        Yeah, I agree with you guys that it's probably not the most efficient way of doing things. What appealed to me was the "thinking outside the box" that it demonstrated. Truth to tell, the application it was in isn't likely to notice the overhead but, yes, use-region-p.

        • I totally stole it from gist.el, and have now replaced the code with if with use-region-p, since that *is* much nicer. =)

          Speaking of programmatically getting data from Org buffers, here's a sneak peek at an upcoming post on reading from Org tables: http://sachachua.com/blog/?p=27871&shareadraft=baba27871_54bffeb9b71e7 Enjoy!

          • jcs

            Very very cool. I'll probably have something to say about it when it appears.