From 55aa8be68022b0ab632e01e7243bcfeabd88e898 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Wed, 17 Sep 2025 16:48:43 +0200 Subject: [PATCH] notmuch: Update hooks --- .dotter/global.toml | 1 + .dotter/sample.toml | 3 ++ office/.config/aerc/aerc.conf | 6 +++ office/.config/notmuch/config | 4 +- office/.config/notmuch/default/hooks/post-new | 12 ++++++ office/.config/notmuch/default/hooks/pre-new | 39 +++++++++++++++++++ 6 files changed, 63 insertions(+), 2 deletions(-) create mode 100755 office/.config/notmuch/default/hooks/post-new create mode 100755 office/.config/notmuch/default/hooks/pre-new diff --git a/.dotter/global.toml b/.dotter/global.toml index ac857f0..e695c82 100644 --- a/.dotter/global.toml +++ b/.dotter/global.toml @@ -61,6 +61,7 @@ pass = "~" "office/.config/msmtp/config" = { target = "~/.config/msmtp/config", type = "template" } "office/.config/aerc/accounts.conf" = { target = "~/.config/aerc/accounts.conf", type = "template" } "office/.config/aerc/Personal.qmap" = { target = "~/.config/aerc/Personal.qmap", type = "template" } +"office/.config/notmuch/default/hooks/post-new" = { target = "~/.config/notmuch/default/hooks/post-new", type = "template" } "office/.config/neomutt/account" = { target = "~/.config/neomutt/account", type = "template" } "office/.config/neomutt/profile.gmail" = { target = "~/.config/neomutt/profile.gmail", type = "template" } "office/.config/neomutt/profile.private" = { target = "~/.config/neomutt/profile.private", type = "template" } diff --git a/.dotter/sample.toml b/.dotter/sample.toml index 1230790..869f496 100644 --- a/.dotter/sample.toml +++ b/.dotter/sample.toml @@ -23,6 +23,9 @@ mail_personal_password_cmd = "" mail_notmuch_name = "" mail_notmuch_primary_email = "" mail_notmuch_other_email = "" +mail_notmuch_hook_labels = [ + "+list -- from:notifications@github.com" +] mail_aerc_from_email = "" mail_aerc_aliases_email = "" diff --git a/office/.config/aerc/aerc.conf b/office/.config/aerc/aerc.conf index 3f12fe9..0cc2fce 100644 --- a/office/.config/aerc/aerc.conf +++ b/office/.config/aerc/aerc.conf @@ -97,5 +97,11 @@ application/vnd.oasis.opendocument.text = pandoc -f odt -t plain .filename,~.*.ics = wezterm -e nvim - [hooks] +mail-deleted = notmuch new +mail-added = notmuch new +mail-sent = notmuch new +flag-changed = notmuch new +tag-modified = notmuch new +aerc-shutdown = notmuch new [templates] diff --git a/office/.config/notmuch/config b/office/.config/notmuch/config index fe92f8b..860e3c0 100644 --- a/office/.config/notmuch/config +++ b/office/.config/notmuch/config @@ -48,8 +48,8 @@ other_email=mo82rimu@studserv.uni-leipzig.de;moehme@ruc.dk; # in the mail store. # [new] -tags=unread;inbox -ignore=.uidvalidity;.mbsyncstate;Trash;Junk +tags=unread;inbox; +ignore=.uidvalidity;.mbsyncstate; # Search configuration # diff --git a/office/.config/notmuch/default/hooks/post-new b/office/.config/notmuch/default/hooks/post-new new file mode 100755 index 0000000..656e744 --- /dev/null +++ b/office/.config/notmuch/default/hooks/post-new @@ -0,0 +1,12 @@ +#!/usr/bin/env sh + +echo " + +-unread -- folder:Trash OR folder:Archive +-unread -flagged -todo -- folder:Trash + +{{#each mail_notmuch_hook_labels}} +{{this}} +{{/each}} +" | + notmuch tag --batch diff --git a/office/.config/notmuch/default/hooks/pre-new b/office/.config/notmuch/default/hooks/pre-new new file mode 100755 index 0000000..5af5606 --- /dev/null +++ b/office/.config/notmuch/default/hooks/pre-new @@ -0,0 +1,39 @@ +#!/usr/bin/env sh +# Little script which moves some tags to specific folders. +# +# These 'folder-tags' (e.g. trash, archive, inbox) are transient tags +# which then get removed when the file is in the correct folder. +# +# So later when trying to find the emails always use the 'folder:< >' +# notmuch search, not the 'tag:< >' search - as the tag only signals that +# an e-mail is currently still in the 'wrong' folder and a move action +# is necessary. + +MAIL_DIR="$MAIL_PATH" +MAIL_DIR="${XDG_DOCUMENTS_DIR:-$HOME/documents}/mail" + +# all folders: -archive -drafts +inbox -jobs -junk -receipts -sent -trash + +# idea: archive, inbox, trash, sent -> transient tags +# if anything is tagged, it should be moved into the folder and the tag removed. +# Transient tags describe the _type_ of a message (essentially its importance to me) +# +# jobs, junk, receipts -> describe _what_ a message is (topics etc) +# -> stable tags, which stay on a message + +move_and_untag() { + tag_to_remove=$1 # e.g. trash + folder=$2 # e.g. Trash + filter="tag:$tag_to_remove not folder:$folder" + id_list=$(notmuch search --output=messages --format=text "$filter") + # echo "Moving $(notmuch count "$filter") messages to $folder." # TODO: optional loud mode? + notmuch search --output=files --format=text0 "$filter" | xargs -0 --no-run-if-empty mv -t "$MAIL_DIR/$folder/new/" + if [ "$id_list" != "" ]; then + echo "$id_list" | sed -e "s/^/-$tag_to_remove -- /" | notmuch tag --batch + fi +} + +move_and_untag trash Trash +move_and_untag inbox Inbox +move_and_untag archive Archive +move_and_untag sent Sent