emacs/notmuch helper scripts part II

Date: 2021-10-02

modDate: 2021-10-02

tags: emacs notmuch

How to delete and move email - Part II

Part I

One of the ugly parts of doing a move this way was the helper shell script was hard coded and quite ugly. Each folder you wanted to support had to have an entry similar to the following:

notmuch search --format=text0 --output=files tag:move:inbox | xargs -n1 -0 -r -t ~/bin/my_mv.sh ~/Maildir/INBOX/cur/.
notmuch tag -move:inbox -flagged tag:move:inbox

I wrote a few elisp functions that pattern match your typed input with what folders exist in your Maildir directory and also allow for tab completion.

;; functions to move mail
;; in message move mail
(defun my-move-dir-msg-execute (&optional new-tags)
  (notmuch-show-tag-message (concat "+move:" new-tags))
  ;;(rename-file (notmuch-show-get-filename) (concat "~/Maildir/" new-tags "/cur/"))
  (notmuch-poll-and-refresh-this-buffer)
  )

(defun my-move-dir-msg (&optional kill)
  (interactive "P")
  (let ((dirs (directory-files "~/Maildir" nil ))
        (prompt (if kill "Copy DIR to kill ring: " "Browse DIR: "))
        (fn (if kill #'kill-new 'my-move-dir-msg-execute)))
    (if dirs
        (funcall fn (completing-read prompt dirs nil nil nil nil (car dirs)))
      (message "No DIRs found."))))

;; map for message
(define-key notmuch-show-mode-map "M" #'my-move-dir-msg)

;; in search view move message
(defun my-move-dir-search-execute (&optional new-tags)
  ;;(message "selected dir: %s" new-tags)
  ;;(message "move: %s" (concat "+move:" new-tags))
  (notmuch-search-tag (list (concat "+move:" new-tags)))
  (notmuch-poll-and-refresh-this-buffer)
  )

(defun my-move-dir-search (&optional kill)
  (interactive "P")
  (let ((dirs (directory-files "~/Maildir" nil ))
        (prompt (if kill "Copy DIR to kill ring: " "Browse DIR: "))
        (fn (if kill #'kill-new 'my-move-dir-search-execute)))
    (if dirs
        (funcall fn (completing-read prompt dirs nil nil nil nil (car dirs)))
      (message "No DIRs found."))))

Then I added the below snippet to my move shell script:

notmuch search tag:/move:.*/ | while read -r i
do
        thread=`echo $i | cut -c1-23`
        #dir=`echo $i | cut -c72- | cut -d':' -f2 | cut -d' ' -f1 | cut -d')' -f1`
        dir=`echo $i | sed -e's/.*(.*move://' -e's/ .*)$//' -e's/)$//'`
        echo 'thread: ' $thread 'dir: ' $dir
        notmuch search --format=text0 --output=files tag:move:$dir | xargs -n1 -0 -r -t ~/bin/my_mv.sh ~/Maildir/$dir/cur/.
        notmuch new >/dev/null 2>&1
        notmuch tag -move:$dir -flagged -unread -- $thread
done

Now the move script has no hard coded folder names.

You can find the scripts here