Compare commits

..

4 Commits

Author SHA1 Message Date
1453fdb3ad feat: open attachments with ctrl-o 2026-03-12 15:32:37 +01:00
eb8f4574bf feat: html rendering 2026-03-12 14:52:29 +01:00
60f763df19 feat: configfile 2026-03-12 08:24:13 +01:00
3210a8bbcf imprv: colors 2026-03-12 08:17:19 +01:00
12 changed files with 275 additions and 109 deletions

View File

@@ -1,49 +0,0 @@
BEGIN {
RS = "\x0c"
FS = "\n"
}
/^message}/ {
mheader = header
mpart = part
mparts = parts
level = 0
multipart = ""
header = ""
part = ""
parts = ""
}
/^header{/ {
header = $0
}
/^(part|attachment)}/ {
level--
}
/^(part|attachment){/ {
parts = sprintf("%s\n%"(2*level)"s%s", parts, "", substr($1, index($1, "ID")))
level++
}
# /^part{ ID: [[:digit:]]+, Content-type: multipart\/alternative/ {
# multipart = "alternative"
# }
# /^part{ ID: [[:digit:]]+, Content-type: multipart\/mixed/ {
# multipart = "mixed"
# }
/^part{ ID: [[:digit:]]+, Content-type: text\/plain/ {
part = $0
}
END {
split(mheader, a, "\n")
delete a[1]
# delete a[2]
for (line in a) {
print a[line]
}
split(mpart, a, "\n")
delete a[1]
for (line in a) {
print a[line]
}
print "Parts:"mparts
}

13
src/awk/messageheader.awk Normal file
View File

@@ -0,0 +1,13 @@
BEGIN {
RS = "\x0c"
FS = "\n"
}
/^header{/ {
split($0, a, "\n")
delete a[1]
delete a[2]
for (line in a)
print a[line]
exit
}

12
src/awk/messageparts.awk Normal file
View File

@@ -0,0 +1,12 @@
BEGIN {
RS = "\x0c"
FS = "\n"
}
/^(part|attachment)}/ {
level--
}
/^(part|attachment){/ {
printf "%"(2*level)"s%s\n", "", substr($1, index($1, "ID"))
level++
}

14
src/awk/partnr.awk Normal file
View File

@@ -0,0 +1,14 @@
# Return the part number of the first text/plain part, or, if the variable
# `html` is set, of the first text/html part.
BEGIN {
RS = "\x0c"
FS = "\n"
}
/^part{.*Content-type: text\// {
match($1, "ID: ([[:digit:]]+).* Content-type: text/([[:alpha:]]+)", a)
if ((html && a[2] == "html") || (!html && a[2] == "plain")) {
print a[1]
exit
}
}

View File

@@ -5,6 +5,9 @@ set -eu
# Application info
. "sh/info.sh"
# Configuration
. "sh/config.sh"
# Theme
. "sh/theme.sh"
@@ -14,47 +17,43 @@ set -eu
# tools
. "sh/tools.sh"
# history
# query history
. "sh/history.sh"
__notmuch_search() {
$NOTMUCH search "$*" \
| sed "s/^thread:\([[:xdigit:]]\+\)\s\+\([^[]\+\)\s\+\[\([^]]\+\)\]\([^;]*\); \(.*\) (\([^(]*\))$/${COLDATE}\2${COLRESET}\t${COLCNTS}\3${COLRESET}\t${COLFROM}\4${COLRESET}\t${COLSUBJ}\5${COLRESET}\t${COLTAGS}\6${COLRESET}\t\1/" \
| column -s "$(printf '\t')" -t -l 3 -R 1,2
}
# notmuch functions
. "sh/notmuch.sh"
__notmuch_thread() {
$NOTMUCH show thread:"$1" \
| awk "$AWK_THREADOVERVIEW" \
| sed "s/^\(\S*\)\t\([^\t]*\)\t\(.*\) (\([^()]*\)) (\([^()]*\))$/${COLFROM}\3${COLRESET}\t${COLSUBJ}\2${COLRESET}\t${COLDATE}\4${COLRESET}\t${COLTAGS}\5${COLRESET}\t\1/"
}
__notmuch_list_tags() {
$NOTMUCH search --output=tags tag:/./
}
__notmuch_list_address() {
$NOTMUCH address '*'
}
# list generators
. "sh/lists.sh"
# preview functions
. "sh/preview.sh"
if [ "${1:-}" = "--preview-thread" ]; then
shift
thread="$1"
$NOTMUCH show thread:$thread \
| awk "$AWK_LASTMESSAGEINTHREAD" \
| tail -n +2 \
| $CAT
threadid="$1"
preview_message "thread" "$threadid"
exit
fi
if [ "${1:-}" = "--preview-html-thread" ]; then
shift
threadid="$1"
preview_message "thread" "$threadid" "html"
exit
fi
if [ "${1:-}" = "--preview-message" ]; then
shift
messageid="$1"
$NOTMUCH show id:$messageid \
| awk "$AWK_LASTMESSAGEINTHREAD" \
| tail -n +2 \
| $CAT
preview_message "id" "$messageid"
exit
fi
if [ "${1:-}" = "--preview-html-message" ]; then
shift
messageid="$1"
preview_message "id" "$messageid" "html"
exit
fi
@@ -65,17 +64,63 @@ if [ "${1:-}" = "--view-source" ]; then
exit
fi
if [ "${1:-}" = "--show-message-parts" ]; then
shift
messageid="$1"
nm_message_parts "$messageid"
exit
fi
# FZF default configs
FZF_DEFAULT_PREVIEW_WINDOW="right,80,border-line,wrap-word"
FZF_ALTERNATE_PREVIEW_WINDOW="bottom,80%,border-line,wrap-word"
FZF_DEFAULT_THREAD_LINE="{1} {3} ({4})"
if [ "${1:-}" = "--open-part" ]; then
shift
messageid="$1"
res=$(nm_message_parts "$messageid" | awk '{print NR"\t"$0}' | $FZF \
--raw \
--reverse \
--wrap=word \
--delimiter="$(printf '\t')" \
--with-nth=2 \
--accept-nth=1 \
--margin='30%' \
--bind='ctrl-d:half-page-down,ctrl-u:half-page-up' \
--bind='page-up:preview-half-page-up,page-down:preview-half-page-down' \
--bind="ctrl-h,backward-eof:abort" || true)
[ "$res" ] || exit
tmpfile=$(mktemp)
nm_message_get_part "$messageid" "$res" > "$tmpfile"
file "$tmpfile"
while true; do
printf "Are you sure you want to open \"%s\"? (yes/no): " "$tmpfile" >/dev/tty
read -r yn
case $yn in
"yes")
open "$tmpfile" >/dev/null 2>/dev/stdout
printf "Press <enter> to continue." >/dev/tty
read -r tmp
break
;;
"no")
break
;;
*)
echo "Please answer \"yes\" or \"no\"." >/dev/tty
;;
esac
done
rm -f "$tmpfile"
exit
fi
if [ "${1:-}" = "--show-thread" ]; then
shift
thread="$1"
res=$(__notmuch_thread "$thread" | fzf \
res=$(list_messages_in_thread "$thread" | $FZF \
--raw \
--ansi \
--reverse \
--multi \
--delimiter="$(printf '\t')" \
@@ -89,6 +134,9 @@ if [ "${1:-}" = "--show-thread" ]; then
--bind="ctrl-h,backward-eof:abort" \
--bind="alt-/:change-preview-window($FZF_ALTERNATE_PREVIEW_WINDOW|$FZF_DEFAULT_PREVIEW_WINDOW)" \
--bind="alt-v:execute:$0 --view-source {5}" \
--bind="alt-h:change-preview:$0 --preview-html-message {5}" \
--bind="focus:change-preview($0 --preview-message {5})+transform-footer:printf '\033[4mMesssage parts\033[0m\n';$0 --show-message-parts {5}" \
--bind="ctrl-o:execute:$0 --open-part {5}" \
--preview="$0 --preview-message {5}" \
--preview-window="$FZF_DEFAULT_PREVIEW_WINDOW" || true)
exit
@@ -106,9 +154,8 @@ CMD_EDIT_QUERY="edit-query"
while true; do
nmquery="${nmquery:-tag:inbox}"
[ "$(tail -1 "$NMFHIST")" = "$nmquery" ] || echo "$nmquery" >> "$NMFHIST"
cmd=$(__notmuch_search "$nmquery" | fzf \
cmd=$(list_threads "$nmquery" | $FZF \
--header="Query: $nmquery" \
--ansi \
--tiebreak=index \
--reverse \
--multi \
@@ -126,37 +173,40 @@ while true; do
--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($FZF_ALTERNATE_PREVIEW_WINDOW|$FZF_DEFAULT_PREVIEW_WINDOW)" \
--bind="alt-h:change-preview:$0 --preview-html-thread {4}" \
--bind="focus:change-preview:$0 --preview-thread {4}" \
--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 \
tag=$(list_tags | $FZF \
--color="border:$ANSICOLORTAGS" \
--color="label:$ANSICOLORTAGS" \
--tac \
--bind='ctrl-d:half-page-down,ctrl-u:half-page-up' \
--margin='20%' \
--bind='enter:accept-or-print-query' \
--border=double \
--border-label='Select tag' || true)
--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'
label_text=" Select sender "
field='from'
;;
*)
label_text='Select recipient'
label_text=" Select recipient "
field='to'
;;
esac
address=$(__notmuch_list_address | fzf \
--ansi \
address=$(list_addresses | $FZF \
--color="border:$ANSICOLORFROM" \
--color="label:$ANSICOLORFROM" \
--tiebreak=index \
--cycle \
--bind='ctrl-d:half-page-down,ctrl-u:half-page-up' \
--margin='20%' \
--bind='enter:accept-or-print-query' \
@@ -173,8 +223,9 @@ while true; do
esac
;;
$CMD_EDIT_QUERY*)
nmquery=$(fzf \
--ansi \
nmquery=$(list_query_history | $FZF \
--color="border:$ANSICOLORSUBJ" \
--color="label:$ANSICOLORSUBJ" \
--tac \
--bind='ctrl-d:half-page-down,ctrl-u:half-page-up' \
--margin='20%' \
@@ -184,7 +235,7 @@ while true; do
--prompt="Query: " \
--bind='focus:replace-query' \
--border=double \
--border-label='Query history' < "$NMFHIST" | head -1 || true)
--border-label=' Query history ' | head -1 || true)
;;
*)
;;

View File

@@ -1,11 +1,4 @@
if [ ! "${AWK_LOADED:-}" ]; then
AWK_LASTMESSAGEINTHREAD=$(
cat <<'EOF'
@@include awk/lastmessageinthread.awk
EOF
)
export AWK_LASTMESSAGEINTHREAD
AWK_THREADOVERVIEW=$(
cat <<'EOF'
@@include awk/threadoverview.awk
@@ -13,5 +6,26 @@ EOF
)
export AWK_THREADOVERVIEW
AWK_MESSAGEHEADER=$(
cat <<'EOF'
@@include awk/messageheader.awk
EOF
)
export AWK_MESSAGEHEADER
AWK_MESSAGEPARTS=$(
cat <<'EOF'
@@include awk/messageparts.awk
EOF
)
export AWK_MESSAGEPARTS
AWK_PARTNR=$(
cat <<'EOF'
@@include awk/partnr.awk
EOF
)
export AWK_PARTNR
export AWK_LOADED=1
fi

11
src/sh/config.sh Normal file
View File

@@ -0,0 +1,11 @@
# Main application configuration. This application does not require a
# configuration file. However, a configuration file may be stored as
# `CONFIGFILE_DEFAULT`. If that file exists, it will be sourced. The path to
# the file may be overwritten by specifying the environment variable
# `CONFIGFILE`. If a configuration file is specified, then it must also exist.
# A configuration file comprises the specification of environment variables
# that are allowed to be set.
CONFIGFILE_DEFAULT="${XDG_CONFIG_HOME:-"$HOME/.config"}/$APP_NAME/config"
CONFIGFILE="${CONFIGFILE:-"$CONFIGFILE_DEFAULT"}"
[ "$CONFIGFILE" != "$CONFIGFILE_DEFAULT" ] && [ ! -f "$CONFIGFILE" ] && err "The configuration file manually specified with the environment variable CONFIGFILE=($CONFIGFILE) does not exist." && exit 1
[ -f "$CONFIGFILE" ] && . "$CONFIGFILE"

32
src/sh/lists.sh Normal file
View File

@@ -0,0 +1,32 @@
# These functions generate the lists that are fed into fzf
# List the messages within a thread
# @argument $1: thread id
list_messages_in_thread() {
$NOTMUCH show thread:"$1" \
| awk "$AWK_THREADOVERVIEW" \
| sed "s/^\(\S*\)\t\([^\t]*\)\t\(.*\) (\([^()]*\)) (\([^()]*\))$/${COLFROM}\3${COLRESET}\t${COLSUBJ}\2${COLRESET}\t${COLDATE}\4${COLRESET}\t${COLTAGS}\5${COLRESET}\t\1/"
}
# List all threads that match a query
# @argument $1: query
list_threads() {
$NOTMUCH search "$1" \
| sed "s/^thread:\([[:xdigit:]]\+\)\s\+\([^[]\+\)\s\+\[\([^]]\+\)\]\([^;]*\); \(.*\) (\([^(]*\))$/${COLDATE}\2${COLRESET}\t${COLCNTS}\3${COLRESET}\t${COLFROM}\4${COLRESET}\t${COLSUBJ}\5${COLRESET}\t${COLTAGS}\6${COLRESET}\t\1/" \
| column -s "$(printf '\t')" -t -l 3 -R 1,2
}
# List all available tags
list_tags() {
$NOTMUCH search --output=tags tag:/./
}
# List all email addresses
list_addresses() {
$NOTMUCH address '*'
}
# List query history
list_query_history() {
cat "$NMFHIST"
}

33
src/sh/notmuch.sh Normal file
View File

@@ -0,0 +1,33 @@
# Wrapper around notmuch
# Print the message id of the last message within a thread.
# @argument $1: thread id
nm_last_message_in_thread() {
$NOTMUCH search --output=messages --offset=-1 thread:"$1" | sed 's/^...//'
}
# Print the header of a message (with trailing empty line)
# @argument $1: message id
nm_message_header() {
$NOTMUCH show --body=false id:"$1" | awk "$AWK_MESSAGEHEADER"
}
# Print the message parts (with indents)
# @argument $1: message id
nm_message_parts() {
$NOTMUCH show --body=true id:"$1" | awk "$AWK_MESSAGEPARTS"
}
# Print the message part number for text/plain or text/html
# @argument $1: message id
# @argument $2: if set, then the html part number will be printed
nm_message_part_nr() {
$NOTMUCH show --body=true id:"$1" | awk -v html="${2:-}" "$AWK_PARTNR"
}
# Print part of message
# @argument $1: message id
# @argument $2: part number
nm_message_get_part() {
$NOTMUCH show --part="$2" id:"$1"
}

21
src/sh/preview.sh Normal file
View File

@@ -0,0 +1,21 @@
# Preview functions
# Preview email
# If a thread identifier is given, then the last message in the thread is
# shown. If the HTML flag is set, then the HTML message is parsed using pandoc.
# This functions reverts to the HTML method if no HTML flag is set yet no
# text/plain part is found.
#
# @argument $1: "id" or "thread"
# @argument $2: message id or thread id
# @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")"
[ "$html" ] && parser="$PANDOC" || parser="cat"
nm_message_header "$messageid" | $CATEMAIL
nm_message_get_part "$messageid" "$nr" | $parser | $CATMD
}

View File

@@ -1,12 +1,18 @@
# Default theme
if [ ! "${THEME_LOADED:-}" ]; then
COLDATE="$(printf '\033[38;5;195m')"
COLCNTS="$(printf '\033[38;5;244m')"
COLFROM="$(printf '\033[38;5;208m')"
COLSUBJ="$(printf '\033[38;5;179m\033[3m')"
COLTAGS="$(printf '\033[38;5;106m\033[2m')"
ANSICOLORDATE="${ANSICOLORDATE:-195}"
ANSICOLORCNTS="${ANSICOLORCNTS:-244}"
ANSICOLORFROM="${ANSICOLORFROM:-208}"
ANSICOLORSUBJ="${ANSICOLORSUBJ:-179}"
ANSICOLORTAGS="${ANSICOLORTAGS:-106}"
COLDATE="$(printf '\033[38;5;%sm' "$ANSICOLORDATE")"
COLCNTS="$(printf '\033[38;5;%sm' "$ANSICOLORCNTS")"
COLFROM="$(printf '\033[38;5;%sm' "$ANSICOLORFROM")"
COLSUBJ="$(printf '\033[38;5;%sm\033[3m' "$ANSICOLORSUBJ")"
COLTAGS="$(printf '\033[38;5;%sm\033[2m' "$ANSICOLORTAGS")"
COLRESET="$(printf '\033[0m')"
export ANSICOLORDATE ANSICOLORCNTS ANSICOLORFROM ANSICOLORSUBJ ANSICOLORTAGS
export COLDATE COLCNTS COLFROM COLSUBJ COLTAGS COLRESET
export THEME_LOADED=1

View File

@@ -1,6 +1,6 @@
if [ ! "${TOOLS_LOADED:-}" ]; then
if command -v "fzf" >/dev/null; then
FZF="fzf --black --ansi --cycle --tiebreak=chunk,index"
FZF="fzf --black --ansi --cycle --info=inline-right"
else
err "Did not find the command-line fuzzy finder fzf."
exit 1
@@ -20,9 +20,17 @@ if [ ! "${TOOLS_LOADED:-}" ]; then
elif command -v "batcat" >/dev/null; then
CAT="batcat"
fi
CAT=${CAT:+$CAT -l email --style=plain --color=always --wrap}
CAT=${CAT:-cat}
export CAT
CATEMAIL=${CAT:+$CAT -l email --style=plain --color=always}
CATMD=${CAT:+$CAT -l markdown --style=plain --color=always}
CATEMAIL=${CATEMAIL:-cat}
CATEMD=${CATMD:-cat}
export CATEMAIL CATMD
if command -v "pandoc" >/dev/null; then
PANDOC="pandoc --from html --to markdown_strict-raw_html"
fi
PANDOC=${PANDOC:-cat}
export PANDOC
export TOOLS_LOADED=1
fi