]> git.cworth.org Git - notmuch-wiki/blob - emacstips.mdwn
Make clear what's the source of notmuch.el exiting.
[notmuch-wiki] / emacstips.mdwn
1 # Tips and Tricks for using notmuch with Emacs
2
3 One of the more popular notmuch message reading clients is
4 **notmuch.el**, an [emacs](http://www.gnu.org/software/emacs/) major
5 mode for interacting with notmuch.  It is included in the notmuch
6 package (notmuch-emacs in Debian).  This page goes over some usage
7 tips for using notmuch with Emacs.
8
9 [[!toc levels=2]]
10
11 ## Setup
12
13 Have a look at the [Howto](http://notmuchmail.org/howto/) for
14 prerequisites.  Be sure you have done the general setup using the
15 notmuch cli command!
16
17 To use the Notmuch emacs mode, first add the following line to your
18 `.emacs` rc file:
19
20         (require 'notmuch)
21
22 or you can load the package via autoload:
23
24         (autoload 'notmuch "notmuch" "notmuch mail" t)
25
26 Then, either run "emacs -f notmuch", or execute the command `M-x
27 notmuch` from within a running emacs.
28
29 ## Navigating & reading mails
30
31 When first starting notmuch in emacs, you will be presented with the
32 notmuch "hello" page.  If it exits with an error after writing
33 "Welcome to notmutch. You have" you need to do the basic notmuch setup
34 first (see above).  
35 From here you can do searches, see lists of recent
36 searches, saved searches, message tags, help information, etc.
37
38 Executing a search will open a new buffer in `notmuch-search-mode`
39 displaying the search results.  Each line in the search results
40 represents a message thread.  Hitting the '?' key will show help for
41 this mode.
42
43 In general, the 'q' will kill the current notmuch buffer and return
44 you to the previous buffer (sort of like a 'pop').
45
46 In search mode, navigating to a thread and hitting return will then
47 open a new buffer in `notmuch-show-mode`, which will show the actual
48 message contents of the thread.
49
50 ## Sending mail
51
52 In any notmuch mode, you can start a new message by hitting the 'm'
53 key.  To reply to a message or thread, just hit the 'r' key.
54
55 When composing new messages, you will be entered in emacs's
56 `message-mode`, which is a powerful mode for composing and sending
57 messages.  When in message mode, you can type `C-c ?` for help.
58
59 If you would like to use address autocompletion when composing
60 messages, see [address completion](#address_completion).
61
62 When you are ready to send a message, type `C-c C-c`. By default
63 message mode will use your sendmail command to send mail, so make sure
64 that works. One annoying standard configuration of message mode is
65 that it will hide the sent mail in your emacs frame stack, but it will
66 not close it. If you type several mails in an emacs session they will
67 accumulate and make switching between buffers more annoying. You can
68 avoid that behavior by adding `(setq message-kill-buffer-on-exit t)`
69 in your `.emacs` file (or doing `M-x
70 customize-variable<RET>message-kill-buffer-on-exit<RET>`) which will
71 really close the mail window after sending it.
72
73 ## Attaching files
74
75 Using the `M-x mml-attach-file` command, you can attach any file to be
76 sent with your mail. By default this command is bound to the menu item
77 *Attachments--Attach File* with the key binding `C-c C-a`. The
78 variable `mml-dnd-attach-options` (`M-x
79 customize-variable<RET>mml-dnd-attach-options<RET>`) can be set to
80 allow the prompting for various attachment options (such as
81 inline/attachment) if you want to do that.
82
83 For those who prefer a more graphical interface, you can also simply
84 drag and drop files from a file manager into a mail composition window
85 to have them attached. In Ubuntu this works without any modifications
86 if files are dragged from the file manager.
87
88 And for those who prefer working from command line, the following
89 script opens new emacs window with empty message and attaches files
90 mentioned as script arguments. (Note: The script expects that you have
91 `(server-start)` in your `.emacs` file.)
92
93         #!/bin/sh
94         attach_cmds=""
95         while [ "$1" ]; do
96             fullpath=$(readlink --canonicalize "$1")
97             attach_cmds="$attach_cmds (mml-attach-file \"$fullpath\")"
98             shift
99         done
100         emacsclient -a '' -c -e "(progn (compose-mail) $attach_cmds)"
101
102 ## Issues with Emacs 24
103
104 If notmuch-show-mode behaves badly for you in emacs 24.x try adding one of
105
106         (setq gnus-inhibit-images nil)
107
108 or
109
110         (require 'gnus-art)
111
112 to your .emacs file.
113
114 -----
115
116 # Advanced tips and tweaks
117
118 ## Use separate emacs lisp file for notmuch configuration
119
120 Instead of adding notmuch configuration code to `.emacs`, there
121 is an option to collect those to a separate file (which is only
122 loaded when `notmuch` is invoked). To do this, write, for example
123 a file called `~/.emacs.d/my-notmuch.el`:
124
125         ;;; my-notmuch.el -- my notmuch mail configuration
126         ;;;
127         
128         ;;; add here stuff required to be configured *before*
129         ;;; notmuch is loaded;
130
131         ;; uncomment and modify in case some elisp files are not found in load-path
132         ;; (add-to-list 'load-path "~/vc/ext/notmuch/emacs")
133
134         ;;; load notmuch 
135         (require 'notmuch)
136
137         ;;; add here stuff required to be configured *after*
138         ;;; notmuch is loaded;
139
140         ;; uncomment & modify if you want to use external smtp server to send mail
141         ;; (setq smtpmail-smtp-server "smtp.server.tld"
142         ;;       message-send-mail-function 'message-smtpmail-send-it)
143         ;; uncomment to debug smtp sending problems
144         ;; (setq smtpmail-debug-info t)
145
146 Then, add to `.emacs`:
147
148         (autoload 'notmuch "~/.emacs.d/my-notmuch" "notmuch mail" t)
149
150
151 ## Add a key binding to add/remove/toggle a tag
152
153 The `notmuch-{search,show}-{add,remove}-tag` functions are very useful
154 for making quick tag key bindings.  For instance, here's an example
155 of how to make a key binding to add the "spam" tag and remove the
156 "inbox" tag in notmuch-show-mode:
157
158 In notmuch versions up to 0.11.x
159
160         (define-key notmuch-show-mode-map "S"
161           (lambda ()
162             "mark message as spam"
163             (interactive)
164             (notmuch-show-add-tag "spam")
165             (notmuch-show-remove-tag "inbox")))
166
167 Starting from notmuch 0.12 the functions `notmuch-show-add-tag` and 
168 `notmuch-show-remove-tag` have changed to be more versatile and lost
169 noninteractive use. When upgrading to 0.12 the above needs to be 
170 changed to this:
171
172         (define-key notmuch-show-mode-map "S"
173           (lambda ()
174             "mark message as spam"
175             (interactive)
176             (notmuch-show-tag-message "+spam" "-inbox")))
177
178 You can do the same for threads in `notmuch-search-mode` by just
179 replacing "show" with "search" in the called functions.
180
181 Starting from notmuch 0.12 use `notmuch-search-tag-thread` instead:
182
183         (define-key notmuch-search-mode-map "S"
184           (lambda ()
185             "mark messages in thread as spam"
186             (interactive)
187             (notmuch-show-tag-thread "+spam" "-inbox")))
188
189 Starting from notmuch 0.13 use `notmuch-search-tag` -- it has a little
190 different usage syntax:
191
192         (define-key notmuch-search-mode-map "S"
193           (lambda ()
194             "mark messages in thread as spam"
195             (interactive)
196             (notmuch-search-tag '("+spam" "-inbox"))))
197
198 The definition above makes use of a lambda function, but you could
199 also define a separate function first:
200
201         (defun notmuch-show-tag-spam ()
202           "mark message as spam"
203           (interactive)
204           (notmuch-show-add-tag "spam")
205           (notmuch-show-remove-tag "inbox")))
206         (define-key notmuch-show-mode-map "S" 'notmuch-show-tag-spam)
207
208 (See above for analogy how to apply this for notmuch 0.12 and later)
209
210 Here's a more complicated example of how to add a toggle "deleted"
211 key:
212
213         (define-key notmuch-show-mode-map "d"
214           (lambda ()
215             "toggle deleted tag for message"
216             (interactive)
217             (if (member "deleted" (notmuch-show-get-tags))
218                 (notmuch-show-remove-tag "deleted")
219               (notmuch-show-add-tag "deleted"))))
220
221 And version for notmuch 0.12
222
223         (define-key notmuch-show-mode-map "d"
224           (lambda ()
225             "toggle deleted tag for message"
226             (interactive)
227             (notmuch-show-tag-message
228               (if (member "deleted" (notmuch-show-get-tags))
229                   "-deleted" "+deleted"))))
230
231 ## Adding many tagging keybindings
232
233 If you want to have have many tagging keybindings, you can save the typing
234 the few lines of  boilerplate for every binding (for versions before 0.12,
235 you will need to change notmuch-show-apply-tag-macro).
236
237     (eval-after-load 'notmuch-show
238       '(define-key notmuch-show-mode-map "`" 'notmuch-show-apply-tag-macro))
239
240     (setq notmuch-show-tag-macro-alist
241       (list
242        '("m" "+notmuch::patch" "+notmuch::moreinfo" "-notmuch::needs-review")
243        '("n" "+notmuch::patch" "+notmuch::needs-review" "-notmuch::pushed")
244        '("o" "+notmuch::patch" "+notmuch::obsolete"
245              "-notmuch::needs-review" "-notmuch::moreinfo")
246        '("p" "-notmuch::pushed" "-notmuch::needs-review"
247          "-notmuch::moreinfo" "+pending")
248        '("P" "-pending" "-notmuch::needs-review" "-notmuch::moreinfo" "+notmuch::pushed")
249        '("r" "-notmuch::patch" "+notmuch::review")
250        '("s" "+notmuch::patch" "-notmuch::obsolete" "-notmuch::needs-review" "-notmuch::moreinfo" "+notmuch::stale")
251        '("t" "+notmuch::patch" "-notmuch::needs-review" "+notmuch::trivial")
252        '("w" "+notmuch::patch" "+notmuch::wip" "-notmuch::needs-review")))
253
254     (defun notmuch-show-apply-tag-macro (key)
255       (interactive "k")
256       (let ((macro (assoc key notmuch-show-tag-macro-alist)))
257         (apply 'notmuch-show-tag-message (cdr macro))))
258
259 ## Restore reply-to-all key binding to 'r'
260
261 Starting from notmuch 0.12 the 'r' key is bound to reply-to-sender instead of
262 reply-to-all. Here's how to swap the reply to sender/all bindings in show mode:
263
264         (define-key notmuch-show-mode-map "r" 'notmuch-show-reply)
265         (define-key notmuch-show-mode-map "R" 'notmuch-show-reply-sender)
266
267 And in search mode:
268
269         (define-key notmuch-search-mode-map "r" 'notmuch-search-reply-to-thread)
270         (define-key notmuch-search-mode-map "R" 'notmuch-search-reply-to-thread-sender)
271
272
273 ## How to do FCC/BCC...
274
275 The Emacs interface to notmuch will automatically add an `Fcc`
276 header to your outgoing mail so that any messages you send will also
277 be saved in your mail store. You can control where this copy of the
278 message is saved by setting the variables `message-directory` (which
279 defines a base directory) and `notmuch-fcc-dirs` which defines the
280 subdirectory relative to `message-directory` in which to save the
281 mail. Enter a directory (without the maildir `/cur` ending which
282 will be appended automatically). To customize both variables at the
283 same time, use the fancy command:
284
285         M-x customize-apropos<RET>\(notmuch-fcc-dirs\)\|\(message-directory\)
286
287 This mechanism also allows you to select different folders to be
288 used for the outgoing mail depending on your selected `From`
289 address. Please see the documentation for the variable
290 `notmuch-fcc-dirs` in the customization window for how to arrange
291 this.
292
293 ## How to customize `notmuch-saved-searches`
294
295 When starting notmuch, a list of saved searches and message counts is
296 displayed, replacing the older `notmuch-folders` command. The set of
297 saved searches displayed can be modified directly from the notmuch
298 interface (using the `[save]` button next to a previous search) or by
299 customising the variable `notmuch-saved-searches`.
300
301 An example setting might be:
302
303         (setq notmuch-saved-searches '(("inbox" . "tag:inbox")
304                         ("unread" . "tag:inbox AND tag:unread")
305                         ("notmuch" . "tag:inbox AND to:notmuchmail.org")))
306
307 Of course, you can have any number of saved searches, each configured
308 with any supported search terms (see "notmuch help search-terms").
309
310 Some users find it useful to add `and not tag:delete` to those
311 searches, as they use the `delete` tag to mark messages as
312 deleted. This causes messages that are marked as deleted to be removed
313 from the commonly used views of messages.  Use whatever seems most
314 useful to you.
315
316 ## Viewing HTML messages with an external viewer
317
318 The emacs client can display an HTML message inline using either the
319 `html2text` library or some text browser, like w3m or lynx. This is
320 controlled by the `mm-text-html-renderer` variable.
321
322 The first option is theorically better, because it can generate
323 strings formatted for emacs and do whatever you want, e.g., substitute
324 text inside &lt;b&gt; tags for bold text in the buffer. The library, however
325 is still in a very early development phase and cannot yet process
326 properly many elements, like tables and <style> directives, and even
327 the generated text is often poorly formatted.
328
329 Among the available browsers, w3m seems to do a better job converting
330 the html, and if you have the w3m emacs package, you can use it,
331 instead of the w3m-standalone, and thus preserve the text formatting.
332
333 But if the rendering fails for one reason or another, or if you really
334 need to see the graphical presentation of the HTML message, it can be
335 useful to display the message in an external viewer, such as a web
336 browser. Here's a little script that Keith Packard wrote, which he
337 calls `view-html`:
338
339         #!/bin/sh
340         dir=`mktemp -d`
341         trap "rm -r $dir" 0
342         cat "$@" > "$dir"/msg
343         if munpack -C "$dir" -t < "$dir"/msg 2>&1 | grep 'Did not find'; then
344             sed -n '/[Hh][Tt][Mm][Ll]/,$p' "$dir"/msg > $dir/part1.html
345             rm "$dir"/msg
346         fi
347         for i in "$dir"/part*; do
348             if grep -q -i -e '<html>' -e 'text/html' "$i"; then
349                 iceweasel "$i" &
350                 sleep 3
351                 exit 0
352             fi
353         done
354
355 Save that script somewhere in your `${PATH}`, make it executable,
356 and change the invocation of `iceweasel` to any other HTML viewer if
357 necessary. Then within the emacs client, press '|' to pipe the
358 current message, then type "view-html".
359
360 Keith mentions the following caveat, "Note that if iceweasel isn't
361 already running, it seems to shut down when the script exits. I
362 don't know why."
363
364 ## msmtp, message mode and multiple accounts
365
366 As an alternative to running a mail server such as sendmail or postfix
367 just to send email, it is possible to use
368 [msmtp](http://msmtp.sourceforge.net/). This small application will
369 look like `/usr/bin/sendmail` to a MUA such as emacs message mode, but
370 will just forward the email to an external SMTP server. It's fairly
371 easy to set up and it supports several accounts for using different
372 SMTP servers. The msmtp pages have several examples.
373
374 A typical scenario is that you want to use the company SMTP server
375 for email coming from your company email address, and your personal
376 server for personal email.  If msmtp is passed the envelope address
377 on the command line (the -f/--from option) it will automatically
378 pick the matching account.  The only trick here seems to be getting
379 emacs to actually pass the envelope from.  There are a number of
380 overlapping configuration variables that control this, and it's a
381 little confusion, but setting these three works for me:
382
383  - `mail-specify-envelope-from`: `t`
384
385  - `message-sendmail-envelope-from`: `header`
386
387  - `mail-envelope-from`: `header`
388
389 With that in place, you need a `.msmtprc` with the accounts configured
390 for the domains you want to send out using specific SMTP servers and
391 the rest will go to the default account.
392
393 If you have a hard time getting the above to work for you, as I did,
394 it's also possible to add a message-send-mail-hook in your .emacs to
395 send the from header explicitly as an argument to msmtp as described
396 [here](http://www.emacswiki.org/cgi-bin/wiki/GnusMSMTP#toc2) on the
397 emacswiki.
398
399
400 ## <span id="address_completion">Address completion when composing</span>
401
402 There are currently three solutions to this:
403
404 ### bbdb
405
406 [bbdb](http://bbdb.sourceforge.net) is a contact database for emacs
407 that works quite nicely together with message mode, including
408 address autocompletion.
409
410 ### notmuch database as an address book
411
412 You can also use the notmuch database as a mail address book itself.
413 To do this you need a command line tool that outputs likely address
414 candidates based on a search string.  There are currently three
415 available:
416
417   * The python tool `notmuch_address.py` (`git clone
418     http://commonmeasure.org/~jkr/git/notmuch_addresses.git`) (slower, but
419     no compilation required so good for testing the setup)
420
421   * The vala-based
422     [addrlookup](http://github.com/spaetz/vala-notmuch) (faster, but
423     needs compiling).  The addrlookup binary needs to be compiled.
424     Grab
425     `http://github.com/spaetz/vala-notmuch/raw/static-sources/src/addrlookup.c`
426     and build it with:
427
428             cc -o addrlookup addrlookup.c `pkg-config --cflags --libs gobject-2.0` -lnotmuch
429
430   * Shell/fgrep/perl combination [nottoomuch-addresses.sh](http://www.iki.fi/too/nottoomuch/nottoomuch-addresses/). 
431     This tools maintains it's own address "database" gathered from email
432     files notmuch knows and search from that "database" is done by `fgrep(1)`.
433
434 You can perform tab-completion using any of these programs.
435 Just add the following to your .emacs:
436
437         (require 'notmuch-address)
438         (setq notmuch-address-command "/path/to/address_fetching_program")
439         (notmuch-address-message-insinuate)
440
441 ### Google Contacts
442
443 [GooBook](http://code.google.com/p/goobook/) is a command-line tool for
444 accessing Google Contacts. Install and set it up according to its documentation.
445
446 To use GooBook with notmuch, use this wrapper script and set it up like the
447 programs above.
448
449         #!/bin/sh
450         goobook query "$*" | sed 's/\(.*\)\t\(.*\)\t.*/\2 \<\1\>/' | sed '/^$/d'
451
452 You can add the sender of a message to Google Contacts by piping the message
453 (`notmuch-show-pipe-message`) to `goobook add`.
454
455 ## How to sign/encrypt messages with gpg
456
457 Messages can by signed using gpg by invoking 
458 `M-x mml-secure-sign-pgpmime` (or `M-x mml-secure-encrypt-pgpmime`).
459 These functions are available via the standard `message-mode` keybindings 
460 `C-c C-m s p` and `C-c C-m c p`. To sign outgoing mail by default, use the
461 `message-setup-hook` in your `.emacs` file:
462
463         ;; Sign messages by default.
464         (add-hook 'message-setup-hook 'mml-secure-sign-pgpmime)
465
466 This inserts the required `<#part sign=pgpmime>` into the beginning
467 of the mail text body and will be converted into a pgp signature
468 when sending (so one can just manually delete that line if signing
469 is not required).
470
471 Alternatively, you may prefer to use `mml-secure-message-sign-pgpmime` instead
472 of `mml-secure-sign-pgpmime` to sign the whole message instead of just one
473 part.
474
475 ### Troubleshooting message-mode gpg support
476
477 - If you have trouble with expired subkeys, you may have encountered
478   emacs bug #7931.  This is fixed in git commit 301ea744c on
479   2011-02-02.  Note that if you have the Debian package easypg
480   installed, it will shadow the fixed version of easypg included with
481   emacs.
482
483 ## Multiple identities using gnus-alias
484
485 [gnus-alias](http://www.emacswiki.org/emacs/GnusAlias) allows you to
486 define multiple identities when using `message-mode`. You can specify
487 the from address, organization, extra headers (including *Bcc*), extra
488 body text, and signature for each identity. Identities are chosen
489 based on a set of rules. When you are in message mode, you can switch
490 identities using gnus-alias.
491
492 ### Installation
493
494 - put `gnus-alias.el` on your load Emacs-Lisp load path (add new directory
495   to load path by writing `(add-to-list 'load-path "/some/load/path")` into
496   your `.emacs`.
497
498 - Add the following to your `.emacs`
499
500         (autoload 'gnus-alias-determine-identity "gnus-alias" "" t)
501         (add-hook 'message-setup-hook 'gnus-alias-determine-identity)
502
503 Looking into `gnus-alias.el` gives a bit more information...
504
505 ### Example Configuration 
506
507 Here is an example configuration.
508
509         ;; Define two identities, "home" and "work"
510         (setq gnus-alias-identity-alist
511               '(("home"
512                  nil ;; Does not refer to any other identity
513                  "John Doe <jdoe@example.net>" ;; Sender address
514                  nil ;; No organization header
515                  nil ;; No extra headers
516                  nil ;; No extra body text
517                  "~/.signature")
518                 ("work"
519                  nil
520                  "John Doe <john.doe@example.com>"
521                  "Example Corp."
522                  (("Bcc" . "john.doe@example.com"))
523                  nil
524                  "~/.signature.work")))
525         ;; Use "home" identity by default
526         (setq gnus-alias-default-identity "home")
527         ;; Define rules to match work identity
528         (setq gnus-alias-identity-rules
529               '(("work" ("any" "john.doe@\\(example\\.com\\|help\\.example.com\\)" both) "work"))
530         ;; Determine identity when message-mode loads
531         (add-hook 'message-setup-hook 'gnus-alias-determine-identity)
532
533 When `gnus-alias` has been loaded (using autoload, require, *M-x load-library*
534 or *M-x load-file* (load-file takes file path -- therefore it can be used
535 without any `.emacs` changes)) the following commands can be used to get(/set)
536 more information (some of these have "extensive documentation"):
537
538         M-x describe-variable RET gnus-alias-identity-alist
539         M-x describe-variable RET gnus-alias-identity-rules
540         M-x describe-variable RET gnus-alias-default-identity
541
542         M-x customize-group RET gnus-alias RET
543           or 
544         M-x gnus-alias-customize RET
545
546 The last two do the same thing.
547
548 See also the **Usage:** section in `gnus-alias.el`.
549
550 ## Resending (or bouncing) messages
551
552 Add the following to your `.emacs` to be able to resend the current message in
553 show mode.
554
555         (define-key notmuch-show-mode-map "b"
556           (lambda (&optional address)
557             "Bounce the current message."
558             (interactive "sBounce To: ")
559             (notmuch-show-view-raw-message)
560             (message-resend address)))
561
562 ## `notmuch-hello` refresh status message
563
564 Add the following to your `.emacs` to get a status message about the change in
565 the number of messages in the mail store when refreshing the `notmuch-hello`
566 buffer.
567
568         (defvar notmuch-hello-refresh-count 0)
569
570         (defun notmuch-hello-refresh-status-message ()
571           (unless no-display
572             (let* ((new-count
573                     (string-to-number
574                      (car (process-lines notmuch-command "count"))))
575                    (diff-count (- new-count notmuch-hello-refresh-count)))
576               (cond
577                ((= notmuch-hello-refresh-count 0)
578                 (message "You have %s messages."
579                          (notmuch-hello-nice-number new-count)))
580                ((> diff-count 0)
581                 (message "You have %s more messages since last refresh."
582                          (notmuch-hello-nice-number diff-count)))
583                ((< diff-count 0)
584                 (message "You have %s fewer messages since last refresh."
585                          (notmuch-hello-nice-number (- diff-count)))))
586               (setq notmuch-hello-refresh-count new-count))))
587
588         (add-hook 'notmuch-hello-refresh-hook 'notmuch-hello-refresh-status-message)
589
590 ## Replacing tabs with spaces in subject and header
591
592 Mailman mailing list software rewrites and rewraps long message subjects in
593 a way that causes TABs to appear in the middle of the subject and header
594 lines. Add this to your `.emacs` to replace tabs with spaces in subject
595 lines:
596
597         (defun notmuch-show-subject-tabs-to-spaces ()
598           "Replace tabs with spaces in subject line."
599           (goto-char (point-min))
600           (when (re-search-forward "^Subject:" nil t)
601             (while (re-search-forward "\t" (line-end-position) t)
602               (replace-match " " nil nil))))
603
604         (add-hook 'notmuch-show-markup-headers-hook 'notmuch-show-subject-tabs-to-spaces)
605
606 And in header lines (this will only work with the yet to be released
607 notmuch version 0.15):
608
609         (defun notmuch-show-header-tabs-to-spaces ()
610           "Replace tabs with spaces in header line."
611           (setq header-line-format
612                 (notmuch-show-strip-re
613                  (replace-regexp-in-string "\t" " " (notmuch-show-get-subject)))))
614
615         (add-hook 'notmuch-show-hook 'notmuch-show-header-tabs-to-spaces)