Mercurial > vim
annotate runtime/ftplugin/man.vim @ 25424:3cf272d6ee04
Added tag v8.2.3248 for changeset 3e56078569cadbe81751d894a3936329836a16da
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 29 Jul 2021 22:30:05 +0200 |
parents | 5b7ea82bc18f |
children | 9c221ad9634a |
rev | line source |
---|---|
7 | 1 " Vim filetype plugin file |
2 " Language: man | |
20241 | 3 " Maintainer: Jason Franklin <vim@justemail.net> |
20317 | 4 " Maintainer: SungHyun Nam <goweol@gmail.com> |
22723 | 5 " Last Change: 2020 Oct 09 |
7 | 6 |
7 " To make the ":Man" command available before editing a manual page, source | |
8 " this script from your startup vimrc file. | |
9 | |
10 " If 'filetype' isn't "man", we must have been called to only define ":Man". | |
11 if &filetype == "man" | |
12 | |
13 " Only do this when not done yet for this buffer | |
14 if exists("b:did_ftplugin") | |
15 finish | |
16 endif | |
17 let b:did_ftplugin = 1 | |
14421 | 18 endif |
7 | 19 |
14421 | 20 let s:cpo_save = &cpo |
21 set cpo-=C | |
22 | |
23 if &filetype == "man" | |
7 | 24 " allow dot and dash in manual page name. |
25 setlocal iskeyword+=\.,- | |
14421 | 26 let b:undo_ftplugin = "setlocal iskeyword<" |
7 | 27 |
28 " Add mappings, unless the user didn't want this. | |
29 if !exists("no_plugin_maps") && !exists("no_man_maps") | |
30 if !hasmapto('<Plug>ManBS') | |
31 nmap <buffer> <LocalLeader>h <Plug>ManBS | |
14421 | 32 let b:undo_ftplugin = b:undo_ftplugin |
33 \ . '|silent! nunmap <buffer> <LocalLeader>h' | |
7 | 34 endif |
816 | 35 nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:setl nomod<CR>'' |
7 | 36 |
20753 | 37 nnoremap <buffer> <silent> <c-]> :call <SID>PreGetPage(v:count)<CR> |
38 nnoremap <buffer> <silent> <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
|
39 nnoremap <buffer> <silent> q :q<CR> |
14421 | 40 |
41 " Add undo commands for the maps | |
42 let b:undo_ftplugin = b:undo_ftplugin | |
43 \ . '|silent! nunmap <buffer> <Plug>ManBS' | |
44 \ . '|silent! nunmap <buffer> <c-]>' | |
45 \ . '|silent! nunmap <buffer> <c-t>' | |
46 \ . '|silent! nunmap <buffer> q' | |
7272
17333ebd2bbd
commit https://github.com/vim/vim/commit/d042dc825c9b97dacd84d4728f88300da4d5b6b9
Christian Brabandt <cb@256bit.org>
parents:
6476
diff
changeset
|
47 endif |
17333ebd2bbd
commit https://github.com/vim/vim/commit/d042dc825c9b97dacd84d4728f88300da4d5b6b9
Christian Brabandt <cb@256bit.org>
parents:
6476
diff
changeset
|
48 |
17333ebd2bbd
commit https://github.com/vim/vim/commit/d042dc825c9b97dacd84d4728f88300da4d5b6b9
Christian Brabandt <cb@256bit.org>
parents:
6476
diff
changeset
|
49 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
|
50 setlocal foldmethod=indent foldnestmax=1 foldenable |
14421 | 51 let b:undo_ftplugin = b:undo_ftplugin |
52 \ . '|silent! setl fdm< fdn< fen<' | |
7 | 53 endif |
54 | |
55 endif | |
56 | |
57 if exists(":Man") != 2 | |
14421 | 58 com -nargs=+ -complete=shellcmd Man call s:GetPage(<q-mods>, <f-args>) |
7 | 59 nmap <Leader>K :call <SID>PreGetPage(0)<CR> |
10548 | 60 nmap <Plug>ManPreGetPage :call <SID>PreGetPage(0)<CR> |
7 | 61 endif |
62 | |
63 " Define functions only once. | |
64 if !exists("s:man_tag_depth") | |
65 | |
66 let s:man_tag_depth = 0 | |
67 | |
1623 | 68 let s:man_sect_arg = "" |
69 let s:man_find_arg = "-w" | |
70 try | |
71 if !has("win32") && $OSTYPE !~ 'cygwin\|linux' && system('uname -s') =~ "SunOS" && system('uname -r') =~ "^5" | |
72 let s:man_sect_arg = "-s" | |
73 let s:man_find_arg = "-l" | |
74 endif | |
75 catch /E145:/ | |
76 " Ignore the error in restricted mode | |
77 endtry | |
7 | 78 |
22723 | 79 func s:PreGetPage(cnt) |
7 | 80 if a:cnt == 0 |
81 let old_isk = &iskeyword | |
6369 | 82 if &ft == 'man' |
83 setl iskeyword+=(,) | |
84 endif | |
7 | 85 let str = expand("<cword>") |
86 let &l:iskeyword = old_isk | |
87 let page = substitute(str, '(*\(\k\+\).*', '\1', '') | |
88 let sect = substitute(str, '\(\k\+\)(\([^()]*\)).*', '\2', '') | |
89 if match(sect, '^[0-9 ]\+$') == -1 | |
90 let sect = "" | |
91 endif | |
92 if sect == page | |
93 let sect = "" | |
94 endif | |
95 else | |
96 let sect = a:cnt | |
97 let page = expand("<cword>") | |
98 endif | |
19813 | 99 call s:GetPage('', sect, page) |
7 | 100 endfunc |
101 | |
22723 | 102 func s:GetCmdArg(sect, page) |
103 | |
104 if empty(a:sect) | |
105 return shellescape(a:page) | |
7 | 106 endif |
22723 | 107 |
108 return s:man_sect_arg . ' ' . shellescape(a:sect) . ' ' . shellescape(a:page) | |
7 | 109 endfunc |
110 | |
22723 | 111 func s:FindPage(sect, page) |
112 let l:cmd = printf('man %s %s', s:man_find_arg, s:GetCmdArg(a:sect, a:page)) | |
113 call system(l:cmd) | |
114 | |
115 if v:shell_error | |
116 return 0 | |
7 | 117 endif |
22723 | 118 |
7 | 119 return 1 |
120 endfunc | |
121 | |
22723 | 122 func s:GetPage(cmdmods, ...) |
7 | 123 if a:0 >= 2 |
124 let sect = a:1 | |
125 let page = a:2 | |
126 elseif a:0 >= 1 | |
127 let sect = "" | |
128 let page = a:1 | |
129 else | |
130 return | |
131 endif | |
132 | |
133 " To support: nmap K :Man <cword> | |
134 if page == '<cword>' | |
135 let page = expand('<cword>') | |
136 endif | |
137 | |
18186 | 138 if !exists('g:ft_man_no_sect_fallback') || (g:ft_man_no_sect_fallback == 0) |
139 if sect != "" && s:FindPage(sect, page) == 0 | |
140 let sect = "" | |
141 endif | |
7 | 142 endif |
143 if s:FindPage(sect, page) == 0 | |
20753 | 144 let msg = 'man.vim: no manual entry for "' . page . '"' |
145 if !empty(sect) | |
146 let msg .= ' in section ' . sect | |
18186 | 147 endif |
20753 | 148 echomsg msg |
7 | 149 return |
150 endif | |
151 exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%") | |
152 exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".") | |
153 exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".") | |
154 let s:man_tag_depth = s:man_tag_depth + 1 | |
155 | |
17470
d2a834aa7cc0
patch 8.1.1733: the man ftplugin leaves an empty buffer behind
Bram Moolenaar <Bram@vim.org>
parents:
15640
diff
changeset
|
156 let open_cmd = 'edit' |
d2a834aa7cc0
patch 8.1.1733: the man ftplugin leaves an empty buffer behind
Bram Moolenaar <Bram@vim.org>
parents:
15640
diff
changeset
|
157 |
7 | 158 " Use an existing "man" window if it exists, otherwise open a new one. |
159 if &filetype != "man" | |
160 let thiswin = winnr() | |
161 exe "norm! \<C-W>b" | |
1125 | 162 if winnr() > 1 |
7 | 163 exe "norm! " . thiswin . "\<C-W>w" |
164 while 1 | |
165 if &filetype == "man" | |
166 break | |
167 endif | |
168 exe "norm! \<C-W>w" | |
169 if thiswin == winnr() | |
170 break | |
171 endif | |
172 endwhile | |
173 endif | |
1125 | 174 if &filetype != "man" |
9326
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
175 if exists("g:ft_man_open_mode") |
17470
d2a834aa7cc0
patch 8.1.1733: the man ftplugin leaves an empty buffer behind
Bram Moolenaar <Bram@vim.org>
parents:
15640
diff
changeset
|
176 if g:ft_man_open_mode == 'vert' |
d2a834aa7cc0
patch 8.1.1733: the man ftplugin leaves an empty buffer behind
Bram Moolenaar <Bram@vim.org>
parents:
15640
diff
changeset
|
177 let open_cmd = 'vsplit' |
d2a834aa7cc0
patch 8.1.1733: the man ftplugin leaves an empty buffer behind
Bram Moolenaar <Bram@vim.org>
parents:
15640
diff
changeset
|
178 elseif g:ft_man_open_mode == 'tab' |
d2a834aa7cc0
patch 8.1.1733: the man ftplugin leaves an empty buffer behind
Bram Moolenaar <Bram@vim.org>
parents:
15640
diff
changeset
|
179 let open_cmd = 'tabedit' |
9326
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
180 else |
17470
d2a834aa7cc0
patch 8.1.1733: the man ftplugin leaves an empty buffer behind
Bram Moolenaar <Bram@vim.org>
parents:
15640
diff
changeset
|
181 let open_cmd = 'split' |
9326
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
182 endif |
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
183 else |
17470
d2a834aa7cc0
patch 8.1.1733: the man ftplugin leaves an empty buffer behind
Bram Moolenaar <Bram@vim.org>
parents:
15640
diff
changeset
|
184 let open_cmd = a:cmdmods . ' split' |
9326
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
185 endif |
1125 | 186 endif |
7 | 187 endif |
17470
d2a834aa7cc0
patch 8.1.1733: the man ftplugin leaves an empty buffer behind
Bram Moolenaar <Bram@vim.org>
parents:
15640
diff
changeset
|
188 |
d2a834aa7cc0
patch 8.1.1733: the man ftplugin leaves an empty buffer behind
Bram Moolenaar <Bram@vim.org>
parents:
15640
diff
changeset
|
189 silent execute open_cmd . " $HOME/" . page . '.' . sect . '~' |
d2a834aa7cc0
patch 8.1.1733: the man ftplugin leaves an empty buffer behind
Bram Moolenaar <Bram@vim.org>
parents:
15640
diff
changeset
|
190 |
7 | 191 " Avoid warning for editing the dummy file twice |
816 | 192 setl buftype=nofile noswapfile |
7 | 193 |
17541
d9aa921b7198
patch 8.1.1768: man plugin changes setting in current window
Bram Moolenaar <Bram@vim.org>
parents:
17470
diff
changeset
|
194 setl fdc=0 ma nofen nonu nornu |
20025
18043e7ab449
patch 8.2.0568: the man filetype plugin overwrites the unnamed register
Bram Moolenaar <Bram@vim.org>
parents:
19968
diff
changeset
|
195 %delete _ |
9326
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
196 let unsetwidth = 0 |
7992
78106b0f2c56
commit https://github.com/vim/vim/commit/cbebd4879cc78e670d79b2c57dc33d7b911c962a
Christian Brabandt <cb@256bit.org>
parents:
7272
diff
changeset
|
197 if empty($MANWIDTH) |
78106b0f2c56
commit https://github.com/vim/vim/commit/cbebd4879cc78e670d79b2c57dc33d7b911c962a
Christian Brabandt <cb@256bit.org>
parents:
7272
diff
changeset
|
198 let $MANWIDTH = winwidth(0) |
9326
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
199 let unsetwidth = 1 |
7992
78106b0f2c56
commit https://github.com/vim/vim/commit/cbebd4879cc78e670d79b2c57dc33d7b911c962a
Christian Brabandt <cb@256bit.org>
parents:
7272
diff
changeset
|
200 endif |
12826 | 201 |
202 " Ensure Vim is not recursively invoked (man-db does this) when doing ctrl-[ | |
203 " on a man page reference by unsetting MANPAGER. | |
13125 | 204 " Some versions of env(1) do not support the '-u' option, and in such case |
205 " we set MANPAGER=cat. | |
206 if !exists('s:env_has_u') | |
207 call system('env -u x true') | |
208 let s:env_has_u = (v:shell_error == 0) | |
209 endif | |
210 let env_cmd = s:env_has_u ? 'env -u MANPAGER' : 'env MANPAGER=cat' | |
19968 | 211 let env_cmd .= ' GROFF_NO_SGR=1' |
20317 | 212 let man_cmd = env_cmd . ' man ' . s:GetCmdArg(sect, page) . ' | col -b' |
13125 | 213 silent exec "r !" . man_cmd |
12826 | 214 |
9326
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
215 if unsetwidth |
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
216 let $MANWIDTH = '' |
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
217 endif |
7 | 218 " Remove blank lines from top and bottom. |
15640 | 219 while line('$') > 1 && getline(1) =~ '^\s*$' |
20025
18043e7ab449
patch 8.2.0568: the man filetype plugin overwrites the unnamed register
Bram Moolenaar <Bram@vim.org>
parents:
19968
diff
changeset
|
220 1delete _ |
7 | 221 endwhile |
15640 | 222 while line('$') > 1 && getline('$') =~ '^\s*$' |
20025
18043e7ab449
patch 8.2.0568: the man filetype plugin overwrites the unnamed register
Bram Moolenaar <Bram@vim.org>
parents:
19968
diff
changeset
|
223 $delete _ |
7 | 224 endwhile |
225 1 | |
226 setl ft=man nomod | |
227 setl bufhidden=hide | |
228 setl nobuflisted | |
9326
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
229 setl noma |
7 | 230 endfunc |
231 | |
22723 | 232 func s:PopPage() |
7 | 233 if s:man_tag_depth > 0 |
234 let s:man_tag_depth = s:man_tag_depth - 1 | |
235 exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth | |
236 exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth | |
237 exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth | |
238 exec s:man_tag_buf."b" | |
239 exec s:man_tag_lin | |
13857 | 240 exec "norm! ".s:man_tag_col."|" |
7 | 241 exec "unlet s:man_tag_buf_".s:man_tag_depth |
242 exec "unlet s:man_tag_lin_".s:man_tag_depth | |
243 exec "unlet s:man_tag_col_".s:man_tag_depth | |
244 unlet s:man_tag_buf s:man_tag_lin s:man_tag_col | |
245 endif | |
246 endfunc | |
247 | |
248 endif | |
249 | |
14421 | 250 let &cpo = s:cpo_save |
251 unlet s:cpo_save | |
252 | |
9326
cd9c4bbe1d03
commit https://github.com/vim/vim/commit/ddf8d1c746ee081d15c9d7e0515f6ac43adbf363
Christian Brabandt <cb@256bit.org>
parents:
7992
diff
changeset
|
253 " vim: set sw=2 ts=8 noet: |