Release new version 2.13.0
[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 ################################################################################
8
9 . /etc/grml/lsb-functions
10 . /etc/grml/script-functions
11
12 check4progs man lzop vim || exit 1
13
14 VERSION='0.1'
15
16 MANDIR=${MANDIR:-$HOME/man}
17 if ! [ -d "$MANDIR" ] ; then
18    einfo "Creating $MANDIR."
19    mkdir $MANDIR ; eend $?
20 fi
21
22 usage() {
23    eerror "Usage: qma [section] manpage" ; eend 1
24    exit 1
25 }
26
27 case $1 in
28   -h*|--h*)
29     usage
30     ;;
31   -v*|--v*)
32     einfo "qma - version $VERSION" ; eend 0
33     exit 0
34     ;;
35   [0-9])
36     SECTION="${1}"
37     SECTIONFILE=".${1}"
38     MANPAGE="$2"
39     SEARCH="$3"
40     ;;
41   *)
42     SECTION=''
43     MANPAGE="$1"
44     SEARCH="$2"
45     ;;
46 esac
47
48
49 if [ -z "$MANPAGE" ] ; then
50    usage
51 fi
52
53 QUICKMAN="${MANDIR}/${MANPAGE}${SECTIONFILE}.txt.lzo"
54
55 if ! [ -f "$QUICKMAN" ] ; then
56    einfo "Writing manpage to $QUICKMAN"
57    if man $SECTION $MANPAGE 1>/dev/null ; then
58       man $SECTION $MANPAGE | col -b | lzop -U >$QUICKMAN ; eend $?
59       einfo "Compressing manpage with lzop"
60       eend $?
61    else
62       exit 1
63       eend 1
64    fi
65 fi
66
67 vimconfig='
68          set filetype=man
69          "set hlsearch can be annoying so use <C-L> to dehighlight"
70         "shut up hlsearch when it confuses more than it helps and"
71          nn <silent> <C-L> :nohlsearch<cr>|redraw
72         "q should really end our document-reading like less does"
73          nn q :qa!<cr>
74         "different than q in that it quits only the current window when multiple windows "
75          nn Q :q!<cr>
76         "honor midnight-commander addicts :) "
77          nn <F10> :qa!<cr>
78         "use one wonderful vim feature to show all matches of the word in the document in ex-mode"
79          nn a ]I
80         "the other way round"
81          nn A [I
82          nn b <C-b>
83          nn <backspace> <C-f>
84          "nn f <C-f>
85          nn p }zz
86         "scroll the page down one line"
87          nn j <C-E>
88         "scroll the page up one line"
89          nn k <C-Y>
90         "the expectation of getting the next search via 'n' can not be ignored"
91          nn -n <ESC> :set invnumber<cr>
92          nn h :set invhlsearch<cr>
93         "Make toggling case-sensitive accessible via the letter i"
94          nn i :set invignorecase<cr>
95         "Make toggling case-sensitive accessible via the less convention via -i"
96          nn -i :set invignorecase<cr>
97          nn <space> <C-f>
98          nn <backspace> <C-b>
99          nn <enter> gg
100          nn S :split<cr>
101          nn V :vsplit<cr>
102         set filetype=man
103         set ignorecase
104         "Really not modify a document here"
105         set nomodifiable
106         set readonly
107         "We have a more modern approach to searching in vim!"
108         set incsearch
109 '
110
111 if [ -n "$SEARCH" ] ; then
112   vim -S  =( echo $vimconfig  ) +/"$SEARCH" =( lzop -cd ${QUICKMAN} )
113 else
114   vim -S =( echo $vimconfig  ) =( lzop -cd $QUICKMAN )
115 fi
116
117 einfo "Thanks for flying qma using vim and lzop." ; eend 0
118
119 ## END OF FILE #################################################################