1" Vim filetype plugin file 2" Language: man 3" Maintainer: SungHyun Nam <[email protected]> 4" Last Change: 2018 Jan 15 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". 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 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 26 nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:setl nomod<CR>'' 27 28 nnoremap <buffer> <c-]> :call <SID>PreGetPage(v:count)<CR> 29 nnoremap <buffer> <c-t> :call <SID>PopPage()<CR> 30 nnoremap <buffer> <silent> q :q<CR> 31 endif 32 33 if exists('g:ft_man_folding_enable') && (g:ft_man_folding_enable == 1) 34 setlocal foldmethod=indent foldnestmax=1 foldenable 35 endif 36 37 let b:undo_ftplugin = "setlocal iskeyword<" 38 39endif 40 41if exists(":Man") != 2 42 com -nargs=+ -complete=shellcmd Man call s:GetPage(<f-args>) 43 nmap <Leader>K :call <SID>PreGetPage(0)<CR> 44 nmap <Plug>ManPreGetPage :call <SID>PreGetPage(0)<CR> 45endif 46 47" Define functions only once. 48if !exists("s:man_tag_depth") 49 50let s:man_tag_depth = 0 51 52let s:man_sect_arg = "" 53let s:man_find_arg = "-w" 54try 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 59catch /E145:/ 60 " Ignore the error in restricted mode 61endtry 62 63func <SID>PreGetPage(cnt) 64 if a:cnt == 0 65 let old_isk = &iskeyword 66 if &ft == 'man' 67 setl iskeyword+=(,) 68 endif 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) 84endfunc 85 86func <SID>GetCmdArg(sect, page) 87 if a:sect == '' 88 return a:page 89 endif 90 return s:man_sect_arg.' '.a:sect.' '.a:page 91endfunc 92 93func <SID>FindPage(sect, page) 94 let where = system("man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page)) 95 if where !~ "^/" 96 if matchstr(where, " [^ ]*$") !~ "^ /" 97 return 0 98 endif 99 endif 100 return 1 101endfunc 102 103func <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" 135 if winnr() > 1 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 147 if &filetype != "man" 148 if exists("g:ft_man_open_mode") 149 if g:ft_man_open_mode == "vert" 150 vnew 151 elseif g:ft_man_open_mode == "tab" 152 tabnew 153 else 154 new 155 endif 156 else 157 new 158 endif 159 setl nonu fdc=0 160 endif 161 endif 162 silent exec "edit $HOME/".page.".".sect."~" 163 " Avoid warning for editing the dummy file twice 164 setl buftype=nofile noswapfile 165 166 setl ma nonu nornu nofen 167 silent exec "norm 1GdG" 168 let unsetwidth = 0 169 if empty($MANWIDTH) 170 let $MANWIDTH = winwidth(0) 171 let unsetwidth = 1 172 endif 173 174 " Ensure Vim is not recursively invoked (man-db does this) when doing ctrl-[ 175 " on a man page reference by unsetting MANPAGER. 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 185 186 if unsetwidth 187 let $MANWIDTH = '' 188 endif 189 " Remove blank lines from top and bottom. 190 while getline(1) =~ '^\s*$' 191 silent keepj norm ggdd 192 endwhile 193 while getline('$') =~ '^\s*$' 194 silent keepj norm Gdd 195 endwhile 196 1 197 setl ft=man nomod 198 setl bufhidden=hide 199 setl nobuflisted 200 setl noma 201endfunc 202 203func <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 217endfunc 218 219endif 220 221" vim: set sw=2 ts=8 noet: 222