1 " Vim filetype plugin file
3 " Maintainer: SungHyun Nam <goweol@gmail.com>
4 " Last Change: 2013 Jul 17
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 " Ensure Vim is not recursively invoked (man-db does this)
19 " when doing ctrl-[ on a man page reference.
20 if exists("$MANPAGER")
24 " allow dot and dash in manual page name.
25 setlocal iskeyword+=\.,-
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
32 nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:setl nomod<CR>''
34 nnoremap <buffer> <c-]> :call <SID>PreGetPage(v:count)<CR>
35 nnoremap <buffer> <c-t> :call <SID>PopPage()<CR>
38 let b:undo_ftplugin = "setlocal iskeyword<"
42 if exists(":Man") != 2
43 com -nargs=+ Man call s:GetPage(<f-args>)
44 nmap <Leader>K :call <SID>PreGetPage(0)<CR>
47 " Define functions only once.
48 if !exists("s:man_tag_depth")
50 let s:man_tag_depth = 0
52 let s:man_sect_arg = ""
53 let s:man_find_arg = "-w"
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"
60 " Ignore the error in restricted mode
63 func <SID>PreGetPage(cnt)
65 let old_isk = &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
79 let page = expand("<cword>")
81 call s:GetPage(sect, page)
84 func <SID>GetCmdArg(sect, page)
88 return s:man_sect_arg.' '.a:sect.' '.a:page
91 func <SID>FindPage(sect, page)
92 let where = system("/usr/bin/man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page))
94 if matchstr(where, " [^ ]*$") !~ "^ /"
101 func <SID>GetPage(...)
112 " To support: nmap K :Man <cword>
114 let page = expand('<cword>')
117 if sect != "" && s:FindPage(sect, page) == 0
120 if s:FindPage(sect, page) == 0
121 echo "\nCannot find a '".page."'."
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
129 " Use an existing "man" window if it exists, otherwise open a new one.
130 if &filetype != "man"
131 let thiswin = winnr()
134 exe "norm! " . thiswin . "\<C-W>w"
136 if &filetype == "man"
140 if thiswin == winnr()
145 if &filetype != "man"
150 silent exec "edit $HOME/".page.".".sect."~"
151 " Avoid warning for editing the dummy file twice
152 setl buftype=nofile noswapfile
154 setl ma nonu nornu nofen
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*$'
162 while getline('$') =~ '^\s*$'
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"
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