Install vim74
[msysgit.git] / share / vim / vim74 / ftplugin / man.vim
blobc6e1e9d4f7e9769f44f59953201204ab5176b141
1 " Vim filetype plugin file
2 " Language:     man
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".
10 if &filetype == "man"
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
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
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
31     endif
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>
36   endif
38   let b:undo_ftplugin = "setlocal iskeyword<"
40 endif
42 if exists(":Man") != 2
43   com -nargs=+ Man call s:GetPage(<f-args>)
44   nmap <Leader>K :call <SID>PreGetPage(0)<CR>
45 endif
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"
54 try
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
59 catch /E145:/
60   " Ignore the error in restricted mode
61 endtry
63 func <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)
82 endfunc
84 func <SID>GetCmdArg(sect, page)
85   if a:sect == ''
86     return a:page
87   endif
88   return s:man_sect_arg.' '.a:sect.' '.a:page
89 endfunc
91 func <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
99 endfunc
101 func <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
112   " To support:     nmap K :Man <cword>
113   if page == '<cword>'
114     let page = expand('<cword>')
115   endif
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
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
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*$'
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
169 endfunc
171 func <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
185 endfunc
187 endif
189 " vim: set sw=2: