blob: c6e1e9d4f7e9769f44f59953201204ab5176b141 [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 Moolenaar9ba7e172013-07-17 22:37:26 +02004" Last Change: 2013 Jul 17
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
Bram Moolenaar84f72352012-03-11 15:57:40 +010038 let b:undo_ftplugin = "setlocal iskeyword<"
39
Bram Moolenaar071d4272004-06-13 20:20:40 +000040endif
41
42if exists(":Man") != 2
43 com -nargs=+ Man call s:GetPage(<f-args>)
44 nmap <Leader>K :call <SID>PreGetPage(0)<CR>
45endif
46
47" Define functions only once.
48if !exists("s:man_tag_depth")
49
50let s:man_tag_depth = 0
51
Bram Moolenaar864207d2008-06-24 22:14:38 +000052let s:man_sect_arg = ""
53let s:man_find_arg = "-w"
54try
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
59catch /E145:/
60 " Ignore the error in restricted mode
61endtry
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
63func <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)
82endfunc
83
84func <SID>GetCmdArg(sect, page)
85 if a:sect == ''
86 return a:page
87 endif
88 return s:man_sect_arg.' '.a:sect.' '.a:page
89endfunc
90
91func <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
99endfunc
100
101func <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
111
112 " To support: nmap K :Man <cword>
113 if page == '<cword>'
114 let page = expand('<cword>')
115 endif
116
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
128
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"
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000133 if winnr() > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 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()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 break
142 endif
143 endwhile
144 endif
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000145 if &filetype != "man"
146 new
147 setl nonu fdc=0
148 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 endif
150 silent exec "edit $HOME/".page.".".sect."~"
151 " Avoid warning for editing the dummy file twice
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000152 setl buftype=nofile noswapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
Bram Moolenaar9ba7e172013-07-17 22:37:26 +0200154 setl ma nonu nornu nofen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 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
169endfunc
170
171func <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
185endfunc
186
187endif
188
189" vim: set sw=2: