Compare commits

...

3 Commits

Author SHA1 Message Date
aa25dc2530 imprv: reply 2026-03-17 14:25:03 +01:00
ce9f16e293 improved POSIX 2026-03-17 14:00:07 +01:00
ea16fb03d5 flag for shellcheck 2026-03-17 13:59:45 +01:00
9 changed files with 45 additions and 47 deletions

View File

@@ -7,15 +7,16 @@ NAME="nmf"
SRC="./src/main.sh"
tmpdir=$(mktemp -d)
echo "🥚 ${GREEN}Internalize sourced files${OFF}"
[ "${1:-}" ] || echo "🥚 ${GREEN}Internalize sourced files${OFF}"
sed -E 's|\. "([^$].+)"$|cat src/\1|e' "$SRC" >"$tmpdir/1.sh"
echo "🐔 ${GREEN}Internalize awk scripts${OFF}"
[ "${1:-}" ] || echo "🐔 ${GREEN}Internalize awk scripts${OFF}"
sed -E 's|@@include (.+)$|cat src/\1|e' "$tmpdir/1.sh" >"$tmpdir/2.sh"
echo "🥚 ${GREEN}Internalize awk libraries${OFF}"
[ "${1:-}" ] || echo "🥚 ${GREEN}Internalize awk libraries${OFF}"
sed -E 's|@include "(.+)"$|cat src/\1|e' "$tmpdir/2.sh" >"$tmpdir/3.sh"
echo "🐔 ${GREEN}Strip comments${OFF}"
[ "${1:-}" ] || echo "🐔 ${GREEN}Strip comments${OFF}"
grep -v "^ *# " "$tmpdir/3.sh" | grep -v "^ *#$" >"$NAME"
echo "🥚 ${GREEN}Make executable and cleanup${OFF}"
[ "${1:-}" ] || echo "🥚 ${GREEN}Make executable and cleanup${OFF}"
chmod +x "$NAME"
rm -rf "$tmpdir"
echo "🍳 ${GREEN}Done:${OFF} Sucessfully built ${BOLD}${GREEN}$NAME${OFF}"
[ "${1:-}" ] || echo "🍳 ${GREEN}Done:${OFF} Sucessfully built ${BOLD}${GREEN}$NAME${OFF}"
[ "${1:-}" ] && shellcheck -f gcc "$NAME"

View File

