* qma: use vim in less-like mode, thanks - wuehlmaus!
[grml-scripts.git] / usr_bin / qma
1 #!/bin/zsh
2 # Filename:      qma
3 # Purpose:       "quick manual access"
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>, Matthias Kopfermann <maddi@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 # Latest change: Mon Dez 04 22:54:47 CET 2006 [maddi]
8 ################################################################################
9
10 . /etc/grml/lsb-functions
11 . /etc/grml/script-functions
12
13 check4progs man lzop vim || exit 1
14
15 VERSION='0.1'
16
17 MANDIR=${MANDIR:-$HOME/man}
18 if ! [ -d "$MANDIR" ] ; then
19    einfo "Creating $MANDIR."
20    mkdir $MANDIR ; eend $?
21 fi
22
23 usage() {
24    eerror "Usage: qma [section] manpage" ; eend 1
25    exit 1
26 }
27
28 case $1 in
29   -h*|--h*)
30     usage
31     ;;
32   -v*|--v*)
33     einfo "qma - version $VERSION" ; eend 0
34     exit 0
35     ;;
36   [0-9])
37     SECTION="${1}"
38     SECTIONFILE=".${1}"
39     MANPAGE="$2"
40     SEARCH="$3"
41     ;;
42   *)
43     SECTION=''
44     MANPAGE="$1"
45     SEARCH="$2"
46     ;;
47 esac
48
49
50 if [ -z "$MANPAGE" ] ; then
51    usage
52 fi
53
54 QUICKMAN="${MANDIR}/${MANPAGE}${SECTIONFILE}.txt.lzo"
55
56 if ! [ -f "$QUICKMAN" ] ; then
57    einfo "Writing manpage to $QUICKMAN"
58    if man $SECTION $MANPAGE 1>/dev/null ; then
59       man $SECTION $MANPAGE | lzop -U >$QUICKMAN ; eend $?
60       einfo "Compressing manpage with lzop"
61       eend $?
62    else
63       exit 1
64       eend 1
65    fi
66 fi
67
68 vimconfig='
69          set filetype=man
70          "set hlsearch can be annoying so use <C-L> to dehighlight"
71          nn <silent> <C-L> :nohlsearch<cr>|redraw "shut up hlsearch when it confuses more than it helps and"
72          nn q :qa!<cr> "q should really end our document-reading like less does"
73          nn Q :q!<cr> "different than q in that it quits only the current window when multiple windows "
74          nn <F10> :qa!<cr> "honor midnight-commander addicts :) "
75          nn a ]I "use one wonderful vim feature to show all matches of the word in the document in ex-mode"
76          nn A [I "the other way round"
77          nn b <C-b>
78          nn <backspace> <C-f>
79          "nn f <C-f>
80          nn p }zz
81          nn j <C-E> "scroll the page down one line"
82          nn k <C-Y> "scroll the page up one line"
83          nn -n <ESC> :set invnumber<cr>  "the expectation of getting the next search via 'n' can not be ignored"
84          nn h :set invhlsearch<cr>
85          nn i :set invignorecase<cr> "Make toggling case-sensitive accessable via the letter i"
86          nn -i :set invignorecase<cr> "Make toggling case-sensitive accessable via the less convention via -i"
87          nn <space> <C-f>
88          nn <backspace> <C-b>
89          nn <enter> gg
90          nn S :split<cr>
91          nn V :vsplit<cr>
92         set filetype=man
93         set ignorecase
94         set nomodifiable "Really not modify a document here"
95         set readonly
96 '
97
98 if [ -n "$SEARCH" ] ; then
99   vim -S  =( echo $vimconfig  ) +/"$SEARCH" =( lzop -cd ${QUICKMAN} )
100 else
101   vim -S =( echo $vimconfig  ) =( lzop -cd $QUICKMAN )
102 fi
103
104 einfo "Thanks for flying qma using vim and lzop." ; eend 0
105
106 ## END OF FILE #################################################################