Merge branch 'vim-with-runtime' into feat/quickfix-title
[vim_extended.git] / runtime / macros / less.vim
blob959a5d73582300fceb3cf11e1bd867459cd1226f
1 " Vim script to work like "less"
2 " Maintainer:   Bram Moolenaar <Bram@vim.org>
3 " Last Change:  2006 Dec 05
5 " Avoid loading this file twice, allow the user to define his own script.
6 if exists("loaded_less")
7   finish
8 endif
9 let loaded_less = 1
11 " If not reading from stdin, skip files that can't be read.
12 " Exit if there is no file at all.
13 if 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
36 endif
38 set nocp
39 syntax on
40 set so=0
41 set hlsearch
42 set incsearch
43 nohlsearch
44 " Don't remember file names and positions
45 set viminfo=
46 set nows
47 " Inhibit screen updates while searching
48 let s:lz = &lz
49 set lz
51 " Used after each command: put cursor at end and display position
52 if &wrap
53   noremap <SID>L L0:redraw<CR>:file<CR>
54   au VimEnter * normal! L0
55 else
56   noremap <SID>L Lg0:redraw<CR>:file<CR>
57   au VimEnter * normal! Lg0
58 endif
60 " When reading from stdin don't consider the file modified.
61 au VimEnter * set nomod
63 " Can't modify the text
64 set noma
66 " Give help
67 noremap h :call <SID>Help()<CR>
68 map H h
69 fun! s:Help()
70   echo "<Space>   One page forward          b         One page backward"
71   echo "d         Half a page forward       u         Half a page backward"
72   echo "<Enter>   One line forward          k         One line backward"
73   echo "G         End of file               g         Start of file"
74   echo "N%        percentage in file"
75   echo "\n"
76   echo "/pattern  Search for pattern        ?pattern  Search backward for pattern"
77   echo "n         next pattern match        N         Previous pattern match"
78   echo "\n"
79   echo ":n<Enter> Next file                 :p<Enter> Previous file"
80   echo "\n"
81   echo "q         Quit                      v         Edit file"
82   let i = input("Hit Enter to continue")
83 endfun
85 " Scroll one page forward
86 noremap <script> <Space> :call <SID>NextPage()<CR><SID>L
87 map <C-V> <Space>
88 map f <Space>
89 map <C-F> <Space>
90 map z <Space>
91 map <Esc><Space> <Space>
92 fun! s:NextPage()
93   if line(".") == line("$")
94     if argidx() + 1 >= argc()
95       quit
96     endif
97     next
98     1
99   else
100     exe "normal! \<C-F>"
101   endif
102 endfun
104 " Re-read file and page forward "tail -f"
105 map F :e<CR>G<SID>L:sleep 1<CR>F
107 " Scroll half a page forward
108 noremap <script> d <C-D><SID>L
109 map <C-D> d
111 " Scroll one line forward
112 noremap <script> <CR> <C-E><SID>L
113 map <C-N> <CR>
114 map e <CR>
115 map <C-E> <CR>
116 map j <CR>
117 map <C-J> <CR>
119 " Scroll one page backward
120 noremap <script> b <C-B><SID>L
121 map <C-B> b
122 map w b
123 map <Esc>v b
125 " Scroll half a page backward
126 noremap <script> u <C-U><SID>L
127 noremap <script> <C-U> <C-U><SID>L
129 " Scroll one line backward
130 noremap <script> k <C-Y><SID>L
131 map y k
132 map <C-Y> k
133 map <C-P> k
134 map <C-K> k
136 " Redraw
137 noremap <script> r <C-L><SID>L
138 noremap <script> <C-R> <C-L><SID>L
139 noremap <script> R <C-L><SID>L
141 " Start of file
142 noremap <script> g gg<SID>L
143 map < g
144 map <Esc>< g
146 " End of file
147 noremap <script> G G<SID>L
148 map > G
149 map <Esc>> G
151 " Go to percentage
152 noremap <script> % %<SID>L
153 map p %
155 " Search
156 noremap <script> / H$:call <SID>Forward()<CR>/
157 if &wrap
158   noremap <script> ? H0:call <SID>Backward()<CR>?
159 else
160   noremap <script> ? Hg0:call <SID>Backward()<CR>?
161 endif
163 fun! s:Forward()
164   " Searching forward
165   noremap <script> n H$nzt<SID>L
166   if &wrap
167     noremap <script> N H0Nzt<SID>L
168   else
169     noremap <script> N Hg0Nzt<SID>L
170   endif
171   cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
172 endfun
174 fun! s:Backward()
175   " Searching backward
176   if &wrap
177     noremap <script> n H0nzt<SID>L
178   else
179     noremap <script> n Hg0nzt<SID>L
180   endif
181   noremap <script> N H$Nzt<SID>L
182   cnoremap <silent> <script> <CR> <CR>:cunmap <lt>CR><CR>zt<SID>L
183 endfun
185 call s:Forward()
187 " Quitting
188 noremap q :q<CR>
190 " Switch to editing (switch off less mode)
191 map v :silent call <SID>End()<CR>
192 fun! s:End()
193   set ma
194   if exists('s:lz')
195     let &lz = s:lz
196   endif
197   unmap h
198   unmap H
199   unmap <Space>
200   unmap <C-V>
201   unmap f
202   unmap <C-F>
203   unmap z
204   unmap <Esc><Space>
205   unmap F
206   unmap d
207   unmap <C-D>
208   unmap <CR>
209   unmap <C-N>
210   unmap e
211   unmap <C-E>
212   unmap j
213   unmap <C-J>
214   unmap b
215   unmap <C-B>
216   unmap w
217   unmap <Esc>v
218   unmap u
219   unmap <C-U>
220   unmap k
221   unmap y
222   unmap <C-Y>
223   unmap <C-P>
224   unmap <C-K>
225   unmap r
226   unmap <C-R>
227   unmap R
228   unmap g
229   unmap <
230   unmap <Esc><
231   unmap G
232   unmap >
233   unmap <Esc>>
234   unmap %
235   unmap p
236   unmap n
237   unmap N
238   unmap q
239   unmap v
240   unmap /
241   unmap ?
242 endfun
244 " vim: sw=2