@@ -136,7 +136,7 @@ if [ "${1:-}" = "--archive" ]; then
[ "$1" = "thread" ] && thread=1
shift
[ "${thread:-}" ] && field="thread" || field="id"
query="$(echo " $@" | sed "s/\s\(\S\)/ $field:\1/g")"
query="$(echo " $*" | sed "s/\s\(\S\)/ $field:\1/g")"
tagmod="$(echo " $TAG_INBOX $TAGS_NEW" | sed "s/\s\(\S\)/ -\1/g")"
$NOTMUCH tag $tagmod "$query"
exit
@@ -147,14 +147,14 @@ if [ "${1:-}" = "--delete" ]; then
[ "$1" = "thread" ] && thread=1
shift
[ "${thread:-}" ] && field="thread" || field="id"
query="$(echo " $@" | sed "s/\s\(\S\)/ $field:\1/g")"
query="$(echo " $*" | sed "s/\s\(\S\)/ $field:\1/g")"
$NOTMUCH tag -$TAG_INBOX +$TAG_DEL "$query"
exit
fi
if [ "${1:-}" = "--undelete" ]; then
shift
query="$(echo " $@" | sed "s/\s\(\S\)/ thread:\1/g")"
query="$(echo " $*" | sed "s/\s\(\S\)/ thread:\1/g")"
$NOTMUCH tag -$TAG_DEL "$query"
exit
fi
@@ -164,7 +164,7 @@ if [ "${1:-}" = "--flag" ]; then
[ "$1" = "thread" ] && thread=1
shift
[ "${thread:-}" ] && field="thread" || field="id"
query="$(echo " $@" | sed "s/\s\(\S\)/ $field:\1/g")"
query="$(echo " $*" | sed "s/\s\(\S\)/ $field:\1/g")"
$NOTMUCH tag +"$TAG_FLAGGED" "$query"
exit
fi
@@ -174,7 +174,7 @@ if [ "${1:-}" = "--unflag" ]; then
[ "$1" = "thread" ] && thread=1
shift
[ "${thread:-}" ] && field="thread" || field="id"
query="$(echo " $@" | sed "s/\s\(\S\)/ $field:\1/g")"
query="$(echo " $*" | sed "s/\s\(\S\)/ $field:\1/g")"
$NOTMUCH tag -"$TAG_FLAGGED" "$query"
exit
fi
@@ -182,11 +182,10 @@ fi
if [ "${1:-}" = "--purge" ]; then
shift
# query="$(echo " $@" | sed "s/\s\(\S*\)/ (thread:\1 and tag:del)/g" | sed 's/)\(\s\+\)(/) or (/g')"
query="tag:$TAG_DEL and ("$(echo " $@" | sed "s/\s\(\S\)/ thread:\1/g")")"
query="tag:$TAG_DEL and ($(echo " $*" | sed "s/\s\(\S\)/ thread:\1/g"))"
files=$(nm_files_all "$query")
if ynprompt "Are you sure you want to purge these files?" "$files"; then
echo -n "$files" | xargs -d "\n" rm
printf '%s' "$files" | xargs -d "\n" rm
nm_new
fi
exit
@@ -199,7 +198,7 @@ if [ "${1:-}" = "--tag-modify" ]; then
[ "$1" = "add" ] && sign=+ || sign=-
shift
[ "${thread:-}" ] && field="thread" || field="id"
query="$(echo " $@" | sed "s/\s\(\S\)/ $field:\1/g")"
query="$(echo " $*" | sed "s/\s\(\S\)/ $field:\1/g")"
if [ "$sign" = "+" ]; then
tagquery=""
label_text="Add tag"
@@ -220,7 +219,7 @@ if [ "${1:-}" = "--tag-modify" ]; then
--border=double \
--border-label=" $label_text " | head -1 || true)
[ "$res" ] || exit
$NOTMUCH tag $sign$res "$query"
$NOTMUCH tag "$sign$res" "$query"
exit
fi
@@ -255,7 +254,7 @@ if [ "${1:-}" = "--show-thread" ]; then
--bind="$KEYS_OPEN:execute:$0 --open-part {5}" \
--bind="$KEYS_FLAG:execute($0 --flag message {+5})+reload:$0 --list-messages-in-thread \"$thread\"" \
--bind="$KEYS_UNFLAG:execute($0 --unflag message {+5})+reload:$0 --list-messages-in-thread \"$thread\"" \
--bind="$KEYS_REPLY:execute:$0 --reply {5}" \
--bind="$KEYS_REPLY:execute:$0 --reply message {5}" \
--bind="$KEYS_VIEW_LOGS:execute:$0 --view-logs" \
--bind="$KEYS_COMPOSE:execute:$0 --compose" \
--bind="focus:change-preview($0 --preview-message {5})+transform-footer:printf '\033[4mMesssage parts\033[0m\n';$0 --show-message-parts {5}" \
@@ -310,6 +309,7 @@ if [ "${1:-}" = "--compose" ]; then
echo "From: $PRIMARY_NAME <$PRIMARY_EMAIL>"
echo "To: $address"
echo "Cc: "
[ "$BCC_TO_SELF" ] && echo "Bcc: $PRIMARY_NAME <$PRIMARY_EMAIL>"
echo "Subject: "
echo ""
echo "Message body goes here"
@@ -323,9 +323,16 @@ fi
if [ "${1:-}" = "--reply" ]; then
shift
messageid="$1"
[ "$1" = "thread" ] && thread=1
shift
[ ! "${thread:-}" ] && messageid="$1" || messageid="$(nm_last_message_in_thread "$1")"
tmpfile=$(mktemp --suffix='.eml')
if [ "$BCC_TO_SELF" ]; then
$NOTMUCH reply id:"$messageid" |
awk '!f && /^From:/ { print "Bcc: "substr($0, 7); f=1 } {print}' > "$tmpfile"
else
$NOTMUCH reply id:"$messageid" > "$tmpfile"
fi
$EDITOR "$tmpfile"
# Confirm and send
send "$tmpfile"
@@ -343,6 +350,7 @@ if [ "${1:-}" = "--send-test-email" ]; then
{
echo "From: $PRIMARY_NAME <$PRIMARY_EMAIL>"
echo "To: $PRIMARY_NAME <$PRIMARY_EMAIL>"
[ "$BCC_TO_SELF" ] && echo "Bcc: $PRIMARY_NAME <$PRIMARY_EMAIL>"
echo "Subject: Test E-Mail from $APP_NAME version $APP_VERSION"
echo ""
echo "This is a test email sent from from [$APP_NAME]($APP_WEBSITE)."
@@ -430,8 +438,10 @@ while true; do
--bind="$KEYS_SEARCH_UNREAD:print($CMD_GOTO_UNREAD)+accept" \
--bind="$KEYS_SEARCH_FLAGGED:print($CMD_GOTO_FLAGGED)+accept" \
--bind="$KEYS_VIEW_LOGS:execute:$0 --view-logs" \
--bind="$KEYS_REPLY:execute:$0 --reply thread {4}" \
--bind="$KEYS_SYNC:execute($0 --sync)+reload:$0 --list-threads \"$nmquery\"" \
--bind="$KEYS_COMPOSE:execute:$0 --compose" \
--bind="$KEYS_GOTO_FIRST:first" \
--bind="focus:change-preview:$0 --preview-thread {4}" \
--preview="$0 --preview-thread {4}" \
--preview-window="$FZF_DEFAULT_PREVIEW_WINDOW" | head -1 || true)
@@ -503,9 +513,9 @@ while true; do
--border=double \
--border-label=' Query history ' | head -1 || true)
;;
$CMD_GOTO_INBOX) nmquery="tag:$TAG_INBOX" ;;
$CMD_GOTO_UNREAD) nmquery="tag:$TAG_UNREAD" ;;
$CMD_GOTO_FLAGGED) nmquery="tag:$TAG_FLAGGED" ;;
"$CMD_GOTO_INBOX") nmquery="tag:$TAG_INBOX" ;;
"$CMD_GOTO_UNREAD") nmquery="tag:$TAG_UNREAD" ;;
"$CMD_GOTO_FLAGGED") nmquery="tag:$TAG_FLAGGED" ;;
*)
;;
esac

