Synchronize tags across machines without a mail server

Subject: Synchronize tags across machines without a mail server

Date: Tue, 19 Mar 2019 11:20:46 +0100



From: Pierre Neidhardt


I know this has been asked a couple of times, and it seems that the
current solutions are:

- nmbug
  It seems that "nmbug init" has not been merged yet.  Using the patch
  above I wasn't able to add an "upstream".  Am I missing the obvious?

- muchsync ( Requires our own mail server if I
  understand correctly.

What I want is sync the `notmuch dump' of non-automatic tags across my
systems, considering I don't run my own webserver.

So far, I came up with the following:

--8<---------------cut here---------------start------------->8---
(require 'seq)

(unless (boundp 'notmuch-command)
  ;; So that this file can be use in external scripts without require
  ;; notmuch.el.
  (setq notmuch-command "notmuch"))

(defun notmuch-all-tags ()
     (with-current-buffer standard-output
       (call-process notmuch-command nil t
                     nil "search" "--output=tags" "--exclude=false" "*")))))

(defvar notmuch-unimportant-tags '("attachment" "deleted" "draft" "encrypted"
                                   "flagged" "inbox" "passed" "replied" "sent"
                                   "signed" "unread"))
(defvar notmuch-dump-file (expand-file-name "~/personal/mail/notmuch.dump"))

(defun notmuch-dump-important-tags (&optional file)
  "Dump notmuch tag database to `notmuch-dump-file'.
Messages with only `notmuch-unimportant-tags' are ignored."
  (setq file (or file notmuch-dump-file))
  (let* ((important-tags (seq-difference (notmuch-all-tags) notmuch-unimportant-tags))
         (tags-arg (cons (concat "tag:" (car important-tags))
                         (cl-loop for tag in (cdr important-tags)
                                  append (list "or" (concat "tag:" tag))))))
    (apply 'call-process notmuch-command nil `(:file ,file) nil
           "dump" tags-arg)))
--8<---------------cut here---------------end--------------->8---

This will save all non-trivial / non-automated tags.  I can call
`notmuch-dump-important-tags' manually from Emacs or automatically
(e.g. mcron job or similar sync script) with

	emacs --quick --batch --load=$HOME/.emacs.d/lisp/init-notmuch-sync.el \
				-f notmuch-dump-important-tags


Pierre Neidhardt
signature.asc (application/pgp-signature)
notmuch mailing list