Mercurial > vim
annotate runtime/ftplugin/man.vim @ 13619:41ed86454233
Added tag v8.0.1681 for changeset ea4f2a8040b4ab7111c02c71aa587e2ac121aa6b
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Tue, 10 Apr 2018 12:45:07 +0200 |
parents | 371ceeebbdaa |
children | e751b5c9dff3 |
rev | line source |
---|---|
7 | 1 " Vim filetype plugin file |
2 " Language: man | |
2034 | 3 " Maintainer: SungHyun Nam <goweol@gmail.com> |
13125 | 4 " Last Change: 2018 Jan 15 |
7 | 5 |
6 " To make the ":Man" command available before editing a manual page, source | |
7 " this script from your startup vimrc file. | |
8 | |
9 " If 'filetype' isn't "man", we must have been called to only define ":Man". | |
10 if &filetype == "man" | |
11 | |
12 " Only do this when not done yet for this buffer | |
13 if exists("b:did_ftplugin") | |
14 finish | |
15 endif | |
16 let b:did_ftplugin = 1 | |
17 | |
18 " allow dot and dash in manual page name. | |
19 setlocal iskeyword+=\.,- | |
20 | |
21 " Add mappings, unless the user didn't want this. | |
22 if !exists("no_plugin_maps") && !exists("no_man_maps") | |
23 if !hasmapto('<Plug>ManBS') | |
24 nmap <buffer> <LocalLeader>h <Plug>ManBS | |
25 endif | |
816 | 26 nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:setl nomod<CR>'' |
7 | 27 |
28 nnoremap <buffer> <c-]> :call <SID>PreGetPage(v:count)<CR> | |
29 nnoremap <buffer> <c-t> :call <SID>PopPage()<CR> | |
7272
17333ebd2bbd
commit https://github.com/vim/vim/commit/d042dc825c9b97dacd84d4728f88300da4d5b6b9
Christian Brabandt <cb@256bit.org>
parents:
6476
diff
changeset
|
30 nnoremap <buffer> <silent> q :q<CR> |
17333ebd2bbd
commit https://github.com/vim/vim/commit/d042dc825c9b97dacd84d4728f88300da4d5b6b9
Christian Brabandt <cb@256bit.org>
parents:
6476
diff
changeset
|
31 endif |
17333ebd2bbd
commit https://github.com/vim/vim/commit/d042dc825c9b97dacd84d4728f88300da4d5b6b9
Christian Brabandt <cb@256bit.org>
parents:
6476
diff
changeset
|
32 |
17333ebd2bbd
commit https://github.com/vim/vim/commit/d042dc825c9b97dacd84d4728f88300da4d5b6b9
Christian Brabandt <cb@256bit.org>
parents:
6476
diff
changeset
|
33 if exists('g:ft_man_folding_enable') && (g:ft_man_folding_enable == 1) |
17333ebd2bbd
commit https://github.com/vim/vim/commit/d042dc825c9b97dacd84d4728f88300da4d5b6b9
Christian Brabandt <cb@256bit.org>
parents:
6476
diff
changeset
|
34 setlocal foldmethod=indent foldnestmax=1 foldenable |
7 | 35 endif |
36 | |
3410
94601b379f38
Updated runtime files. Add Dutch translations.
Bram Moolenaar <bram@vim.org>
parents:
3082
diff
changeset
|
37 let b:undo_ftplugin = "setlocal iskeyword<" |
94601b379f38
Updated runtime files. Add Dutch translations.
Bram Moolenaar <bram@vim.org>
parents:
3082
diff
changeset
|
38 |
7 | 39 endif |
40 | |
41 if exists(":Man") != 2 | |
13125 | 42 com -nargs=+ -complete=shellcmd Man call s:GetPage(<f-args>) |
7 | 43 nmap <Leader>K :call <SID>PreGetPage(0)<CR> |
10548 | 44 nmap <Plug>ManPreGetPage :call <SID>PreGetPage(0)<CR> |
7 | 45 endif |
46 | |
47 " Define functions only once. | |
48 if !exists("s:man_tag_depth") | |
49 | |
50 let s:man_tag_depth = 0 | |
51 | |
1623 | 52 let s:man_sect_arg = "" |
53 let s:man_find_arg = "-w" | |
54 try | |
55 if !has("win32") && $OSTYPE !~ 'cygwin\|linux' && system('uname -s') =~ "SunOS" && system('uname -r') =~ "^5" | |
56 let s:man_sect_arg = "-s" | |
57 let s:man_find_arg = "-l" | |
58 endif | |
59 catch /E145:/ | |
60 " Ignore the error in restricted mode | |
61 endtry | |
7 | 62 |
63 func <SID>PreGetPage(cnt) | |
64 if a:cnt == 0 | |
65 let old_isk = &iskeyword | |
6369 | 66 if &ft == 'man' |
67 setl iskeyword+=(,) | |
68 endif | |
7 | 69 let str = expand("<cword>") |
70 let &l:iskeyword = old_isk | |
71 let page = substitute(str, '(*\(\k\+\).*', '\1', '') | |
72 let sect = substitute(str, '\(\k\+\)(\([^()]*\)).*', '\2', '') | |
73 if match(sect, '^[0-9 ]\+$') == -1 | |
74 let sect = "" | |
75 endif | |
76 if sect == page | |
77 let sect = "" | |
78 endif | |
79 else | |
80 let sect = a:cnt | |
81 let page = expand("<cword>") | |
82 endif | |
83 call s:GetPage(sect, page) | |
84 endfunc | |
85 | |
86 func <SID>GetCmdArg(sect, page) | |
87 if a:sect == '' | |
88 return a:page | |
89 endif | |
90 return s:man_sect_arg.' '.a:sect.' '.a:page | |
91 endfunc | |
92 | |
93 func <SID>FindPage(sect, page) | |
10734 | 94 let where = system("man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page)) |
7 | 95 if where !~ "^/" |
96 if matchstr(where, " [^ ]*$") !~ "^ /" | |
97 return 0 | |
98 endif | |
99 endif | |
100 return 1 | |
101 endfunc | |
102 | |
103 func <SID>GetPage(...) | |
104 if a:0 >= 2 | |
105 let sect = a:1 | |
106 let page = a:2 | |
107 elseif a:0 >= 1 | |
108 let sect = "" | |
109 let page = a:1 | |
110 else | |
111 return | |
112 endif | |
113 | |
114 " To support: nmap K :Man <cword> | |
115 if page == '<cword>' | |
116 let page = expand('<cword>') | |
117 endif | |
118 | |
119 if sect != "" && s:FindPage(sect, page) == 0 | |
120 let sect = "" | |
121 endif | |
122 if s:FindPage(sect, page) == 0 | |
123 echo "\nCannot find a '".page."'." | |
124 return | |
125 endif | |
126 exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%") | |
127 exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".") | |
128 exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".") | |
129 let s:man_tag_depth = s:man_tag_depth + 1 | |
130 | |
131 " Use an existing "man" window if it exists, otherwise open a new one. | |
132 if &filetype != "man" | |
133 let thiswin = winnr() | |
134 exe "norm! \<C-W>b" | |
1125 | 135 if winnr() > 1 |
7 | 136 exe "norm! " . thiswin . "\<C-W>w" |
137 while 1 | |
138 if &filetype == "man" | |
139 break | |
140 endif | |
141 exe "norm! \<C-W>w" | |
142 if thiswin == winnr() | |
143 break | |
144 endif | |
145 endwhile | |
146 endif | |
1125 | 147 if &filetype != "man" |
9326
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
148 if exists("g:ft_man_open_mode") |
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
149 if g:ft_man_open_mode == "vert" |
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
150 vnew |
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
151 elseif g:ft_man_open_mode == "tab" |
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
152 tabnew |
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
153 else |
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
154 new |
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
155 endif |
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
156 else |
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
157 new |
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
158 endif |
1125 | 159 setl nonu fdc=0 |
160 endif | |
7 | 161 endif |
162 silent exec "edit $HOME/".page.".".sect."~" | |
163 " Avoid warning for editing the dummy file twice | |
816 | 164 setl buftype=nofile noswapfile |
7 | 165 |
5220 | 166 setl ma nonu nornu nofen |
7 | 167 silent exec "norm 1GdG" |
9326
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
168 let unsetwidth = 0 |
7992
78106b0f2c56
commit https://github.com/vim/vim/commit/cbebd4879cc78e670d79b2c57dc33d7b911c962a
Christian Brabandt <cb@256bit.org>
parents:
7272
diff
changeset
|
169 if empty($MANWIDTH) |
78106b0f2c56
commit https://github.com/vim/vim/commit/cbebd4879cc78e670d79b2c57dc33d7b911c962a
Christian Brabandt <cb@256bit.org>
parents:
7272
diff
changeset
|
170 let $MANWIDTH = winwidth(0) |
9326
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
171 let unsetwidth = 1 |
7992
78106b0f2c56
commit https://github.com/vim/vim/commit/cbebd4879cc78e670d79b2c57dc33d7b911c962a
Christian Brabandt <cb@256bit.org>
parents:
7272
diff
changeset
|
172 endif |
12826 | 173 |
174 " Ensure Vim is not recursively invoked (man-db does this) when doing ctrl-[ | |
175 " on a man page reference by unsetting MANPAGER. | |
13125 | 176 " Some versions of env(1) do not support the '-u' option, and in such case |
177 " we set MANPAGER=cat. | |
178 if !exists('s:env_has_u') | |
179 call system('env -u x true') | |
180 let s:env_has_u = (v:shell_error == 0) | |
181 endif | |
182 let env_cmd = s:env_has_u ? 'env -u MANPAGER' : 'env MANPAGER=cat' | |
183 let man_cmd = env_cmd . ' man ' . s:GetCmdArg(sect, page) . ' | col -b' | |
184 silent exec "r !" . man_cmd | |
12826 | 185 |
9326
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
186 if unsetwidth |
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
187 let $MANWIDTH = '' |
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
188 endif |
7 | 189 " Remove blank lines from top and bottom. |
190 while getline(1) =~ '^\s*$' | |
6476 | 191 silent keepj norm ggdd |
7 | 192 endwhile |
193 while getline('$') =~ '^\s*$' | |
6476 | 194 silent keepj norm Gdd |
7 | 195 endwhile |
196 1 | |
197 setl ft=man nomod | |
198 setl bufhidden=hide | |
199 setl nobuflisted | |
9326
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
200 setl noma |
7 | 201 endfunc |
202 | |
203 func <SID>PopPage() | |
204 if s:man_tag_depth > 0 | |
205 let s:man_tag_depth = s:man_tag_depth - 1 | |
206 exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth | |
207 exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth | |
208 exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth | |
209 exec s:man_tag_buf."b" | |
210 exec s:man_tag_lin | |
211 exec "norm ".s:man_tag_col."|" | |
212 exec "unlet s:man_tag_buf_".s:man_tag_depth | |
213 exec "unlet s:man_tag_lin_".s:man_tag_depth | |
214 exec "unlet s:man_tag_col_".s:man_tag_depth | |
215 unlet s:man_tag_buf s:man_tag_lin s:man_tag_col | |
216 endif | |
217 endfunc | |
218 | |
219 endif | |
220 | |
9326
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
221 " vim: set sw=2 ts=8 noet: |