1" Vim filetype plugin file 2" Language: man 3" Maintainer: Nam SungHyun <[email protected]> 4" Last Change: 2006 Dec 04 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 endif 31 32endif 33 34if exists(":Man") != 2 35 com -nargs=+ Man call s:GetPage(<f-args>) 36 nmap <Leader>K :call <SID>PreGetPage(0)<CR> 37endif 38 39" Define functions only once. 40if !exists("s:man_tag_depth") 41 42let s:man_tag_depth = 0 43 44if !has("win32") && $OSTYPE !~ 'cygwin\|linux' && system('uname -s') =~ "SunOS" && system('uname -r') =~ "^5" 45 let s:man_sect_arg = "-s" 46 let s:man_find_arg = "-l" 47else 48 let s:man_sect_arg = "" 49 let s:man_find_arg = "-w" 50endif 51 52func <SID>PreGetPage(cnt) 53 if a:cnt == 0 54 let old_isk = &iskeyword 55 setl iskeyword+=(,) 56 let str = expand("<cword>") 57 let &l:iskeyword = old_isk 58 let page = substitute(str, '(*\(\k\+\).*', '\1', '') 59 let sect = substitute(str, '\(\k\+\)(\([^()]*\)).*', '\2', '') 60 if match(sect, '^[0-9 ]\+$') == -1 61 let sect = "" 62 endif 63 if sect == page 64 let sect = "" 65 endif 66 else 67 let sect = a:cnt 68 let page = expand("<cword>") 69 endif 70 call s:GetPage(sect, page) 71endfunc 72 73func <SID>GetCmdArg(sect, page) 74 if a:sect == '' 75 return a:page 76 endif 77 return s:man_sect_arg.' '.a:sect.' '.a:page 78endfunc 79 80func <SID>FindPage(sect, page) 81 let where = system("/usr/bin/man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page)) 82 if where !~ "^/" 83 if matchstr(where, " [^ ]*$") !~ "^ /" 84 return 0 85 endif 86 endif 87 return 1 88endfunc 89 90func <SID>GetPage(...) 91 if a:0 >= 2 92 let sect = a:1 93 let page = a:2 94 elseif a:0 >= 1 95 let sect = "" 96 let page = a:1 97 else 98 return 99 endif 100 101 " To support: nmap K :Man <cword> 102 if page == '<cword>' 103 let page = expand('<cword>') 104 endif 105 106 if sect != "" && s:FindPage(sect, page) == 0 107 let sect = "" 108 endif 109 if s:FindPage(sect, page) == 0 110 echo "\nCannot find a '".page."'." 111 return 112 endif 113 exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%") 114 exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".") 115 exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".") 116 let s:man_tag_depth = s:man_tag_depth + 1 117 118 " Use an existing "man" window if it exists, otherwise open a new one. 119 if &filetype != "man" 120 let thiswin = winnr() 121 exe "norm! \<C-W>b" 122 if winnr() > 1 123 exe "norm! " . thiswin . "\<C-W>w" 124 while 1 125 if &filetype == "man" 126 break 127 endif 128 exe "norm! \<C-W>w" 129 if thiswin == winnr() 130 break 131 endif 132 endwhile 133 endif 134 if &filetype != "man" 135 new 136 setl nonu fdc=0 137 endif 138 endif 139 silent exec "edit $HOME/".page.".".sect."~" 140 " Avoid warning for editing the dummy file twice 141 setl buftype=nofile noswapfile 142 143 setl ma 144 silent exec "norm 1GdG" 145 let $MANWIDTH = winwidth(0) 146 silent exec "r!/usr/bin/man ".s:GetCmdArg(sect, page)." | col -b" 147 " Remove blank lines from top and bottom. 148 while getline(1) =~ '^\s*$' 149 silent norm ggdd 150 endwhile 151 while getline('$') =~ '^\s*$' 152 silent norm Gdd 153 endwhile 154 1 155 setl ft=man nomod 156 setl bufhidden=hide 157 setl nobuflisted 158endfunc 159 160func <SID>PopPage() 161 if s:man_tag_depth > 0 162 let s:man_tag_depth = s:man_tag_depth - 1 163 exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth 164 exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth 165 exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth 166 exec s:man_tag_buf."b" 167 exec s:man_tag_lin 168 exec "norm ".s:man_tag_col."|" 169 exec "unlet s:man_tag_buf_".s:man_tag_depth 170 exec "unlet s:man_tag_lin_".s:man_tag_depth 171 exec "unlet s:man_tag_col_".s:man_tag_depth 172 unlet s:man_tag_buf s:man_tag_lin s:man_tag_col 173 endif 174endfunc 175 176endif 177 178" vim: set sw=2: 179