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