From 31e981478b6b5f2d1109c928f43f468f3adb98ea Mon Sep 17 00:00:00 2001 From: OVlasiuk Date: Fri, 9 Aug 2019 18:35:51 -0400 Subject: [PATCH] Add history support to notmuch search - add history support in notmuch search line - escape "$" in notmuch output - remove dangling "|" in notmuch output --- bin/mailsearch | 29 +++++++++++++++++++++++++++++ share/mutt-wizard.muttrc | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100755 bin/mailsearch diff --git a/bin/mailsearch b/bin/mailsearch new file mode 100755 index 0000000..dcbf35d --- /dev/null +++ b/bin/mailsearch @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +# Run notmuch with a search term and clean up the output. +HFILE=$HOME"/.cache/notmuch_search_hist" +SEARCH=$HOME"/.cache/notmuch_search_res" +if [ ! -e "$HFILE" ]; then + touch "$HFILE" +fi +export HISTCONTROL=ignoreboth +history -r "$HFILE" +# Uncomment to use the Vi mode in readline: +# set -o vi +x="" +echo > $SEARCH + + +echo "Enter a search term to find with notmuch:" +read -e x +history -s "$x" + +notmuch search --output=messages "$x" > "$SEARCH" +cat "$SEARCH" | + sed 's/^id:\(.*\)/\1|/' | # remove the initial "id:" + sed 's/\+/\\+/' | # replace "+" with "\+" + sed 's/\$/\\\$/' | # replace "$" with "\$" + tr -d "\n" | # remove newlines + sed 's/.$//' | # remove the last redundant "|" + cat > "$SEARCH" + +history -w "$HFILE" diff --git a/share/mutt-wizard.muttrc b/share/mutt-wizard.muttrc index ad4d2fe..d762274 100644 --- a/share/mutt-wizard.muttrc +++ b/share/mutt-wizard.muttrc @@ -63,7 +63,7 @@ bind editor complete-query macro index,pager a "|abook --add-email\n" 'add sender to abook' macro index \Cr "T~UN." "mark all messages as read" macro index O "mailsync -Va" "run offlineimap to sync all mail" -macro index \Cf "unset wait_keyread -p 'Enter a search term to find with notmuch: ' x; echo \$x >~/.cache/mutt_terms~i \"\`notmuch search --output=messages \$(cat ~/.cache/mutt_terms) | head -n 600 | perl -le '@a=<>;s/\^id:// for@a;$,=\"|\";print@a' | perl -le '@a=<>; chomp@a; s/\\+/\\\\+/ for@a;print@a' \`\"" "show only messages matching a notmuch pattern" +macro index \Cf "mailsearch~i \"\`cat ~/.cache/notmuch_search_res\`\"" "show only messages matching a notmuch pattern" macro index A "all\n" "show all messages (undo limit)" # Sidebar mappings