2009-12-30

automatically checking your spelling

When I'm typing fast, it's easy to make spelling mistakes (as you may have noticed reading Emacs-Fu). It's not so much that I don't know how to write things, but sometimes there seems to be a bit of a disconnect between brain and fingers.

One tool that helps me to make fewer mistakes is automatic spelling checking, and in particular on-the-fly spelling checking. Spell-checking in emacs is taken care of by the ispell-package. Traditionally, this was a way to interface emacs with the ispell-program on Unix, but nowadays it's often used with other programs, such as aspell.

aspell is GNU's intended replacement for ispell, and has been for a long time. In the meantime, other spelling checkers have come up; in the Free Software world, the most prominent one is probably hunspell, which is used by e.g. Mozilla and OpenOffice. As I said, Emacs supports all of those; I'm using aspell, which works well for me. To use aspell for spelling-checking, I have the following in my .emacs:

(setq ispell-program-name "aspell"
  ispell-extra-args '("--sug-mode=ultra"))

Please consult the aspell documentation for the details.

You can spell-check your text with ispell-buffer and friends, which are also available through the menu (Tools/Spell Checking/...). This works fine, but it makes spelling checking a separate step you have to go through and you may forget. So, I like to do the spelling-checking on-the-fly, that is, while I am typing. This can be done using flyspell-mode (you can also use flyspell-prog-mode, to do spell checking inside comments in computer programs, but I find that a bit over the top).

When flyspell-mode is enabled, it will mark misspelt eh misspelled words by painting them in a different color while you are typing -- the common way many word processing programs do it. A common way to enable flyspell-mode is to put it the the mode-hook for the modes where you want to use it. For example, to enable flyspell-mode for all your org-mode buffers, you can add something like the following to your .emacs:

(add-hook 'org-mode-hook
  (lambda()
    (flyspell-mode 1)))

Note, you can use the middle mouse button to get alternatives for a misspelled word.

By default, it uses the English dictionary, but it's easy to switch to another using M-x ispell-change-dictionary. To make it easier, I have defined the C-c N key binding to activate Dutch-language ("nederlands") spelling checking, and update the buffer.

(global-set-key (kbd "C-c N") 
  (lambda()(interactive)
    (ispell-change-dictionary "nederlands")
    (flyspell-buffer))) 

Now, there's another category of mistakes – their-they're-there, its-it's or to-too-two that require a spelling checker that's a bit smarter. There are some free implementations in OpenOffice and Abiword; it'd be interesting to see if those could be integrated with emacs as well.

Now, laugh about the sweet irony of the spelling errors that I failed to notice :)

2009-12-13

scrolling

Scrolling It's an integral part of just about any graphical user interface, including emacs. However, I always found that the default way scrolling works in emacs left something to be desired. It puts the scroll bar on the left (!), and when scrolling around, it does not scroll smoothly, but instead it seem to do so in bursts. But, this being emacs, we can change it!

First, the position of the scroll bar. Presumably for historical reasons, emacs puts the scroll bar on the left of the window, unlike most other programs. We can easily change that, by putting the following in .emacs (or ~/.emacs.d/init.el):

