1 " Vim filetype plugin file
3 " Maintainer: Nam SungHyun <namsh@kldp.org>
4 " Last Change: 2006 Dec 04
6 " To make the ":Man" command available before editing a manual page, source
7 " this script from your startup vimrc file.
9 " If 'filetype' isn't "man", we must have been called to only define ":Man".
12 " Only do this when not done yet for this buffer
13 if exists("b:did_ftplugin")
16 let b:did_ftplugin = 1
18 " allow dot and dash in manual page name.
19 setlocal iskeyword+=\.,-
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
26 nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:setl nomod<CR>''
28 nnoremap <buffer> <c-]> :call <SID>PreGetPage(v:count)<CR>
29 nnoremap <buffer> <c-t> :call <SID>PopPage()<CR>
34 if exists(":Man") != 2
35 com -nargs=+ Man call s:GetPage(<f-args>)
36 nmap <Leader>K :call <SID>PreGetPage(0)<CR>
39 " Define functions only once.
40 if !exists("s:man_tag_depth")
42 let s:man_tag_depth = 0
44 if !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"
48 let s:man_sect_arg = ""
49 let s:man_find_arg = "-w"
52 func <SID>PreGetPage(cnt)
54 let old_isk = &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
68 let page = expand("<cword>")
70 call s:GetPage(sect, page)
73 func <SID>GetCmdArg(sect, page)
77 return s:man_sect_arg.' '.a:sect.' '.a:page
80 func <SID>FindPage(sect, page)
81 let where = system("/usr/bin/man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page))
83 if matchstr(where, " [^ ]*$") !~ "^ /"
90 func <SID>GetPage(...)
101 " To support: nmap K :Man <cword>
103 let page = expand('<cword>')
106 if sect != "" && s:FindPage(sect, page) == 0
109 if s:FindPage(sect, page) == 0
110 echo "\nCannot find a '".page."'."
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
118 " Use an existing "man" window if it exists, otherwise open a new one.
119 if &filetype != "man"
120 let thiswin = winnr()
123 exe "norm! " . thiswin . "\<C-W>w"
125 if &filetype == "man"
129 if thiswin == winnr()
134 if &filetype != "man"
139 silent exec "edit $HOME/".page.".".sect."~"
140 " Avoid warning for editing the dummy file twice
141 setl buftype=nofile noswapfile
144 silent exec "norm 1GdG"
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*$'
151 while getline('$') =~ '^\s*$'
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"
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