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