1" Vim script to work like "less"
2" Maintainer:	Bram Moolenaar <Bram@vim.org>
3" Last Change:	2020 Dec 17
4
5" Avoid loading this file twice, allow the user to define his own script.
6if exists("loaded_less")
7  finish
8endif
9let loaded_less = 1
10
11" If not reading from stdin, skip files that can't be read.
12" Exit if there is no file at all.
13if argc() > 0
14  let s:i = 0
15  while 1
16    if filereadable(argv(s:i))
17      if s:i != 0
18	sleep 3
19      endif
20      break
21    endif
22    if isdirectory(argv(s:i))
23      echomsg "Skipping directory " . argv(s:i)
24    elseif getftime(argv(s:i)) < 0
25      echomsg "Skipping non-existing file " . argv(s:i)
26    else
27      echomsg "Skipping unreadable file " . argv(s:i)
28    endif
29    echo "\n"
30    let s:i = s:i + 1
31    if s:i == argc()
32      quit
33    endif
34    next
35  endwhile
36endif
37
38" we don't want 'compatible' here
39if &cp
40  set nocp
41endif
42
43" enable syntax highlighting if not done already
44if !get(g:, 'syntax_on', 0)
45  syntax enable
46endif
47
48set so=0
49set hlsearch
50set incsearch
51nohlsearch
52" Don't remember file names and positions
53set shada=
54set nows
55" Inhibit screen updates while searching
56let s:lz = &lz
57set lz
58
59" Allow the user to define a function, which can set options specifically for
60" this script.
61if exists('*LessInitFunc')
62  call LessInitFunc()
63endif
64
65" Used after each command: put cursor at end and display position
66if &wrap
67  noremap <SID>L L0:redraw<CR>:file<CR>
68  au VimEnter * normal! L0
69else
70  noremap <SID>L Lg0:redraw<CR>:file<CR>
71  au VimEnter * normal! Lg0
72endif
73
74" When reading from stdin don't consider the file modified.
75au VimEnter * set nomod
76
77" Can't modify the text or write the file.
78set nomodifiable readonly
79
80" Give help
81noremap h :call <SID>Help()<CR>
82map H h
83fun! s:Help()
84  echo "<Space>   One page forward          b         One page backward"
85  echo "d         Half a page forward       u         Half a page backward"
86  echo "<Enter>   One line forward          k         One line backward"
87  echo "G         End of file               g         Start of file"
88  echo "N%        percentage in file"
89  echo "\n"
90  echo "/pattern  Search for pattern        ?pattern  Search backward for pattern"
91  echo "n         next pattern match        N         Previous pattern match"
92  if &foldmethod != "manual"
93  echo "\n"
94    echo "zR        open all folds            zm        increase fold level"
95  endif
96  echo "\n"
97  echo ":n<Enter> Next file                 :p<Enter> Previous file"
98  echo "\n"
99  echo "q         Quit                      v         Edit file"
100  let i = input("Hit Enter to continue")
101endfun
102
103" Scroll one page forward
104noremap <script> <Space> :call <SID>NextPage()<CR><SID>L
105map <C-V> <Space>
106map f <Space>
107map <C-F> <Space>
108map <PageDown> <Space>
109map <kPageDown> <Space>
110map <S-Down> <Space>
111" If 'foldmethod' was changed keep the "z" commands, e.g. "zR" to open all
112" folds.
113if &foldmethod == "manual"
114  map z <Space>
115endif
116map <Esc><Space> <Space>
117fun! s:NextPage()
118  if line(".") == line("$")
119    if argidx() + 1 >= argc()
120      " Don't quit at the end of the last file
121      return
122    endif
123    next
124    1
125  else
126    exe "normal! \<C-F>"
127  endif
128endfun
129
130" Re-read file and page forward "tail -f"
131map F :e<CR>G<SID>L:sleep 1<CR>F
132
133" Scroll half a page forward
134noremap <script> d <C-D><SID>L
135map <C-D> d
136
137" Scroll one line forward
138noremap <script> <CR> <C-E><SID>L
139map <C-N> <CR>
140map e <CR>
141map <C-E> <CR>
142map j <CR>
143map <C-J> <CR>
144map <Down> <CR>
145
146" Scroll one page backward
147noremap <script> b <C-B><SID>L
148map <C-B> b
149map <PageUp> b
150map <kPageUp> b
151map <S-Up> b
152map w b
153map <Esc>v b
154
155" Scroll half a page backward
156noremap <script> u <C-U><SID>L
157noremap <script> <C-U> <C-U><SID>L
158
159" Scroll one line backward
160noremap <script> k <C-Y><SID>L
161map y k
162map <C-Y> k
163map <C-P> k
164map <C-K> k
165map <Up> k
166
167" Redraw
168noremap <script> r <C-L><SID>L
169noremap <script> <C-R> <C-L><SID>L
170noremap <script> R <C-L><SID>L
171
172" Start of file
173noremap <script> g gg<SID>L
174map < g
175map <Esc>< g
176map <Home> g
177map <kHome> g
178
179" End of file
180noremap <script> G G<SID>L
181map > G
182map <Esc>> G
183map <End> G
184map <kEnd> G
185
186" Go to percentage
187noremap <script> % %<SID>L
188map p %
189
190" Search
191noremap <script> / H$:call <SID>Forward()<CR>/
192if &wrap
193  noremap <script> ? H0:call <SID>Backward()<CR>?
194else
195  noremap <script> ? Hg0:call <SID>Backward()<CR>?
196endif
197
198fun! s:Forward()
199  " Searching forward
200  noremap <script> n H$nzt<SID>L
201  if &wrap
202    noremap <script> N H0Nzt<SID>L
203  else
204    noremap <script> N Hg0Nzt<SID>L
205  endif
206  cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
207endfun
208
209fun! s:Backward()
210  " Searching backward
211  if &wrap
212    noremap <script> n H0nzt<SID>L
213  else
214    noremap <script> n Hg0nzt<SID>L
215  endif
216  noremap <script> N H$Nzt<SID>L
217  cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
218endfun
219
220call s:Forward()
221cunmap <CR>
222
223" Quitting
224noremap q :q<CR>
225
226" Switch to editing (switch off less mode)
227map v :silent call <SID>End()<CR>
228fun! s:End()
229  set ma
230  if exists('s:lz')
231    let &lz = s:lz
232  endif
233  unmap h
234  unmap H
235  unmap <Space>
236  unmap <C-V>
237  unmap f
238  unmap <C-F>
239  unmap z
240  unmap <Esc><Space>
241  unmap F
242  unmap d
243  unmap <C-D>
244  unmap <CR>
245  unmap <C-N>
246  unmap e
247  unmap <C-E>
248  unmap j
249  unmap <C-J>
250  unmap b
251  unmap <C-B>
252  unmap w
253  unmap <Esc>v
254  unmap u
255  unmap <C-U>
256  unmap k
257  unmap y
258  unmap <C-Y>
259  unmap <C-P>
260  unmap <C-K>
261  unmap r
262  unmap <C-R>
263  unmap R
264  unmap g
265  unmap <
266  unmap <Esc><
267  unmap G
268  unmap >
269  unmap <Esc>>
270  unmap %
271  unmap p
272  unmap n
273  unmap N
274  unmap q
275  unmap v
276  unmap /
277  unmap ?
278  unmap <Up>
279  unmap <Down>
280  unmap <PageDown>
281  unmap <kPageDown>
282  unmap <PageUp>
283  unmap <kPageUp>
284  unmap <S-Down>
285  unmap <S-Up>
286  unmap <Home>
287  unmap <kHome>
288  unmap <End>
289  unmap <kEnd>
290endfun
291
292" vim: sw=2
293