1" Vim filetype plugin file 2" Language: man 3" Maintainer: SungHyun Nam <[email protected]> 4" Last Change: 2019 Jul 22 5" (fix by Jason Franklin) 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". 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 18endif 19 20let s:cpo_save = &cpo 21set cpo-=C 22 23if &filetype == "man" 24 " allow dot and dash in manual page name. 25 setlocal iskeyword+=\.,- 26 let b:undo_ftplugin = "setlocal iskeyword<" 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 32 let b:undo_ftplugin = b:undo_ftplugin 33 \ . '|silent! nunmap <buffer> <LocalLeader>h' 34 endif 35 nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:setl nomod<CR>'' 36 37 nnoremap <buffer> <c-]> :call <SID>PreGetPage(v:count)<CR> 38 nnoremap <buffer> <c-t> :call <SID>PopPage()<CR> 39 nnoremap <buffer> <silent> q :q<CR> 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' 47 endif 48 49 if exists('g:ft_man_folding_enable') && (g:ft_man_folding_enable == 1) 50 setlocal foldmethod=indent foldnestmax=1 foldenable 51 let b:undo_ftplugin = b:undo_ftplugin 52 \ . '|silent! setl fdm< fdn< fen<' 53 endif 54 55endif 56 57if exists(":Man") != 2 58 com -nargs=+ -complete=shellcmd Man call s:GetPage(<q-mods>, <f-args>) 59 nmap <Leader>K :call <SID>PreGetPage(0)<CR> 60 nmap <Plug>ManPreGetPage :call <SID>PreGetPage(0)<CR> 61endif 62 63" Define functions only once. 64if !exists("s:man_tag_depth") 65 66let s:man_tag_depth = 0 67 68let 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 78 79func <SID>PreGetPage(cnt) 80 if a:cnt == 0 81 let old_isk = &iskeyword 82 if &ft == 'man' 83 setl iskeyword+=(,) 84 endif 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 99 call s:GetPage(sect, page) 100endfunc 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) 110 let where = system("man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page)) 111 if where !~ "^/" 112 if matchstr(where, " [^ ]*$") !~ "^ /" 113 return 0 114 endif 115 endif 116 return 1 117endfunc 118 119func <SID>GetPage(cmdmods, ...) 120 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 135 if sect != "" && s:FindPage(sect, page) == 0 136 let sect = "" 137 endif 138 if s:FindPage(sect, page) == 0 139 echo "\nCannot find a '".page."'." 140 return 141 endif 142 exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%") 143 exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".") 144 exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".") 145 let s:man_tag_depth = s:man_tag_depth + 1 146 147 let open_cmd = 'edit' 148 149 " Use an existing "man" window if it exists, otherwise open a new one. 150 if &filetype != "man" 151 let thiswin = winnr() 152 exe "norm! \<C-W>b" 153 if winnr() > 1 154 exe "norm! " . thiswin . "\<C-W>w" 155 while 1 156 if &filetype == "man" 157 break 158 endif 159 exe "norm! \<C-W>w" 160 if thiswin == winnr() 161 break 162 endif 163 endwhile 164 endif 165 if &filetype != "man" 166 if exists("g:ft_man_open_mode") 167 if g:ft_man_open_mode == 'vert' 168 let open_cmd = 'vsplit' 169 elseif g:ft_man_open_mode == 'tab' 170 let open_cmd = 'tabedit' 171 else 172 let open_cmd = 'split' 173 endif 174 else 175 let open_cmd = a:cmdmods . ' split' 176 endif 177 endif 178 endif 179 180 silent execute open_cmd . " $HOME/" . page . '.' . sect . '~' 181 182 " Avoid warning for editing the dummy file twice 183 setl buftype=nofile noswapfile 184 185 setl fdc=0 ma nofen nonu nornu 186 silent exec "norm! 1GdG" 187 let unsetwidth = 0 188 if empty($MANWIDTH) 189 let $MANWIDTH = winwidth(0) 190 let unsetwidth = 1 191 endif 192 193 " Ensure Vim is not recursively invoked (man-db does this) when doing ctrl-[ 194 " on a man page reference by unsetting MANPAGER. 195 " Some versions of env(1) do not support the '-u' option, and in such case 196 " we set MANPAGER=cat. 197 if !exists('s:env_has_u') 198 call system('env -u x true') 199 let s:env_has_u = (v:shell_error == 0) 200 endif 201 let env_cmd = s:env_has_u ? 'env -u MANPAGER' : 'env MANPAGER=cat' 202 let man_cmd = env_cmd . ' man ' . s:GetCmdArg(sect, page) . ' | col -b' 203 silent exec "r !" . man_cmd 204 205 if unsetwidth 206 let $MANWIDTH = '' 207 endif 208 " Remove blank lines from top and bottom. 209 while line('$') > 1 && getline(1) =~ '^\s*$' 210 silent keepj norm! ggdd 211 endwhile 212 while line('$') > 1 && getline('$') =~ '^\s*$' 213 silent keepj norm! Gdd 214 endwhile 215 1 216 setl ft=man nomod 217 setl bufhidden=hide 218 setl nobuflisted 219 setl noma 220endfunc 221 222func <SID>PopPage() 223 if s:man_tag_depth > 0 224 let s:man_tag_depth = s:man_tag_depth - 1 225 exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth 226 exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth 227 exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth 228 exec s:man_tag_buf."b" 229 exec s:man_tag_lin 230 exec "norm! ".s:man_tag_col."|" 231 exec "unlet s:man_tag_buf_".s:man_tag_depth 232 exec "unlet s:man_tag_lin_".s:man_tag_depth 233 exec "unlet s:man_tag_col_".s:man_tag_depth 234 unlet s:man_tag_buf s:man_tag_lin s:man_tag_col 235 endif 236endfunc 237 238endif 239 240let &cpo = s:cpo_save 241unlet s:cpo_save 242 243" vim: set sw=2 ts=8 noet: 244