summaryrefslogtreecommitdiffstats
path: root/vimpager
blob: 554c60f290429d3738f1b643cae4f1b4f1077917 (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
#!/bin/sh

# Script for using ViM as a PAGER.
# Based on Bram's less.sh.
# Version 1.4.3
# git://github.com/rkitover/vimpager.git

# Just pass through if not on a tty
if [ ! -t 1 ]; then
	exec cat "$@"
fi

case `uname -s` in
	Cygwin) cygwin=1 ;;
	Linux) linux=1 ;;
	SunOS) solaris=1 ;;
	*) bsd=1 ;;
esac

less_vim() {
	vim -R \
	-c 'let no_plugin_maps = 1' \
	-c 'set scrolloff=999' \
	-c 'runtime! macros/less.vim' \
	-c 'set foldlevel=999' \
	-c 'set mouse=h' \
	-c 'set nonu' \
	-c 'nmap <ESC>u :nohlsearch<cr>' \
	"${@:--}"
}

awk_pstree() {
	awk -v mypid=$1 '{
		cmd[$1]=$3
		ppid[$1]=$2
	}
	END {
		while (mypid != 1 && cmd[mypid]) {
			ptree=mypid " " cmd[mypid] "\n" ptree
			mypid=ppid[mypid]
		}
		print ptree
	}'
}

do_ptree() {
	if [ $solaris ]; then
		# Tested on Solaris 8 and 10
		ptree $$
	else
		# Tested on Linux and OS X
		ps awo pid=,ppid=,comm= | awk_pstree $$
	fi
}

# Check if called from man, perldoc or pydoc
ptree="`do_ptree`"
if echo "$ptree" | awk '$2 ~ /(^|\/)(man|perl(doc)?([0-9.]*)?|py(thon|doc|doc2))/ {t=1} END { exit 1-t }'; then
	sed -e 's/\[[^m]*m//g' -e 's/.//g' "${@:--}" \
		| less_vim -c 'set ft=man' -c "set nonumber" -
	exit
fi

case "$@" in
	*.gz) gunzip -c "${@:--}" | sed -e 's/\[[^m]*m//g' -e 's/.//g' | less_vim - ;;
	*.Z) uncompress -c "${@:--}" | sed -e 's/\[[^m]*m//g' -e 's/.//g' | less_vim - ;;
	*) sed -e 's/\[[^m]*m//g' -e 's/.//g' "${@:--}" | less_vim - ;;
esac


# CONTRIBUTORS:
#
# Rafael Kitover
# Antonio Ospite
# Jean-Marie Gaillourdet
# Perry Hargrave
# Koen Smits
# Ivan S. Freitas <ivansichfreitas@gmail.com>
# Wout Mertens (Solaris compatibility, less processes)