1" Vim filetype plugin file 2" Language: man 3" Maintainer: SungHyun Nam <[email protected]> 4" Last Change: 2014 Dec 29 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 " Ensure Vim is not recursively invoked (man-db does this) 19 " when doing ctrl-[ on a man page reference. 20 if exists("$MANPAGER") 21 let $MANPAGER = "" 22 endif 23 24 " allow dot and dash in manual page name. 25 setlocal iskeyword+=\.,- 26 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 31 endif 32 nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:setl nomod<CR>'' 33 34 nnoremap <buffer> <c-]> :call <SID>PreGetPage(v:count)<CR> 35 nnoremap <buffer> <c-t> :call <SID>PopPage()<CR> 36 endif 37 38 let b:undo_ftplugin = "setlocal iskeyword<" 39 40endif 41 42if exists(":Man") != 2 43 com -nargs=+ Man call s:GetPage(<f-args>) 44 nmap <Leader>K :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("/usr/bin/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 new 149 setl nonu fdc=0 150 endif 151 endif 152 silent exec "edit $HOME/".page.".".sect."~" 153 " Avoid warning for editing the dummy file twice 154 setl buftype=nofile noswapfile 155 156 setl ma nonu nornu nofen 157 silent exec "norm 1GdG" 158 let $MANWIDTH = winwidth(0) 159 silent exec "r!/usr/bin/man ".s:GetCmdArg(sect, page)." | col -b" 160 " Remove blank lines from top and bottom. 161 while getline(1) =~ '^\s*$' 162 silent keepj norm ggdd 163 endwhile 164 while getline('$') =~ '^\s*$' 165 silent keepj norm Gdd 166 endwhile 167 1 168 setl ft=man nomod 169 setl bufhidden=hide 170 setl nobuflisted 171endfunc 172 173func <SID>PopPage() 174 if s:man_tag_depth > 0 175 let s:man_tag_depth = s:man_tag_depth - 1 176 exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth 177 exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth 178 exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth 179 exec s:man_tag_buf."b" 180 exec s:man_tag_lin 181 exec "norm ".s:man_tag_col."|" 182 exec "unlet s:man_tag_buf_".s:man_tag_depth 183 exec "unlet s:man_tag_lin_".s:man_tag_depth 184 exec "unlet s:man_tag_col_".s:man_tag_depth 185 unlet s:man_tag_buf s:man_tag_lin s:man_tag_col 186 endif 187endfunc 188 189endif 190 191" vim: set sw=2: 192