]> git.cworth.org Git - notmuch-wiki/blob - emacstips.mdwn
Increment the number of options
[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 ## Initial cursor position in notmuch 0.15 hello window
151
152 In notmuch version 0.15 emacs client the handling of cursor position in
153 notmuch hello window has been simplified to a version which suits best
154 most cases.
155
156 Initially the cursor is positioned at the beginning of buffer.
157
158 Some users liked the "ancient" version where cursor was moved to the
159 first `Saved searches` button.
160
161 Add the following code to your notmuch emacs configuration file in
162 case you want this behaviour:
163
164         (add-hook 'notmuch-hello-refresh-hook
165                   (lambda ()
166                     (if (and (eq (point) (point-min))
167                              (search-forward "Saved searches:" nil t))
168                         (progn
169                           (forward-line)
170                           (widget-forward 1))
171                       (if (eq (widget-type (widget-at)) 'editable-field)
172                           (beginning-of-line)))))
173
174 ## Add a key binding to add/remove/toggle a tag
175
176 The `notmuch-{search,show}-{add,remove}-tag` functions are very useful
177 for making quick tag key bindings.  For instance, here's an example
178 of how to make a key binding to add the "spam" tag and remove the
179 "inbox" tag in notmuch-show-mode:
180
181 In notmuch versions up to 0.11.x
182
183         (define-key notmuch-show-mode-map "S"
184           (lambda ()
185             "mark message as spam"
186             (interactive)
187             (notmuch-show-add-tag "spam")
188             (notmuch-show-remove-tag "inbox")))
189
190 Starting from notmuch 0.12 the functions `notmuch-show-add-tag` and
191 `notmuch-show-remove-tag` have changed to be more versatile and lost
192 noninteractive use. When upgrading to 0.12 the above needs to be
193 changed to this:
194
195         (define-key notmuch-show-mode-map "S"
196           (lambda ()
197             "mark message as spam"
198             (interactive)
199             (notmuch-show-tag-message "+spam" "-inbox")))
200
201 You can do the same for threads in `notmuch-search-mode` by just
202 replacing "show" with "search" in the called functions.
203
204 Starting from notmuch 0.12 use `notmuch-search-tag-thread` instead:
205
206         (define-key notmuch-search-mode-map "S"
207           (lambda ()
208             "mark messages in thread as spam"
209             (interactive)
210             (notmuch-show-tag-thread "+spam" "-inbox")))
211
212 Starting from notmuch 0.13 use `notmuch-search-tag` -- it has a little
213 different usage syntax:
214
215         (define-key notmuch-search-mode-map "S"
216           (lambda ()
217             "mark messages in thread as spam"
218             (interactive)
219             (notmuch-search-tag '("+spam" "-inbox"))))
220
221 The definition above makes use of a lambda function, but you could
222 also define a separate function first:
223
224         (defun notmuch-show-tag-spam ()
225           "mark message as spam"
226           (interactive)
227           (notmuch-show-add-tag "spam")
228           (notmuch-show-remove-tag "inbox")))
229         (define-key notmuch-show-mode-map "S" 'notmuch-show-tag-spam)
230
231 (See above for analogy how to apply this for notmuch 0.12 and later)
232
233 Here's a more complicated example of how to add a toggle "deleted"
234 key:
235
236         (define-key notmuch-show-mode-map "d"
237           (lambda ()
238             "toggle deleted tag for message"
239             (interactive)
240             (if (member "deleted" (notmuch-show-get-tags))
241                 (notmuch-show-remove-tag "deleted")
242               (notmuch-show-add-tag "deleted"))))
243
244 And version for notmuch 0.12
245
246         (define-key notmuch-show-mode-map "d"
247           (lambda ()
248             "toggle deleted tag for message"
249             (interactive)
250             (notmuch-show-tag-message
251               (if (member "deleted" (notmuch-show-get-tags))
252                   "-deleted" "+deleted"))))
253
254 ## Adding many tagging keybindings
255
256 If you want to have have many tagging keybindings, you can save the typing
257 the few lines of  boilerplate for every binding (for versions before 0.12,
258 you will need to change notmuch-show-apply-tag-macro).
259
260     (eval-after-load 'notmuch-show
261       '(define-key notmuch-show-mode-map "`" 'notmuch-show-apply-tag-macro))
262
263     (setq notmuch-show-tag-macro-alist
264       (list
265        '("m" "+notmuch::patch" "+notmuch::moreinfo" "-notmuch::needs-review")
266        '("n" "+notmuch::patch" "+notmuch::needs-review" "-notmuch::pushed")
267        '("o" "+notmuch::patch" "+notmuch::obsolete"
268              "-notmuch::needs-review" "-notmuch::moreinfo")
269        '("p" "-notmuch::pushed" "-notmuch::needs-review"
270          "-notmuch::moreinfo" "+pending")
271        '("P" "-pending" "-notmuch::needs-review" "-notmuch::moreinfo" "+notmuch::pushed")
272        '("r" "-notmuch::patch" "+notmuch::review")
273        '("s" "+notmuch::patch" "-notmuch::obsolete" "-notmuch::needs-review" "-notmuch::moreinfo" "+notmuch::stale")
274        '("t" "+notmuch::patch" "-notmuch::needs-review" "+notmuch::trivial")
275        '("w" "+notmuch::patch" "+notmuch::wip" "-notmuch::needs-review")))
276
277     (defun notmuch-show-apply-tag-macro (key)
278       (interactive "k")
279       (let ((macro (assoc key notmuch-show-tag-macro-alist)))
280         (apply 'notmuch-show-tag-message (cdr macro))))
281
282 ## Restore reply-to-all key binding to 'r'
283
284 Starting from notmuch 0.12 the 'r' key is bound to reply-to-sender instead of
285 reply-to-all. Here's how to swap the reply to sender/all bindings in show mode:
286
287         (define-key notmuch-show-mode-map "r" 'notmuch-show-reply)
288         (define-key notmuch-show-mode-map "R" 'notmuch-show-reply-sender)
289
290 And in search mode:
291
292         (define-key notmuch-search-mode-map "r" 'notmuch-search-reply-to-thread)
293         (define-key notmuch-search-mode-map "R" 'notmuch-search-reply-to-thread-sender)
294
295
296 ## How to do FCC/BCC...
297
298 The Emacs interface to notmuch will automatically add an `Fcc`
299 header to your outgoing mail so that any messages you send will also
300 be saved in your mail store. You can control where this copy of the
301 message is saved by setting the variable `notmuch-fcc-dirs` which defines the
302 subdirectory relative to the `database.path` setting from your
303 notmuch configuration in which to save the mail. Enter a directory
304 (without the maildir `/cur` ending which will be appended
305 automatically). Additional information can be found as usual using:
306
307        M-x describe-variable notmuch-fcc-dirs
308
309 An additional variable that can affect FCC settings in some cases is
310 `message-directory`. Emacs message-mode uses this variable for
311 postponed messages.
312
313 To customize both variables at the same time, use the fancy command:
314
315         M-x customize-apropos<RET>\(notmuch-fcc-dirs\)\|\(message-directory\)
316
317 This mechanism also allows you to select different folders to be
318 used for the outgoing mail depending on your selected `From`
319 address. Please see the documentation for the variable
320 `notmuch-fcc-dirs` in the customization window for how to arrange
321 this.
322
323 ## How to customize `notmuch-saved-searches`
324
325 When starting notmuch, a list of saved searches and message counts is
326 displayed, replacing the older `notmuch-folders` command. The set of
327 saved searches displayed can be modified directly from the notmuch
328 interface (using the `[save]` button next to a previous search) or by
329 customising the variable `notmuch-saved-searches`.
330
331 An example setting might be:
332
333         (setq notmuch-saved-searches '(("inbox" . "tag:inbox")
334                         ("unread" . "tag:inbox AND tag:unread")
335                         ("notmuch" . "tag:inbox AND to:notmuchmail.org")))
336
337 Of course, you can have any number of saved searches, each configured
338 with any supported search terms (see "notmuch help search-terms").
339
340 Some users find it useful to add `and not tag:delete` to those
341 searches, as they use the `delete` tag to mark messages as
342 deleted. This causes messages that are marked as deleted to be removed
343 from the commonly used views of messages.  Use whatever seems most
344 useful to you.
345
346 ## Viewing HTML messages with an external viewer
347
348 The emacs client can display an HTML message inline using either the
349 `html2text` library or some text browser, like w3m or lynx. This is
350 controlled by the `mm-text-html-renderer` variable.
351
352 The first option is theorically better, because it can generate
353 strings formatted for emacs and do whatever you want, e.g., substitute
354 text inside &lt;b&gt; tags for bold text in the buffer. The library, however
355 is still in a very early development phase and cannot yet process
356 properly many elements, like tables and <style> directives, and even
357 the generated text is often poorly formatted.
358
359 Among the available browsers, w3m seems to do a better job converting
360 the html, and if you have the w3m emacs package, you can use it,
361 instead of the w3m-standalone, and thus preserve the text formatting.
362
363 But if the rendering fails for one reason or another, or if you really
364 need to see the graphical presentation of the HTML message, it can be
365 useful to display the message in an external viewer, such as a web
366 browser. Here's a little script that Keith Packard wrote, which he
367 calls `view-html`:
368
369         #!/bin/sh
370         dir=`mktemp -d`
371         trap "rm -r $dir" 0
372         cat "$@" > "$dir"/msg
373         if munpack -C "$dir" -t < "$dir"/msg 2>&1 | grep 'Did not find'; then
374             sed -n '/[Hh][Tt][Mm][Ll]/,$p' "$dir"/msg > $dir/part1.html
375             rm "$dir"/msg
376         fi
377         for i in "$dir"/part*; do
378             if grep -q -i -e '<html>' -e 'text/html' "$i"; then
379                 iceweasel "$i" &
380                 sleep 3
381                 exit 0
382             fi
383         done
384
385 Save that script somewhere in your `${PATH}`, make it executable,
386 and change the invocation of `iceweasel` to any other HTML viewer if
387 necessary. Then within the emacs client, press '|' to pipe the
388 current message, then type "view-html".
389
390 Keith mentions the following caveat, "Note that if iceweasel isn't
391 already running, it seems to shut down when the script exits. I
392 don't know why."
393
394 ## msmtp, message mode and multiple accounts
395
396 As an alternative to running a mail server such as sendmail or postfix
397 just to send email, it is possible to use
398 [msmtp](http://msmtp.sourceforge.net/). This small application will
399 look like `/usr/bin/sendmail` to a MUA such as emacs message mode, but
400 will just forward the email to an external SMTP server. It's fairly
401 easy to set up and it supports several accounts for using different
402 SMTP servers. The msmtp pages have several examples.
403
404 A typical scenario is that you want to use the company SMTP server
405 for email coming from your company email address, and your personal
406 server for personal email.  If msmtp is passed the envelope address
407 on the command line (the -f/--from option) it will automatically
408 pick the matching account.  The only trick here seems to be getting
409 emacs to actually pass the envelope from.  There are a number of
410 overlapping configuration variables that control this, and it's a
411 little confusion, but setting these three works for me:
412
413  - `mail-specify-envelope-from`: `t`
414
415  - `message-sendmail-envelope-from`: `header`
416
417  - `mail-envelope-from`: `header`
418
419 With that in place, you need a `.msmtprc` with the accounts configured
420 for the domains you want to send out using specific SMTP servers and
421 the rest will go to the default account.
422
423 If you have a hard time getting the above to work for you, as I did,
424 it's also possible to add a message-send-mail-hook in your .emacs to
425 send the from header explicitly as an argument to msmtp as described
426 [here](http://www.emacswiki.org/cgi-bin/wiki/GnusMSMTP#toc2) on the
427 emacswiki.
428
429
430 ## <span id="address_completion">Address completion when composing</span>
431
432 There are currently three solutions to this:
433
434 ### bbdb
435
436 [bbdb](http://bbdb.sourceforge.net) is a contact database for emacs
437 that works quite nicely together with message mode, including
438 address autocompletion.
439
440 ### notmuch database as an address book
441
442 You can also use the notmuch database as a mail address book itself.
443 To do this you need a command line tool that outputs likely address
444 candidates based on a search string.  There are currently four
445 available:
446
447   * The python tool `notmuch_address.py` (`git clone
448     http://commonmeasure.org/~jkr/git/notmuch_addresses.git`) (slower, but
449     no compilation required so good for testing the setup)
450
451   * The vala-based
452     [addrlookup](http://github.com/spaetz/vala-notmuch) (faster, but
453     needs compiling).  The addrlookup binary needs to be compiled.
454     Grab
455     `http://github.com/spaetz/vala-notmuch/raw/static-sources/src/addrlookup.c`
456     and build it with:
457
458             cc -o addrlookup addrlookup.c `pkg-config --cflags --libs gobject-2.0` -lnotmuch
459
460   * Shell/fgrep/perl combination [nottoomuch-addresses.sh](https://github.com/domo141/nottoomuch/blob/master/nottoomuch-addresses.rst).
461     This tools maintains it's own address "database" gathered from email
462     files notmuch knows and search from that "database" is done by `fgrep(1)`.
463
464   * python/sqlite combination [notmuch-abook](https://github.com/guyzmo/notmuch-abook/)
465     This tools also maintains an address database in sqlite after harvesting
466     from notmuch.  It also includes a vim plugin.
467
468 You can perform tab-completion using any of these programs.
469 Just add the following to your .emacs:
470
471         (require 'notmuch-address)
472         (setq notmuch-address-command "/path/to/address_fetching_program")
473         (notmuch-address-message-insinuate)
474
475 ### Google Contacts
476
477 [GooBook](http://code.google.com/p/goobook/) is a command-line tool for
478 accessing Google Contacts. Install and set it up according to its documentation.
479
480 To use GooBook with notmuch, use this wrapper script and set it up like the
481 programs above.
482
483         #!/bin/sh
484         goobook query "$*" | sed 's/\(.*\)\t\(.*\)\t.*/\2 \<\1\>/' | sed '/^$/d'
485
486 You can add the sender of a message to Google Contacts by piping the message
487 (`notmuch-show-pipe-message`) to `goobook add`.
488
489 ### Akonadi
490
491         git clone https://github.com/mmehnert/akonadimailsearch
492
493 Install the development packages for kdepim on your system.
494 Enter the cloned repository and create a build directory:
495
496         mkdir build
497         cd build
498         cmake ..; make;
499
500 You will find the akonadimailsearch binary in the build/src directory.
501 Create a ~/bin/akonadimailsearch.sh file with the following content and make it executable:
502 (Adjust the path for the akonadimailsearch binary.)
503
504         #!/bin/sh
505         akonadimailsearch "$@" 2>/dev/null
506
507 As described above, you can now add the following settings to your ~/.emacs file:
508
509         (require 'notmuch-address)
510         (setq notmuch-address-command "~/bin/akonadimailsearch.sh")
511         (notmuch-address-message-insinuate)
512
513
514
515 ## How to sign/encrypt messages with gpg
516
517 Messages can by signed using gpg by invoking
518 `M-x mml-secure-sign-pgpmime` (or `M-x mml-secure-encrypt-pgpmime`).
519 These functions are available via the standard `message-mode` keybindings
520 `C-c C-m s p` and `C-c C-m c p`. To sign outgoing mail by default, use the
521 `message-setup-hook` in your `.emacs` file:
522
523         ;; Sign messages by default.
524         (add-hook 'message-setup-hook 'mml-secure-sign-pgpmime)
525
526 This inserts the required `<#part sign=pgpmime>` into the beginning
527 of the mail text body and will be converted into a pgp signature
528 when sending (so one can just manually delete that line if signing
529 is not required).
530
531 Alternatively, you may prefer to use `mml-secure-message-sign-pgpmime` instead
532 of `mml-secure-sign-pgpmime` to sign the whole message instead of just one
533 part.
534
535 ### Troubleshooting message-mode gpg support
536
537 - If you have trouble with expired subkeys, you may have encountered
538   emacs bug #7931.  This is fixed in git commit 301ea744c on
539   2011-02-02.  Note that if you have the Debian package easypg
540   installed, it will shadow the fixed version of easypg included with
541   emacs.
542
543 ## Multiple identities using gnus-alias
544
545 [gnus-alias](http://www.emacswiki.org/emacs/GnusAlias) allows you to
546 define multiple identities when using `message-mode`. You can specify
547 the from address, organization, extra headers (including *Bcc*), extra
548 body text, and signature for each identity. Identities are chosen
549 based on a set of rules. When you are in message mode, you can switch
550 identities using gnus-alias.
551
552 ### Installation
553
554 - put `gnus-alias.el` on your load Emacs-Lisp load path (add new directory
555   to load path by writing `(add-to-list 'load-path "/some/load/path")` into
556   your `.emacs`.
557
558 - Add the following to your `.emacs`
559
560         (autoload 'gnus-alias-determine-identity "gnus-alias" "" t)
561         (add-hook 'message-setup-hook 'gnus-alias-determine-identity)
562
563 Looking into `gnus-alias.el` gives a bit more information...
564
565 ### Example Configuration
566
567 Here is an example configuration.
568
569         ;; Define two identities, "home" and "work"
570         (setq gnus-alias-identity-alist
571               '(("home"
572                  nil ;; Does not refer to any other identity
573                  "John Doe <jdoe@example.net>" ;; Sender address
574                  nil ;; No organization header
575                  nil ;; No extra headers
576                  nil ;; No extra body text
577                  "~/.signature")
578                 ("work"
579                  nil
580                  "John Doe <john.doe@example.com>"
581                  "Example Corp."
582                  (("Bcc" . "john.doe@example.com"))
583                  nil
584                  "~/.signature.work")))
585         ;; Use "home" identity by default
586         (setq gnus-alias-default-identity "home")
587         ;; Define rules to match work identity
588         (setq gnus-alias-identity-rules
589               '(("work" ("any" "john.doe@\\(example\\.com\\|help\\.example.com\\)" both) "work"))
590         ;; Determine identity when message-mode loads
591         (add-hook 'message-setup-hook 'gnus-alias-determine-identity)
592
593 When `gnus-alias` has been loaded (using autoload, require, *M-x load-library*
594 or *M-x load-file* (load-file takes file path -- therefore it can be used
595 without any `.emacs` changes)) the following commands can be used to get(/set)
596 more information (some of these have "extensive documentation"):
597
598         M-x describe-variable RET gnus-alias-identity-alist
599         M-x describe-variable RET gnus-alias-identity-rules
600         M-x describe-variable RET gnus-alias-default-identity
601
602         M-x customize-group RET gnus-alias RET
603           or
604         M-x gnus-alias-customize RET
605
606 The last two do the same thing.
607
608 See also the **Usage:** section in `gnus-alias.el`.
609
610 ## Resending (or bouncing) messages
611
612 Add the following to your `.emacs` to be able to resend the current message in
613 show mode.
614
615         (define-key notmuch-show-mode-map "b"
616           (lambda (&optional address)
617             "Bounce the current message."
618             (interactive "sBounce To: ")
619             (notmuch-show-view-raw-message)
620             (message-resend address)))
621
622 ## `notmuch-hello` refresh status message
623
624 Add the following to your `.emacs` to get a status message about the change in
625 the number of messages in the mail store when refreshing the `notmuch-hello`
626 buffer.
627
628         (defvar notmuch-hello-refresh-count 0)
629
630         (defun notmuch-hello-refresh-status-message ()
631           (unless no-display
632             (let* ((new-count
633                     (string-to-number
634                      (car (process-lines notmuch-command "count"))))
635                    (diff-count (- new-count notmuch-hello-refresh-count)))
636               (cond
637                ((= notmuch-hello-refresh-count 0)
638                 (message "You have %s messages."
639                          (notmuch-hello-nice-number new-count)))
640                ((> diff-count 0)
641                 (message "You have %s more messages since last refresh."
642                          (notmuch-hello-nice-number diff-count)))
643                ((< diff-count 0)
644                 (message "You have %s fewer messages since last refresh."
645                          (notmuch-hello-nice-number (- diff-count)))))
646               (setq notmuch-hello-refresh-count new-count))))
647
648         (add-hook 'notmuch-hello-refresh-hook 'notmuch-hello-refresh-status-message)
649
650 ## Replacing tabs with spaces in subject and header
651
652 Mailman mailing list software rewrites and rewraps long message subjects in
653 a way that causes TABs to appear in the middle of the subject and header
654 lines. Add this to your `.emacs` to replace tabs with spaces in subject
655 lines:
656
657         (defun notmuch-show-subject-tabs-to-spaces ()
658           "Replace tabs with spaces in subject line."
659           (goto-char (point-min))
660           (when (re-search-forward "^Subject:" nil t)
661             (while (re-search-forward "\t" (line-end-position) t)
662               (replace-match " " nil nil))))
663
664         (add-hook 'notmuch-show-markup-headers-hook 'notmuch-show-subject-tabs-to-spaces)
665
666 And in header lines (this will only work with the yet to be released
667 notmuch version 0.15):
668
669         (defun notmuch-show-header-tabs-to-spaces ()
670           "Replace tabs with spaces in header line."
671           (setq header-line-format
672                 (notmuch-show-strip-re
673                  (replace-regexp-in-string "\t" " " (notmuch-show-get-subject)))))
674
675         (add-hook 'notmuch-show-hook 'notmuch-show-header-tabs-to-spaces)
676
677 ## Hiding unread messages in notmuch-show
678
679 I like to have an inbox saved search, but only show unread messages when they
680 view a thread. This takes two steps:
681
682 1. Apply
683 [this patch from Mark Walters](http://notmuchmail.org/pipermail/notmuch/2012/010817.html)
684 to add the `notmuch-show-filter-thread` function.
685 1. Add the following hook to your emacs configuration:
686
687         (defun expand-only-unread-hook () (interactive)
688           (let ((unread nil)
689                 (open (notmuch-show-get-message-ids-for-open-messages)))
690             (notmuch-show-mapc (lambda ()
691                                  (when (member "unread" (notmuch-show-get-tags))
692                                    (setq unread t))))
693             (when unread
694               (let ((notmuch-show-hook (remove 'expand-only-unread-hook notmuch-show-hook)))
695                 (notmuch-show-filter-thread "tag:unread")))))
696
697         (add-hook 'notmuch-show-hook 'expand-only-unread-hook)
698
699 ## Changing the color of a saved search based on some other search
700
701 I like to have a saved search for my inbox, but have it change color when there
702 are thread with unread messages in the inbox. I accomplish this with the
703 following code in my emacs config:
704
705         (defun color-inbox-if-unread () (interactive)
706           (save-excursion
707             (goto-char (point-min))
708             (let ((cnt (car (process-lines "notmuch" "count" "tag:inbox and tag:unread"))))
709               (when (> (string-to-number cnt) 0)
710                 (save-excursion
711                   (when (search-forward "inbox" (point-max) t)
712                     (let* ((overlays (overlays-in (match-beginning 0) (match-end 0)))
713                            (overlay (car overlays)))
714                       (when overlay
715                         (overlay-put overlay 'face '((:inherit bold) (:foreground "green")))))))))))
716         (add-hook 'notmuch-hello-refresh-hook 'color-inbox-if-unread)
717
718 ## Linking to notmuch messages and threads from the Circe IRC client
719
720 [Circe](https://github.com/jorgenschaefer/circe/wiki) is an IRC client for emacs.
721 To have clickable buttons for notmuch messages and threads, add the following to
722 `lui-buttons-list` (using, e.g. M-x customize-variable)
723
724     ("\\(?:id\\|mid\\|thread\\):[0-9A-Za-z][0-9A-Za-z.@-]*" 0 notmuch-show 0)
725
726 If you have notmuch-pick installed, it works fine for this as well.
727
728 ## Linking to notmuch messages from org-mode
729
730 Support for linking to notmuch messages is distributed with org-mode,
731 but as a contrib file, so you might have to work a bit to load it.
732
733 In Debian and derivatives,
734
735         (add-to-list 'load-path "/usr/share/org-mode/lisp")
736
737 Then
738
739         (require 'org-notmuch)
740
741 In general it is nice to have a key for org-links (not just for notmuch). For example
742
743     (define-key global-map "\C-cl" 'org-store-link)