]> git.cworth.org Git - notmuch-wiki/blob - emacstips.mdwn
Add smtpmail
[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 attachements
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-search-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 And 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
165 ## How to do FCC/BCC...
166
167 The Emacs interface to notmuch will automatically add an `Fcc`
168 header to your outgoing mail so that any messages you send will also
169 be saved in your mail store. You can control where this copy of the
170 message is saved by setting the variable `notmuch-fcc-dirs` which defines the
171 subdirectory relative to the `database.path` setting from your
172 notmuch configuration in which to save the mail. Enter a directory
173 (without the maildir `/cur` ending which will be appended
174 automatically). Additional information can be found as usual using:
175
176        M-x describe-variable notmuch-fcc-dirs
177
178 An additional variable that can affect FCC settings in some cases is
179 `message-directory`. Emacs message-mode uses this variable for
180 postponed messages.
181
182 To customize both variables at the same time, use the fancy command:
183
184         M-x customize-apropos<RET>\(notmuch-fcc-dirs\)\|\(message-directory\)
185
186 This mechanism also allows you to select different folders to be
187 used for the outgoing mail depending on your selected `From`
188 address. Please see the documentation for the variable
189 `notmuch-fcc-dirs` in the customization window for how to arrange
190 this.
191
192 ## How to customize `notmuch-saved-searches`
193
194 When starting notmuch, a list of saved searches and message counts is
195 displayed, replacing the older `notmuch-folders` command. The set of
196 saved searches displayed can be modified directly from the notmuch
197 interface (using the `[save]` button next to a previous search) or by
198 customising the variable `notmuch-saved-searches`.
199
200 An example setting for notmuch versions up to 0.17.x might be:
201
202         (setq notmuch-saved-searches '(("inbox" . "tag:inbox")
203                         ("unread" . "tag:inbox AND tag:unread")
204                         ("notmuch" . "tag:inbox AND to:notmuchmail.org")))
205
206 Starting from notmuch 0.18 the variable changed. It is backwards
207 compatible so the above will still work but the new style will be used
208 if you use customize and there are some new features available. The above would become
209
210         (setq notmuch-saved-searches '((:name "inbox" :query "tag:inbox")
211                         (:name "unread" :query "tag:inbox AND tag:unread")
212                         (:name "notmuch" :query "tag:inbox AND to:notmuchmail.org")))
213
214 The additional features are the possibility to set the search order
215 for the search, and the possibility to specify a different query for
216 displaying the count for the saved-search. For example
217
218         (setq notmuch-saved-searches '((:name "inbox"
219                                         :query "tag:inbox"
220                                         :count-query "tag:inbox and tag:unread"
221                                         :sort-order oldest-first)))
222
223 specifies a single saved search for inbox, but the number displayed by
224 the search will be the number of unread messages in the inbox, and the
225 sort order for this search will be oldest-first.
226
227 Of course, you can have any number of saved searches, each configured
228 with any supported search terms (see "notmuch help search-terms"), and
229 in the new style variable they can each have different count-queries
230 and sort orders.
231
232 Some users find it useful to add `and not tag:delete` to those
233 searches, as they use the `delete` tag to mark messages as
234 deleted. This causes messages that are marked as deleted to be removed
235 from the commonly used views of messages.  Use whatever seems most
236 useful to you.
237
238 ## Viewing HTML messages with an external viewer
239
240 The Emacs client can generally display HTML messages inline using one of the
241 supported HTML renderers. This is controlled by the `mm-text-html-renderer`
242 variable.
243
244 Sometimes it may be necessary to display the message, or a single MIME part, in
245 an external browser. This can be done by `(notmuch-show-view-part)`, bound to
246 `. v` by default.
247
248 ## msmtp, message mode and multiple accounts
249
250 As an alternative to running a mail server such as sendmail or postfix
251 just to send email, it is possible to use
252 [msmtp](http://msmtp.sourceforge.net/). This small application will
253 look like `/usr/bin/sendmail` to a MUA such as emacs message mode, but
254 will just forward the email to an external SMTP server. It's fairly
255 easy to set up and it supports several accounts for using different
256 SMTP servers. The msmtp pages have several examples.
257
258 A typical scenario is that you want to use the company SMTP server
259 for email coming from your company email address, and your personal
260 server for personal email.  If msmtp is passed the envelope address
261 on the command line (the -f/--from option) it will automatically
262 pick the matching account.  The only trick here seems to be getting
263 emacs to actually pass the envelope from.  There are a number of
264 overlapping configuration variables that control this, and it's a
265 little confusion, but setting these three works for me:
266
267  - `mail-specify-envelope-from`: `t`
268
269  - `message-sendmail-envelope-from`: `header`
270
271  - `mail-envelope-from`: `header`
272
273 With that in place, you need a `.msmtprc` with the accounts configured
274 for the domains you want to send out using specific SMTP servers and
275 the rest will go to the default account.
276
277 ## sending mail using smtpmail
278
279 another option is to use remov
280
281 <!-- By default message mode will use the system `sendmail` command to send
282 mail. However, on a typical desktop machine there may not be local SMTP
283 daemon running (nor it is configured to send mail outside of the system). -->
284
285 If setting up local `sendmail` or `msmtp` is not feasible or desirable,
286 the Emacs `smtpmail` package can be used to send email by talking to remote
287 SMTP server via TCP connection. It is pretty easy to configure:
288
289 1. `M-x customize-group smtpmail`
290
291    as as minimum, 'Smtpmail Smtp Server' needs to be set.
292
293    - then continue with `M-x load-library message` and
294      `M-x customize-variable message-send-mail-function`
295      -- choose `message-smtpmail-send-it` for that.
296
297 1. Set some variables in .emacs or in [notmuch init file](/notmuch-emacs#notmuch_init_file)
298
299       (setq smtpmail-smtp-server "smtp.server.tld" ;; <-- edit this !!!
300       ;; smtpmail-smtp-service 25 ;; 25 is default -- uncomment and edit if needed
301       ;; smtpmail-stream-type 'starttls
302       ;; smtpmail-debug-info t
303       ;; smtpmail-debug-verb t
304          message-send-mail-function 'message-smtpmail-send-it)
305
306 More information for smtpmail is available:
307
308 * In Emacs: `M-x info-display-manual smtpmail`
309 * At http://www.emacswiki.org/emacs/SendingMail
310
311
312 ## <span id="address_completion">Address completion when composing</span>
313
314 There are currently three solutions to this:
315
316 ### bbdb
317
318 [bbdb](http://bbdb.sourceforge.net) is a contact database for emacs
319 that works quite nicely together with message mode, including
320 address autocompletion.
321
322 ### notmuch database as an address book
323
324 You can also use the notmuch database as a mail address book itself.
325 To do this you need a command line tool that outputs likely address
326 candidates based on a search string.  There are currently four
327 available:
328
329   * The python tool `notmuch_address.py` (`git clone
330     http://commonmeasure.org/~jkr/git/notmuch_addresses.git`) (slower, but
331     no compilation required so good for testing the setup)
332
333   * 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.
334
335         git clone https://github.com/aperezdc/notmuch-addrlookup-c
336         cd notmuch-addrlookup-c
337         make
338
339   * The vala-based
340     [addrlookup](http://github.com/spaetz/vala-notmuch) The addrlookup binary needs to be compiled.
341     Grab
342     `http://github.com/spaetz/vala-notmuch/raw/static-sources/src/addrlookup.c`
343     and build it with:
344
345             cc -o addrlookup addrlookup.c `pkg-config --cflags --libs gobject-2.0` -lnotmuch
346
347   * Shell/fgrep/perl combination [nottoomuch-addresses.sh](https://github.com/domo141/nottoomuch/blob/master/nottoomuch-addresses.rst).
348     This tools maintains it's own address "database" gathered from email
349     files notmuch knows and search from that "database" is done by `fgrep(1)`.
350
351   * python/sqlite combination [notmuch-abook](https://github.com/guyzmo/notmuch-abook/)
352     This tools also maintains an address database in sqlite after harvesting
353     from notmuch.  It also includes a vim plugin.
354
355 You can perform tab-completion using any of these programs.
356 Just add the following to your [notmuch init file](/notmuch-emacs#notmuch_init_file):
357
358         (require 'notmuch-address)
359         (setq notmuch-address-command "/path/to/address_fetching_program")
360         (notmuch-address-message-insinuate)
361
362 ### Google Contacts
363
364 [GooBook](http://code.google.com/p/goobook/) is a command-line tool for
365 accessing Google Contacts. Install and set it up according to its documentation.
366
367 To use GooBook with notmuch, use this wrapper script and set it up like the
368 programs above.
369
370         #!/bin/sh
371         goobook query "$*" | sed 's/\(.*\)\t\(.*\)\t.*/\2 \<\1\>/' | sed '/^$/d'
372
373 You can add the sender of a message to Google Contacts by piping the message
374 (`notmuch-show-pipe-message`) to `goobook add`.
375
376 ### Akonadi
377
378         git clone https://github.com/mmehnert/akonadimailsearch
379
380 Install the development packages for kdepim on your system.
381 Enter the cloned repository and create a build directory:
382
383         mkdir build
384         cd build
385         cmake ..; make;
386
387 You will find the akonadimailsearch binary in the build/src directory. Copy it to ~/bin .
388
389 You can now add the following settings to your
390 [notmuch init file](/notmuch-emacs#notmuch_init_file):
391
392         (require 'notmuch-address)
393         (setq notmuch-address-command "~/bin/akonadimailsearch")
394         (notmuch-address-message-insinuate)
395
396 ### Completion selection with helm
397
398 An address query might return multiple possible matches from which you
399 will have to select one. To ease this task, several different
400 frameworks in emacs support completion selection. One of them is
401 [helm](https://github.com/emacs-helm/helm). The following snippet
402 improves the out-of-the-box support for helm in notmuch as it enables
403 the required-match option and also does not ignore the first returned
404 address.
405
406         (setq notmuch-address-selection-function
407           (lambda (prompt collection initial-input)
408             (completing-read prompt (cons initial-input collection) nil t nil 'notmuch-address-history)))
409
410
411 ## How to sign/encrypt messages with gpg
412
413 Messages can by signed using gpg by invoking
414 `M-x mml-secure-sign-pgpmime` (or `M-x mml-secure-encrypt-pgpmime`).
415 These functions are available via the standard `message-mode` keybindings
416 `C-c C-m s p` and `C-c C-m c p`. To sign outgoing mail by default, use the
417 `message-setup-hook` in your `.emacs` file:
418
419         ;; Sign messages by default.
420         (add-hook 'message-setup-hook 'mml-secure-sign-pgpmime)
421
422 This inserts the required `<#part sign=pgpmime>` into the beginning
423 of the mail text body and will be converted into a pgp signature
424 when sending (so one can just manually delete that line if signing
425 is not required).
426
427 Alternatively, you may prefer to use `mml-secure-message-sign-pgpmime` instead
428 of `mml-secure-sign-pgpmime` to sign the whole message instead of just one
429 part.
430
431 ### Troubleshooting message-mode gpg support
432
433 - If you have trouble with expired subkeys, you may have encountered
434   emacs bug #7931.  This is fixed in git commit 301ea744c on
435   2011-02-02.  Note that if you have the Debian package easypg
436   installed, it will shadow the fixed version of easypg included with
437   emacs.
438
439 - If you wish `mml-secure-encrypt` to encrypt also for the sender, then
440   `M-x customize-variable mml2015-encrypt-to-self` might suit your need.
441
442 ## Reading and verifying encrypted and signed messages
443
444 Encrypted and signed mime messages can be read and verified with:
445
446         (notmuch-crypto-process-mime t)
447
448 Decrypting or verifying inline pgp messages can be done by selecting
449 an the inline pgp area and and using:
450
451         M-x epa-decrypt-region RET
452
453 ## Multiple identities using gnus-alias
454
455 [gnus-alias](http://www.emacswiki.org/emacs/GnusAlias) allows you to
456 define multiple identities when using `message-mode`. You can specify
457 the from address, organization, extra headers (including *Bcc*), extra
458 body text, and signature for each identity. Identities are chosen
459 based on a set of rules. When you are in message mode, you can switch
460 identities using gnus-alias.
461
462 ### Installation
463
464 - put `gnus-alias.el` on your load Emacs-Lisp load path (add new directory
465   to load path by writing `(add-to-list 'load-path "/some/load/path")` into
466   your `.emacs`.
467
468 - Add the following to your `.emacs`
469
470         (autoload 'gnus-alias-determine-identity "gnus-alias" "" t)
471         (add-hook 'message-setup-hook 'gnus-alias-determine-identity)
472
473 Looking into `gnus-alias.el` gives a bit more information...
474
475 ### Example Configuration
476
477 Here is an example configuration.
478
479         ;; Define two identities, "home" and "work"
480         (setq gnus-alias-identity-alist
481               '(("home"
482                  nil ;; Does not refer to any other identity
483                  "John Doe <jdoe@example.net>" ;; Sender address
484                  nil ;; No organization header
485                  nil ;; No extra headers
486                  nil ;; No extra body text
487                  "~/.signature")
488                 ("work"
489                  nil
490                  "John Doe <john.doe@example.com>"
491                  "Example Corp."
492                  (("Bcc" . "john.doe@example.com"))
493                  nil
494                  "~/.signature.work")))
495         ;; Use "home" identity by default
496         (setq gnus-alias-default-identity "home")
497         ;; Define rules to match work identity
498         (setq gnus-alias-identity-rules)
499               '(("work" ("any" "john.doe@\\(example\\.com\\|help\\.example.com\\)" both) "work"))
500         ;; Determine identity when message-mode loads
501         (add-hook 'message-setup-hook 'gnus-alias-determine-identity)
502
503 When `gnus-alias` has been loaded (using autoload, require, *M-x load-library*
504 or *M-x load-file* (load-file takes file path -- therefore it can be used
505 without any `.emacs` changes)) the following commands can be used to get(/set)
506 more information (some of these have "extensive documentation"):
507
508         M-x describe-variable RET gnus-alias-identity-alist
509         M-x describe-variable RET gnus-alias-identity-rules
510         M-x describe-variable RET gnus-alias-default-identity
511
512         M-x customize-group RET gnus-alias RET
513           or
514         M-x gnus-alias-customize RET
515
516 The last two do the same thing.
517
518 See also the **Usage:** section in `gnus-alias.el`.
519
520 ## Resending (or bouncing) messages
521
522 Add the following to your [notmuch init file](/notmuch-emacs#notmuch_init_file) to be able
523 to resend the current message in show mode.
524
525         (define-key notmuch-show-mode-map "b"
526           (lambda (&optional address)
527             "Bounce the current message."
528             (interactive "sBounce To: ")
529             (notmuch-show-view-raw-message)
530             (message-resend address)))
531
532 ## `notmuch-hello` refresh status message
533
534 Add the following to your [notmuch init file](/notmuch-emacs#notmuch_init_file) to get a
535 status message about the change in the number of messages in the mail store
536 when refreshing the `notmuch-hello` buffer.
537
538         (defvar notmuch-hello-refresh-count 0)
539
540         (defun notmuch-hello-refresh-status-message ()
541           (unless no-display
542             (let* ((new-count
543                     (string-to-number
544                      (car (process-lines notmuch-command "count"))))
545                    (diff-count (- new-count notmuch-hello-refresh-count)))
546               (cond
547                ((= notmuch-hello-refresh-count 0)
548                 (message "You have %s messages."
549                          (notmuch-hello-nice-number new-count)))
550                ((> diff-count 0)
551                 (message "You have %s more messages since last refresh."
552                          (notmuch-hello-nice-number diff-count)))
553                ((< diff-count 0)
554                 (message "You have %s fewer messages since last refresh."
555                          (notmuch-hello-nice-number (- diff-count)))))
556               (setq notmuch-hello-refresh-count new-count))))
557
558         (add-hook 'notmuch-hello-refresh-hook 'notmuch-hello-refresh-status-message)
559
560 ## Replacing tabs with spaces in subject and header
561
562 Mailman mailing list software rewrites and rewraps long message subjects in
563 a way that causes TABs to appear in the middle of the subject and header
564 lines. Add this to your [notmuch init file](/notmuch-emacs#notmuch_init_file) to replace
565 tabs with spaces in subject lines:
566
567         (defun notmuch-show-subject-tabs-to-spaces ()
568           "Replace tabs with spaces in subject line."
569           (goto-char (point-min))
570           (when (re-search-forward "^Subject:" nil t)
571             (while (re-search-forward "\t" (line-end-position) t)
572               (replace-match " " nil nil))))
573
574         (add-hook 'notmuch-show-markup-headers-hook 'notmuch-show-subject-tabs-to-spaces)
575
576 And in header lines (this will only work with the yet to be released
577 notmuch version 0.15):
578
579         (defun notmuch-show-header-tabs-to-spaces ()
580           "Replace tabs with spaces in header line."
581           (setq header-line-format
582                 (notmuch-show-strip-re
583                  (replace-regexp-in-string "\t" " " (notmuch-show-get-subject)))))
584
585         (add-hook 'notmuch-show-hook 'notmuch-show-header-tabs-to-spaces)
586
587 ## Hiding unread messages in notmuch-show
588
589 I like to have an inbox saved search, but only show unread messages when they
590 view a thread. This takes two steps:
591
592 1. Apply
593 [this patch from Mark Walters](http://notmuchmail.org/pipermail/notmuch/2012/010817.html)
594 to add the `notmuch-show-filter-thread` function.
595 1. Add the following hook to your emacs configuration:
596
597         (defun expand-only-unread-hook () (interactive)
598           (let ((unread nil)
599                 (open (notmuch-show-get-message-ids-for-open-messages)))
600             (notmuch-show-mapc (lambda ()
601                                  (when (member "unread" (notmuch-show-get-tags))
602                                    (setq unread t))))
603             (when unread
604               (let ((notmuch-show-hook (remove 'expand-only-unread-hook notmuch-show-hook)))
605                 (notmuch-show-filter-thread "tag:unread")))))
606
607         (add-hook 'notmuch-show-hook 'expand-only-unread-hook)
608
609 ## Changing the color of a saved search based on some other search
610
611 I like to have a saved search for my inbox, but have it change color when there
612 are thread with unread messages in the inbox. I accomplish this with the
613 following code in my emacs config:
614
615         (defun color-inbox-if-unread () (interactive)
616           (save-excursion
617             (goto-char (point-min))
618             (let ((cnt (car (process-lines "notmuch" "count" "tag:inbox and tag:unread"))))
619               (when (> (string-to-number cnt) 0)
620                 (save-excursion
621                   (when (search-forward "inbox" (point-max) t)
622                     (let* ((overlays (overlays-in (match-beginning 0) (match-end 0)))
623                            (overlay (car overlays)))
624                       (when overlay
625                         (overlay-put overlay 'face '((:inherit bold) (:foreground "green")))))))))))
626         (add-hook 'notmuch-hello-refresh-hook 'color-inbox-if-unread)
627
628 ## Linking to notmuch messages and threads from the Circe IRC client
629
630 [Circe](https://github.com/jorgenschaefer/circe/wiki) is an IRC client for emacs.
631 To have clickable buttons for notmuch messages and threads, add the following to
632 `lui-buttons-list` (using, e.g. M-x customize-variable)
633
634     ("\\(?:id\\|mid\\|thread\\):[0-9A-Za-z][0-9A-Za-z.@-]*" 0 notmuch-show 0)
635
636 If you have notmuch-pick installed, it works fine for this as well.
637
638 ## Linking to notmuch messages from org-mode
639
640 Support for linking to notmuch messages is distributed with org-mode,
641 but as a contrib file, so you might have to work a bit to load it.
642
643 In Debian and derivatives,
644
645     (add-to-list 'load-path "/usr/share/org-mode/lisp")
646
647 Then
648
649     (require 'org-notmuch)
650
651 In general it is nice to have a key for org-links (not just for notmuch). For example
652
653     (define-key global-map "\C-cl" 'org-store-link)
654
655 ## Viewing diffs in notmuch
656
657 The following code allows you to view an inline patch in diff-mode
658 directly from notmuch. This means that normal diff-mode commands like
659 refine, next hunk etc all work.
660
661     (defun my-notmuch-show-view-as-patch ()
662       "View the the current message as a patch."
663       (interactive)
664       (let* ((id (notmuch-show-get-message-id))
665              (subject (concat "Subject: " (notmuch-show-get-subject) "\n"))
666              (diff-default-read-only t)
667              (buf (get-buffer-create (concat "*notmuch-patch-" id "*")))
668              (map (make-sparse-keymap)))
669         (define-key map "q" 'notmuch-kill-this-buffer)
670         (switch-to-buffer buf)
671         (let ((inhibit-read-only t))
672           (erase-buffer)
673           (insert subject)
674           (insert (notmuch-get-bodypart-internal id 1 nil)))
675         (set-buffer-modified-p nil)
676         (diff-mode)
677         (lexical-let ((new-ro-bind (cons 'buffer-read-only map)))
678                      (add-to-list 'minor-mode-overriding-map-alist new-ro-bind))
679         (goto-char (point-min))))
680
681 and then this function needs to bound into the keymap with something like
682
683     (define-key 'notmuch-show-mode-map "D" 'my-notmuch-show-view-as-patch)
684
685 ## Interfacing with Patchwork
686
687 [Patchwork](http://jk.ozlabs.org/projects/patchwork/) is a web-based system for
688 tracking patches sent to a mailing list. While the Notmuch project doesn't use
689 it, many other open source projects do. Having an easy way to get from a patch
690 email in your favorite mail client to the web page of the patch in the Patchwork
691 instance is a cool thing to have. Here's how to abuse the notmuch stash feature
692 to achieve this. (Don't know stash? See `notmuch-show-stash-mlarchive-link`,
693 bound to `c l` in `notmuch-show`.)
694
695 The trick needed is turning the email Message-ID into a unique Patchwork ID
696 assigned by Patchwork. We'll use the `pwclient` command-line tool to achieve
697 this. You'll first need to get that working and configured for the Patchwork
698 instance you're using. That part is beyond this tip here; please refer to
699 Patchwork documentation.
700
701 Check your configuration on the command-line, for example:
702
703     /path/to/pwclient -p <the-project> -n 5 -f "%{id}"
704
705 Note that the -f format argument may require a reasonably new version of the
706 client. Once you have the above working, you can `M-x customize-variable RET
707 notmuch-show-stash-mlarchive-link-alist RET`.
708
709 Add a new entry with "Function returning the URL:" set to:
710
711     (lambda (message-id)
712       (concat "http://patchwork.example.com/patch/"
713               (nth 0
714                    (process-lines "/path/to/pwclient" "search"
715                                   "-p" "the-project"
716                                   "-m" (concat "<" message-id ">")
717                                   "-n" "1"
718                                   "-f" "%{id}"))))
719
720 Replacing `http://patchwork.example.com/patch/`, `/path/to/pwclient`, and
721 `the-project` appropiately. You should now be able to stash the Patchwork URL
722 using `c l`.
723
724 Going further, if the patch has been committed, you can get the commit hash with
725 this:
726
727     (lambda (message-id)
728       (nth 0
729            (process-lines "/path/to/pwclient" "search"
730                           "-p" "the-project"
731                           "-m" (concat "<" message-id ">")
732                           "-n" "1"
733                           "-f" "%{commit_ref}")))
734
735 And finally, if the project has a web interface to its source repository, you
736 can turn the commit hash into a URL pointing there, for example:
737
738     (lambda (message-id)
739       (concat "http://cgit.example.com/the-project/commit/?id="
740               (nth 0
741                    (process-lines "/path/to/pwclient" "search"
742                                   "-p" "the-project"
743                                   "-m" (concat "<" message-id ">")
744                                   "-n" "1"
745                                   "-f" "%{commit_ref}"))))