1 " Vim filetype plugin file
3 " Maintainer: Nam SungHyun <namsh@kldp.org>
4 " Last Change: 2007 Nov 30
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 let s:man_sect_arg = ""
45 let s:man_find_arg = "-w"
47 if !has("win32") && $OSTYPE !~ 'cygwin\|linux' && system('uname -s') =~ "SunOS" && system('uname -r') =~ "^5"
48 let s:man_sect_arg = "-s"
49 let s:man_find_arg = "-l"
52 " Ignore the error in restricted mode
55 func <SID>PreGetPage(cnt)
57 let old_isk = &iskeyword
59 let str = expand("<cword>")
60 let &l:iskeyword = old_isk
61 let page = substitute(str, '(*\(\k\+\).*', '\1', '')
62 let sect = substitute(str, '\(\k\+\)(\([^()]*\)).*', '\2', '')
63 if match(sect, '^[0-9 ]\+$') == -1
71 let page = expand("<cword>")
73 call s:GetPage(sect, page)
76 func <SID>GetCmdArg(sect, page)
80 return s:man_sect_arg.' '.a:sect.' '.a:page
83 func <SID>FindPage(sect, page)
84 let where = system("/usr/bin/man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page))
86 if matchstr(where, " [^ ]*$") !~ "^ /"
93 func <SID>GetPage(...)
104 " To support: nmap K :Man <cword>
106 let page = expand('<cword>')
109 if sect != "" && s:FindPage(sect, page) == 0
112 if s:FindPage(sect, page) == 0
113 echo "\nCannot find a '".page."'."
116 exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%")
117 exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".")
118 exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".")
119 let s:man_tag_depth = s:man_tag_depth + 1
121 " Use an existing "man" window if it exists, otherwise open a new one.
122 if &filetype != "man"
123 let thiswin = winnr()
126 exe "norm! " . thiswin . "\<C-W>w"
128 if &filetype == "man"
132 if thiswin == winnr()
137 if &filetype != "man"
142 silent exec "edit $HOME/".page.".".sect."~"
143 " Avoid warning for editing the dummy file twice
144 setl buftype=nofile noswapfile
147 silent exec "norm 1GdG"
148 let $MANWIDTH = winwidth(0)
149 silent exec "r!/usr/bin/man ".s:GetCmdArg(sect, page)." | col -b"
150 " Remove blank lines from top and bottom.
151 while getline(1) =~ '^\s*$'
154 while getline('$') =~ '^\s*$'
164 if s:man_tag_depth > 0
165 let s:man_tag_depth = s:man_tag_depth - 1
166 exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth
167 exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth
168 exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth
169 exec s:man_tag_buf."b"
171 exec "norm ".s:man_tag_col."|"
172 exec "unlet s:man_tag_buf_".s:man_tag_depth
173 exec "unlet s:man_tag_lin_".s:man_tag_depth
174 exec "unlet s:man_tag_col_".s:man_tag_depth
175 unlet s:man_tag_buf s:man_tag_lin s:man_tag_col