blob: 242a7712c7ae2e081c62aeb08e4c47f87d3e69a6 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim filetype plugin file
2" Language: man
Bram Moolenaar5c736222010-01-06 20:54:52 +01003" Maintainer: SungHyun Nam <goweol@gmail.com>
Bram Moolenaar5302d9e2011-09-14 17:55:08 +02004" Last Change: 2011 Jul 25
Bram Moolenaar071d4272004-06-13 20:20:40 +00005
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
Bram Moolenaar81af9252010-12-10 20:35:50 +010018 " Ensure Vim is not recursively invoked (man-db does this)
19 " when doing ctrl-[ on a man page reference.
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020020 if exists("$MANPAGER")
21 let $MANPAGER = ""
22 endif
Bram Moolenaar81af9252010-12-10 20:35:50 +010023
Bram Moolenaar071d4272004-06-13 20:20:40 +000024 " allow dot and dash in manual page name.
25 setlocal iskeyword+=\.,-
26
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
Bram Moolenaard2cec5b2006-03-28 21:08:56 +000032 nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:setl nomod<CR>''
Bram Moolenaar071d4272004-06-13 20:20:40 +000033
34 nnoremap <buffer> <c-]> :call <SID>PreGetPage(v:count)<CR>
35 nnoremap <buffer> <c-t> :call <SID>PopPage()<CR>
36 endif
37
38endif
39
40if exists(":Man") != 2
41 com -nargs=+ Man call s:GetPage(<f-args>)
42 nmap <Leader>K :call <SID>PreGetPage(0)<CR>
43endif
44
45" Define functions only once.
46if !exists("s:man_tag_depth")
47
48let s:man_tag_depth = 0
49
Bram Moolenaar864207d2008-06-24 22:14:38 +000050let s:man_sect_arg = ""
51let s:man_find_arg = "-w"
52try
53 if !has("win32") && $OSTYPE !~ 'cygwin\|linux' && system('uname -s') =~ "SunOS" && system('uname -r') =~ "^5"
54 let s:man_sect_arg = "-s"
55 let s:man_find_arg = "-l"
56 endif
57catch /E145:/
58 " Ignore the error in restricted mode
59endtry
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
61func <SID>PreGetPage(cnt)
62 if a:cnt == 0
63 let old_isk = &iskeyword
64 setl iskeyword+=(,)
65 let str = expand("<cword>")
66 let &l:iskeyword = old_isk
67 let page = substitute(str, '(*\(\k\+\).*', '\1', '')
68 let sect = substitute(str, '\(\k\+\)(\([^()]*\)).*', '\2', '')
69 if match(sect, '^[0-9 ]\+$') == -1
70 let sect = ""
71 endif
72 if sect == page
73 let sect = ""
74 endif
75 else
76 let sect = a:cnt
77 let page = expand("<cword>")
78 endif
79 call s:GetPage(sect, page)
80endfunc
81
82func <SID>GetCmdArg(sect, page)
83 if a:sect == ''
84 return a:page
85 endif
86 return s:man_sect_arg.' '.a:sect.' '.a:page
87endfunc
88
89func <SID>FindPage(sect, page)
90 let where = system("/usr/bin/man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page))
91 if where !~ "^/"
92 if matchstr(where, " [^ ]*$") !~ "^ /"
93 return 0
94 endif
95 endif
96 return 1
97endfunc
98
99func <SID>GetPage(...)
100 if a:0 >= 2
101 let sect = a:1
102 let page = a:2
103 elseif a:0 >= 1
104 let sect = ""
105 let page = a:1
106 else
107 return
108 endif
109
110 " To support: nmap K :Man <cword>
111 if page == '<cword>'
112 let page = expand('<cword>')
113 endif
114
115 if sect != "" && s:FindPage(sect, page) == 0
116 let sect = ""
117 endif
118 if s:FindPage(sect, page) == 0
119 echo "\nCannot find a '".page."'."
120 return
121 endif
122 exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%")
123 exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".")
124 exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".")
125 let s:man_tag_depth = s:man_tag_depth + 1
126
127 " Use an existing "man" window if it exists, otherwise open a new one.
128 if &filetype != "man"
129 let thiswin = winnr()
130 exe "norm! \<C-W>b"
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000131 if winnr() > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 exe "norm! " . thiswin . "\<C-W>w"
133 while 1
134 if &filetype == "man"
135 break
136 endif
137 exe "norm! \<C-W>w"
138 if thiswin == winnr()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 break
140 endif
141 endwhile
142 endif
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000143 if &filetype != "man"
144 new
145 setl nonu fdc=0
146 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 endif
148 silent exec "edit $HOME/".page.".".sect."~"
149 " Avoid warning for editing the dummy file twice
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000150 setl buftype=nofile noswapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000152 setl ma
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 silent exec "norm 1GdG"
154 let $MANWIDTH = winwidth(0)
155 silent exec "r!/usr/bin/man ".s:GetCmdArg(sect, page)." | col -b"
156 " Remove blank lines from top and bottom.
157 while getline(1) =~ '^\s*$'
158 silent norm ggdd
159 endwhile
160 while getline('$') =~ '^\s*$'
161 silent norm Gdd
162 endwhile
163 1
164 setl ft=man nomod
165 setl bufhidden=hide
166 setl nobuflisted
167endfunc
168
169func <SID>PopPage()
170 if s:man_tag_depth > 0
171 let s:man_tag_depth = s:man_tag_depth - 1
172 exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth
173 exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth
174 exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth
175 exec s:man_tag_buf."b"
176 exec s:man_tag_lin
177 exec "norm ".s:man_tag_col."|"
178 exec "unlet s:man_tag_buf_".s:man_tag_depth
179 exec "unlet s:man_tag_lin_".s:man_tag_depth
180 exec "unlet s:man_tag_col_".s:man_tag_depth
181 unlet s:man_tag_buf s:man_tag_lin s:man_tag_col
182 endif
183endfunc
184
185endif
186
187" vim: set sw=2: