blob: f627035eb266aa148033f22b5ddc9ea45974a819 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim filetype plugin file
2" Language: man
Bram Moolenaar5c736222010-01-06 20:54:52 +01003" Maintainer: SungHyun Nam <goweol@gmail.com>
Bram Moolenaar55b0fb72020-04-13 14:58:37 +02004" Last Change: 2020 Apr 13
Bram Moolenaar071d4272004-06-13 20:20:40 +00005
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".
10if &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
Bram Moolenaar91f84f62018-07-29 15:07:52 +020017endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
Bram Moolenaar91f84f62018-07-29 15:07:52 +020019let s:cpo_save = &cpo
20set cpo-=C
21
22if &filetype == "man"
Bram Moolenaar071d4272004-06-13 20:20:40 +000023 " allow dot and dash in manual page name.
24 setlocal iskeyword+=\.,-
Bram Moolenaar91f84f62018-07-29 15:07:52 +020025 let b:undo_ftplugin = "setlocal iskeyword<"
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
27 " Add mappings, unless the user didn't want this.
28 if !exists("no_plugin_maps") && !exists("no_man_maps")
29 if !hasmapto('<Plug>ManBS')
30 nmap <buffer> <LocalLeader>h <Plug>ManBS
Bram Moolenaar91f84f62018-07-29 15:07:52 +020031 let b:undo_ftplugin = b:undo_ftplugin
32 \ . '|silent! nunmap <buffer> <LocalLeader>h'
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 endif
Bram Moolenaard2cec5b2006-03-28 21:08:56 +000034 nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:setl nomod<CR>''
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
36 nnoremap <buffer> <c-]> :call <SID>PreGetPage(v:count)<CR>
37 nnoremap <buffer> <c-t> :call <SID>PopPage()<CR>
Bram Moolenaard042dc82015-11-24 19:18:36 +010038 nnoremap <buffer> <silent> q :q<CR>
Bram Moolenaar91f84f62018-07-29 15:07:52 +020039
40 " Add undo commands for the maps
41 let b:undo_ftplugin = b:undo_ftplugin
42 \ . '|silent! nunmap <buffer> <Plug>ManBS'
43 \ . '|silent! nunmap <buffer> <c-]>'
44 \ . '|silent! nunmap <buffer> <c-t>'
45 \ . '|silent! nunmap <buffer> q'
Bram Moolenaard042dc82015-11-24 19:18:36 +010046 endif
47
48 if exists('g:ft_man_folding_enable') && (g:ft_man_folding_enable == 1)
49 setlocal foldmethod=indent foldnestmax=1 foldenable
Bram Moolenaar91f84f62018-07-29 15:07:52 +020050 let b:undo_ftplugin = b:undo_ftplugin
51 \ . '|silent! setl fdm< fdn< fen<'
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 endif
53
54endif
55
56if exists(":Man") != 2
Bram Moolenaar91f84f62018-07-29 15:07:52 +020057 com -nargs=+ -complete=shellcmd Man call s:GetPage(<q-mods>, <f-args>)
Bram Moolenaar071d4272004-06-13 20:20:40 +000058 nmap <Leader>K :call <SID>PreGetPage(0)<CR>
Bram Moolenaar68563932017-01-10 13:31:15 +010059 nmap <Plug>ManPreGetPage :call <SID>PreGetPage(0)<CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +000060endif
61
62" Define functions only once.
63if !exists("s:man_tag_depth")
64
65let s:man_tag_depth = 0
66
Bram Moolenaar864207d2008-06-24 22:14:38 +000067let s:man_sect_arg = ""
68let s:man_find_arg = "-w"
69try
70 if !has("win32") && $OSTYPE !~ 'cygwin\|linux' && system('uname -s') =~ "SunOS" && system('uname -r') =~ "^5"
71 let s:man_sect_arg = "-s"
72 let s:man_find_arg = "-l"
73 endif
74catch /E145:/
75 " Ignore the error in restricted mode
76endtry
Bram Moolenaar071d4272004-06-13 20:20:40 +000077
78func <SID>PreGetPage(cnt)
79 if a:cnt == 0
80 let old_isk = &iskeyword
Bram Moolenaarc2299672014-11-13 14:25:38 +010081 if &ft == 'man'
82 setl iskeyword+=(,)
83 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 let str = expand("<cword>")
85 let &l:iskeyword = old_isk
86 let page = substitute(str, '(*\(\k\+\).*', '\1', '')
87 let sect = substitute(str, '\(\k\+\)(\([^()]*\)).*', '\2', '')
88 if match(sect, '^[0-9 ]\+$') == -1
89 let sect = ""
90 endif
91 if sect == page
92 let sect = ""
93 endif
94 else
95 let sect = a:cnt
96 let page = expand("<cword>")
97 endif
Bram Moolenaar191acfd2020-03-27 20:42:43 +010098 call s:GetPage('', sect, page)
Bram Moolenaar071d4272004-06-13 20:20:40 +000099endfunc
100
101func <SID>GetCmdArg(sect, page)
102 if a:sect == ''
103 return a:page
104 endif
105 return s:man_sect_arg.' '.a:sect.' '.a:page
106endfunc
107
108func <SID>FindPage(sect, page)
Bram Moolenaar690afe12017-01-28 18:34:47 +0100109 let where = system("man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110 if where !~ "^/"
111 if matchstr(where, " [^ ]*$") !~ "^ /"
112 return 0
113 endif
114 endif
115 return 1
116endfunc
117
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200118func <SID>GetPage(cmdmods, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 if a:0 >= 2
120 let sect = a:1
121 let page = a:2
122 elseif a:0 >= 1
123 let sect = ""
124 let page = a:1
125 else
126 return
127 endif
128
129 " To support: nmap K :Man <cword>
130 if page == '<cword>'
131 let page = expand('<cword>')
132 endif
133
Bram Moolenaar5be4cee2019-09-27 19:34:08 +0200134 if !exists('g:ft_man_no_sect_fallback') || (g:ft_man_no_sect_fallback == 0)
135 if sect != "" && s:FindPage(sect, page) == 0
136 let sect = ""
137 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 endif
139 if s:FindPage(sect, page) == 0
Bram Moolenaar5be4cee2019-09-27 19:34:08 +0200140 let msg = "\nNo manual entry for ".page
141 if sect != ""
142 let msg .= " in section ".sect
143 endif
144 echo msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 return
146 endif
147 exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%")
148 exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".")
149 exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".")
150 let s:man_tag_depth = s:man_tag_depth + 1
151
Bram Moolenaare5e69502019-07-22 22:09:21 +0200152 let open_cmd = 'edit'
153
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 " Use an existing "man" window if it exists, otherwise open a new one.
155 if &filetype != "man"
156 let thiswin = winnr()
157 exe "norm! \<C-W>b"
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000158 if winnr() > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 exe "norm! " . thiswin . "\<C-W>w"
160 while 1
161 if &filetype == "man"
162 break
163 endif
164 exe "norm! \<C-W>w"
165 if thiswin == winnr()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 break
167 endif
168 endwhile
169 endif
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000170 if &filetype != "man"
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200171 if exists("g:ft_man_open_mode")
Bram Moolenaare5e69502019-07-22 22:09:21 +0200172 if g:ft_man_open_mode == 'vert'
173 let open_cmd = 'vsplit'
174 elseif g:ft_man_open_mode == 'tab'
175 let open_cmd = 'tabedit'
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200176 else
Bram Moolenaare5e69502019-07-22 22:09:21 +0200177 let open_cmd = 'split'
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200178 endif
179 else
Bram Moolenaare5e69502019-07-22 22:09:21 +0200180 let open_cmd = a:cmdmods . ' split'
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200181 endif
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000182 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 endif
Bram Moolenaare5e69502019-07-22 22:09:21 +0200184
185 silent execute open_cmd . " $HOME/" . page . '.' . sect . '~'
186
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 " Avoid warning for editing the dummy file twice
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000188 setl buftype=nofile noswapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
Bram Moolenaarbca9c302019-07-28 15:28:45 +0200190 setl fdc=0 ma nofen nonu nornu
Bram Moolenaar55b0fb72020-04-13 14:58:37 +0200191 %delete _
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200192 let unsetwidth = 0
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100193 if empty($MANWIDTH)
194 let $MANWIDTH = winwidth(0)
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200195 let unsetwidth = 1
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100196 endif
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100197
198 " Ensure Vim is not recursively invoked (man-db does this) when doing ctrl-[
199 " on a man page reference by unsetting MANPAGER.
Bram Moolenaar40962ec2018-01-28 22:47:25 +0100200 " Some versions of env(1) do not support the '-u' option, and in such case
201 " we set MANPAGER=cat.
202 if !exists('s:env_has_u')
203 call system('env -u x true')
204 let s:env_has_u = (v:shell_error == 0)
205 endif
206 let env_cmd = s:env_has_u ? 'env -u MANPAGER' : 'env MANPAGER=cat'
Bram Moolenaard1caa942020-04-10 22:10:56 +0200207 let env_cmd .= ' GROFF_NO_SGR=1'
Bram Moolenaar40962ec2018-01-28 22:47:25 +0100208 let man_cmd = env_cmd . ' man ' . s:GetCmdArg(sect, page) . ' | col -b'
209 silent exec "r !" . man_cmd
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100210
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200211 if unsetwidth
212 let $MANWIDTH = ''
213 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 " Remove blank lines from top and bottom.
Bram Moolenaar2a953fc2019-01-26 17:41:47 +0100215 while line('$') > 1 && getline(1) =~ '^\s*$'
Bram Moolenaar55b0fb72020-04-13 14:58:37 +0200216 1delete _
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 endwhile
Bram Moolenaar2a953fc2019-01-26 17:41:47 +0100218 while line('$') > 1 && getline('$') =~ '^\s*$'
Bram Moolenaar55b0fb72020-04-13 14:58:37 +0200219 $delete _
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 endwhile
221 1
222 setl ft=man nomod
223 setl bufhidden=hide
224 setl nobuflisted
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200225 setl noma
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226endfunc
227
228func <SID>PopPage()
229 if s:man_tag_depth > 0
230 let s:man_tag_depth = s:man_tag_depth - 1
231 exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth
232 exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth
233 exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth
234 exec s:man_tag_buf."b"
235 exec s:man_tag_lin
Bram Moolenaar85eee132018-05-06 17:57:30 +0200236 exec "norm! ".s:man_tag_col."|"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 exec "unlet s:man_tag_buf_".s:man_tag_depth
238 exec "unlet s:man_tag_lin_".s:man_tag_depth
239 exec "unlet s:man_tag_col_".s:man_tag_depth
240 unlet s:man_tag_buf s:man_tag_lin s:man_tag_col
241 endif
242endfunc
243
244endif
245
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200246let &cpo = s:cpo_save
247unlet s:cpo_save
248
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200249" vim: set sw=2 ts=8 noet: