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