View File

@@ -14,13 +14,14 @@ if [ ! "${CONFIG_LOADED:-}" ]; then
PRIMARY_NAME=${PRIMARY_NAME:-$($NOTMUCH config get user.name)}
PRIMARY_EMAIL=${PRIMARY_EMAIL:-$($NOTMUCH config get user.primary_email)}
BCC_TO_SELF=${BCC_TO_SELF:-}
if [ ! "${SMTPLOGFILE:-}" ]; then
SMTPLOGFILE=$(mktemp)
trap 'rm -f "$SMTPLOGFILE"' EXIT INT
fi
export PRIMARY_NAME PRIMARY_EMAIL SET_TERMINAL_TITLE SMTPLOGFILE
export PRIMARY_NAME PRIMARY_EMAIL BCC_TO_SELF SET_TERMINAL_TITLE SMTPLOGFILE
export CONFIG_LOADED=1
fi

View File

@@ -1,5 +1,6 @@
if [ ! "${HISTORY_LOADED:-}" ]; then
export NMFHIST=$(mktemp)
NMFHIST="$(mktemp)"
export NMFHIST
touch "$NMFHIST"
trap 'rm -f "$NMFHIST"' EXIT INT

View File

@@ -24,6 +24,7 @@ if [ ! "${KEYS_LOADED:-}" ]; then
KEYS_COMPOSE="ctrl-n"
KEYS_VIEW_LOGS="alt-0"
KEYS_SYNC="ctrl-s"
KEYS_GOTO_FIRST="home"
# Preview window
KEYS_PREVIEW_DOWN="alt-j"
@@ -52,12 +53,12 @@ if [ ! "${KEYS_LOADED:-}" ]; then
KEYS_ENTER_ALTERNATIVE KEYS_CYCLE_DISPLAY KEYS_VIEW_SOURCE \
KEYS_HTML_PREVIEW KEYS_TAG_ADD KEYS_TAG_REMOVE KEYS_ARCHIVE KEYS_DELETE \
KEYS_FLAG KEYS_UNFLAG KEYS_OPEN KEYS_REPLY KEYS_COMPOSE KEYS_VIEW_LOGS \
KEYS_SYNC KEYS_PREVIEW_DOWN KEYS_PREVIEW_UP KEYS_PREVIEW_DOWN_HP \
KEYS_SYNC KEYS_GOTO_FIRST KEYS_PREVIEW_DOWN KEYS_PREVIEW_UP KEYS_PREVIEW_DOWN_HP \
KEYS_PREVIEW_UP_HP KEYS_PREVIEW_TOGGLE_SIZE KEYS_SEARCH_TAG_REPLCE \
KEYS_SEARCH_TAG_APPEND KEYS_SEARCH_FROM_REPLCE KEYS_SEARCH_FROM_APPEND \
KEYS_SEARCH_TO_REPLCE KEYS_SEARCH_TO_APPEND KEYS_SEARCH_EDIT_QUERY \
KEYS_SEARCH_DELETED KEYS_SEARCH_INBOX KEYS_SEARCH_UNREAD \
KEYS_SEARCH_FLAGGED
KEYS_SEARCH_FLAGGED KEYS_PURGE_ALL
export KEYS_LOADED=1
fi

