#!/bin/sh set -eu # Application info . "sh/info.sh" # Theme . "sh/theme.sh" # awk scripts . "sh/awk.sh" # tools . "sh/tools.sh" __notmuch_search() { $NOTMUCH search "$*" \ | sed "s/^thread:\([[:xdigit:]]\+\)\s\+\([^[]\+\)\s\+\[\([^]]\+\)\]\([^;]*\); \(.*\) (\([^(]*\))$/${COLDATE}\2${RESET}\t${COLCNTS}\3${RESET}\t${COLFROM}\4${RESET}\t${COLSUBJ}\5${RESET}\t${COLTAGS}\6${RESET}\t\1/" \ | column -s "$(printf '\t')" -t -l 3 -R 1,2 } __notmuch_thread() { $NOTMUCH show thread:"$1" \ | awk "$AWK_THREADOVERVIEW" \ | sed "s/^\(\S*\)\t\([^\t]*\)\t\(.*\) (\([^()]*\)) (\([^()]*\))$/${COLFROM}\3${RESET}\t${COLSUBJ}\2${RESET}\t${COLDATE}\4${RESET}\t${COLTAGS}\5${RESET}\t\1/" } __notmuch_list_tags() { $NOTMUCH search --output=tags tag:/./ } __notmuch_list_address() { $NOTMUCH address '*' } if [ "${1:-}" = "--preview-thread" ]; then shift thread="$1" $NOTMUCH show thread:$thread \ | awk "$AWK_LASTMESSAGEINTHREAD" \ | tail -n +2 \ | $CAT exit fi if [ "${1:-}" = "--preview-message" ]; then shift messageid="$1" $NOTMUCH show id:$messageid \ | awk "$AWK_LASTMESSAGEINTHREAD" \ | tail -n +2 \ | $CAT exit fi # FZF default configs FZF_DEFAULT_PREVIEW_WINDOW="right,80,border-line,wrap-word" FZF_DEFAULT_THREAD_LINE="{1} {3} ({4})" if [ "${1:-}" = "--show-thread" ]; then shift thread="$1" res=$(__notmuch_thread "$thread" | fzf \ --raw \ --ansi \ --reverse \ --multi \ --delimiter="$(printf '\t')" \ --with-nth="$FZF_DEFAULT_THREAD_LINE" \ --bind="alt-s:change-with-nth({1} {2} {3} ({4})|$FZF_DEFAULT_THREAD_LINE)" \ --accept-nth=5 \ --bind='alt-j:preview-down,alt-k:preview-up' \ --bind='page-up:preview-half-page-up,page-down:preview-half-page-down' \ --bind="enter:" \ --bind="ctrl-h,backward-eof:abort" \ --bind="alt-/:change-preview-window(right,90%,border-line,wrap-word|$FZF_DEFAULT_PREVIEW_WINDOW)" \ --preview="$0 --preview-message {5}" \ --preview-window="$FZF_DEFAULT_PREVIEW_WINDOW" || true) exit fi # Modes APPEND="append" NWSRCH="search" CMD_SEARCH_TAG="tag-" ADDRESS_TO="to-" ADDRESS_FROM="from-" CMD_SEARCH_ADDRESS="address-" while true; do nmquery="${nmquery:-tag:inbox}" cmd=$(__notmuch_search "$nmquery" | fzf \ --header="$nmquery" --footer="FOOTER" \ --ansi \ --tiebreak=index \ --reverse \ --multi \ --delimiter="$(printf '\t')" \ --with-nth="{1} {2} ({3})" \ --bind="alt-t:print($CMD_SEARCH_TAG$NWSRCH)+accept" \ --bind="alt-T:print($CMD_SEARCH_TAG$APPEND)+accept" \ --bind="alt-s:print($CMD_SEARCH_ADDRESS$ADDRESS_FROM$NWSRCH)+accept" \ --bind="alt-S:print($CMD_SEARCH_ADDRESS$ADDRESS_FROM$APPEND)+accept" \ --bind="alt-r:print($CMD_SEARCH_ADDRESS$ADDRESS_TO$NWSRCH)+accept" \ --bind="alt-R:print($CMD_SEARCH_ADDRESS$ADDRESS_TO$APPEND)+accept" \ --bind='alt-j:preview-down,alt-k:preview-up' \ --bind='page-up:preview-half-page-up,page-down:preview-half-page-down' \ --bind="enter,ctrl-l:execute:$0 --show-thread {4}" \ --bind="alt-/:change-preview-window(right,90%,border-line,wrap-word|$FZF_DEFAULT_PREVIEW_WINDOW)" \ --preview="$0 --preview-thread {4}" \ --preview-window="$FZF_DEFAULT_PREVIEW_WINDOW" | head -1 || true) [ -n "$cmd" ] || exit 0 case "$cmd" in $CMD_SEARCH_TAG*) tag=$(__notmuch_list_tags | fzf \ --ansi \ --cycle \ --margin='20%' \ --bind='enter:accept-or-print-query' \ --border=double \ --border-label='Select tag' || true) [ -n "$tag" ] || continue [ "$cmd" = "$CMD_SEARCH_TAG$APPEND" ] && nmquery="$nmquery and tag:$tag" || nmquery="tag:$tag" ;; $CMD_SEARCH_ADDRESS*) case "$cmd" in *$ADDRESS_FROM*) label_text='Select sender' field='from' ;; *) label_text='Select recipient' field='to' ;; esac address=$(__notmuch_list_address | fzf \ --ansi \ --tiebreak=index \ --cycle \ --margin='20%' \ --bind='enter:accept-or-print-query' \ --border=double \ --border-label="$label_text" || true) [ -n "$address" ] || continue case "$cmd" in *$APPEND) nmquery="$nmquery and $field:$address" ;; *) nmquery="$field:$address" ;; esac ;; *) ;; esac done