]> git.cworth.org Git - notmuch-wiki/blob - emacstips.mdwn
Set mml-secure-openpgp-sign-with-sender to sign messages
[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 Here are some tips and tricks for using Notmuch with Emacs. See the [[Notmuch
5 Emacs Interface|notmuch-emacs]] page for basics.
6
7 [[!toc levels=2]]
8
9 ## Issues with Emacs 24
10
11 If notmuch-show-mode behaves badly for you in emacs 24.x try adding one of
12
13         (setq gnus-inhibit-images nil)
14
15 or
16
17         (require 'gnus-art)
18
19 to your .emacs file.
20
21 ## Controlling external handlers for attachments
22
23 You can choose e.g. which pdf viewer to invoke from notmuch-show mode by
24 adding a .mailcap file in your home directory. Here is an example:
25
26     application/pdf; /usr/bin/mupdf %s; test=test "$DISPLAY" != ""; description=Portable Document Format; nametemplate=%s.pdf
27     application/x-pdf; /usr/bin/mupdf %s; test=test "$DISPLAY" != ""; description=Portable Document Format; nametemplate=%s.pdf
28
29 ## Overwriting the sender address
30
31 If you want to always use the same sender address, then the following
32 defadvice can help you.
33
34        (defadvice notmuch-mua-reply (around notmuch-fix-sender)
35          (let ((sender "Max Monster <max.monster@example.com>"))
36            ad-do-it))
37        (ad-activate 'notmuch-mua-reply)
38
39 ## Initial cursor position in notmuch 0.15 hello window
40
41 In notmuch version 0.15 emacs client the handling of cursor position in
42 notmuch hello window has been simplified to a version which suits best
43 most cases.
44
45 Initially the cursor is positioned at the beginning of buffer.
46
47 Some users liked the "ancient" version where cursor was moved to the
48 first `Saved searches` button.
49
50 Add the following code to your notmuch emacs configuration file in
51 case you want this behaviour:
52
53         (add-hook 'notmuch-hello-refresh-hook
54                   (lambda ()
55                     (if (and (eq (point) (point-min))
56                              (search-forward "Saved searches:" nil t))
57                         (progn
58                           (forward-line)
59                           (widget-forward 1))
60                       (if (eq (widget-type (widget-at)) 'editable-field)
61                           (beginning-of-line)))))
62
63 ## Add a key binding to add/remove/toggle a tag
64
65 The `notmuch-{search,show,tree}-tag` functions are very useful for
66 making quick tag key bindings.  The arguments to these functions have
67 changed as notmuch has evolved but the following should work on all
68 versions of notmuch from 0.13 on.  These functions take a list of
69 tag changes as argument. For example, an argument of (list "+spam"
70 "-inbox") adds the tag spam and deletes the tag inbox. Note the
71 argument must be a list even if there is only a single tag change
72 e.g., use (list "+deleted") to add the deleted tag.
73
74 For instance, here's an example of how to make a key binding to add
75 the "spam" tag and remove the "inbox" tag in notmuch-show-mode:
76
77         (define-key notmuch-show-mode-map "S"
78           (lambda ()
79             "mark message as spam"
80             (interactive)
81             (notmuch-show-tag (list "+spam" "-inbox"))))
82
83 You can do the same for threads in `notmuch-search-mode` by just
84 replacing "show" with "search" in the keymap and called functions, or
85 for messages in `notmuch-tree-mode` by replacing "show" by "tree". If
86 you want to tag a whole thread in `notmuch-tree-mode` use
87 `notmuch-tree-tag-thread` instead of `notmuch-tree-tag`.
88
89 You may also want the function in search mode apply to the all threads
90 in the selected region (if there is one). For notmuch prior to 0.17
91 this behaviour will occur automatically with the functions given
92 above. To get this behaviour on 0.17+ do the following:
93
94         (define-key notmuch-search-mode-map "S"
95           (lambda (&optional beg end)
96             "mark thread as spam"
97             (interactive (notmuch-interactive-region))
98             (notmuch-search-tag (list "+spam" "-inbox") beg end)))
99
100 The analogous functionality in notmuch-tree is currently missing.
101
102 The definitions above make use of a lambda function, but you could
103 also define a separate function first:
104
105         (defun notmuch-show-tag-spam ()
106           "mark message as spam"
107           (interactive)
108           (notmuch-show-add-tag (list "+spam" "-inbox")))
109
110         (define-key notmuch-show-mode-map "S" 'notmuch-show-tag-spam)
111
112 Here's a more complicated example of how to add a toggle "deleted"
113 key:
114
115         (define-key notmuch-show-mode-map "d"
116           (lambda ()
117             "toggle deleted tag for message"
118             (interactive)
119             (if (member "deleted" (notmuch-show-get-tags))
120                 (notmuch-show-tag (list "-deleted"))
121               (notmuch-show-tag (list "+deleted")))))
122
123 ## Adding many tagging keybindings
124
125 If you want to have have many tagging keybindings, you can save the typing
126 the few lines of  boilerplate for every binding (for versions before 0.12,
127 you will need to change notmuch-show-apply-tag-macro).
128
129     (eval-after-load 'notmuch-show
130       '(define-key notmuch-show-mode-map "`" 'notmuch-show-apply-tag-macro))
131
132     (setq notmuch-show-tag-macro-alist
133       (list
134        '("m" "+notmuch::patch" "+notmuch::moreinfo" "-notmuch::needs-review")
135        '("n" "+notmuch::patch" "+notmuch::needs-review" "-notmuch::pushed")
136        '("o" "+notmuch::patch" "+notmuch::obsolete"
137              "-notmuch::needs-review" "-notmuch::moreinfo")
138        '("p" "-notmuch::pushed" "-notmuch::needs-review"
139          "-notmuch::moreinfo" "+pending")
140        '("P" "-pending" "-notmuch::needs-review" "-notmuch::moreinfo" "+notmuch::pushed")
141        '("r" "-notmuch::patch" "+notmuch::review")
142        '("s" "+notmuch::patch" "-notmuch::obsolete" "-notmuch::needs-review" "-notmuch::moreinfo" "+notmuch::stale")
143        '("t" "+notmuch::patch" "-notmuch::needs-review" "+notmuch::trivial")
144        '("w" "+notmuch::patch" "+notmuch::wip" "-notmuch::needs-review")))
145
146     (defun notmuch-show-apply-tag-macro (key)
147       (interactive "k")
148       (let ((macro (assoc key notmuch-show-tag-macro-alist)))
149         (apply 'notmuch-show-tag-message (cdr macro))))
150
151 ## Restore reply-to-all key binding to 'r'
152
153 Starting from notmuch 0.12 the 'r' key is bound to reply-to-sender instead of
154 reply-to-all. Here's how to swap the reply to sender/all bindings in show mode:
155
156         (define-key notmuch-show-mode-map "r" 'notmuch-show-reply)
157         (define-key notmuch-show-mode-map "R" 'notmuch-show-reply-sender)
158
159 In search mode:
160
161         (define-key notmuch-search-mode-map "r" 'notmuch-search-reply-to-thread)
162         (define-key notmuch-search-mode-map "R" 'notmuch-search-reply-to-thread-sender)
163
164 And in tree mode:
165
166         (define-key notmuch-tree-mode-map "r" (notmuch-tree-close-message-pane-and #'notmuch-show-reply))
167         (define-key notmuch-tree-mode-map "R" (notmuch-tree-close-message-pane-and #'notmuch-show-reply-sender))
168
169 ## How to do FCC/BCC...
170
171 The Emacs interface to notmuch will automatically add an `Fcc`
172 header to your outgoing mail so that any messages you send will also
173 be saved in your mail store. You can control where this copy of the
174 message is saved by setting the variable `notmuch-fcc-dirs` which defines the
175 subdirectory relative to the `database.path` setting from your
176 notmuch configuration in which to save the mail. Enter a directory
177 (without the maildir `/cur` ending which will be appended
178 automatically). Additional information can be found as usual using:
179
180        M-x describe-variable notmuch-fcc-dirs
181
182 An additional variable that can affect FCC settings in some cases is
183 `message-directory`. Emacs message-mode uses this variable for
184 postponed messages.
185
186 To customize both variables at the same time, use the fancy command:
187
188         M-x customize-apropos<RET>\(notmuch-fcc-dirs\)\|\(message-directory\)
189
190 This mechanism also allows you to select different folders to be
191 used for the outgoing mail depending on your selected `From`
192 address. Please see the documentation for the variable
193 `notmuch-fcc-dirs` in the customization window for how to arrange
194 this.
195
196 ## How to customize `notmuch-saved-searches`
197
198 When starting notmuch, a list of saved searches and message counts is
199 displayed, replacing the older `notmuch-folders` command. The set of
200 saved searches displayed can be modified directly from the notmuch
201 interface (using the `[save]` button next to a previous search) or by
202 customising the variable `notmuch-saved-searches`.
203
204 An example setting for notmuch versions up to 0.17.x might be:
205
206         (setq notmuch-saved-searches '(("inbox" . "tag:inbox")
207                         ("unread" . "tag:inbox AND tag:unread")
208                         ("notmuch" . "tag:inbox AND to:notmuchmail.org")))
209
210 Starting from notmuch 0.18 the variable changed. It is backwards
211 compatible so the above will still work but the new style will be used
212 if you use customize and there are some new features available. The above would become
213
214         (setq notmuch-saved-searches '((:name "inbox" :query "tag:inbox")
215                         (:name "unread" :query "tag:inbox AND tag:unread")
216                         (:name "notmuch" :query "tag:inbox AND to:notmuchmail.org")))
217
218 The additional features are the possibility to set the search order
219 for the search, and the possibility to specify a different query for
220 displaying the count for the saved-search. For example
221
222         (setq notmuch-saved-searches '((:name "inbox"
223                                         :query "tag:inbox"
224                                         :count-query "tag:inbox and tag:unread"
225                                         :sort-order oldest-first)))
226
227 specifies a single saved search for inbox, but the number displayed by
228 the search will be the number of unread messages in the inbox, and the
229 sort order for this search will be oldest-first.
230
231 Of course, you can have any number of saved searches, each configured
232 with any supported search terms (see "notmuch help search-terms"), and
233 in the new style variable they can each have different count-queries
234 and sort orders.
235
236 Some users find it useful to add `and not tag:delete` to those
237 searches, as they use the `delete` tag to mark messages as
238 deleted. This causes messages that are marked as deleted to be removed
239 from the commonly used views of messages.  Use whatever seems most
240 useful to you.
241
242 ## Viewing HTML messages with an external viewer
243
244 The Emacs client can generally display HTML messages inline using one of the
245 supported HTML renderers. This is controlled by the `mm-text-html-renderer`
246 variable.
247
248 Sometimes it may be necessary to display the message, or a single MIME part, in
249 an external browser. This can be done by `(notmuch-show-view-part)`, bound to
250 `. v` by default.
251
252 This command will try to view the message part the point is on with an
253 external viewer. The mime-type of the part will determine what viewer
254 will be used. Typically a 'text/html' part will be send to your
255 browser. 
256
257 The configuration for this is kept in so called `mailcap`
258 files. (typically the file is `~/.mailcap` or `/etc/mailcap`) If the
259 wrong viewer is started or something else goes wrong, there's a good
260 chance something needs to be adapted in the mailcap configuration.
261
262 For Example: The `copiousoutput` setting in mailcap files needs to be
263 removed for some mime-types to prevent immediate removal of tempory
264 files so the configured viewer can access them.
265
266
267 ## msmtp, message mode and multiple accounts
268
269 As an alternative to running a mail server such as sendmail or postfix
270 just to send email, it is possible to use
271 [msmtp](http://msmtp.sourceforge.net/). This small application will
272 look like `/usr/bin/sendmail` to a MUA such as emacs message mode, but
273 will just forward the email to an external SMTP server. It's fairly
274 easy to set up and it supports several accounts for using different
275 SMTP servers. The msmtp pages have several examples.
276
277 A typical scenario is that you want to use the company SMTP server
278 for email coming from your company email address, and your personal
279 server for personal email.  If msmtp is passed the envelope address
280 on the command line (the -f/--from option) it will automatically
281 pick the matching account.  The only trick here seems to be getting
282 emacs to actually pass the envelope from.  There are a number of
283 overlapping configuration variables that control this, and it's a
284 little confusion, but setting these three works for me:
285
286  - `mail-specify-envelope-from`: `t`
287
288  - `message-sendmail-envelope-from`: `header`
289
290  - `mail-envelope-from`: `header`
291
292 With that in place, you need a `.msmtprc` with the accounts configured
293 for the domains you want to send out using specific SMTP servers and
294 the rest will go to the default account.
295
296 ## sending mail using smtpmail
297
298 <!-- By default message mode will use the system `sendmail` command to send
299 mail. However, on a typical desktop machine there may not be local SMTP
300 daemon running (nor it is configured to send mail outside of the system). -->
301
302 If setting up local `sendmail` or `msmtp` is not feasible or desirable,
303 the Emacs `smtpmail` package can be used to send email by talking to remote
304 SMTP server via TCP connection. It is pretty easy to configure:
305
306 1. Emacs variable `message-send-mail-function` has not been set
307
308    Initially, Emacs variable `message-send-mail-function` has value of
309    `sendmail-query-once`. When (notmuch) message mode is about to send email,
310    `sendmail-query-once` will ask how emacs should send email. Typing `smtp`
311    will configure `smtpmail` and Emacs may prompt for SMTP settings.
312
313 1. `M-x customize-group RET smtpmail`
314
315    As a minimum, 'Smtpmail Smtp Server' needs to be set.
316
317    After doing that, continue with `M-x load-library RET message` and
318    `M-x customize-variable RET message-send-mail-function`.
319    In the customization buffer select `message-smtpmail-send-it`.
320
321 1. Set some variables in .emacs or in [notmuch init file](/notmuch-emacs#notmuch_init_file)
322
323         (setq smtpmail-smtp-server "smtp.server.tld" ;; <-- edit this !!!
324         ;;    smtpmail-smtp-service 25 ;; 25 is default -- uncomment and edit if needed
325         ;;    smtpmail-stream-type 'starttls
326         ;;    smtpmail-debug-info t
327         ;;    smtpmail-debug-verb t
328               message-send-mail-function 'message-smtpmail-send-it)
329
330 Note that emacs 24 or newer is required for `smtpmail-stream-type`
331 (and smtp authentication) to be effective.
332
333 More information for smtpmail is available:
334
335 * In Emacs: `M-x info-display-manual smtpmail`
336 * [EmacsWiki Page](http://www.emacswiki.org/emacs/SendingMail)
337
338
339 ## <span id="address_completion">Address completion when composing</span>
340
341 There are currently three solutions to this:
342
343 ### notmuch address
344
345 Starting with Notmuch 0.21, there is a builtin command to perform
346 autocompletion directly within Notmuch. Starting with 0.22, it is
347 configured by default, so if you have previously configured another
348 completion mechanism, you may want to try out the new internal
349 method. Use `M-x customize-variable RET notmuch-address-command` and
350 reset the value to "internal address completion" (`'internal` in
351 lisp).
352
353 If you are not yet running 0.22, you can still use it by adding a
354 wrapper around the command called, say, `notmuch-address`:
355
356     #!/bin/sh
357     exec notmuch address from:"$*"
358
359 Then you can set the `notmuch-address-command` to `notmuch-address`
360 (if it is in your `$PATH` of course, otherwise use an absolute path).
361
362 ### bbdb
363
364 [bbdb](http://bbdb.sourceforge.net) is a contact database for emacs
365 that works quite nicely together with message mode, including
366 address autocompletion.
367
368 ### notmuch database as an address book
369
370 You can also use the notmuch database as a mail address book itself.
371 To do this you need a command line tool that outputs likely address
372 candidates based on a search string.  There are currently four
373 available:
374
375   * The python tool `notmuch_address.py` (`git clone
376     http://commonmeasure.org/~jkr/git/notmuch_addresses.git`) (slower, but
377     no compilation required so good for testing the setup)
378
379   * The C-based [notmuch-addrlookup](https://github.com/aperezdc/notmuch-addrlookup-c) by [Adrian Perez](http://perezdecastro.org/), which is faster but needs to be compiled.
380
381         git clone https://github.com/aperezdc/notmuch-addrlookup-c
382         cd notmuch-addrlookup-c
383         make
384
385   * The vala-based
386     [addrlookup](http://github.com/spaetz/vala-notmuch) The addrlookup binary needs to be compiled.
387     Grab
388     `http://github.com/spaetz/vala-notmuch/raw/static-sources/src/addrlookup.c`
389     and build it with:
390
391             cc -o addrlookup addrlookup.c `pkg-config --cflags --libs gobject-2.0` -lnotmuch
392
393   * Shell/fgrep/perl combination [nottoomuch-addresses.sh](https://github.com/domo141/nottoomuch/blob/master/nottoomuch-addresses.rst).
394     This tools maintains its own address "database" gathered from email
395     files notmuch knows and search from that "database" is done by `fgrep(1)`.
396
397   * python/sqlite combination [notmuch-abook](https://github.com/guyzmo/notmuch-abook/)
398     This tools also maintains an address database in sqlite after harvesting
399     from notmuch.  It also includes a vim plugin.
400
401 You can perform tab-completion using any of these programs.
402 Just add the following to your [notmuch init file](/notmuch-emacs#notmuch_init_file):
403
404         (require 'notmuch-address)
405         (setq notmuch-address-command "/path/to/address_fetching_program")
406         (notmuch-address-message-insinuate)
407
408 ### Google Contacts
409
410 [GooBook](http://code.google.com/p/goobook/) is a command-line tool for
411 accessing Google Contacts. Install and set it up according to its documentation.
412
413 To use GooBook with notmuch, use this wrapper script and set it up like the
414 programs above.
415
416         #!/bin/sh
417         goobook query "$*" | sed 's/\(.*\)\t\(.*\)\t.*/\2 \<\1\>/' | sed '/^$/d'
418
419 You can add the sender of a message to Google Contacts by piping the message
420 (`notmuch-show-pipe-message`) to `goobook add`.
421
422 ### Akonadi
423
424         git clone https://github.com/mmehnert/akonadimailsearch
425
426 Install the development packages for kdepim on your system.
427 Enter the cloned repository and create a build directory:
428
429         mkdir build
430         cd build
431         cmake ..; make;
432
433 You will find the akonadimailsearch binary in the build/src directory. Copy it to ~/bin .
434
435 You can now add the following settings to your
436 [notmuch init file](/notmuch-emacs#notmuch_init_file):
437
438         (require 'notmuch-address)
439         (setq notmuch-address-command "~/bin/akonadimailsearch")
440         (notmuch-address-message-insinuate)
441
442 ### Completion selection with helm
443
444 An address query might return multiple possible matches from which you
445 will have to select one. To ease this task, several different
446 frameworks in emacs support completion selection. One of them is
447 [helm](https://github.com/emacs-helm/helm). The following snippet
448 improves the out-of-the-box support for helm in notmuch as it enables
449 the required-match option and also does not ignore the first returned
450 address.
451
452         (setq notmuch-address-selection-function
453           (lambda (prompt collection initial-input)
454             (completing-read prompt (cons initial-input collection) nil t nil 'notmuch-address-history)))
455
456
457 ## How to sign/encrypt messages with gpg
458
459 Messages can be signed using gpg by invoking
460 `M-x mml-secure-sign-pgpmime` (or `M-x mml-secure-encrypt-pgpmime`).
461 These functions are available via the standard `message-mode` keybindings
462 `C-c C-m s p` and `C-c C-m c p`. To sign outgoing mail by default, use the
463 `message-setup-hook` in your `.emacs` file:
464
465         ;; Sign messages by default.
466         (add-hook 'message-setup-hook 'mml-secure-sign-pgpmime)
467         (setq mml-secure-openpgp-sign-with-sender t)
468
469 This inserts the required `<#part sign=pgpmime>` into the beginning
470 of the mail text body and will be converted into a pgp signature
471 when sending (so one can just manually delete that line if signing
472 is not required).
473
474 Alternatively, you may prefer to use `mml-secure-message-sign-pgpmime` instead
475 of `mml-secure-sign-pgpmime` to sign the whole message instead of just one
476 part.
477
478 If you want to automatically encrypt outgoing messages if the keyring
479 contains a public key for every recipient, you can add something like
480 that to your `.emacs` file:
481
482     (defun message-recipients ()
483       "Return a list of all recipients in the message, looking at TO, CC and BCC.
484
485     Each recipient is in the format of `mail-extract-address-components'."
486       (mapcan (lambda (header)
487                 (let ((header-value (message-fetch-field header)))
488                   (and
489                    header-value
490                    (mail-extract-address-components header-value t))))
491               '("To" "Cc" "Bcc")))
492
493     (defun message-all-epg-keys-available-p ()
494       "Return non-nil if the pgp keyring has a public key for each recipient."
495       (require 'epa)
496       (let ((context (epg-make-context epa-protocol)))
497         (catch 'break
498           (dolist (recipient (message-recipients))
499             (let ((recipient-email (cadr recipient)))
500               (when (and recipient-email (not (epg-list-keys context recipient-email)))
501                 (throw 'break nil))))
502           t)))
503
504     (defun message-sign-encrypt-if-all-keys-available ()
505       "Add MML tag to encrypt message when there is a key for each recipient.
506
507     Consider adding this function to `message-send-hook' to
508     systematically send encrypted emails when possible."
509       (when (message-all-epg-keys-available-p)
510         (mml-secure-message-sign-encrypt)))
511
512     (add-hook 'message-send-hook #'message-sign-encrypt-if-all-keys-available
513
514 ### Troubleshooting message-mode gpg support
515
516 - If you have trouble with expired subkeys, you may have encountered
517   emacs bug #7931.  This is fixed in git commit 301ea744c on
518   2011-02-02.  Note that if you have the Debian package easypg
519   installed, it will shadow the fixed version of easypg included with
520   emacs.
521
522 - If you wish `mml-secure-encrypt` to encrypt also for the sender, then
523   `M-x customize-variable mml2015-encrypt-to-self` might suit your need.
524
525 ## Reading and verifying encrypted and signed messages
526
527 Encrypted and signed mime messages can be read and verified with:
528
529         (setq notmuch-crypto-process-mime t)
530
531 Decrypting inline pgp messages can be done by selecting an the inline pgp area
532 and using:
533
534         M-x epa-decrypt-region RET
535
536 Verifying of inline pgp messages is not supported directly ([reasons
537 here](https://dkg.fifthhorseman.net/notes/inline-pgp-harmful/)). You can still
538 verify a part using
539
540         M-x notmuch-show-pipe-part RET gpg --verify RET
541
542 ## Multiple identities using gnus-alias
543
544 [gnus-alias](http://www.emacswiki.org/emacs/GnusAlias) allows you to
545 define multiple identities when using `message-mode`. You can specify
546 the from address, organization, extra headers (including *Bcc*), extra
547 body text, and signature for each identity. Identities are chosen
548 based on a set of rules. When you are in message mode, you can switch
549 identities using gnus-alias.
550
551 ### Installation
552
553 - put `gnus-alias.el` on your load Emacs-Lisp load path (add new directory
554   to load path by writing `(add-to-list 'load-path "/some/load/path")` into
555   your `.emacs`.
556
557 - Add the following to your `.emacs`
558
559         (autoload 'gnus-alias-determine-identity "gnus-alias" "" t)
560         (add-hook 'message-setup-hook 'gnus-alias-determine-identity)
561
562 Looking into `gnus-alias.el` gives a bit more information...
563
564 ### Example Configuration
565
566 Here is an example configuration.
567
568         ;; Define two identities, "home" and "work"
569         (setq gnus-alias-identity-alist
570               '(("home"
571                  nil ;; Does not refer to any other identity
572                  "John Doe <jdoe@example.net>" ;; Sender address
573                  nil ;; No organization header
574                  nil ;; No extra headers
575                  nil ;; No extra body text
576                  "~/.signature")
577                 ("work"
578                  nil
579                  "John Doe <john.doe@example.com>"
580                  "Example Corp."
581                  (("Bcc" . "john.doe@example.com"))
582                  nil
583                  "~/.signature.work")))
584         ;; Use "home" identity by default
585         (setq gnus-alias-default-identity "home")
586         ;; Define rules to match work identity
587         (setq gnus-alias-identity-rules
588               '(("work" ("any" "john.doe@\\(example\\.com\\|help\\.example.com\\)" both) "work")))
589         ;; Determine identity when message-mode loads
590         (add-hook 'message-setup-hook 'gnus-alias-determine-identity)
591
592 When `gnus-alias` has been loaded (using autoload, require, *M-x load-library*
593 or *M-x load-file* (load-file takes file path -- therefore it can be used
594 without any `.emacs` changes)) the following commands can be used to get(/set)
595 more information (some of these have "extensive documentation"):
596
597         M-x describe-variable RET gnus-alias-identity-alist
598         M-x describe-variable RET gnus-alias-identity-rules
599         M-x describe-variable RET gnus-alias-default-identity
600
601         M-x customize-group RET gnus-alias RET
602           or
603         M-x gnus-alias-customize RET
604
605 The last two do the same thing.
606
607 See also the **Usage:** section in `gnus-alias.el`.
608
609 ## Multiple identities (and more) with message-templ
610
611 Another option for multiple identities is
612 [message-templ](http://pivot.cs.unb.ca/git?p=message-templ.git;a=summary)
613 (also a available in marmalade).  This provides roughly the same
614 facilities as wanderlust's template facility.
615
616 See
617 [example.emacs.el](http://pivot.cs.unb.ca/git?p=message-templ.git;a=blob;f=example.emacs.el;hb=HEAD)
618 for some simple examples of usage.
619
620 ## Resending (or bouncing) messages
621
622 Add the following to your [notmuch init file](/notmuch-emacs#notmuch_init_file) to be able
623 to resend the current message in show mode.
624
625         (define-key notmuch-show-mode-map "b"
626           (lambda (&optional address)
627             "Bounce the current message."
628             (interactive "sBounce To: ")
629             (notmuch-show-view-raw-message)
630             (message-resend address)))
631
632 ## `notmuch-hello` refresh status message
633
634 Add the following to your [notmuch init file](/notmuch-emacs#notmuch_init_file) to get a
635 status message about the change in the number of messages in the mail store
636 when refreshing the `notmuch-hello` buffer.
637
638         (defvar notmuch-hello-refresh-count 0)
639
640         (defun notmuch-hello-refresh-status-message ()
641           (unless no-display
642             (let* ((new-count
643                     (string-to-number
644                      (car (process-lines notmuch-command "count"))))
645                    (diff-count (- new-count notmuch-hello-refresh-count)))
646               (cond
647                ((= notmuch-hello-refresh-count 0)
648                 (message "You have %s messages."
649                          (notmuch-hello-nice-number new-count)))
650                ((> diff-count 0)
651                 (message "You have %s more messages since last refresh."
652                          (notmuch-hello-nice-number diff-count)))
653                ((< diff-count 0)
654                 (message "You have %s fewer messages since last refresh."
655                          (notmuch-hello-nice-number (- diff-count)))))
656               (setq notmuch-hello-refresh-count new-count))))
657
658         (add-hook 'notmuch-hello-refresh-hook 'notmuch-hello-refresh-status-message)
659
660 ## Replacing tabs with spaces in subject and header
661
662 Mailman mailing list software rewrites and rewraps long message subjects in
663 a way that causes TABs to appear in the middle of the subject and header
664 lines. Add this to your [notmuch init file](/notmuch-emacs#notmuch_init_file) to replace
665 tabs with spaces in subject lines:
666
667         (defun notmuch-show-subject-tabs-to-spaces ()
668           "Replace tabs with spaces in subject line."
669           (goto-char (point-min))
670           (when (re-search-forward "^Subject:" nil t)
671             (while (re-search-forward "\t" (line-end-position) t)
672               (replace-match " " nil nil))))
673
674         (add-hook 'notmuch-show-markup-headers-hook 'notmuch-show-subject-tabs-to-spaces)
675
676 And in header lines (this will only work with the yet to be released
677 notmuch version 0.15):
678
679         (defun notmuch-show-header-tabs-to-spaces ()
680           "Replace tabs with spaces in header line."
681           (setq header-line-format
682                 (notmuch-show-strip-re
683                  (replace-regexp-in-string "\t" " " (notmuch-show-get-subject)))))
684
685         (add-hook 'notmuch-show-hook 'notmuch-show-header-tabs-to-spaces)
686
687 ## Hiding unread messages in notmuch-show
688
689 I like to have an inbox saved search, but only show unread messages when they
690 view a thread. This takes two steps:
691
692 1. Apply
693 [this patch from Mark Walters](https://notmuchmail.org/pipermail/notmuch/2012/010817.html)
694 to add the `notmuch-show-filter-thread` function.
695 1. Add the following hook to your emacs configuration:
696
697         (defun expand-only-unread-hook () (interactive)
698           (let ((unread nil)
699                 (open (notmuch-show-get-message-ids-for-open-messages)))
700             (notmuch-show-mapc (lambda ()
701                                  (when (member "unread" (notmuch-show-get-tags))
702                                    (setq unread t))))
703             (when unread
704               (let ((notmuch-show-hook (remove 'expand-only-unread-hook notmuch-show-hook)))
705                 (notmuch-show-filter-thread "tag:unread")))))
706
707         (add-hook 'notmuch-show-hook 'expand-only-unread-hook)
708
709 ## Changing the color of a saved search based on some other search
710
711 I like to have a saved search for my inbox, but have it change color when there
712 are thread with unread messages in the inbox. I accomplish this with the
713 following code in my emacs config:
714
715         (defun color-inbox-if-unread () (interactive)
716           (save-excursion
717             (goto-char (point-min))
718             (let ((cnt (car (process-lines "notmuch" "count" "tag:inbox and tag:unread"))))
719               (when (> (string-to-number cnt) 0)
720                 (save-excursion
721                   (when (search-forward "inbox" (point-max) t)
722                     (let* ((overlays (overlays-in (match-beginning 0) (match-end 0)))
723                            (overlay (car overlays)))
724                       (when overlay
725                         (overlay-put overlay 'face '((:inherit bold) (:foreground "green")))))))))))
726         (add-hook 'notmuch-hello-refresh-hook 'color-inbox-if-unread)
727
728 ## Linking to notmuch messages and threads from the Circe IRC client
729
730 [Circe](https://github.com/jorgenschaefer/circe/wiki) is an IRC client for emacs.
731 To have clickable buttons for notmuch messages and threads, add the following to
732 `lui-buttons-list` (using, e.g. M-x customize-variable)
733
734     ("\\(?:id\\|mid\\|thread\\):[0-9A-Za-z][0-9A-Za-z.@-]*" 0 notmuch-show 0)
735
736 If you have notmuch-pick installed, it works fine for this as well.
737
738 ## Linking to notmuch messages from org-mode
739
740 Support for linking to notmuch messages is distributed with org-mode,
741 but as a contrib file, so you might have to work a bit to load it.
742
743 In Debian and derivatives,
744
745     (add-to-list 'load-path "/usr/share/org-mode/lisp")
746
747 In NixOS, using `emacsWithPackages (epkgs: [ epkgs.orgPackages.org-plus-contrib ])`,
748
749     (loop for p in load-path
750           do (if (file-accessible-directory-p p)
751                  (let ((m (directory-files-recursively p "^org-notmuch.el$")))
752                       (if m (add-to-list 'load-path (file-name-directory (car m)))))))
753
754 Then
755
756     (require 'org-notmuch)
757
758 In general it is nice to have a key for org-links (not just for notmuch). For example
759
760     (define-key global-map "\C-cl" 'org-store-link)
761
762 Note the package was renamed from `org-notmuch` to `ol-notmuch` in recent
763 versions of org-mode, and you might want to `(require 'ol-notmuch)` instead.
764
765 ## Viewing diffs in notmuch
766
767 The following code allows you to view an inline patch in diff-mode
768 directly from notmuch. This means that normal diff-mode commands like
769 refine, next hunk etc all work.
770
771     (defun my-notmuch-show-view-as-patch ()
772       "View the the current message as a patch."
773       (interactive)
774       (let* ((id (notmuch-show-get-message-id))
775              (msg (notmuch-show-get-message-properties))
776              (part (notmuch-show-get-part-properties))
777              (subject (concat "Subject: " (notmuch-show-get-subject) "\n"))
778              (diff-default-read-only t)
779              (buf (get-buffer-create (concat "*notmuch-patch-" id "*")))
780              (map (make-sparse-keymap)))
781         (define-key map "q" 'notmuch-bury-or-kill-this-buffer)
782         (switch-to-buffer buf)
783         (let ((inhibit-read-only t))
784           (erase-buffer)
785           (insert subject)
786           (insert (notmuch-get-bodypart-text msg part nil)))
787         (set-buffer-modified-p nil)
788         (diff-mode)
789         (lexical-let ((new-ro-bind (cons 'buffer-read-only map)))
790                      (add-to-list 'minor-mode-overriding-map-alist new-ro-bind))
791         (goto-char (point-min))))
792
793 and then this function needs to bound to `. d` in the keymap
794
795     (define-key 'notmuch-show-part-map "d" 'my-notmuch-show-view-as-patch)
796
797 ## Interfacing with Patchwork
798
799 [Patchwork](http://jk.ozlabs.org/projects/patchwork/) is a web-based system for
800 tracking patches sent to a mailing list. While the Notmuch project doesn't use
801 it, many other open source projects do. Having an easy way to get from a patch
802 email in your favorite mail client to the web page of the patch in the Patchwork
803 instance is a cool thing to have. Here's how to abuse the notmuch stash feature
804 to achieve this. (Don't know stash? See `notmuch-show-stash-mlarchive-link`,
805 bound to `c l` in `notmuch-show`.)
806
807 The trick needed is turning the email Message-ID into a unique Patchwork ID
808 assigned by Patchwork. We'll use the `pwclient` command-line tool to achieve
809 this. You'll first need to get that working and configured for the Patchwork
810 instance you're using. That part is beyond this tip here; please refer to
811 Patchwork documentation.
812
813 Check your configuration on the command-line, for example:
814
815     /path/to/pwclient -p <the-project> -n 5 -f "%{id}"
816
817 Note that the -f format argument may require a reasonably new version of the
818 client. Once you have the above working, you can `M-x customize-variable RET
819 notmuch-show-stash-mlarchive-link-alist RET`.
820
821 Add a new entry with "Function returning the URL:" set to:
822
823     (lambda (message-id)
824       (concat "http://patchwork.example.com/patch/"
825               (nth 0
826                    (process-lines "/path/to/pwclient" "search"
827                                   "-p" "the-project"
828                                   "-m" (concat "<" message-id ">")
829                                   "-n" "1"
830                                   "-f" "%{id}"))))
831
832 Replacing `http://patchwork.example.com/patch/`, `/path/to/pwclient`, and
833 `the-project` appropriately. You should now be able to stash the Patchwork URL
834 using `c l`.
835
836 Going further, if the patch has been committed, you can get the commit hash with
837 this:
838
839     (lambda (message-id)
840       (nth 0
841            (process-lines "/path/to/pwclient" "search"
842                           "-p" "the-project"
843                           "-m" (concat "<" message-id ">")
844                           "-n" "1"
845                           "-f" "%{commit_ref}")))
846
847 And finally, if the project has a web interface to its source repository, you
848 can turn the commit hash into a URL pointing there, for example:
849
850     (lambda (message-id)
851       (concat "http://cgit.example.com/the-project/commit/?id="
852               (nth 0
853                    (process-lines "/path/to/pwclient" "search"
854                                   "-p" "the-project"
855                                   "-m" (concat "<" message-id ">")
856                                   "-n" "1"
857                                   "-f" "%{commit_ref}"))))
858
859 ## Never forget attachments
860
861 Very often we forget to actually attach the file when we send an email
862 that's supposed to have an attachment. Did this never happen to you?
863 If not, then it will.
864
865 There is a hook out there that checks the content of the email for
866 keywords and warns you before the email is sent out if there's no
867 attachment. This is currently work in progress, but you can already
868 add the hook to your `~/.emacs.d/notmuch-config.el` file to test
869 it. Details available (and feedback welcome) in the [relevant
870 discussion](https://notmuchmail.org/pipermail/notmuch/2018/026414.html).
871
872 ## Applying patches to git repositories
873
874 The `notmuch-extract-thread-patches` and
875 `notmuch-extract-message-patches` commands from the `elpa-mailscripts`
876 package in Debian (and its derivatives) can do this for you.