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