blob: 5f8a639f0dde9b3cd290fe5a45f19bfb69fd9006 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim filetype plugin file
2" Language: man
Bram Moolenaar7ceefb32020-05-01 16:07:38 +02003" Maintainer: Jason Franklin <vim@justemail.net>
Bram Moolenaar2cfb4a22020-05-07 18:56:00 +02004" Maintainer: SungHyun Nam <goweol@gmail.com>
Bram Moolenaaracc22402020-06-07 21:07:18 +02005" Last Change: 2020 Jun 01
Bram Moolenaar071d4272004-06-13 20:20:40 +00006
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".
11if &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
Bram Moolenaar91f84f62018-07-29 15:07:52 +020018endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019
Bram Moolenaar91f84f62018-07-29 15:07:52 +020020let s:cpo_save = &cpo
21set cpo-=C
22
23if &filetype == "man"
Bram Moolenaar071d4272004-06-13 20:20:40 +000024 " allow dot and dash in manual page name.
25 setlocal iskeyword+=\.,-
Bram Moolenaar91f84f62018-07-29 15:07:52 +020026 let b:undo_ftplugin = "setlocal iskeyword<"
Bram Moolenaar071d4272004-06-13 20:20:40 +000027
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
Bram Moolenaar91f84f62018-07-29 15:07:52 +020032 let b:undo_ftplugin = b:undo_ftplugin
33 \ . '|silent! nunmap <buffer> <LocalLeader>h'
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 endif
Bram Moolenaard2cec5b2006-03-28 21:08:56 +000035 nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:setl nomod<CR>''
Bram Moolenaar071d4272004-06-13 20:20:40 +000036
Bram Moolenaaracc22402020-06-07 21:07:18 +020037 nnoremap <buffer> <silent> <c-]> :call <SID>PreGetPage(v:count)<CR>
38 nnoremap <buffer> <silent> <c-t> :call <SID>PopPage()<CR>
Bram Moolenaard042dc82015-11-24 19:18:36 +010039 nnoremap <buffer> <silent> q :q<CR>
Bram Moolenaar91f84f62018-07-29 15:07:52 +020040
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'
Bram Moolenaard042dc82015-11-24 19:18:36 +010047 endif
48
49 if exists('g:ft_man_folding_enable') && (g:ft_man_folding_enable == 1)
50 setlocal foldmethod=indent foldnestmax=1 foldenable
Bram Moolenaar91f84f62018-07-29 15:07:52 +020051 let b:undo_ftplugin = b:undo_ftplugin
52 \ . '|silent! setl fdm< fdn< fen<'
Bram Moolenaar071d4272004-06-13 20:20:40 +000053 endif
54
55endif
56
57if exists(":Man") != 2
Bram Moolenaar91f84f62018-07-29 15:07:52 +020058 com -nargs=+ -complete=shellcmd Man call s:GetPage(<q-mods>, <f-args>)
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 nmap <Leader>K :call <SID>PreGetPage(0)<CR>
Bram Moolenaar68563932017-01-10 13:31:15 +010060 nmap <Plug>ManPreGetPage :call <SID>PreGetPage(0)<CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +000061endif
62
63" Define functions only once.
64if !exists("s:man_tag_depth")
65
66let s:man_tag_depth = 0
67
Bram Moolenaar864207d2008-06-24 22:14:38 +000068let s:man_sect_arg = ""
69let s:man_find_arg = "-w"
70try
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
75catch /E145:/
76 " Ignore the error in restricted mode
77endtry
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
79func <SID>PreGetPage(cnt)
80 if a:cnt == 0
81 let old_isk = &iskeyword
Bram Moolenaarc2299672014-11-13 14:25:38 +010082 if &ft == 'man'
83 setl iskeyword+=(,)
84 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 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
Bram Moolenaar191acfd2020-03-27 20:42:43 +010099 call s:GetPage('', sect, page)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100endfunc
101
102func <SID>GetCmdArg(sect, page)
103 if a:sect == ''
104 return a:page
105 endif
106 return s:man_sect_arg.' '.a:sect.' '.a:page
107endfunc
108
109func <SID>FindPage(sect, page)
Bram Moolenaar690afe12017-01-28 18:34:47 +0100110 let where = system("man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 if where !~ "^/"
112 if matchstr(where, " [^ ]*$") !~ "^ /"
113 return 0
114 endif
115 endif
116 return 1
117endfunc
118
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200119func <SID>GetPage(cmdmods, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 if a:0 >= 2
121 let sect = a:1
122 let page = a:2
123 elseif a:0 >= 1
124 let sect = ""
125 let page = a:1
126 else
127 return
128 endif
129
130 " To support: nmap K :Man <cword>
131 if page == '<cword>'
132 let page = expand('<cword>')
133 endif
134
Bram Moolenaar5be4cee2019-09-27 19:34:08 +0200135 if !exists('g:ft_man_no_sect_fallback') || (g:ft_man_no_sect_fallback == 0)
136 if sect != "" && s:FindPage(sect, page) == 0
137 let sect = ""
138 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 endif
140 if s:FindPage(sect, page) == 0
Bram Moolenaaracc22402020-06-07 21:07:18 +0200141 let msg = 'man.vim: no manual entry for "' . page . '"'
142 if !empty(sect)
143 let msg .= ' in section ' . sect
Bram Moolenaar5be4cee2019-09-27 19:34:08 +0200144 endif
Bram Moolenaaracc22402020-06-07 21:07:18 +0200145 echomsg msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 return
147 endif
148 exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%")
149 exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".")
150 exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".")
151 let s:man_tag_depth = s:man_tag_depth + 1
152
Bram Moolenaare5e69502019-07-22 22:09:21 +0200153 let open_cmd = 'edit'
154
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 " Use an existing "man" window if it exists, otherwise open a new one.
156 if &filetype != "man"
157 let thiswin = winnr()
158 exe "norm! \<C-W>b"
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000159 if winnr() > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 exe "norm! " . thiswin . "\<C-W>w"
161 while 1
162 if &filetype == "man"
163 break
164 endif
165 exe "norm! \<C-W>w"
166 if thiswin == winnr()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 break
168 endif
169 endwhile
170 endif
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000171 if &filetype != "man"
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200172 if exists("g:ft_man_open_mode")
Bram Moolenaare5e69502019-07-22 22:09:21 +0200173 if g:ft_man_open_mode == 'vert'
174 let open_cmd = 'vsplit'
175 elseif g:ft_man_open_mode == 'tab'
176 let open_cmd = 'tabedit'
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200177 else
Bram Moolenaare5e69502019-07-22 22:09:21 +0200178 let open_cmd = 'split'
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200179 endif
180 else
Bram Moolenaare5e69502019-07-22 22:09:21 +0200181 let open_cmd = a:cmdmods . ' split'
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200182 endif
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000183 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 endif
Bram Moolenaare5e69502019-07-22 22:09:21 +0200185
186 silent execute open_cmd . " $HOME/" . page . '.' . sect . '~'
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 " Avoid warning for editing the dummy file twice
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000189 setl buftype=nofile noswapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190
Bram Moolenaarbca9c302019-07-28 15:28:45 +0200191 setl fdc=0 ma nofen nonu nornu
Bram Moolenaar55b0fb72020-04-13 14:58:37 +0200192 %delete _
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200193 let unsetwidth = 0
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100194 if empty($MANWIDTH)
195 let $MANWIDTH = winwidth(0)
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200196 let unsetwidth = 1
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100197 endif
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100198
199 " Ensure Vim is not recursively invoked (man-db does this) when doing ctrl-[
200 " on a man page reference by unsetting MANPAGER.
Bram Moolenaar40962ec2018-01-28 22:47:25 +0100201 " Some versions of env(1) do not support the '-u' option, and in such case
202 " we set MANPAGER=cat.
203 if !exists('s:env_has_u')
204 call system('env -u x true')
205 let s:env_has_u = (v:shell_error == 0)
206 endif
207 let env_cmd = s:env_has_u ? 'env -u MANPAGER' : 'env MANPAGER=cat'
Bram Moolenaard1caa942020-04-10 22:10:56 +0200208 let env_cmd .= ' GROFF_NO_SGR=1'
Bram Moolenaar2cfb4a22020-05-07 18:56:00 +0200209 let man_cmd = env_cmd . ' man ' . s:GetCmdArg(sect, page) . ' | col -b'
Bram Moolenaar40962ec2018-01-28 22:47:25 +0100210 silent exec "r !" . man_cmd
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100211
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200212 if unsetwidth
213 let $MANWIDTH = ''
214 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 " Remove blank lines from top and bottom.
Bram Moolenaar2a953fc2019-01-26 17:41:47 +0100216 while line('$') > 1 && getline(1) =~ '^\s*$'
Bram Moolenaar55b0fb72020-04-13 14:58:37 +0200217 1delete _
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 endwhile
Bram Moolenaar2a953fc2019-01-26 17:41:47 +0100219 while line('$') > 1 && getline('$') =~ '^\s*$'
Bram Moolenaar55b0fb72020-04-13 14:58:37 +0200220 $delete _
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 endwhile
222 1
223 setl ft=man nomod
224 setl bufhidden=hide
225 setl nobuflisted
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200226 setl noma
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227endfunc
228
229func <SID>PopPage()
230 if s:man_tag_depth > 0
231 let s:man_tag_depth = s:man_tag_depth - 1
232 exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth
233 exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth
234 exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth
235 exec s:man_tag_buf."b"
236 exec s:man_tag_lin
Bram Moolenaar85eee132018-05-06 17:57:30 +0200237 exec "norm! ".s:man_tag_col."|"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 exec "unlet s:man_tag_buf_".s:man_tag_depth
239 exec "unlet s:man_tag_lin_".s:man_tag_depth
240 exec "unlet s:man_tag_col_".s:man_tag_depth
241 unlet s:man_tag_buf s:man_tag_lin s:man_tag_col
242 endif
243endfunc
244
245endif
246
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200247let &cpo = s:cpo_save
248unlet s:cpo_save
249
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200250" vim: set sw=2 ts=8 noet: