 ;;; Commentary:
 ;;
 ;;; Code:
+(require 'dash)
 (require 'evil)
 (require 'evil-leader)
 (require 'message)
 (require 'notmuch)
+(require 's)
 
 (eval-when-compile
   (require 'notmuch-tree)

(pjones:evil-override-notmuch notmuch-hello-mode)

(pjones:evil-override-notmuch notmuch-search-mode
  "\C-b" #'notmuch-search-scroll-up
  "\C-f" #'notmuch-search-scroll-down
  "G" #'notmuch-search-last-thread
  "gg" #'notmuch-search-first-thread
  "gj" #'notmuch-search-next-thread

  "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
