summary refs log tree commit diff
path: root/mless
blob: 6111a60fb9bda5c15dcee9e6183db16afc4b6b2e (plain) (blame)
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
#!/bin/sh
# mless RANGES... - 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 }
hdr && /^From:/ { print so(fg(119, $0)); next }
hdr { print fg(120, $0); next }
/^--- .* ---/ { print fg(242, $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 }'
}

mnums() {
	mscan "$@" 2>/dev/null | awk '$2 ~ /^[0-9]*$/ { print $2 }'
}

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

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

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

	mshow "$2" | colormsg
	exit $?
fi

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

if ! [ -t 1 ]; then
	exec mseq "$@"
fi

case "$0" in
	*next*) d=1;;
	*prev*) d=-1;;
	*) d=0;;
esac
curcmd=$(mnums "$@" |
	awk -v cur=$(mseq -n .) -v d=$d '$1 == cur {print "+" FNR+d ":x"; quit}')

LESSOPEN="|$0 --filter %s" exec less -Ps"mless %f?m (message %i of %m).." -R $curcmd $(mnums "$@")