1" Vim filetype plugin file 2" Language: man 3" Maintainer: SungHyun Nam <[email protected]> 4" Last Change: 2012 Mar 6 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 setl iskeyword+=(,) 67 let str = expand("<cword>") 68 let &l:iskeyword = old_isk 69 let page = substitute(str, '(*\(\k\+\).*', '\1', '') 70 let sect = substitute(str, '\(\k\+\)(\([^()]*\)).*', '\2', '') 71 if match(sect, '^[0-9 ]\+$') == -1 72 let sect = "" 73 endif 74 if sect == page 75 let sect = "" 76 endif 77 else 78 let sect = a:cnt 79 let page = expand("<cword>") 80 endif 81 call s:GetPage(sect, page) 82endfunc 83 84func <SID>GetCmdArg(sect, page) 85 if a:sect == '' 86 return a:page 87 endif 88 return s:man_sect_arg.' '.a:sect.' '.a:page 89endfunc 90 91func <SID>FindPage(sect, page) 92 let where = system("/usr/bin/man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page)) 93 if where !~ "^/" 94 if matchstr(where, " [^ ]*$") !~ "^ /" 95 return 0 96 endif 97 endif 98 return 1 99endfunc 100 101func <SID>GetPage(...) 102 if a:0 >= 2 103 let sect = a:1 104 let page = a:2 105 elseif a:0 >= 1 106 let sect = "" 107 let page = a:1 108 else 109 return 110 endif 111 112 " To support: nmap K :Man <cword> 113 if page == '<cword>' 114 let page = expand('<cword>') 115 endif 116 117 if sect != "" && s:FindPage(sect, page) == 0 118 let sect = "" 119 endif 120 if s:FindPage(sect, page) == 0 121 echo "\nCannot find a '".page."'." 122 return 123 endif 124 exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%") 125 exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".") 126 exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".") 127 let s:man_tag_depth = s:man_tag_depth + 1 128 129 " Use an existing "man" window if it exists, otherwise open a new one. 130 if &filetype != "man" 131 let thiswin = winnr() 132 exe "norm! \<C-W>b" 133 if winnr() > 1 134 exe "norm! " . thiswin . "\<C-W>w" 135 while 1 136 if &filetype == "man" 137 break 138 endif 139 exe "norm! \<C-W>w" 140 if thiswin == winnr() 141 break 142 endif 143 endwhile 144 endif 145 if &filetype != "man" 146 new 147 setl nonu fdc=0 148 endif 149 endif 150 silent exec "edit $HOME/".page.".".sect."~" 151 " Avoid warning for editing the dummy file twice 152 setl buftype=nofile noswapfile 153 154 setl ma 155 silent exec "norm 1GdG" 156 let $MANWIDTH = winwidth(0) 157 silent exec "r!/usr/bin/man ".s:GetCmdArg(sect, page)." | col -b" 158 " Remove blank lines from top and bottom. 159 while getline(1) =~ '^\s*$' 160 silent norm ggdd 161 endwhile 162 while getline('$') =~ '^\s*$' 163 silent norm Gdd 164 endwhile 165 1 166 setl ft=man nomod 167 setl bufhidden=hide 168 setl nobuflisted 169endfunc 170 171func <SID>PopPage() 172 if s:man_tag_depth > 0 173 let s:man_tag_depth = s:man_tag_depth - 1 174 exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth 175 exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth 176 exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth 177 exec s:man_tag_buf."b" 178 exec s:man_tag_lin 179 exec "norm ".s:man_tag_col."|" 180 exec "unlet s:man_tag_buf_".s:man_tag_depth 181 exec "unlet s:man_tag_lin_".s:man_tag_depth 182 exec "unlet s:man_tag_col_".s:man_tag_depth 183 unlet s:man_tag_buf s:man_tag_lin s:man_tag_col 184 endif 185endfunc 186 187endif 188 189" vim: set sw=2: 190