View File

@@ -9,8 +9,7 @@ nm_new() {
# Print the message id of the last message within a thread.
# @argument $1: thread id
nm_last_message_in_thread() {
# TODO: We may be smarter here that just incorporating excluded messages
$NOTMUCH search --output=messages --sort=oldest-first --offset=-1 thread:"$1" | sed 's/^...//'
$NOTMUCH search --output=messages --sort=newest-first --limit=1 thread:"$1" | sed 's/^...//'
}
# Print the header of a message (with trailing empty line)

View File

@@ -11,7 +11,6 @@
# @argument #3: if set, then the HTML messages will be taken
preview_message() {
[ "$1" = "id" ] && messageid="$2" || messageid="$(nm_last_message_in_thread "$2")"
parts="$(nm_message_parts "$messageid")"
html="${3:-}"
nr="$(nm_message_part_nr "$messageid" "$html")"
[ ! "$nr" ] && [ ! "$html" ] && html="html" && nr="$(nm_message_part_nr "$messageid" "html")"

View File

@@ -2,12 +2,12 @@
# @argument $1: Path to email file
# @argument $2: if set, don't prompt the user (optional)
send() {
if [ ! "${2:-}" ] && ! ynprompt "Do you want to send this email?" "$($CATEMAIL $tmpfile)"; then
if [ ! "${2:-}" ] && ! ynprompt "Do you want to send this email?" "$($CATEMAIL "$tmpfile")"; then
return
fi
$MSMTP -t --read-envelope-from -X "$SMTPLOGFILE" < "$1" >/dev/null 2>&1 || true
log=$(tail -1 "$SMTPLOGFILE")
if [ "${2:-}" ] || echo -n "$log" | grep -q '\bsmtpstatus=250\b'; then
if [ "${2:-}" ] || printf '%s' "$log" | grep -q '\bsmtpstatus=250\b'; then
return
fi
# Handle errors

View File

@@ -5,7 +5,7 @@
# @argument $2: Content to show in preview window
# @return: 0 if the user answered "yes" and 1 otherwise.
ynprompt() {
return $(printf '0 \033[32myes\033[0m\n1 \033[31mno\033[0m\n' |
return "$(printf '0 \033[32myes\033[0m\n1 \033[31mno\033[0m\n' |
$FZF \
--sync \
--bind='start:pos(2)' \
@@ -19,7 +19,7 @@ ynprompt() {
--with-nth=2.. \
--accept-nth=1 \
--header-border='line' \
--color='border:yellow,header:yellow')
--color='border:yellow,header:yellow')"
}
# This prompts the user for a series of options.
@@ -43,17 +43,3 @@ multiprompt() {
--bind="ctrl-c,esc,ctrl-q:" \
--color='border:yellow,header:yellow' | head -1 || true
}
# ASCII-encode address
# asciiaddress() {
# firstword=1
# for word in $*; do
# [ "${firstword:-}" ] || echo -n ' ' && firstword=
# case $word in
# *[![:cntrl:][:print:]]*) echo -n $word ;;
# *) return 0 ;;
# esac
# isascii "$word" && echo -n "$word" || echo -n "=?UTF-8?B?$(echo -n "$word" | base64)?="
# done
# }