(set-scroll-bar-mode 'right)

Instead of right, you can also use left, or nil to hide the scroll bar completely. You can also do this through the menu (Options / Show/Hide / Scroll bar). Note that on X, when the cursor (point) reaches the end of the document, the slider on the scroll bar may not be at the bottom; I understand this is because of some disagreement between Emacs and the toolkit (GTK+ in this case).

Now, what about the other issue, the non-smoothness when scrolling with the cursor-keys or with C-n, C-p? Below are my settings for making scrolling a bit smoother, and the explanation. Of course, these are just my personal preferences.

(setq
  scroll-margin 0                  
  scroll-conservatively 100000
  scroll-preserve-screen-position 1)
  • The scroll-margin. This determines when scrolling should start; by setting it to 0, emacs will start to scroll whenever you are entering the top or bottom line of the window. You can also this to, say, 5 to let scrolling start whenever you're getting closer than 5 lines from top or bottom
  • Then, scroll-conservatively determines how far the cursor is allowed to be distanced from the center of the screen when scrolling start. The default sets this to 0, which means that whenever you start scrolling, the cursor jumps to the center of the screen. I find that quite annoying, so I set it to some big number (the 'effective maximum' for that is lines-in-window / 2, but you can put any bigger number there to avoid the jumpiness)
  • scroll-preserve-screen-position tries to maintain the current screen position when you scroll using Page-Up/Page-Down. I like that.

There are also the variables scroll-up-aggressively and scroll-down-aggressively. Normally, they determine how far emacs will scroll (up and down, respectively) when it does so. However, they don't make any difference with a big scroll-conservatively like I am using. Still, if you want to play with it, their values are fractions between 0.0 and 1.0 (inclusive); a value of 1 means that it will move a full screen when scrolling starts, a value of 0.0 causes a move of only one single line.

2009-12-06

changing the cursor color and shape dynamically

When typing some text, it's often useful to know what mode we're in – are we in overwrite-mode, in read-only-mode, or in normal insert-mode. The information is available in the mode-line – but wouldn't it be nicer to get some small visual cue, for example by changing the cursor color or style?

That indeed is possible. There are some existing ways to do this, described in EmacsWiki. However, I want to be able to control both the cursor color and cursor shape, and also distinguish between overwrite, read-only and 'normal' mode. Below is my attempt.

By putting the following snippet in your .emacs, the cursor becomes a yellow vertical bar during normal mode, it becomes a red block when you're in overwrite-mode and it becomes a gray vertical bar when you're in read-only mode.

;; Change cursor color according to mode; inspired by
;; http://www.emacswiki.org/emacs/ChangingCursorDynamically
(setq djcb-read-only-color       "gray")
;; valid values are t, nil, box, hollow, bar, (bar . WIDTH), hbar,
;; (hbar. HEIGHT); see the docs for set-cursor-type

(setq djcb-read-only-cursor-type 'hbar)
(setq djcb-overwrite-color       "red")
(setq djcb-overwrite-cursor-type 'box)
(setq djcb-normal-color          "yellow")
(setq djcb-normal-cursor-type    'bar)

(defun djcb-set-cursor-according-to-mode ()
  "change cursor color and type according to some minor modes."

  (cond
    (buffer-read-only
      (set-cursor-color djcb-read-only-color)
      (setq cursor-type djcb-read-only-cursor-type))
    (overwrite-mode
      (set-cursor-color djcb-overwrite-color)
      (setq cursor-type djcb-overwrite-cursor-type))
    (t 
      (set-cursor-color djcb-normal-color)
      (setq cursor-type djcb-normal-cursor-type))))

(add-hook 'post-command-hook 'djcb-set-cursor-according-to-mode)

You can change the colors and cursor types by modifying the various variables.

I should probably turn this into a proper minor mode, but for now this seems to work well.

2009-11-29

making buffer names unique

When you open a file in emacs, the buffer gets the name of that file. That's all fine, but what if you open multiple files with the same name? At least for me, it's a fairly common to have a number of different Makefile.am buffers Makefile.am<3> etc., but that does really help to find the right one at the same time. Emacs does make those names unique – Makefile.am<2>, quickly.

To do that, emacs provides uniquify – it makes buffer names unique. In your .emacs:

(require 'uniquify) 
(setq 
  uniquify-buffer-name-style 'post-forward
  uniquify-separator ":")

This is emacs, so you can influence the way in which the names are made unique. I prefer post-forward, and as separator I use a : rather than the default |. Note, instead of post-forward there are other bizarre styles, please see the documentation.

Anyway, now, when opening ('visiting') files test/a/foo and test/b/foo, their buffers get the names foo:a and foo:b. In other words, the name followed by a colon and part of the path. I think it's much clearer than the default names foo and foo<2>. One could ask why emacs should not use uniquify as its default behavior; it seems a clear improvement.

Uniquify is a small convenience that's been a documented part of emacs for 20 years. Still, somehow I missed it until this year. I suspect I am not the only one - which is why I write this.

2009-11-19

showing pop-ups

Updated: yes, it's %s, not %d Sometimes, it's nice when emacs can warn you when something is happening or should happen. For example, when a new e-mail has arrived, or when there's a meeting in 15 minutes you should attend.

As always, there are different way to do this, but here's what I've been using for while. Various versions of this have been circulating around mailing lists, so I don't know whom to credit with the original idea – anyway, this is the (modified) version that I'm using.

(defun djcb-popup (title msg &optional icon sound)
  "Show a popup if we're on X, or echo it otherwise; TITLE is the title
of the message, MSG is the context. Optionally, you can provide an ICON and
a sound to be played"

  (interactive)
  (when sound (shell-command
                (concat "mplayer -really-quiet " sound " 2> /dev/null")))
  (if (eq window-system 'x)
    (shell-command (concat "notify-send "

                     (if icon (concat "-i " icon) "")
                     " '" title "' '" msg "'"))
    ;; text only version

    (message (concat title ": " msg))))

A couple of notes:

  • I'm using notify-send for sending notifications; this assumes you are using that system (it's part of the libnotify-bin package in Debian/Ubuntu). You can of course replace it with whatever is available on your system. Alternatives are zenity or kdialog or xmessage (for old-timers) and their equivalents (?) on Windows, MacOS.
  • I'm now using mplayer for playing sounds. This is a bit heavy, but at least plays all kinds of audio files. If you only care about .wav-files, you could replace it with e.g. aplay;
  • as always, please ignore my ego-centric function names :-)

Now, we can use this function by evaluation e.g.

(djcb-popup "Warning" "The end is near"
   "/usr/share/icons/test.png" "/usr/share/sounds/beep.ogg")

showing pop-ups from org-mode appointments

The above popup function is most useful when it's does its work based on some event. To be notified of appointments and the like, there is the emacs appt facility. Here, we set up this appt, and then hook it up with org-mode, so appt can warn us when there's something happening soon…

;; the appointment notification facility
(setq
  appt-message-warning-time 15 ;; warn 15 min in advance

  appt-display-mode-line t     ;; show in the modeline
  appt-display-format 'window) ;; use our func
(appt-activate 1)              ;; active appt (appointment notification)
(display-time)                 ;; time display is required for this...

 ;; update appt each time agenda opened

(add-hook 'org-finalize-agenda-hook 'org-agenda-to-appt)

;; our little façade-function for djcb-popup
 (defun djcb-appt-display (min-to-app new-time msg)
    (djcb-popup (format "Appointment in %s minute(s)" min-to-app) msg 
      "/usr/share/icons/gnome/32x32/status/appointment-soon.png"

      "/usr/share/sounds/ubuntu/stereo/phone-incoming-call.ogg"))
  (setq appt-disp-window-function (function djcb-appt-display))

Of course, you can freely choose a icon / sound to your liking.

showing pop-ups for new mail

Another event you might want to be warned about is new mail. There is something to be set for not letting yourself be disturbed for new mail, but if you sufficiently filter your mails before they enter your inbox, it can be a good way to periodically bring you back from your deep sl ^H^H thinking. For Wanderlust, I use something like this:

(add-hook 'wl-biff-notify-hook
    (lambda()
      (djcb-popup "Wanderlust" "You have new mail!"
        "/usr/share/icons/gnome/32x32/status/mail-unread.png"
        "/usr/share/sounds/ubuntu/stereo/phone-incoming-call.ogg")))

Exercise for the reader: adapt this for your chosen mail client.

2009-11-12

copying lines without selecting them

When I'm programming, I often need to copy a line. Normally, this requires me to first select ('mark') the line I want to copy. That does not seem like a big deal, but when I'm in the 'flow' I want to avoid any little obstacle that can slow me down.

So, how can I copy the current line without selection? I found a nice trick by MacChan on EmacsWiki to accomplish this. It also adds ta function to kill (cut) the current line (similar to kill-line (C-k), but kills the whole line, not just from point (cursor) to the end.

The code below simply embellishes the normal functions with the functionality 'if nothing is selected, assume we mean the current line'. The key bindings stay the same (M-w, C-w).

To enable this, put the following in your .emacs:

(defadvice kill-ring-save (before slick-copy activate compile) "When called
  interactively with no active region, copy a single line instead."
  (interactive (if mark-active (list (region-beginning) (region-end)) (message
  "Copied line") (list (line-beginning-position) (line-beginning-position
  2)))))

(defadvice kill-region (before slick-cut activate compile)
  "When called interactively with no active region, kill a single line instead."
  (interactive
    (if mark-active (list (region-beginning) (region-end))
      (list (line-beginning-position)
        (line-beginning-position 2)))))

It also shows the power of Emacs-Lisp with the defadvice-macro – see the fine documentation. Using defadvice, you can 'decorate' any function with your own modifications. This great power should be used with caution, of course, as to not break other usage that assumes the undecorated versions. In this case, that seem unlikely. And note that the 'advise' only applies when the functions are called interactively.

2009-11-05

bookmarks

Emacs has a very useful system for bookmarks – shortcuts to often-used files. It's also one of those features I only really started using after years of emacs – there seem to be many of such obvious features…

Bookmarks are especially handy if you have long file names, or for examples the special file names for editing root-owned files discussed here before.

To start using bookmarks effectively, there are only a few important key bindings to memorize: C-x r m ('make') will create a new bookmark, defaulting to the current file. Then, you can jump to an existing bookmark with C-x r b ('bookmark') Finally, you can see the list of your bookmarks with C-x r l ('list').

There are a few customizations you can put in your .emacs:

(setq 
  bookmark-default-file "~/.emacs.d/bookmarks" ;; keep my ~/ clean
  bookmark-save-flag 1)                        ;; autosave each change)

2009-10-21

editing files owned by root with tramp

One of the times when many emacs-users still reach for vi is when editing system configuration files – the stuff in /etc/, etc. I think that is because of the now mostly false impression that emacs startup is slow, and because people don't want to run an open-ended program as emacs as root.

There is truth in that last point – but it applies just as well to, say, vim. Anyway, running emacs as root is not a good idea.

So how what can we do it? Easy! Using the tramp package (included with GNU/Emacs since version 22), you can run emacs as a normal user, but edit root-and-other-owned files. It does its magic using sudo, but you won't normally notice. It does require you to have sudo-rights of course.

How does this work? Well, instead of

C-x C-f /etc/hosts

to open a file as a normal user, you use:

C-x C-f /sudo:root@localhost:/etc/hosts

or even shorter, as noted by Alexander Kojevnikov (because Tramp defaults to root@localhost):

C-x C-f /sudo::/etc/hosts

It asks for a password - and you should use your user password for that (not the root password!). This usually works fine, but due to way Tramp works, it can get confused if root has some very weird command prompt. If so, you of course configure tramp. Also note that sudo usually remembers that you logged in, an does not require you to re-enter you password when opening ('visiting') another file for some time period – but you can change this. See the sudo(8).

This automatically invokes tramp which does all the magic for you. If you don't like the somewhat longer (pseudo)paths for files, you can of course use the emacs bookmarks facilty. After you load the file, you can use it like any other file.

Note, this is only of the many useful things you can do with Tramp. Tramp was actually written for editing files on remote machines (using ssh or other protocol), and I very happily used it to edit files on some European machine whilst in Australia. It caches the file locally, and only sends it over when you save it, so it very fast – it simply makes you forget your file is so far away.

2009-10-18

writing presentations with org-mode and beamer

[Updated:fixed an error in the template] Things have been a bit quiet at Emacs-Fu lately; this is mostly because I have been very busy with work, traveling, traveling for work etc. Emacs-wise, this has been a very intensive time, and I have been able to move more and more task to emacs – both professionally and privately. This is mostly because thing become easier when I can do all my things in one place.

Anyhow, one of the tricks I picked up recently is to write presentations with the combination of org-mode and a LaTeX-package called beamer. The most common tool for doing presentations is Microsoft's Powerpoint program. It gets a lot of criticism, most famously from prof. Tufte in his Powerpoint is Evil essay. Of course, the problem is in misuse of the tool, not so much the tool itself.

Still, I didn't want to use Powerpoint (or Powerpoint-wannabees, like OpenOffice's Impress). For the technically-inclined, the knee-jerk reaction to this kind of problem is to shout 'LaTeX!'. Indeed - the LaTeX text-processing system offers a package called Beamer, which allows you to write presentations with LaTeX. It is quite powerful, and even allows for all kinds of fancy graphical bling (fade-in, fade out etc.); even better, it generates PDF-files, which can be viewed just about anywhere. The various themes and color settings it offers are quite nice, even though they tend to fill only a small corner of the design-universe…

beamer and org-mode

Now, while I am no stranger to LaTeX, especially for writing a quick presentation, it can be a painful to remember the various directives and options. I am not really a daily LaTeX-user, so I tend to forget these things. I am a daily org-mode user though, and org-mode can export to LaTeX (which then, in turn, are translated into PDFs). So why not use org-mode to generate presentations?

It turns out that that is quite easy.

First, we need to define some of the LaTeX-boilerplate, and tell org-mode about it, so we never need to think about it again. Put the following in your .emacs:

;; allow for export=>beamer by placing

;; #+LaTeX_CLASS: beamer in org files
(unless (boundp 'org-export-latex-classes)
  (setq org-export-latex-classes nil))
(add-to-list 'org-export-latex-classes
  ;; beamer class, for presentations
  '("beamer"
     "\\documentclass[11pt]{beamer}\n
      \\mode<{{{beamermode}}}>\n
      \\usetheme{{{{beamertheme}}}}\n
      \\usecolortheme{{{{beamercolortheme}}}}\n
      \\beamertemplateballitem\n
      \\setbeameroption{show notes}
      \\usepackage[utf8]{inputenc}\n
      \\usepackage[T1]{fontenc}\n
      \\usepackage{hyperref}\n
      \\usepackage{color}
      \\usepackage{listings}
      \\lstset{numbers=none,language=[ISO]C++,tabsize=4,
  frame=single,
  basicstyle=\\small,
  showspaces=false,showstringspaces=false,
  showtabs=false,
  keywordstyle=\\color{blue}\\bfseries,
  commentstyle=\\color{red},
  }\n
      \\usepackage{verbatim}\n
      \\institute{{{{beamerinstitute}}}}\n          
       \\subject{{{{beamersubject}}}}\n"

     ("\\section{%s}" . "\\section*{%s}")
     
     ("\\begin{frame}[fragile]\\frametitle{%s}"
       "\\end{frame}"
       "\\begin{frame}[fragile]\\frametitle{%s}"
       "\\end{frame}")))

  ;; letter class, for formal letters

  (add-to-list 'org-export-latex-classes

  '("letter"
     "\\documentclass[11pt]{letter}\n
      \\usepackage[utf8]{inputenc}\n
      \\usepackage[T1]{fontenc}\n
      \\usepackage{color}"
     
     ("\\section{%s}" . "\\section*{%s}")
     ("\\subsection{%s}" . "\\subsection*{%s}")
     ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
     ("\\paragraph{%s}" . "\\paragraph*{%s}")
     ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))

This is based on the template by Thomas S. Dye on the org-mode mailing list. You can of course add other packages to it with \usepackage. In my version, I have add the Listings-package for including syntax-highlighted snippets of source code in my presentations. Importantly, I added the [fragile] options to the frame-settings, otherwise you cannot include such source code fragments without LaTeX complaining in various unhelpful ways.

Note, you can customize the way the Listings package works by changing the template above; or by setting the options in the org-file; this involves some 'raw LaTeX' though. It might make sense to define some macros for that.

Now, we can easily make a presentation in org-mode; simply start the file with something like:

#+LaTeX_CLASS: beamer
#+MACRO: BEAMERMODE presentation
#+MACRO: BEAMERTHEME Antibes
#+MACRO: BEAMERCOLORTHEME lily
#+MACRO: BEAMERSUBJECT RMRF
#+MACRO: BEAMERINSTITUTE Miskatonic University, Astrology Dept.
#+TITLE: Presentation with Org-Mode and Beamer
#+AUTHOR: Someone

Of course, you can change these parameters; for example, you might want to change the BEAMERTHEME into Madrid or Warsaw, or … – see the Beamer User Guide (PDF).

After having set up these things, you can write presentations in the familiar org-mode mark-up.

including source code

As mentioned before, you can use the listings-package to include source code snippets in your presentation. You'd write this something like:

#+BEGIN_LaTeX
\begin{lstlisting}[language=c]
for (int i = 1; i != 10; ++i) 
    std::cout << i << ": hello, world!"
              << std::endl;
\end{lstlisting}

#+END_LaTeX

In other words, we include some 'raw' LaTeX to do this. Now, the org-mode-way of doing this, would be to use something like

#+BEGIN_SRC c
  /* code */
#+END_SRC

as discussed before. This works well when exporting to HTML, but at this moment this will simply translate into a verbatim environment in LaTeX - so we use lstlisting to get some syntax-highlighting.

including pictures

Of course, the full arsenal of org-mode tools is available as well, for example Ditaa, as discussed before. Ditaa is now shipped as part of org-mode, and you can use it to create picture which are then included in your presentation. Very nice.

For including existing images (PNGs, JPGs etc.), it's probably easiest to put use some raw LaTeX for that, e.g., something like

#+LaTeX:\includegraphics{/some/path/emacs.png}

putting it together

Now, let's put it all together. Below is an example presentation. Assuming you have everything installed (ie., LaTeX with the listings package, a fairly recent org-mode, ditaa), you create your presentation.org and then press C-c C-e d and your presentation (presentation.pdf) is generated and automatically shown in your PDF-viewer. Easy!

The intermediate files (such as presentation.tex) are there as well, so you can check them if something went wrong.

I have uploaded the resulting PDF to Slideshare, so you can see what it looks like. However, the Slideshare-converted version is extremely blurry, unlike the crisp PDF I actually created. I'd be happy to upload the file somewhere else if someone can point me to a good place, thanks!

So hopefully this shows that you can quite easily make presentations with org-mode, with some help from Beamer, LaTeX etc. Beamer actually provides a lot more, such as funky slide-transitions and other tricks – but the things here should give you a good starting point.

#+LaTeX_CLASS: beamer
#+MACRO: BEAMERMODE presentation
#+MACRO: BEAMERTHEME Antibes

#+MACRO: BEAMERCOLORTHEME lily
#+MACRO: BEAMERSUBJECT RMRF
#+MACRO: BEAMERINSTITUTE Miskatonic University, Astrology Dept.
#+TITLE: Presentation with Org-Mode and Beamer
#+AUTHOR: Someone

* My presentation

** Overview

   1. Introduction

   2. Main part

   3. Questions
    
    
** Some interesting stuff
    
*** an important point
    
    - subpoint a
      
    - subpoint b

** Graphics

*** a picture

#+begin_ditaa blue.png -r -S
+---------+
| cBLU    |
|    +----+
|    |cPNK|
+----+----+
#+end_ditaa

*** another picture
#+LaTeX:\includegraphics{emacs.png}

** More interesting stuff

*** some C++ code
#+begin_LaTeX
\begin{lstlisting}[language=c]
for (int i = 1; i != 10; ++i) 
    std::cout << i << ": hello, world!"
              << std::endl;
\end{lstlisting}
#+end_LaTeX

*** and some Python...

#+begin_LaTeX
\begin{lstlisting}[language=python]
for i in range(1,10):
        print i, "hello, world!"
\end{lstlisting}
#+end_LaTeX

2009-09-19

wanderlust tips and tricks

Earlier, I spoke of the wonderful Wanderlust e-mail client. After years of using mutt, I am a quite happy Wanderlust-user now. Now, it's few months since my conversion, time to discuss some of the customizations I did. Not all the defaults are so well-chosen (in my opinion), but fortunately, the package is very configurable.

If you are interested in Wanderlust, this entry might save you some time in figuring out such customizations and some other tricks. If you haven't done so before, I'd recommend you to read the older entry first. Also, the entry about BBDB may be useful.

Before going into the customizations, let me first answer a question I got asked a couple of times: why I am using Wanderlust and not, say, VM, gnus, Mew or even mutt or some other client?

To start with the last part, an emacs-based client fits in very well with my workflow, which is (duh) revolves around emacs. Doing my email there as well makes a lot of sense - a little return-on-investment for the time spent taming emacs and its bag of tricks.

The reason I particularly like Wanderlust, is that it works very well with mail stored in maildirs - as you may know, maildir is a one-file-per-message way of storing your mail on disk. That's great for backing up things, and sync'ing different machines.

Unlike VM and gnus, Wanderlust keeps the mail in the maildir as-is, and does not use a separate spoolfile – thus, all changes are reflected in the maildir itself, making it possible to use different clients (ie., use mutt when needed). Even more important, the wonderful tool offlineimap does two-way synchronization with IMAP-servers, and downloads everything into a maildir. So, I can download all the mail on my laptop machine, go offline and work on the messages (delete, move, reply etc.) during a flight, and when I'm back online, I can synchronize things. All this 'cloud'-stuff is nice, but I like to have my mails on my side of the intertubes.

Ok, now let's take a look at some of the customizations and tricks. All of these are little snippets to add to your ~/.wl-file.

Forwarded mails should use 'Fwd:', not 'Forward:'

I wonder why this is not the default.

(setq
  wl-forward-subject-prefix "Fwd: " )    ;; use "Fwd: " not "Forward: "

Reply-to-all should not be the default

By default, Wanderlust uses Reply-to-All; that is usually not what we (well, I) want. The code below makes Reply-to-Sender the default, with Reply-to-All behind C-u; ie. A or a will reply to sender, C-u A and C-u a reply to all.

(Note, the uppercase A is for replying with quoting the original message, while the lowercase version starts the reply with an empty message)

;; from a WL-mailinglist post by David Bremner

;; Invert behaviour of with and without argument replies.
;; just the author
(setq wl-draft-reply-without-argument-list
  '(("Reply-To" ("Reply-To") nil nil)
     ("Mail-Reply-To" ("Mail-Reply-To") nil nil)
     ("From" ("From") nil nil)))


;; bombard the world
(setq wl-draft-reply-with-argument-list
  '(("Followup-To" nil nil ("Followup-To"))
     ("Mail-Followup-To" ("Mail-Followup-To") nil ("Newsgroups"))
     ("Reply-To" ("Reply-To") ("To" "Cc" "From") ("Newsgroups"))
     ("From" ("From") ("To" "Cc") ("Newsgroups"))))

Setting up spam-handling

If you're using spamassassin for spamfiltering, you can quite easily integrate it with Wanderlust:

(require 'wl-spam)
(wl-spam-setup)
(setq elmo-spam-scheme 'sa)   ;; sa for spamassassin, see the elmo-spam-scheme
                              ;; docs for alternatives
(setq wl-spam-folder ".spam") ;; maildir to store spam

After this, you quite easily handle spam in the 'Summary' with some keybindings:

  • k C : check whether spamassassin considers this message 'spam'
  • k m : mark message(s) as spam (move to spam folder)
  • k n : learn this message is 'ham'
  • k s : learn this message is 'spam'

Note, there are some hooks for other spamfiltering solutions as well.

How to easily refile messages

I receive all my messages in only two mailboxes: one for personal mail, and one for mailing lists. If, after reading, I want to keep the message, I'll refile it to some other folder (after all, it's good to empty your mailboxes quite often. Wanderlust makes this refiling quite easy; the first way is to do it semi-automatic, i.e., let Wanderlust 'guess' the folder for you, based on the contents of the message. Then, when pressing 'o' in the summary, it will suggest this folder, and you can refile (move) the message. You can set up this 'guessing' something like this:

(setq
  ;; refile rules determine the default where mails are put

  ;; when you mark them for refiling ('o'); cfg. save-hooks in mutt
   wl-refile-rule-alist  
  '(
     ("Subject" ;; put more specific rules before more general ones.
       ("emacs"   . ".emacs")   ;; emacs-related mail

       ("running" . ".running") ;; running-related mail
       )
     
     (("To" "Cc" "Delivered-To") 
       ("myself@company.com"      . ".workmail") 
       ("myself@home.com"         . ".privatemail"))
     
     (("Precedence" "Priority")
       ("bulk\|1\|2\|list"       . ".bulkmail"))))

Explicit refiling

Semi-automatic refiling works fairly well, but you might also want to have some explicit shortcuts to move messages to specific folders. For example, to move message from your inbox to your Project X-folder, or your Project Y-folder.

(defun djcb-wl-summary-refile (&optional folder)
  "refile the current message to FOLDER; if FOLDER is nil, use the default"
  (interactive)
  (wl-summary-refile (wl-summary-message-number) folder)
  (wl-summary-next)
  (message (concat "refiled to " folder)))

(define-key wl-summary-mode-map (kbd "b x") ;; => Project X

  '(lambda()(interactive)(djcb-wl-summary-refile ".project-x"))) 
(define-key wl-summary-mode-map (kbd "b y") ;; => Project Y
  '(lambda()(interactive)(djcb-wl-summary-refile ".project-y")))

Assuming you have (Maildir) folders project-x and project-y.

Check outgoing mail

It's not uncommon to forget to add a subject or an attachment when you send a mail (or at least, when I send a mail…). However, using wl-mail-send-pre-hook we can let Wanderlust warn us when something like that happens.

;; suggested by Masaru Nomiya on the WL mailing list

(defun djcb-wl-draft-subject-check ()
  "check whether the message has a subject before sending"
  (if (and (< (length (std11-field-body "Subject")) 1)
        (null (y-or-n-p "No subject! Send current draft?")))
      (error "Abort.")))


;; note, this check could cause some false positives; anyway, better
;; safe than sorry...
(defun djcb-wl-draft-attachment-check ()
  "if attachment is mention but none included, warn the the user"
  (save-excursion
    (goto-char 0)
    (unless ;; don't we have an attachment?

      (re-search-forward "^Content-Disposition: attachment" nil t) 
     (when ;; no attachment; did we mention an attachment?
        (re-search-forward "attach" nil t)
        (unless (y-or-n-p "Possibly missing an attachment. Send current draft?")
          (error "Abort."))))))

(add-hook 'wl-mail-send-pre-hook 'djcb-wl-draft-subject-check)
(add-hook 'wl-mail-send-pre-hook 'djcb-wl-draft-attachment-check)

Ok, that's all for now… I'll get back to Wanderlust in the future; of course, feel free to add your own tricks in the comments-section.

2009-08-16

managing e-mail addresses with bbdb

BBDB, the Insidious Big Brother Database is an Emacs addressbook application that works particularly well with e-mail. It's one of the classic emacs packages, written in 1991 by Jamie Zawinski.

Personally, I do not use BBDB as a general address book. Instead, I use it to harvest the e-mail addresses of the people that send me mail, so that the next time I'd like to send e-mail, I can auto-complete the names / e-mail addresses. If you're not using some emacs-based email client, BBDB might not be that useful in practice.

Note that other people are using BBDB for much more, see the Emacswiki-entry for some examples of that. Here, I just give some basics to get you started; please refer to the fine manual for all the details.

configuration

So, after you've installed bbdb (just follow the instructions; alternatively Ubuntu/Debian users can simply install the bbdb package), we can add the following to .emacs to set it up:

(setq bbdb-file "~/.emacs.d/bbdb")           ;; keep ~/ clean; set before loading
(require 'bbdb) 
(bbdb-initialize)
(setq 
    bbdb-offer-save 1                        ;; 1 means save-without-asking

    
    bbdb-use-pop-up t                        ;; allow popups for addresses
    bbdb-electric-p t                        ;; be disposable with SPC
    bbdb-popup-target-lines  1               ;; very small
    
    bbdb-dwim-net-address-allow-redundancy t ;; always use full name
    bbdb-quiet-about-name-mismatches 2       ;; show name-mismatches 2 secs

    bbdb-always-add-address t                ;; add new addresses to existing...
                                             ;; ...contacts automatically
    bbdb-canonicalize-redundant-nets-p t     ;; x@foo.bar.cx => x@bar.cx

    bbdb-completion-type nil                 ;; complete on anything

    bbdb-complete-name-allow-cycling t       ;; cycle through matches
                                             ;; this only works partially

    bbbd-message-caching-enabled t           ;; be fast
    bbdb-use-alternate-names t               ;; use AKA


    bbdb-elided-display t                    ;; single-line addresses

    ;; auto-create addresses from mail
    bbdb/mail-auto-create-p 'bbdb-ignore-some-messages-hook   
    bbdb-ignore-some-messages-alist ;; don't ask about fake addresses
    ;; NOTE: there can be only one entry per header (such as To, From)
    ;; http://flex.ee.uec.ac.jp/texi/bbdb/bbdb_11.html

    '(( "From" . "no.?reply\\|DAEMON\\|daemon\\|facebookmail\\|twitter")))
)

This will set up BBDB for you. I have commented the various settings; you can of course get more information for each of them by putting your cursor on them (in emacs) and issuing C-h v.

integration with e-mail clients

Another important part is the integration with e-mail - which is why I am using BBDB in the first place.

As I mentioned before, I am using the wonderful Wanderlust e-mail client for emacs, and you can easily integrate it with BBDB by putting the following in your .wl-file:

(require 'bbdb-wl)
(bbdb-wl-setup)

;; i don't want to store addresses from my mailing folders
(setq 
  bbdb-wl-folder-regexp    ;; get addresses only from these folders
  "^\.inbox$\\|^.sent")    ;; 


(define-key wl-draft-mode-map (kbd "<C-tab>") 'bbdb-complete-name)

For gnus, you'd use something like:

(add-hook 'gnus-startup-hook 'bbdb-insinuate-gnus)

(I am not using gnus myself, feel free to add you setup in the comments)

The BBDB-manual has the details for some other mail clients as well.

Use

So, we have everything set up now – but how does it work? Well, whenever you read e-mails, BBDB stores the various e-mail addresses. Whenever you write an e-mail, you can complete the names with M-x bbdb-complete-name, by default bound to M-TAB. This keybinding conflicts with many window managers, which already use M-TAB (Alt-TAB) for window switching. So, it's useful to rebind it to something else, for example C-TAB (I already did that for Wanderlust, in the example above).

I don't use BBDB too much directly, but you can manipulate the address; first display one with M-x bbdb-display-address, they you can edit a field by pressing 'e', 'd' for deleting the field or record, C-o for adding a new field and so on – see the whole list.

2009-08-03

interview with Chong Yidong and Stefan Monnier

Earlier this year, Chong Yidong and Stefan Monnier took over Emacs maintainership from Richard Stallman, and they successfully completed the Emacs 23.1 release. I asked them a couple of questions about the process, Emacs-development and some of the plans for the future.

I'd like to thank Chong (CYD) and Stefan (SM) to take the time to answer my (djcb) questions and even more so for doing an excellent job bringing us Emacs 23!


djcb: First of all, could you tell us a bit about yourself? For example, what you do when not hacking on Emacs?

CYD: I'm a postdoc in theoretical physics, specializing in photonic crystals and other optical phenomena.

SM: I'm a professor at the University of Montréal, teaching and researching theory of computer languages. More specifically, I design new type systems and try and abuse existing type systems for "type based formal methods" purposes.

djcb: Earlier this year, the two of you took over the maintainership of Emacs from Richard Stallman. How did you get involved in hacking on Emacs? How has the transition gone?

CYD: My first involvement in Emacs-related development was around 2004 or 2005—very recent by Emacs hacker standards—when I found myself with some free time on my hands after college. At that time, I wrote wikipedia-mode, a major mode for editing Wikipedia articles, plus some word-wrapping code that eventually became longlines-mode, and patches to emacs-devel fixing a few minor bugs. My level of involvement gradually grew, until eventually I was helping Richard to roll the pretest tarballs for the Emacs 22 release.

Because I was quite active in the Emacs 22 release process, I've been pretty comfortable with my role in Emacs 23. It helps, of course, that many parts of Emacs have their own dedicated and experienced maintainers, e.g. the major Lisp packages such as CC-mode, Gnus, and Org-mode.

SM: I started hacking on Emacs a fairly long time ago when I was waiting to start my PhD, but it only got more serious during my PhD when I decided that PCL-CVS was a neat idea but unusable as it stood (for lack of maintainership). It all went downhill from there.

The transition to maintainership happened very smoothly. I had already considered maintaining Emacs when Gerd [ Gerd Moellmann ] left (i.e. when 21.1 was released; at which point Richard ended up regaining maintainership for lack of any other volunteer), but it was a pretty busy time for me, so I decided not to. This time Richard kept a very active role, which coupled with the help of Chong made it very pleasant.

There's a fair bit of pressure, of course, because it's a very old package, so people have a lot invested in it, making some changes terribly delicate. As a maintainer, I did get to steer the direction of Emacs development, tho mostly by my own contributions and by imposing some contentious new defaults. The role of a maintainer as I see it is mostly to make sure the package keeps its integrity.

But I have to say, that while Chong started maybe a bit more of a "rookie maintainer" than I, he quickly took over and he deserves much of the credit for 23.1, while I was too busy with my work to do much good.

djcb: Talking about Emacs development: there are of course many people involved. Can you give a estimate of how many?

CYD: There are about 120 people who have commit access to the code repository; of these, I think around 20 contribute regularly. This does not count the packages that are maintained separately from Emacs.

Additionally, we do of course receive a steady stream of small patches from various users.

Emacs 23 has just been released (on July 29 2009), congratulations, a great accomplishment indeed! From your perspective, what are the most important improvements in Emacs 23 for end-users? And what about the internals? Are there any big changes in the way Emacs operates?

CYD: I'd describe the Emacs 23 release cycle as dominated by internals changes, in contrast with Emacs 22, where most of the major improvements occurred at the Lisp level. There are two fundamental changes. First, the internal character representation is now Unicode-based, which simplifies various aspects of multilingual editing. Second, the font engine has been revamped, and, among other things, we now support anti-aliasing on X. Both these changes are due largely to Kenichi Handa, who deserves a huge amount of credit for patiently developing the code a period of years.

One other major internals change is a restructuring of the terminal interaction code, by Károly Lőrentey, which allows a single Emacs process to display on X and text terminals simultaneously. Building on this "multi-tty" code, Dan Nicolescu implemented a small but clever hack, allowing Emacs to run as a daemon serving emacsclient connections.

There are several Lisp-level changes, large and small. For instance, Stefan revamped the minibuffer completion code, which is now more sophisticated about generating completions. And there are, as usual, new modes and packages: Doc-view mode, Ruby mode, nXml mode, etc.

SM: Better support for Unicode, and better support for fonts, multi-tty support, plus lots of new modes as always. Of course, I'm very happy with my new completion code, which makes partial-completion-mode obsolete (and enabled by default).

The new support for Unicode and for fonts required significant changes. Big thanks to Kenichi Handa for most of that.

djcb: Are there any features that you would have liked to add, but that were somehow not yet ready?

CYD: One feature that I'd have liked to include into 23.1 is CEDET, a set of packages by Eric Ludlum (the author of Speedbar), which turns Emacs into an IDE. There was no time to merge it for 23.1, but hopefully it will be included in 23.2.

SM: Several packages were planned for inclusion, but didn't make it. Support for GNUstep was planned (and is actually in there) but doesn't work. Also I hoped the new VC code would be developed further, but it sadly stayed at the stage where it mostly provides the same features as the old one (with all kinds of improvements in the way it supports them, tho).

djcb: There is always a bit of tension in Emacs between keeping things as they are, and changing things to be more like other programs - for example when thinking about key bindings and various defaults. What is your take on this? Should Emacs try to accommodate new users, or instead try to keep things as they are?

CYD: My impression is that I'm a little more conservative than Stefan with regards to changes, though I'm not sure what he thinks ;-) That said, we seem to arrive at the same conclusions with surprising frequency.

SM: Emacs standard key bindings (like C-x and C-c prefixes) clash badly with "standard" key bindings of other apps, so I don't think there's much hope to make Emacs like other applications. But yes, I generally believe that, all things being equal, it's better to be like others than to be different in this respect. But since changing bindings (or behaviors) is disruptive, I only consider it worthwhile if I believe the new default is really superior (not just for new users).

djcb: For example, in Emacs 23, transient-mark-mode is the default, but delete-selection-mode is not. How do you decide such things?

CYD: Typically, we try not to make flashy changes. The transient mark mode change is the exception that proves the rule: transient-mark-mode is so useful, and is so widely used (even Richard uses it), that it doesn't make sense to leave it off by default. But the rule of thumb is to improve Emacs on Emacs' own terms; for instance, CUA mode will not become the default anytime soon, I think.

SM: transient-mark-mode is an enabler: it allows some commands to behave differently depending on the activation state of the region. So it's a clear improvement. delete-selection-mode is not as important in this regard. We may see something along the lines of delete-selection-mode at some point, tho probably something more minor that only caters to the few cases where delete-selection-mode is more than just a way to avoid hitting C-w.

djcb: How do you see the competition with other text editors? Do you look for ideas elsewhere? Is there any other editor you would be using if Emacs did not exist?

CYD: I'm afraid I don't pay much attention to other editors.

SM: I used Zmacs, XEmacs, and Epoch at some point. That's about it. I do like structured editors, and I think Emacs should and will move in this direction (with more parsing going on).

djcb: It's a bit premature of course, but it's always interesting to speculate a bit about the future. Do you have any particular post-Emacs-23 plans? Obviously, this all depends on what people come up with, but are there any directions you would like Emacs to go?

CYD: The present plan is for Emacs 23.2 to contain a small number of new features, in addition to bugfixes. As mentioned above, I'd like to try to include CEDET. In general, I hope to move to shorter, more disciplined release cycles. Emacs 23 was a good step in that direction, as it was shorter than the previous cycle.

SM: My main goal for Emacs-23 was to shorten the release cycle. Hopefully, the quality has not been reduced accordingly. For 23.N there are several improvements planned (or even done), mostly about inclusion of packages like js2-mode and CEDET. In the longer term, the main goals for me are the integration of the lexical-scoped branch, the support for bidirectional display, and adding more parsing technology (basically replace syntax-tables with something like lex & yacc, maybe).

Thanks a lot Stefan and Chong!

2009-07-28

emacs 23 has been released!

Emacs 23(.1) has been released! After only just over two years since the previous version, emacs 23 has been released on July 29 2009. Time to celebrate! And a big thanks all the talented hackers that made it possible!

Emacs 23 brings many new features. Some of those have been discussed before in emacs-fu, and of course you can get the full list from the NEWS-file (C-h n). This is just a list of some of the highlights, in particular the end-user-visible ones. Let's go through them - of course I cannot mention all of them, so feel free to add your favorite one in the comments.

fonts & anti-aliasing

Finally, emacs 23 brings support for anti-aliased fonts to X (Mac/Windows users have had this for a while). If you're running emacs on X, this one change is enough to never want to go back to an older version of Emacs again.

The new font system mentioned in some posts before: setting fonts and emacs 23.

support for D-Bus and Zeroconf

You can now call other services on the desktop using the DBUS IPC system (popular on X); using d-bus: an example shows how to communicate with the Tomboy note taker program from emacs. As more and more desktop services are accessible using D-BUS, this offers great opportunities for better integrating emacs with the rest of the desktop. For example, one could imagine that emacs could communicate with a network manager when it needs a connection. Or it could show a desktop notification when an appointment is near. And all without adding hard dependencies or calling external binaries.

Using Zeroconf is also quite easy; for example, to get a list of all zeroconf-support printers, you could do something silly like:

(require 'zeroconf)
(zeroconf-init)
(dolist (srv (zeroconf-list-services "_printer._tcp"))
  (insert (format "\nprinter: %s" (nth 2 srv))))

to insert a list of the available printers in your current buffer. As with D-Bus, it's really an enabler for a lot of cool things.

support for 'headless' emacs-daemon

If you have a lot of support packages, emacs startup can be a bit slow. However, emacs 23 brings emacs --daemon, which enables you to start emacs in the background (for example when you log in). You can instantly pop up new emacs windows (frames) with emacsclient. Of course, you could already have an emacs 'server' in older versions, but being able to start it in the background makes this a much nicer solution.

The Emacs-daemon was discussed in emacs-fu in emacs –daemon and windows and daemons (for MS-Windows). Popping up new emacs windows is so fast that you can use emacs for any editing job, for example as an editor for an email program, or for quick rememember notes.

You can even combine X and console clients in one session now.

Emacs now support Xembed

This enables you to embed emacs in other programs (on X) using XEmbed. Haven't played with that yet. There is some more information in EmacsWiki/Xembed.

Support for frame opacity

You can now make you emacs frames transparent. Discussed before in transparent emacs. Not sure how useful this is, but it surely looks nice :)

Internationalization

Emacs's character set is a superset of Unicode, with about four times the space available. That should be enough for the foreseeable future… There are also many new character sets available, as well as new language environments, such as Chinese-GB18030, Khmer, Bengali, Punjabi, Gujarati, Oriya, Telugu, Sinhala, and TaiViet.

Note that, internally, emacs uses UTF-8 now.

New defaults

  • Screen motion now goes by screen lines by default, that is when you move the cursor up or down, it follows the line as you see them. Nice.
  • Transient mark mode (visible selection) is now on by default, so no need for (transient-mark-mode t) in your .emacs anymore. However, you still need to put (delete-selection-mode t) if you like replace the current selection with your typing.
  • Also shift-select mode is enabled by default, so you can select with shift + the arrow keys.

New modes and packages, and updated ones

2009-07-13

stepping through your window configurations

As a small addendum to the entry about keeping related buffers together (with Elscreen): there is also winner-mode, as was mentioned by ustunozgur. winner-mode is a bit simpler than Elscreen. It allows you to step through the various window configuration you had before - instead of having separate 'tabs' as in elscreen, you just step through your history.

I have the following in my .emacs:

(require 'winner)
(setq winner-dont-bind-my-keys t) ;; default bindings conflict with org-mode

(global-set-key (kbd "<C-s-left>") 'winner-undo)
(global-set-key (kbd "<C-s-right>") 'winner-redo)
(winner-mode t) ;; turn on the global minor mode

As you see, I am using the windows-key in the key bindings once more, but of course you can use something else as well.

2009-07-09

keeping related buffers together with elscreen

I wrote about switching buffers a couple of times. Switching buffers is one of the things I do a lot when using emacs – switching between e-mail, IRC and some blog entry. Or simply between a couple of source files in a project.

With some of the tricks mentioned, switching buffers can be made pretty easy.

However, what about programs that consist of multiple buffers? For example, my e-mail program (Wanderlust) splits the screen in separate buffers with mail headers, mail folders and the contents of one message. The same for fancy debugging with gdb, or simply when I have split my screen in two (C-x 2) to view two files at the same time. When I then try to switch buffers, only one of the related buffers will be switched – which is usually not what I want.

I'd like to treat a set of related buffers plus their screen layout as a logical unit.

There are different ways to do that in emacs. One obvious way is to use multiple frames (windows). But I prefer to keep everything in one - and I found the that the easiest solution is elscreen.

Installation is simple; download from here, and follow the instructions in the package. I've set up some easy key-bindings in my .emacs:

(load "elscreen" "ElScreen" )

;; F9 creates a new elscreen, shift-F9 kills it
(global-set-key (kbd "<f9>"    ) 'elscreen-create)
(global-set-key (kbd "S-<f9>"  ) 'elscreen-kill)  


;; Windowskey+PgUP/PgDown switches between elscreens
(global-set-key (kbd "<s-prior>") 'elscreen-previous) 
(global-set-key (kbd "<s-next>")  'elscreen-next) 

Now, whenever I want to switch to a new task, say, read e-mail, I press F9, and a new elscreen will appear (it's visible as a sort-of 'tab' at the top of your screen), and start e-mail there. I can then switch to other elscreens, and all of them maintain their buffers and window layout. I have found this very useful. There are some more tricks, and some add-on packages, but this should give you a good start.

There is one small item on my todo-list. Example: when I push M-x wl, emacs automatically switches to the Wanderlust-buffer – or starts it. Now, when using elscreen, I'd like to automatically switch to the correct screen instead of switching the current buffer.

2009-07-03

keyboard shortcuts

Emacs has many useful functions, but for the most efficient use you really need to make keybindings (as previously discussed) for the ones you use often. There's no shortage of possible shortcuts, but the number of easy-to-remember bindings is fairly limited.

I can remember bindings that I use all the time like M-w for copy (well, kill-ring-save), or M-q (fill-paragraph) to word-wrap a paragraph, even if they're not that mnemonic (interestingly, it can be hard to verbalize these bindings when someone ask me – but my fingers have no such trouble).

For functions that I use every minute, any weird key binding will do (as long as it is short). And functions that I use seldomly I can look up in a menu. The trouble is with those functions in between :)

Bindings that involve the arrow-keys are attractive, but I've found that e.g. org-mode binds many of those already. And in the olden days, the C-c-prefix was reserved for user-defined key bindings – but again, org-mode and other packages have overtaken that those. So what's left? Recently, I have been using the Windows-key a bit more. Under (my) X, it's bound to super, and emacs does not use it for anything else, so I have been using it to create a set of useful key bindings. I am trying to be a bit systematic, where super + lowercase letter starts some 'application':

(global-set-key (kbd "s-a") 'org-agenda-list) ;; Agenda
(global-set-key (kbd "s-n") 'org-todo-list)   ;; todo-list (nextactions)

;; program shortcuts
(global-set-key (kbd "s-b") 'browse-url)  ;; Browse (W3M)
(global-set-key (kbd "s-f") 'browse-url-firefox)  ;; Firefox...
(global-set-key (kbd "s-t") 'twitter-get-friends-timeline) ;; Twitter
(global-set-key (kbd "s-w") 'wl)            ;; Wanderlust

and super + uppercase opens a file / special buffer, for example:

(global-set-key (kbd "s-S") ;; scratch
  (lambda()(interactive)(switch-to-buffer "*scratch*")))
(global-set-key (kbd "s-E") ;; .emacs
  (lambda()(interactive)(find-file "~/.emacs")))
(global-set-key (kbd "s-G") ;; gtd.org
  (lambda()(interactive)(find-file "~/.emacs.d/org/agenda/gtd.org"))) 

super + arrow keys I now use for windmove (see here).

The windows-keys are not perfect of course – they don't work from the console (easily) or, ironically, on Windows. But they are mnemonic – I can remember s-t for twitter; contrast this with C-x 5 2 to create a new frame.

So, I wonder, does anyone have some clever scheme for their key bindings? I think I am getting much of the efficiency from emacs from the fact that I can do just about anything with some quick actions on the keyboard, so I'm very interested.

2009-06-25

jumping back to past locations

With the pop-global-mark-command, you can quickly jump back to the locations you were before, like tracking back your bread crumbs.

A typical example of this is when doing some programming and looking up some function in another file, which refers to a function in yet another file, and so on – for example, see navigating through source code using tags.

Press C-x C-SPC (the default key binding for pop-global-mark) to make your journey back to where you came from; it works in a cyclical fashion as well, so you can go on and on.

Quite useful – you may want to use a more convenient key binding though. Admittedly, it's hard to come up with any intuitive keybinding which is not already taken by something else…

2009-06-23

setting fonts

NOTE: if you like Org-Mode, please go and vote for it in SourceForge Community Choice Award – it's in the Most Likely to Change Change the Way You Do Everything-category.


For pleasant working with emacs, one of the more important things is choosing the right font ('face'). Especially within a windowing system, and especially with Emacs 23, there are a lot of possibilities. I am thinking from the Linux/X-Window perspective here – the support for anti-aliased fonts makes things look so much nicer – as discussed before.

On X, there are different ways to set your font. One way is through the menu (Options/Set default font.../). We can also set it in our .emacs, with (set-default-font "<font>"), or using ~/.Xdefaults. The latter method makes emacs-startup quite a bit faster, but this may have become less important in the age of emacs –daemon (Emacs 23).

barb wire

In either case – .emacs or .Xdefaults – you must provide some string describing the font. Up to Emacs-23, under X you had to use the 'barb wire'-style X font description, which you could get from a tool like UI-designers' dream xfontsel; the font description would then look something like:

  -*-bitstream vera sans mono-medium-r-*-*-*-120-*-*-*-*-iso8859-*

emacs 23

In the brave new world of Emacs 23, on X, you can also use the somewhat clearer Fontname-<size> format. You can get a list of the fonts installed on your system with the fc-list commond; if you only want to get the monospaced fonts, use

 $ fc-list :spacing=mono

For details, see the FontConfig user manual.

Note that installing fonts under X is rather easy as well these days; in most cases all you need to do is put the .ttf-files in your ~/.fonts directory and all will be find, although some program might require a restart.

Once you have chosen a font, you can put it in your ~/.Xdefaults:

Emacs.font: Envy Code R-10

and don't forget to run xrdb ~/.Xdefaults afterwards, to tell X about the changes. All of this should happen before you start emacs.

Alternatively, you can put in your .emacs something like:

(if (eq system-type 'windows-nt)
  (set-default-font "-outline-Consolas-normal-r-normal-normal-14-97-96-96-c-*-iso8859-1"))

(if (eq window-system 'x)
  (set-default-font "Inconsolata-11"))

This will set a different default font, based on whether you are running on Windows or X. You can freely adapt it to your own desires of course.

Side note: if there is yellow in the code snippet above, that is because of hightlighting lines that are too long.

face value

There are many fonts; which one is the 'best' for you, obviously depends on personal taste and also what you want to with it. As I use emacs for coding but also for reading e-mails and writing documents, there are some things that are important for me:

  • monospace
  • most important: clear and crisp, even when using smaller font sizes;
  • clearly separate O (capital O) and 0 (zero);
  • support italic display;
  • support the characters I might use (incl. accented characters and some greek ones).

Following these rules, I found the Envy Code R font to work very nicely. It's not fully Free though: Free to use but distribution prohibited. Raph Levien's Inconsolata is nice as well and truly Free; it does not provide an italic font though (at least it does not show in Emacs). Here's a list of programming fonts.

more

Hmmm… I wanted to write some small entry… And there is so much more to say about fonts. As often, EmacsWiki has a lot of information; for example about FontSets, which allow you to use a sort-of combination-font, which is nice if you have to work with mixed character sets (Latin, Arabic, CJK etc.).

Also, the emacs-fu entry on zooming in/out is useful in this context, even though Emacs 23 has gained something similar by default.

Also, the entry on color theming may be interesting, in this entry we only look at the default font, but you can change fonts governing only part of emacs as well; see M-x list-faces-display.

Or get information about the font at point with C-u C-x =.