   "y T" #'notmuch-show-stash-tags
   "y t" #'notmuch-show-stash-to)
 
+(defun pjones:notmuch-delete-thread (&optional undelete beg end)
+  "Delete messages between BEG and END.
+
+If UNDELETE is non-nil then reverse the delete operation."
+  (interactive (cons current-prefix-arg (notmuch-interactive-region)))
+  (let* ((tags '("+deleted" "-unread" "+move"))
+         (query (notmuch-tag-change-list tags undelete)))
+    (pcase major-mode
+      ('notmuch-search-mode
+       (notmuch-search-tag query)
+       (when (eq beg end) (notmuch-search-next-thread)))
+      ('notmuch-tree-mode
+       (notmuch-tree-tag query)
+       (notmuch-tree-next-matching-message))
+      ('notmuch-show-mode
+       (apply #'notmuch-show-tag-message query)
+       (unless (notmuch-show-next-open-message) (notmuch-show-next-thread t))))))
+
+(defun pjones:notmuch-mail-folders (&optional keep)
+  "Return a list of valid folder names.
+
+If KEEP is non-nil then don't remove uninteresting folders."
+  (let ((not-interesting
+         (lambda (file)
+           (or (string= ".notmuch" file)
+               (string= ".Archive" file)
+               (string= ".Sent" file)
+               (string= ".Trash" file)
+               (string= ".Junk" file))))
+        (strip-dot
+         (lambda (file)
+           (s-chop-prefix "." file))))
+    (cons "INBOX"
+     (-map strip-dot
+      (-remove (if keep (lambda (_) nil) not-interesting)
+       (directory-files
+        (notmuch-database-path) nil "^\\.[^\\.]"))))))
+
+(defun pjones:notmuch-move-message (&optional only-matched)
+  "Move the message under point to another folder.
+
+If ONLY-MATCHED is non-nil only move matched messages."
+  (interactive "P")
+  (let ((query
+         (pcase major-mode
+           ('notmuch-search-mode
+            (let ((beg (car (notmuch-interactive-region)))
+                  (end (cadr (notmuch-interactive-region))))
+              (notmuch-search-find-stable-query-region beg end only-matched)))
+           ('notmuch-tree-mode
+            (notmuch-tree-get-message-id))
+           ('notmuch-show-mode
+            (notmuch-show-get-message-id))))
+        (maildir
+         (completing-read
+          "Move message(s) to: "
+          (pjones:notmuch-mail-folders)
+          nil
+          t)))
+    (start-process "notmuch-refile" nil "notmuch-refile" maildir query)))
+
+;; This variable comes from notmuch-tag.el.
+(defvar tag-changes)
+
+(defun pjones:notmuch-after-tag-hook ()
+  "Process mail messages after they have been tagged."
+  (when (member "+move" tag-changes)
+    (message "Refiling tagged messages...")
+    (let ((script (concat (notmuch-database-path) "/.notmuch/hooks/x-post-tag")))
+      (start-process "notmuch-x-post-tag" nil script))))
+
+(add-hook 'notmuch-after-tag-hook #'pjones:notmuch-after-tag-hook)
 (add-hook 'notmuch-mua-send-hook #'notmuch-mua-attachment-check)
 
 ;;; notmuch-conf.el ends here

 '(notmuch-hello-thousands-separator ",")
 '(notmuch-mua-cite-function #'message-cite-original-without-signature)
 '(notmuch-draft-folder ".Drafts")
 '(notmuch-archive-tags '("+archived"))
 '(notmuch-fcc-dirs ".Sent +sent +from-me")
 '(notmuch-message-replied-tags '("+replied" "+from-me"))
 '(notmuch-message-forwarded-tags '("+forwarded" "+from-me"))
 '(notmuch-mua-user-agent-function 'notmuch-mua-user-agent-full)
 '(notmuch-saved-searches
   '((:name "Inbox"
      :query "folder:INBOX"
      :count-query "folder:INBOX and tag:unread"
      :key "i")
     (:name "Unread"
      :query "tag:unread"

      :key "f")
     (:name "Drafts"
      :query "tag:draft"
      :key "d")))
 '(notmuch-tagging-keys
  '(("a" notmuch-archive-tags "Archive")
    ("b" ("+blacklisted" "+spam") "Blacklisted")
    ("d" ("+deleted" "+move") "Delete")
    ("f" ("+flagged") "Flag")
    ("r" notmuch-show-mark-read-tags "Mark read")
    ("s" ("+spam" "+move") "Mark as spam")
    ("u" ("+unread") "Mark unread"))))

(defmacro pjones:evil-override-notmuch (mode &rest bindings)
  "Override MODE bindings in evil-normal mode with BINDINGS."
  (declare (indent defun))
  `(pjones:evil-override-mode ,mode
     ,@bindings
     "d" #'pjones:notmuch-delete-thread
     "c" #'notmuch-mua-new-mail
     "gR" #'notmuch-poll-and-refresh-this-buffer
     "gr" #'notmuch-refresh-this-buffer
     "H" #'notmuch-help
     "M" #'pjones:notmuch-move-message
     "q" #'notmuch-bury-or-kill-this-buffer
     "s" #'notmuch-search
     "'" #'notmuch-jump-search))
