init-notmuch-sync.el 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. ;; TODO: Report notmuch dump/tag sync technique upstream.
  2. (require 'seq)
  3. (require 'cl-lib)
  4. (unless (boundp 'notmuch-command)
  5. ;; So that this file can be use in external scripts without require
  6. ;; notmuch.el.
  7. (setq notmuch-command "notmuch"))
  8. (defun notmuch-all-tags ()
  9. (split-string
  10. (with-output-to-string
  11. (with-current-buffer standard-output
  12. (call-process notmuch-command nil t
  13. nil "search" "--output=tags" "--exclude=false" "*")))))
  14. (defun notmuch-config-get-tags (query)
  15. (split-string
  16. (with-output-to-string
  17. (with-current-buffer standard-output
  18. (call-process notmuch-command nil t
  19. nil "config" "get" query)))))
  20. (defvar notmuch-unimportant-tags (append '("attachment" "draft" "encrypted"
  21. "flagged" "passed" "replied" "sent"
  22. "signed")
  23. (notmuch-config-get-tags "new.tags")
  24. (notmuch-config-get-tags "search.exclude_tags")))
  25. (defvar notmuch-dump-file (expand-file-name "mail/notmuch.dump" (getenv "PERSONAL")))
  26. (defun notmuch-dump-important-tags (&optional file)
  27. "Dump notmuch tag database to `notmuch-dump-file'.
  28. Messages with only `notmuch-unimportant-tags' are ignored."
  29. (interactive)
  30. (setq file (or file notmuch-dump-file))
  31. (let* ((important-tags (seq-difference (notmuch-all-tags) notmuch-unimportant-tags))
  32. (tags-arg (cons (concat "tag:" (car important-tags))
  33. (cl-loop for tag in (cdr important-tags)
  34. append (list "or" (concat "tag:" tag))))))
  35. (apply 'call-process notmuch-command nil `(:file ,file) nil
  36. "dump" tags-arg)))
  37. (provide 'init-notmuch-sync)