Blogging from Emacs

October 20, 2008

After switching to WordPress, I can now post entries from Emacs, thanks to weblogger.el.

Note that there seems to be several “weblogger mode” packages for Emacs. Follow the link above to see how I got it working.

weblogger.el had a few problems, most of which were fixed by ciju. Thanks for that! However, it did require a few additional hacks to get it working in the way I wanted to.

Below is the diff between ciju’s version and mine.

> ;; NOTE: Modified by ciju as described here:
> ;;
> ;; Also, further hacking by Chopmo
 		    ; (list "Subject" title) ; fixed by chopmo
>           (list "Subject" (or title"\n"))
<   (if (buffer-modified-p)
   ;; modified by ciju, disabled by chopmo
> ;;   (if (buffer-modified-p)
> ;;       (weblogger-prepare-and-send nil nil)) ;; ciju's modification

You can also download the patched weblogger.el here.

The diff above tweaks the weblogger feature in two ways:

  1. Fixes a small bug which caused the newline after “Subject:” to be missing when starting a new entry.
  2. Reverts a change made by ciju which caused the current entry to be saved as a draft when navigating to the next (with C-c C-n) or the previous (with C-c C-p) entry.

The second change means that published entries stay published and that navigation between entries is very fast, but also that you need to actively save a modified entry before leaving it. This is done with C-c C-s to save it as published or C-c C-c to save it as draft.

Both commands simply overwrite the previous published/draft state of the entry, so there’s not explicit “publish” step. You just save the entry as published. Also, both commands can be used for initially saving the entry.

Another small tip: Note that after initially saving an entry, the URL to the entry is displayed in the header:


As long as you’re logged into your WordPress account, you can use this URL to see a preview of the entry as you work on it. The preview is updated as soon as you save the entry in Emacs.

Quickly jumping to symbols

September 3, 2008

This is brilliant.

For a while, I have been using ido to quickly jump between buffers and files by just typing any part of the file/buffer name I want. In fact, typing any sequence of letters narrows the interactive search to candidates with names that contain those letters in that order:

 fb -> Foobar, FrancisGailBrown

But for finding method definitions in a big source file, I have always just used isearch. It turns out that there is a better way.

By combining imenu with ido, I can quickly find methods as well:

All it takes is this method in my init.el:

(defun ido-goto-symbol ()
  "Will update the imenu index
   and then use ido to select a
   symbol to navigate to"
  (let ((name-and-pos '())
        (symbol-names '()))
    (flet ((addsymbols (symbol-list)
                       (when (listp symbol-list)
                         (dolist (symbol symbol-list)
                           (let ((name nil) (position nil))
                              ((and (listp symbol) (imenu--subalist-p symbol))
                               (addsymbols symbol))

                              ((listp symbol)
                               (setq name (car symbol))
                               (setq position (cdr symbol)))

                              ((stringp symbol)
                               (setq name symbol)
                               (setq position (get-text-property 1 'org-imenu-marker symbol))))

                             (unless (or (null position) (null name))
                               (add-to-list 'symbol-names name)
                               (add-to-list 'name-and-pos (cons name position))))))))
      (addsymbols imenu--index-alist))
    (let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
           (position (cdr (assoc selected-symbol name-and-pos))))
      (goto-char position))))

I have bound it to C-t, because I realized that I don’t use transpose-chars (or whichever transpose method was there..I don’t use any of those, really).

I found this trick on the imenu wiki page.


May 21, 2008

This small addon by William W. Wong really put a smile on my face!

Many of the source files I work with are huge (> 16000 lines), so I almost always need to do an isearch to find the point I need (which is often a pain due to false positives). Also, I often need to temporarily jump to another place in the same file.

Breadcrumbs are a “light” alternative to regular bookmarks, using a trail (or rather, a circular buffer) of anonymous bookmarks across all buffers. I have set up the keybindings to drop a breadcrumb with S-<space> and navigate back/forward with M-C-; and M-C-' respectively.

As a special bonus, a breadcrumb is automatically dropped when using find-tag to make it easy to backtrack after looking up a chain of symbol definitions. Very useful!

The feature is very well documented, by the way.

Effective tags?

March 26, 2008

A lot of my work hours are spent programming C++ in Emacs on Windows.

Earlier, I’ve done a lot of Java work in Eclipse and C# work in VS2005. And while I’m overall very, very happy with Emacs, I do miss the source code navigation features from the IDEs (especially Eclipse).

Right now, I’m using the version of ctags that comes with Emacs. Every night a scheduled task scans my source trees and rebuilds the tag files. I know only one keybinding: M-. (find-tag). Of course, I should spend some time familiarizing myself with the build-in tags package, but to be honest, I’m not very impressed with the list of features. Plus, find-tag quite often misses the mark completely.

So, I would like to check out the alternatives. I have been told that the Exuberant version of ctags is better, so I might give that a go.

If anyone has a decent Emacs configuration for setting this up, I’d be happy to see it. Also, any tips on C/C++ code navigation would be greatly appreciated.

I have always been annoyed that dired mode opens a new buffer for each directory it visits. I typically only use each buffer once anyway, and they get in the way of the autocompletion when switching between buffers with C-x b.

Only recently did I learn that simply pressing a instead of enter or C-m makes it reuse the same buffer. Nice!

For some reason, the function is marked dangerous, but that’s easily fixed:

(put 'dired-find-alternate-file 'disabled nil)

This one is simple, but important.

To move the cursor left, right, up and down, use C-b, f, p and n respectively.
Page down: C-v
Page up: M-v
Beginning of line: C-a
End of line: C-e

See the point (no pun intended)? Don’t move your wrists!

Everytime you do, you waste a little time repositioning your fingers. You may be thinking that the amount of time wasted this way is very small. Well, I disagree. Sure, moving your hand takes only a few tenths of a second, but programmers need to reposition the cursor and scroll up and down ridiculously often.

I’m really surprised to see how many otherwise fast typers haven’t taken the time to learn these shortcuts. Really, it’s easier than you think, especially because you use these commands all the time. Getting used to the new shortcuts should take you a few hours of determination, at most.

Faster point movement

February 21, 2008

My day job doesn’t include a lot of Lisp hacking.

On the positive side, though, this means that I’m free to redefine "M-C-p" (backward-list) and "M-C-n" (forward-list), because I never use them.

So here is today’s init.el addition:

(global-set-key "\M-\C-p"
  '(lambda () (interactive) (previous-line 5)))
           (global-set-key "\M-\C-n"
           '(lambda () (interactive) (next-line 5)))

I use a rather small font, so my 1600×1200 monitors can hold a lot of lines. This snippet allows me to move 5 lines at a time instead of one.