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 =.

2009-06-19

search and replace in files

I always try to do more things from within emacs; as said before, it's all about return on investment for all the time spent mastering emacs. One of the things I sometimes still used a separate terminal for, was search and replace of text in files – a quick grep or sed from the command line.

source code: tags

If you're searching (not replacing) symbols in source code, the most convenient way is to use a tagfile. A while ago, I discussed navigating through source code using tags (using GNU Global). I still often see people writing code in emacs, and then opening a terminal open to grep for function names, definition and so on… There is really no need for that; see the linked entry.

The GNU Global tagfile system unfortunately does not have a search-and-replace function; if you are using etags, however, you can use M-x tags-query-replace to replace the symbols in all files in the tagfile.

GNU Global users can of course still use the other search-and-replace mechanisms below.

other files: rgrep

For searching recursively for some strings (regexps) in a file tree, there is the very useful M-x rgrep. It will ask what you want to search for, some pattern for the files to match, and the top of the directory to search.

So, for example, if you want to find all occurences of the string 'FIXME' in txt-files in your ~/Documents directory tree, you would do something like:

M-x rgrep
FIXME
*.txt
~/Documents

and you will get at buffer with all matches. For this, it uses the same kind of buffer you use for compilation, and you can jump to the matching locations just like you can jump to error locations in the compilation output - that is why with M-x next-error you can jump from match to match, even though of course they are not really errors. The default shortcut for next-error is C-x`, but you can of course remap that to something saner, exempli gratia:

(global-set-key (kbd "<M-prior>") 'previous-error) 
(global-set-key (kbd "<M-next>")  'next-error)

so you can use M- with PgUp/PgDown to jump through the matches (or errors).

One final comment about rgrep: in the above example, the FIXME is a regular expression (as discussed in building regular expressions, while the *.txt is a shell pattern (see e.g. the findutils manual).

replacing: dired

If you want to replace text in multiple files, your best bet is to use dired, the emacs file manager.

dired deserves its own entry (and probably more than one), but if we just look at search-replace, the steps are not too hard. Suppose we want to replace all strings FOO with BAR in a bunch of files in ~/myfiles/. We open dired with C-x d, enter ~/myfiles, and a list of the files in that directory appears.

Now, we mark the files we'd like to change by moving the cursor to them and press m (unmark with u). You can also mark filenames matching some regexp with M-x dired-mark-files-regexp (or '* %', obviously) or files containing some regexp with M-x dired-mark-files-containing-regexp (or '* g').

After marking some files, you can use M-x dired-do-query-replace-regexp to interactively replace some regular expression in all of them – you have to press 'y' to confirm the changes. You can quit this process by pressing 'q'.

dired can even work recursively, as an anonymous commenter remarked (thanks!); slightly edited: You can type 'i' to insert a subdirectory to the dired buffer. You can also run M-x find-name-dired or M-x find-dired to generate a dired buffer with the results of the find-command. Then you can mark wanted files and perform query replace with 'Q'. Also see: Emacs Nerdery: Search & replace across files.

As an alternative, you can use an external package like FindR for recursive search & replace. I haven't used that one myself though, as I haven't had the need yet.

2009-06-16

emacs-fu feed updated

At one point in your life, you reach the conclusion that you are not perfect. Even I did :-). For example, some of the Emacs-Fu posts contain errors (typos, out-of-context copy-past etc.), which are usually quickly found by some attentive reader. I do attempt to test things of course, but sometimes things slip through.

Of course, I can quickly fix those mistakes; I do know how to use a text editor after all. The trouble with that is that it provokes aggregators like Planet Emacsen and others to consider this as a new post, which is shown as such on the main page. Of course I don't want to spam the poor Planet Emacsen readers with updates just because I fixed a typo in some old entry.

The problem is not with those aggregators – it's with blogger.com, which does not invoke the right formula to say that something is updated instead of published. And my power to change blogger.com is rather limited.

Blogger does support tag-specific feeds though; so, my workaround is the following:

When I update an entry, I will remove the new-tag, so aggregators that use the first feed will not be bombarded with these small changes - and I am planning a few..

So, if you only want to get the new posts, I'd recommend changing to the first feed. Planet Emacsen has already done so (thanks Edward!).

Now keeping my fingers crossed it'll all work as expected...

2009-06-13

ERC: the emacs IRC client

ERC: IRC with emacs

I am migrating more and more of my computer tasks to emacs; I already discussed Twitter and e-mail (with Wanderlust); I'm also using emacs-w3m for some of my web browsing (more about that some other time).

Reason for this ongoing emacsication is to get some return on investment for those countless hours spent on mastering the One True Editor. And it pays off - I can manipulate and glue all these programs together so they fit my workflow like a glove.

Recently, I have also started use emacs for IRC, another great Finnish invention. I am not really an IRC power-user, but I do use; before, I used irssi for that, in a ansi-term console (as explained). But now I have switched to ERC, the emacs IRC-client. You can do just about anything with ERC – I have only barely scratched the surface. But let me share my basic setup.

There first step is getting ERC; it's bundled with any recent emacs, so all you need to do is to add it to your .emacs:

(require 'erc)

That was not too hard, was it?

Now, the thing this is to determine what irc-servers / channels you'd like to join (of course you can do that later manually, but automate what we can). So, we define erc-autojoin-channels-alist:

;; joining && autojoing

;; make sure to use wildcards for e.g. freenode as the actual server
;; name can be be a bit different, which would screw up autoconnect
(erc-autojoin-mode t)
(setq erc-autojoin-channels-alist
  '((".*\\.freenode.net" "#emacs" "#gnu" "#gcc" "#modest" "#maemo")
     (".*\\.gimp.org" "#unix" "#gtk+")))

So, the first element is the IRC-server (with appropriate wildcards), the other elements are channels. This will make ERC automatically join the channels when it notices you're connected to the IRC-server.

And read the comment – it took me some time to figure out why things weren't working. Also, you might want to change these IRC-channels to your own favorites.

The next thing is tracking: ERC can track the various channels you're in, and notify you when there is something new in the channel (it will colorize the channelname in your modeline). This is generally useful, but I don't really care about when people join or leave, or other IRC-meta spam. So:

;; check channels
(erc-track-mode t)
(setq erc-track-exclude-types '("JOIN" "NICK" "PART" "QUIT" "MODE"

                                 "324" "329" "332" "333" "353" "477"))
;; don't show any of this
(setq erc-hide-list '("JOIN" "PART" "QUIT" "NICK"))

Note, the last line has nothing to do with this tracking, that's just telling ERC that I am not interested in those things at all, so it should hide them.

Almost there. Now, I have some function to actually start ERC; it's based on some code in found somewhere, but cannot find right now. The function will start ERC, or switch to it if it's already running – it will actually switch to the most recently active ERC-buffer, which makes things a bit easier to manage.

(defun djcb-erc-start-or-switch ()
  "Connect to ERC, or switch to last active buffer"
  (interactive)
  (if (get-buffer "irc.freenode.net:6667") ;; ERC already active?

    (erc-track-switch-buffer 1) ;; yes: switch to last active
    (when (y-or-n-p "Start ERC? ") ;; no: maybe start ERC
      (erc :server "irc.freenode.net" :port 6667 :nick "foo" :full-name "bar")
      (erc :server "irc.gimp.org" :port 6667 :nick "foo" :full-name "bar"))))

Note: don't forget to replace the :server-value as well as foo and bar with your own preferences. It should be clear what they mean…

You can bind this function to some easy key, and things get really convenient; for example:

;; switch to ERC with Ctrl+c e
(global-set-key (kbd "C-c e") 'djcb-erc-start-or-switch) ;; ERC

Once you have started ERC and are connected to some channels, things work pretty much as in any IRC-client. For each channel, a buffer is opened, and from their on it is up to you to have enlightening conversations with other people around the globe…

Once more, I have only barely scratched the surface of ERC. These are just some basic instructions for getting things working. After that, there are a million things you can do; EmacsWiki has an extensive section on it, with more information and customization then you'll probably ever need.

2009-06-08

e-mail with wanderlust

e-mail with wanderlust

(This is part I of entry on the wanderlust e-mail client; part II will appear soon).

Earlier, I have written about how I am using mutt as my e-mailclient. I discussed running mutt inside emacs as well. Of course, mutt is an external program, which puts some limits on its integration with emacs. I did try various emacs-based clients, such as VM (ViewMail) and GNUS, but they always left me a bit unsatisfied.

To start with, it was rather hard to set these programs up – and I am an Emacs-user, I like tweaking things… Still, it was hard to get even simple things working. Maybe I have uncommon wishes, but my desired setup already sent me to the edges of the googleable universe.

But now I have found an emacs-based client that seems to work really well for me. It's called Wanderlust, and it's a fine piece of Japanese engineering. It can be a little bit intimidating to set up at first, even though I found it still much easier than the other emacs-based clients I tried. Anyway, I am sharing a very basic setup here, enough to get you going.

What about my setup? Well: I use maildirs – that is, I download my mail into a local ~/Maildir directory-tree, either with e.g. fetchmail or with offlineimap. It's a particulary nice setup for offline-usage: whenever there's a network connection, I suck up all the mails and have them available offline. I work like this since the days when there was only expensive dial-up access to the net, and later I found it very convenient when I was traveling with a laptop and had only occasional net-access.

So, Maildir access is pretty important for me, and I'll describe my setup for using Wanderlust with it here. If you're using IMAP instead of Maildirs, you might be interested in the Emacs with Wanderlust and GMail-article.

Getting started

So, how to get started with Wanderlust?

In short: install Wanderlust and put some stuff in two files (~/.wl and ~/.folders).

A little bit longer:

  • get wanderlist - I am using the wl-beta packages from Ubuntu/Debian, which makes this a painless process, but you can also use source packages;
  • put your Wanderlust-configuration in a special file: ~/.wl;
  • put a list of the mail folders you're interested in, in a file called

~/.folders. (yes, you can customize all this)

For the rest of the discussion, let's assume we have a Maildir which contains some folders:

  • inbox for incoming e-mail
  • bulk for incoming Mailing List mail
  • drafts for drafts
  • sent for sent e-mail
  • trash for junk email

All incoming mail is going to either inbox or bulk. I'm not going to discuss how to get the mails there – I assume you're already have these thing set up; otherwise, you can take a look at tools like fetchmail, retchmail, procmail and friends. Note that much of the discussion here applies as well if you're using Wanderlust with POP or IMAP.

What to put in ~/.wl?

So, how to setup Wanderlust to use this? Well, our configuration goes into a file called ~/.wl. There's a million more things you can set up here , but let's stick to the basics here. I'll discuss more tricks and extensions later.

;; mode:-*-emacs-lisp-*-
;; wanderlust 
(setq 
  elmo-maildir-folder-path "~/Maildir"          ;; where i store my mail

  wl-stay-folder-window t                       ;; show the folder pane (left)
  wl-folder-window-width 25                     ;; toggle on/off with 'i'
  
  wl-smtp-posting-server "localhost"            ;; put the smtp server here
  wl-local-domain "myhost.example.com"          ;; put something here...
  wl-message-id-domain "myhost.example.com"     ;; ...

  wl-from "Me <me@example.com>"                  ;; my From:

  ;; note: all below are dirs (Maildirs) under elmo-maildir-folder-path 
  ;; the '.'-prefix is for marking them as maildirs
  wl-fcc ".sent"                       ;; sent msgs go to the "sent"-folder
  wl-fcc-force-as-read t               ;; mark sent messages as read 
  wl-default-folder ".inbox"           ;; my main inbox 
  wl-draft-folder ".drafts"            ;; store drafts in 'postponed'
  wl-trash-folder ".trash"             ;; put trash in 'trash'
  wl-spam-folder ".trash"              ;; ...spam as well
  wl-queue-folder ".queue"             ;; we don't use this

  ;; check this folder periodically, and update modeline
  wl-biff-check-folder-list '(".todo") ;; check every 180 seconds
                                       ;; (default: wl-biff-check-interval)

  ;; hide many fields from message buffers
  wl-message-ignored-field-list '("^.*:")
  wl-message-visible-field-list
  '("^\\(To\\|Cc\\):"
    "^Subject:"
    "^\\(From\\|Reply-To\\):"
    "^Organization:"
    "^Message-Id:"
    "^\\(Posted\\|Date\\):"
    )
  wl-message-sort-field-list
  '("^From"
    "^Organization:"
    "^X-Attribution:"
     "^Subject"
     "^Date"
     "^To"
     "^Cc"))

What to put in ~/.folders?

So, that was the basic setup. Now we need to tell wanderlust about the folders we'd like to see in the user-interface: ~/.folders. Wanderlust does not automatically use all the folder in your ~/Maildir. The folder names in ~/.folders can refer to maildirs, newsgroups, POP-account, IMAP-folders and a couple of other ones. In particular, you can combine folders to show as aggregate folders in Wanderlust.

Wanderlust distinguishes the kind of folder something is by looking at a special prefix character. For maildirs, this is a dot ('.'), IMAP-folders get a '+', newsgroups get a '-' and so on. After the mailbox name, you can have a 'friendly name' in quotes, which is what will show up in the user-interface.

Something like this:

# WL folders (put the '#' always at the beginning of the line)
.inbox     "Inbox"
.bulk      "Mailinglists"
.drafts    "Drafts"
.sent      "Sent Mail"
.trash     "Junk"

Note, the little format has some more tricks; e.g., if you had some folders, you cold have something like:

# WL folders (put the '#' always at the beginning of the line)

# 'Lists' will be a tree node with three elements
Lists {
        .mailinglist1 "A mailinglist"
        .mailinglist2 "Another Mailinglist" 
        .mailinglist3 "Yet one more mailinglist"
}

# you make virtual folders from combined lists with the '*'
AllMyMailingLists  *.mailinglist1,.mailinglist2,.mailinglist3

Anyway, there's plenty of room for experimentation here…

Now, we're ready! Start Wanderlust with M-x wl. You will be greeted, and things are relatively easy from there on. You might want to use the menu at first at least, as the default keybindings are somewhat surprising (like a for reply, and w to compose a new message).

What's next?

As I said, there are 10E6 things to configure and to customize, but this should give you a reasonable setup to start with; you might want to change some details (like the folder names) to match your situation. Good luck!

As a true emacs-mailer, Wanderlust nicely integrates with e.g. BBDB (the addressbook) and org-mode. You can also use GPG (through mailcrypt), spamfiltering, and so on. I will discuss some of those things in the second part of the discussion of Wanderlust.

2009-06-02

tetris

The game of Tetris has turned 25 24. I don't care very much about computer games, but I've nevertheless spent quite a bit of time playing Tetris - its brilliance is in its simplicity.

Obviously, emacs comes with a Tetris-clone; just type M-x tetris RET and there you go! There is very little useful things to customize about the game, except maybe you'd like to keep your ~/ clean and set:

(setq tetris-score-file 
  "~/.emacs.d/tetris-scores")

I'd also like to point to the lisp source code, which is only 658 lines long - almost CodeGolf-worthy. It's an interesting read if you are that kind of person. To see the source code, access the help for M-x tetris, i.e., press C-h f tetris and Enter (as discussed in Helpful Emacs). You'll get a help buffer then, which includes a link to tetris.el, the source code; obviously, you need to have the sources installed. In Ubuntu/Debian, you can install the source code through the emacs22-el or emacs-snapshot-el packages, depending on the version of emacs you're using.

Thanks to emacs, you can actually read the source code in one buffer, while playing tetris in the other one!