blob: 28b7e245b6f9bb3aaf056fda286fc528e6a4b25c [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 Moolenaar8feef4f2015-01-07 16:57:10 +01004" Last Change: 2014 Dec 29
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
Bram Moolenaarc2299672014-11-13 14:25:38 +010066 if &ft == 'man'
67 setl iskeyword+=(,)
68 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000069 let str = expand("<cword>")
70 let &l:iskeyword = old_isk
71 let page = substitute(str, '(*\(\k\+\).*', '\1', '')
72 let sect = substitute(str, '\(\k\+\)(\([^()]*\)).*', '\2', '')
73 if match(sect, '^[0-9 ]\+$') == -1
74 let sect = ""
75 endif
76 if sect == page
77 let sect = ""
78 endif
79 else
80 let sect = a:cnt
81 let page = expand("<cword>")
82 endif
83 call s:GetPage(sect, page)
84endfunc
85
86func <SID>GetCmdArg(sect, page)
87 if a:sect == ''
88 return a:page
89 endif
90 return s:man_sect_arg.' '.a:sect.' '.a:page
91endfunc
92
93func <SID>FindPage(sect, page)
94 let where = system("/usr/bin/man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page))
95 if where !~ "^/"
96 if matchstr(where, " [^ ]*$") !~ "^ /"
97 return 0
98 endif
99 endif
100 return 1
101endfunc
102
103func <SID>GetPage(...)
104 if a:0 >= 2
105 let sect = a:1
106 let page = a:2
107 elseif a:0 >= 1
108 let sect = ""
109 let page = a:1
110 else
111 return
112 endif
113
114 " To support: nmap K :Man <cword>
115 if page == '<cword>'
116 let page = expand('<cword>')
117 endif
118
119 if sect != "" && s:FindPage(sect, page) == 0
120 let sect = ""
121 endif
122 if s:FindPage(sect, page) == 0
123 echo "\nCannot find a '".page."'."
124 return
125 endif
126 exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%")
127 exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".")
128 exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".")
129 let s:man_tag_depth = s:man_tag_depth + 1
130
131 " Use an existing "man" window if it exists, otherwise open a new one.
132 if &filetype != "man"
133 let thiswin = winnr()
134 exe "norm! \<C-W>b"
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000135 if winnr() > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136 exe "norm! " . thiswin . "\<C-W>w"
137 while 1
138 if &filetype == "man"
139 break
140 endif
141 exe "norm! \<C-W>w"
142 if thiswin == winnr()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 break
144 endif
145 endwhile
146 endif
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000147 if &filetype != "man"
148 new
149 setl nonu fdc=0
150 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 endif
152 silent exec "edit $HOME/".page.".".sect."~"
153 " Avoid warning for editing the dummy file twice
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000154 setl buftype=nofile noswapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
Bram Moolenaar9ba7e172013-07-17 22:37:26 +0200156 setl ma nonu nornu nofen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 silent exec "norm 1GdG"
158 let $MANWIDTH = winwidth(0)
159 silent exec "r!/usr/bin/man ".s:GetCmdArg(sect, page)." | col -b"
160 " Remove blank lines from top and bottom.
161 while getline(1) =~ '^\s*$'
Bram Moolenaar8feef4f2015-01-07 16:57:10 +0100162 silent keepj norm ggdd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 endwhile
164 while getline('$') =~ '^\s*$'
Bram Moolenaar8feef4f2015-01-07 16:57:10 +0100165 silent keepj norm Gdd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 endwhile
167 1
168 setl ft=man nomod
169 setl bufhidden=hide
170 setl nobuflisted
171endfunc
172
173func <SID>PopPage()
174 if s:man_tag_depth > 0
175 let s:man_tag_depth = s:man_tag_depth - 1
176 exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth
177 exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth
178 exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth
179 exec s:man_tag_buf."b"
180 exec s:man_tag_lin
181 exec "norm ".s:man_tag_col."|"
182 exec "unlet s:man_tag_buf_".s:man_tag_depth
183 exec "unlet s:man_tag_lin_".s:man_tag_depth
184 exec "unlet s:man_tag_col_".s:man_tag_depth
185 unlet s:man_tag_buf s:man_tag_lin s:man_tag_col
186 endif
187endfunc
188
189endif
190
191" vim: set sw=2: