aboutsummaryrefslogtreecommitdiff
path: root/mless
blob: 97bce56cecf67bf23d4f8224aebfc99fcdc87e84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/sh
# mless [MSG] - less(1)-wrapper around mshow

PATH="${0%/*}:$PATH"

colormsg() {
awk '
function fg(c, s) { return sprintf("\033[38;5;%03dm%s\033[0m", c, s) }
function so(s) { return sprintf("\033[1m%s\033[0m", s) }
BEGIN { hdr = 1 }
/^$/ { hdr = 0 }
/^-- $/ { ftr = 1 }
/^--- .* ---/ { print fg(242, $0); ftr = 0; sig = 0; next }
/^-----BEGIN .* SIGNATURE-----/ { sig = 1 }
hdr && /^From:/ { print so(fg(119, $0)); next }
hdr { print fg(120, $0); next }
ftr { print fg(244, $0); next }
/^-----BEGIN .* MESSAGE-----/  ||
/^-----END .* SIGNATURE-----/ { print fg(244, $0); sig = 0; next }
sig { print fg(244, $0); next }
/^>/ { print fg(151, $0); next }
{ print }'
}

colorscan() {
awk '
function fg(c, s) { return sprintf("\033[38;5;%03dm%s\033[0m", c, s) }
function so(s) { return sprintf("\033[1m%s\033[0m", s) }
/^>/ { print so(fg(119, $0)); next }
/^ *\\_/ { print fg(242, $0); next }
{ print }'
}

if [ "$1" = --filter ]; then
	if [ "$2" = //scan ]; then
		mscan : 2>/dev/null | colorscan
		exit $?
	fi

	mseq -C "$2"
	mscan .-2:.+3 2>/dev/null | colorscan
	echo

	if ! [ -f "$(mseq -r "$2")" ]; then
		mseq "$2"
		exit
	fi

	if [ $MLESS_RAW -eq 0 ]; then
		mshow "$2" | colormsg
	else
		mseq -r $2
		echo
		cat "$(mseq -r $2)"
	fi
	exit $?
fi

if [ "$#" -eq 0 ] && ! [ -t 0 ]; then
	mseq -S >/dev/null
	set -- :
fi

if ! [ -t 1 ]; then
	exec mseq :
fi

case "$0" in
	*mnext) set -- +;;
	*mprev) set -- -;;
	*) set -- ${1:-.};;
esac

if [ "$#" -eq 1 ]; then
	mseq -C "$1"
fi

nl="
"
export MLESS_RAW=0
while :; do
	[ -f $HOME/.mless ] && export LESSKEY=$HOME/.mless
	LESSOPEN="|$0 --filter %s" \
		less -Ps"mless %f?m (message %i of %m).." -R \
			"+:e $(mseq -n .)$nl" //scan $(mseq -n :)
	case "$?" in
	0|1) exit $?;;
	78) # N go to next unseen message
		mseq -C "$(mseq -r .: |
			awk -F':2,' '$1 !~ /^</ && $2 !~ /S/ { print; exit }')";;
	107) # k next thread
		mseq -C "$(mseq .+1: | grep -m1 '^[^ <]')";;
	100) # d mark read
		mflag -S .
		mseq -f : | mseq -S
		mseq -C +
		;;
	82) # R toggle raw mode
		MLESS_RAW=$((1-$MLESS_RAW))
	esac
done