blob: cfcabc3fb9a59fc021e38bd0add3cd596c7c9a22 [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 Moolenaar7f2e9d72017-11-11 20:58:53 +01004" Last Change: 2017 Nov 11
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
18 " allow dot and dash in manual page name.
19 setlocal iskeyword+=\.,-
20
21 " Add mappings, unless the user didn't want this.
22 if !exists("no_plugin_maps") && !exists("no_man_maps")
23 if !hasmapto('<Plug>ManBS')
24 nmap <buffer> <LocalLeader>h <Plug>ManBS
25 endif
Bram Moolenaard2cec5b2006-03-28 21:08:56 +000026 nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:setl nomod<CR>''
Bram Moolenaar071d4272004-06-13 20:20:40 +000027
28 nnoremap <buffer> <c-]> :call <SID>PreGetPage(v:count)<CR>
29 nnoremap <buffer> <c-t> :call <SID>PopPage()<CR>
Bram Moolenaard042dc82015-11-24 19:18:36 +010030 nnoremap <buffer> <silent> q :q<CR>
31 endif
32
33 if exists('g:ft_man_folding_enable') && (g:ft_man_folding_enable == 1)
34 setlocal foldmethod=indent foldnestmax=1 foldenable
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 endif
36
Bram Moolenaar84f72352012-03-11 15:57:40 +010037 let b:undo_ftplugin = "setlocal iskeyword<"
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039endif
40
41if exists(":Man") != 2
42 com -nargs=+ Man call s:GetPage(<f-args>)
43 nmap <Leader>K :call <SID>PreGetPage(0)<CR>
Bram Moolenaar68563932017-01-10 13:31:15 +010044 nmap <Plug>ManPreGetPage :call <SID>PreGetPage(0)<CR>
Bram Moolenaar071d4272004-06-13 20:20:40 +000045endif
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)
Bram Moolenaar690afe12017-01-28 18:34:47 +010094 let where = system("man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page))
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 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"
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200148 if exists("g:ft_man_open_mode")
149 if g:ft_man_open_mode == "vert"
150 vnew
151 elseif g:ft_man_open_mode == "tab"
152 tabnew
153 else
154 new
155 endif
156 else
157 new
158 endif
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000159 setl nonu fdc=0
160 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 endif
162 silent exec "edit $HOME/".page.".".sect."~"
163 " Avoid warning for editing the dummy file twice
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000164 setl buftype=nofile noswapfile
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165
Bram Moolenaar9ba7e172013-07-17 22:37:26 +0200166 setl ma nonu nornu nofen
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 silent exec "norm 1GdG"
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200168 let unsetwidth = 0
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100169 if empty($MANWIDTH)
170 let $MANWIDTH = winwidth(0)
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200171 let unsetwidth = 1
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100172 endif
Bram Moolenaar7f2e9d72017-11-11 20:58:53 +0100173
174 " Ensure Vim is not recursively invoked (man-db does this) when doing ctrl-[
175 " on a man page reference by unsetting MANPAGER.
176 silent exec "r !env -u MANPAGER man ".s:GetCmdArg(sect, page)." | col -b"
177
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200178 if unsetwidth
179 let $MANWIDTH = ''
180 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 " Remove blank lines from top and bottom.
182 while getline(1) =~ '^\s*$'
Bram Moolenaar8feef4f2015-01-07 16:57:10 +0100183 silent keepj norm ggdd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 endwhile
185 while getline('$') =~ '^\s*$'
Bram Moolenaar8feef4f2015-01-07 16:57:10 +0100186 silent keepj norm Gdd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 endwhile
188 1
189 setl ft=man nomod
190 setl bufhidden=hide
191 setl nobuflisted
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200192 setl noma
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193endfunc
194
195func <SID>PopPage()
196 if s:man_tag_depth > 0
197 let s:man_tag_depth = s:man_tag_depth - 1
198 exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth
199 exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth
200 exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth
201 exec s:man_tag_buf."b"
202 exec s:man_tag_lin
203 exec "norm ".s:man_tag_col."|"
204 exec "unlet s:man_tag_buf_".s:man_tag_depth
205 exec "unlet s:man_tag_lin_".s:man_tag_depth
206 exec "unlet s:man_tag_col_".s:man_tag_depth
207 unlet s:man_tag_buf s:man_tag_lin s:man_tag_col
208 endif
209endfunc
210
211endif
212
Bram Moolenaarddf8d1c2016-06-20 11:22:54 +0200213" vim: set sw=2 ts=8 noet: