Thursday, December 27, 2007

Emacs TIp #6: advice

This is an advanced tip, but it has simple applications.

Don't you wish you could change some of the built-in routines? You
like the way they work, but would tweak it just a little bit.

For example, 'ispell' does a great service: it provides suggestions
for the correct spelling of a misspelled word. Wonderful, I use it
all the time. However, it often has the order of choices exactly
opposite of what I'd expect.

e.g. `waht' - the correct option 'what' is #7!

`sopporting' - the correction option 'supporting' is #2!

It's much easier if the choice I usually pick is the first one.
So, it'd be great if I could just tell 'ispell' to reverse the order
of the choices provided.

You can!

Advice is a mechanism that lets you get code to run before, after, or
"around" existing elisp routines. You can modify the arguments going
in, you can modify the result, you can call the existing routing
multiple times. And this advice can be nested too.

Why use advice as opposed to re-writing the routine?

Because (especially in this example) - you don't want to muck with
having your own copy of the code. It's messy, and you might miss
upgrades to that routine with new versions of Emacs.

So, with this case in hand, I spent a minute tracking down the routine
that could be advised. I spent a minute writing the advice:

(defadvice ispell-command-loop (before ispell-reverse-miss-list activate)
"reverse the first argument to ispell-command-loop"
(ad-set-arg 0 (reverse (ad-get-arg 0))))

And now my spelling choices are in the right order! yay.

For more information on advice:
C-h i m advice RET

Emacs Tip #5: hippie-expand

One thing about Emacs is that it's always trying to save you from typing. Sure, there are jokes about repetitive strain injury, but, in fact, Emacs is trying to save you from typing.

One of the cooler built-in completion commands is 'dabbrev-expand, bound to M-/ by default.

What 'dabbrev-expand does is that it looks at the word you're currently typing and tries to expand it to match one a word you've already typed. It searches the text before the current point, then the text after the current point, and then it searches through other buffers. Repeated M-/ cycle through the possible completions.

A better binding is hippie expand:

(global-set-key (kbd "M-/") 'hippie-expand)

What 'hippie-expand adds to the mix is that a variety of completion functions, including the above-mentioned 'dabbrev-expand, along with others like expanding file names, expanding from the kill ring, etc.

For more documentation, you can read the info page:
C-h F hippie-expand RET
The documentation in the code for hippie-expand is useful (the *info* pages point you there. To read that do:
M-x find-library hippie-exp RET

My preferred setting for the 'hippie-expand functions is the following:

(setq hippie-expand-try-functions-list '(try-expand-dabbrev try-expand-dabbrev-all-buffers try-expand-dabbrev-from-kill try-complete-file-name-partially try-complete-file-name try-expand-all-abbrevs try-expand-list try-expand-line try-complete-lisp-symbol-partially try-complete-lisp-symbol))

9 Tips for the aspiring Emacs playboy | LispCast

Ha, saw this today and had to laugh because of the images. They are pretty good tips.

9 Tips for the aspiring Emacs playboy

The only other tip I'd add would be to subscribe to this blog.

Monday, December 17, 2007

Emacs Tip #4: keybinding FAQ

Jari Aalto has written probably the most comprehensive keybinding
guide for Emacs around. It can clear up any questions you have that weren't addressed by the *info* pages.

Edited to update linkLink

Monday, December 10, 2007

Emacs Tip #3: disabling commands

Emacs disables some commands by default, upcase-region is one such
command. I'm not sure why the commands are disabled, but they are,
and it can be real confusing when you come across one.

When you do, Emacs will prompt you with something like:

You have typed C-x C-u, invoking disabled command upcase-region:
Convert the region to upper case. In programs, wants two arguments.
These arguments specify the starting and ending character numbers of
the region to operate on. When used as a command, the text between
point and the mark is operated on.
See also `capitalize-region'.

You can now type
Space to try the command just this once, but leave it disabled,
Y to try it and enable it (no questions if you use it again),
! to try it and enable all commands in this session, or
N to do nothing (command remains disabled).

Very confusing for me.

One way of turning off such behavior is to add this to your .emacs:

(setq disabled-command-hook nil)

For more information on disabling commands, read the info page reached
by the following key sequence:

C-h i m elisp RET m disabling RET

Friday, December 7, 2007

Emacs Tip #2: global-auto-revert-mode

If you often look at files that are updated (perhaps a log file for a
running process), or perhaps ClearCase files (that change when you
update the config spec), you'll want to make sure you're looking at
the most recent version of the file.

To do this, add the following to your .emacs:

(global-auto-revert-mode 1)

For more documentation type:

C-h f global-auto-revert-mode RET

Thursday, December 6, 2007

Emacs Tip #1: where to start

This week's tip is dedicated to all the Emacs newbies. Here are a couple of places you new Emacs users might want to visit to get a taste of what is available.

C-h t
runs the tutorial

C-h i
loads up the *info* pages - hyperlinked documentation

C-h N
emacs news (what's new in this version)
the wiki pages
gnu emacs help newsgroup
this blog (indispensable)