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