+
+- If you wish `mml-secure-encrypt` to encrypt also for the sender, then
+ `M-x customize-variable mml2015-encrypt-to-self` might suit your need.
+
+## Reading and verifying encrypted and signed messages
+
+Encrypted and signed mime messages can be read and verified with:
+
+ (setq notmuch-crypto-process-mime t)
+
+Decrypting inline pgp messages can be done by selecting an the inline pgp area
+and using:
+
+ M-x epa-decrypt-region RET
+
+Verifying of inline pgp messages is not supported directly ([reasons
+here](https://dkg.fifthhorseman.net/notes/inline-pgp-harmful/)). You can still
+verify a part using
+
+ M-x notmuch-show-pipe-part RET gpg --verify RET
+
+## Multiple identities using gnus-alias
+
+[gnus-alias](http://www.emacswiki.org/emacs/GnusAlias) allows you to
+define multiple identities when using `message-mode`. You can specify
+the from address, organization, extra headers (including *Bcc*), extra
+body text, and signature for each identity. Identities are chosen
+based on a set of rules. When you are in message mode, you can switch
+identities using gnus-alias.
+
+### Installation
+
+- put `gnus-alias.el` on your load Emacs-Lisp load path (add new directory
+ to load path by writing `(add-to-list 'load-path "/some/load/path")` into
+ your `.emacs`.
+
+- Add the following to your `.emacs`
+
+ (autoload 'gnus-alias-determine-identity "gnus-alias" "" t)
+ (add-hook 'message-setup-hook 'gnus-alias-determine-identity)
+
+Looking into `gnus-alias.el` gives a bit more information...
+
+### Example Configuration
+
+Here is an example configuration.
+
+ ;; Define two identities, "home" and "work"
+ (setq gnus-alias-identity-alist
+ '(("home"
+ nil ;; Does not refer to any other identity
+ "John Doe <jdoe@example.net>" ;; Sender address
+ nil ;; No organization header
+ nil ;; No extra headers
+ nil ;; No extra body text
+ "~/.signature")
+ ("work"
+ nil
+ "John Doe <john.doe@example.com>"
+ "Example Corp."
+ (("Bcc" . "john.doe@example.com"))
+ nil
+ "~/.signature.work")))
+ ;; Use "home" identity by default
+ (setq gnus-alias-default-identity "home")
+ ;; Define rules to match work identity
+ (setq gnus-alias-identity-rules
+ '(("work" ("any" "john.doe@\\(example\\.com\\|help\\.example.com\\)" both) "work")))
+ ;; Determine identity when message-mode loads
+ (add-hook 'message-setup-hook 'gnus-alias-determine-identity)
+
+When `gnus-alias` has been loaded (using autoload, require, *M-x load-library*
+or *M-x load-file* (load-file takes file path -- therefore it can be used
+without any `.emacs` changes)) the following commands can be used to get(/set)
+more information (some of these have "extensive documentation"):
+
+ M-x describe-variable RET gnus-alias-identity-alist
+ M-x describe-variable RET gnus-alias-identity-rules
+ M-x describe-variable RET gnus-alias-default-identity
+
+ M-x customize-group RET gnus-alias RET
+ or
+ M-x gnus-alias-customize RET
+
+The last two do the same thing.
+
+See also the **Usage:** section in `gnus-alias.el`.
+
+## Multiple identities (and more) with message-templ
+
+Another option for multiple identities is
+[message-templ](http://git.tethera.net/message-templ.git)
+(also a available in marmalade). This provides roughly the same
+facilities as wanderlust's template facility.
+
+See
+[example.emacs.el](https://git.tethera.net/message-templ.git/tree/example.emacs.el)
+for some simple examples of usage.
+
+## Resending (or bouncing) messages
+
+Add the following to your [notmuch init file](/notmuch-emacs#notmuch_init_file) to be able
+to resend the current message in show mode.
+
+ (define-key notmuch-show-mode-map "b"
+ (lambda (&optional address)
+ "Bounce the current message."
+ (interactive "sBounce To: ")
+ (notmuch-show-view-raw-message)
+ (message-resend address)))
+
+## `notmuch-hello` refresh status message
+
+Add the following to your [notmuch init file](/notmuch-emacs#notmuch_init_file) to get a
+status message about the change in the number of messages in the mail store
+when refreshing the `notmuch-hello` buffer.
+
+ (defvar notmuch-hello-refresh-count 0)
+
+ (defun notmuch-hello-refresh-status-message ()
+ (unless no-display
+ (let* ((new-count
+ (string-to-number
+ (car (process-lines notmuch-command "count"))))
+ (diff-count (- new-count notmuch-hello-refresh-count)))
+ (cond
+ ((= notmuch-hello-refresh-count 0)
+ (message "You have %s messages."
+ (notmuch-hello-nice-number new-count)))
+ ((> diff-count 0)
+ (message "You have %s more messages since last refresh."
+ (notmuch-hello-nice-number diff-count)))
+ ((< diff-count 0)
+ (message "You have %s fewer messages since last refresh."
+ (notmuch-hello-nice-number (- diff-count)))))
+ (setq notmuch-hello-refresh-count new-count))))
+
+ (add-hook 'notmuch-hello-refresh-hook 'notmuch-hello-refresh-status-message)
+
+## Replacing tabs with spaces in subject and header
+
+Mailman mailing list software rewrites and rewraps long message subjects in
+a way that causes TABs to appear in the middle of the subject and header
+lines. Add this to your [notmuch init file](/notmuch-emacs#notmuch_init_file) to replace
+tabs with spaces in subject lines:
+
+ (defun notmuch-show-subject-tabs-to-spaces ()
+ "Replace tabs with spaces in subject line."
+ (goto-char (point-min))
+ (when (re-search-forward "^Subject:" nil t)
+ (while (re-search-forward "\t" (line-end-position) t)
+ (replace-match " " nil nil))))
+
+ (add-hook 'notmuch-show-markup-headers-hook 'notmuch-show-subject-tabs-to-spaces)
+
+And in header lines:
+
+ (defun notmuch-show-header-tabs-to-spaces ()
+ "Replace tabs with spaces in header line."
+ (setq header-line-format
+ (notmuch-show-strip-re
+ (replace-regexp-in-string "\t" " " (notmuch-show-get-subject)))))
+
+ (add-hook 'notmuch-show-hook 'notmuch-show-header-tabs-to-spaces)
+
+## Hiding unread messages in notmuch-show
+
+I like to have an inbox saved search, but only show unread messages when they
+view a thread. This takes two steps:
+
+1. Apply
+[this patch from Mark Walters](https://notmuchmail.org/pipermail/notmuch/2012/010817.html)
+to add the `notmuch-show-filter-thread` function.
+1. Add the following hook to your emacs configuration:
+
+ (defun expand-only-unread-hook () (interactive)
+ (let ((unread nil)
+ (open (notmuch-show-get-message-ids-for-open-messages)))
+ (notmuch-show-mapc (lambda ()
+ (when (member "unread" (notmuch-show-get-tags))
+ (setq unread t))))
+ (when unread
+ (let ((notmuch-show-hook (remove 'expand-only-unread-hook notmuch-show-hook)))
+ (notmuch-show-filter-thread "tag:unread")))))
+
+ (add-hook 'notmuch-show-hook 'expand-only-unread-hook)
+
+## Changing the color of a saved search based on some other search
+
+I like to have a saved search for my inbox, but have it change color when there
+are thread with unread messages in the inbox. I accomplish this with the
+following code in my emacs config:
+
+ (defun color-inbox-if-unread () (interactive)
+ (save-excursion
+ (goto-char (point-min))
+ (let ((cnt (car (process-lines "notmuch" "count" "tag:inbox and tag:unread"))))
+ (when (> (string-to-number cnt) 0)
+ (save-excursion
+ (when (search-forward "inbox" (point-max) t)
+ (let* ((overlays (overlays-in (match-beginning 0) (match-end 0)))
+ (overlay (car overlays)))
+ (when overlay
+ (overlay-put overlay 'face '((:inherit bold) (:foreground "green")))))))))))
+ (add-hook 'notmuch-hello-refresh-hook 'color-inbox-if-unread)
+
+## Linking to notmuch messages and threads from the Circe IRC client
+
+[Circe](https://github.com/jorgenschaefer/circe/wiki) is an IRC client for emacs.
+To have clickable buttons for notmuch messages and threads, add the following to
+`lui-buttons-list` (using, e.g. M-x customize-variable)
+
+ ("\\(?:id\\|mid\\|thread\\):[0-9A-Za-z][0-9A-Za-z.@-]*" 0 notmuch-show 0)
+
+If you have notmuch-pick installed, it works fine for this as well.
+
+## Linking to notmuch messages from org-mode
+
+Support for linking to notmuch messages is distributed with org-mode,
+but as a contrib file, so you might have to work a bit to load it.
+
+In Debian and derivatives,
+
+ (add-to-list 'load-path "/usr/share/org-mode/lisp")
+
+In NixOS, using `emacsWithPackages (epkgs: [ epkgs.orgPackages.org-plus-contrib ])`,
+
+ (loop for p in load-path
+ do (if (file-accessible-directory-p p)
+ (let ((m (directory-files-recursively p "^ol-notmuch.el$")))
+ (if m (add-to-list 'load-path (file-name-directory (car m)))))))
+
+Then
+
+ (require 'ol-notmuch)
+
+In general it is nice to have a key for org-links (not just for notmuch). For example
+
+ (define-key global-map "\C-c l" 'org-store-link)
+
+If you're using `use-package` the package can be loaded using the following:
+
+```emacs-lisp
+(use-package ol-notmuch
+ :ensure t
+ :bind
+ ("C-c l" . org-store-link))
+```
+
+Note the package was renamed from `org-notmuch` to `ol-notmuch` in recent
+versions of org-mode. If you're using an old version of notmuch you might want
+to `(require 'org-notmuch)` instead.
+
+## Viewing diffs in notmuch
+
+The following code allows you to view an inline patch in diff-mode
+directly from notmuch. This means that normal diff-mode commands like
+refine, next hunk etc all work.
+
+ (defun my-notmuch-show-view-as-patch ()
+ "View the the current message as a patch."
+ (interactive)
+ (let* ((id (notmuch-show-get-message-id))
+ (msg (notmuch-show-get-message-properties))
+ (part (notmuch-show-get-part-properties))
+ (subject (concat "Subject: " (notmuch-show-get-subject) "\n"))
+ (diff-default-read-only t)
+ (buf (get-buffer-create (concat "*notmuch-patch-" id "*")))
+ (map (make-sparse-keymap)))
+ (define-key map "q" 'notmuch-bury-or-kill-this-buffer)
+ (switch-to-buffer buf)
+ (let ((inhibit-read-only t))
+ (erase-buffer)
+ (insert subject)
+ (insert (notmuch-get-bodypart-text msg part nil)))
+ (set-buffer-modified-p nil)
+ (diff-mode)
+ (lexical-let ((new-ro-bind (cons 'buffer-read-only map)))
+ (add-to-list 'minor-mode-overriding-map-alist new-ro-bind))
+ (goto-char (point-min))))
+
+and then this function needs to bound to `. d` in the keymap
+
+ (define-key 'notmuch-show-part-map "d" 'my-notmuch-show-view-as-patch)
+
+## Interfacing with Patchwork
+
+[Patchwork](http://jk.ozlabs.org/projects/patchwork/) is a web-based system for
+tracking patches sent to a mailing list. While the Notmuch project doesn't use
+it, many other open source projects do. Having an easy way to get from a patch
+email in your favorite mail client to the web page of the patch in the Patchwork
+instance is a cool thing to have. Here's how to abuse the notmuch stash feature
+to achieve this. (Don't know stash? See `notmuch-show-stash-mlarchive-link`,
+bound to `c l` in `notmuch-show`.)
+
+The trick needed is turning the email Message-ID into a unique Patchwork ID
+assigned by Patchwork. We'll use the `pwclient` command-line tool to achieve
+this. You'll first need to get that working and configured for the Patchwork
+instance you're using. That part is beyond this tip here; please refer to
+Patchwork documentation.
+
+Check your configuration on the command-line, for example:
+
+ /path/to/pwclient -p <the-project> -n 5 -f "%{id}"
+
+Note that the -f format argument may require a reasonably new version of the
+client. Once you have the above working, you can `M-x customize-variable RET
+notmuch-show-stash-mlarchive-link-alist RET`.
+
+Add a new entry with "Function returning the URL:" set to:
+
+ (lambda (message-id)
+ (concat "http://patchwork.example.com/patch/"
+ (nth 0
+ (process-lines "/path/to/pwclient" "search"
+ "-p" "the-project"
+ "-m" (concat "<" message-id ">")
+ "-n" "1"
+ "-f" "%{id}"))))
+
+Replacing `http://patchwork.example.com/patch/`, `/path/to/pwclient`, and
+`the-project` appropriately. You should now be able to stash the Patchwork URL
+using `c l`.
+
+Going further, if the patch has been committed, you can get the commit hash with
+this:
+
+ (lambda (message-id)
+ (nth 0
+ (process-lines "/path/to/pwclient" "search"
+ "-p" "the-project"
+ "-m" (concat "<" message-id ">")
+ "-n" "1"
+ "-f" "%{commit_ref}")))
+
+And finally, if the project has a web interface to its source repository, you
+can turn the commit hash into a URL pointing there, for example:
+
+ (lambda (message-id)
+ (concat "http://cgit.example.com/the-project/commit/?id="
+ (nth 0
+ (process-lines "/path/to/pwclient" "search"
+ "-p" "the-project"
+ "-m" (concat "<" message-id ">")
+ "-n" "1"
+ "-f" "%{commit_ref}"))))
+
+## Never forget attachments
+
+Very often we forget to actually attach the file when we send an email
+that's supposed to have an attachment. Did this never happen to you?
+If not, then it will.
+
+There is a hook out there that checks the content of the email for
+keywords and warns you before the email is sent out if there's no
+attachment. This is currently work in progress, but you can already
+add the hook to your `~/.emacs.d/notmuch-config.el` file to test
+it. Details available (and feedback welcome) in the [relevant
+discussion](https://notmuchmail.org/pipermail/notmuch/2018/026414.html).
+
+## Applying patches to git repositories
+
+The `notmuch-extract-thread-patches` and
+`notmuch-extract-message-patches` commands from the `elpa-mailscripts`
+package in Debian (and its derivatives) can do this for you.
+
+## Allow content preference based on message context
+
+The preference for which sub-part of a multipart/alternative part is shown is
+globally set. For example, if you prefer showing the html version over the text
+based, you can set:
+
+ (setq notmuch-multipart/alternative-discouraged '("text/plain" "text/html"))
+
+However, sometimes you might want to adapt your preference depending on the
+context. You can override the default settings on a per-message basis by
+providing a function that has access to the message and which returns the
+discouraged type list. For example:
+
+ (defun my/determine-discouraged (msg)
+ (let* ((headers (plist-get msg :headers))
+ (from (or (plist-get headers :From) "")))
+ (cond
+ ((string-match "whatever@mail.address.com" from)
+ '("text/plain"))
+ (t
+ '("text/html" "multipart/related")))))
+
+ (setq notmuch-multipart/alternative-discouraged
+ 'my/determine-discouraged)
+
+This would discourage text/html and multipart/related generally, but discourage
+text/plain should the message be sent from whatever@mail.address.com.
+
+## See the recipient address instead of your address when listing sent messages
+
+If you like to see your sent messages in unthreaded view, by default you will
+see your address in the authors column, which is maybe not what you want. The
+following code allows for showing the recipients if your email address (an
+arbitrary address, whatever@mail.address.com in the example) is included in the
+From field.
+
+ (defun my/notmuch-unthreaded-show-recipient-if-sent (format-string result)
+ (let* ((headers (plist-get result :headers))
+ (to (plist-get headers :To))
+ (author (plist-get headers :From))
+ (face (if (plist-get result :match)
+ 'notmuch-tree-match-author-face
+ 'notmuch-tree-no-match-author-face)))
+ (propertize
+ (format format-string
+ (if (string-match "whatever@mail.address.com" author)
+ (concat "↦ " (notmuch-tree-clean-address to))
+ (notmuch-tree-clean-address to)
+ author))
+ 'face face)))
+
+ (setq notmuch-unthreaded-result-format
+ '(("date" . "%12s ")
+ (my/notmuch-unthreaded-show-recipient-if-sent . "%-20.20s")
+ ((("subject" . "%s"))
+ . " %-54s ")
+ ("tags" . "(%s)")))
+
+## Issues with Emacs 24 (unsupported since notmuch 0.31 (2020-09-05))
+
+If notmuch-show-mode behaves badly for you in emacs 24.x try adding one of
+
+ (setq gnus-inhibit-images nil)
+
+or
+
+ (require 'gnus-art)
+
+to your .emacs file.