blob: d9dbaf47d4f0c11b60241e42ed6bb6185522c91b [file] [log] [blame]
Bram Moolenaard799daa2022-06-20 11:17:32 +01001" Vim filetype plugin autoload file
2" Language: man
lifecrisis17befac2023-08-20 18:41:25 +00003" Maintainer: Jason Franklin <jason@oneway.dev>
Bram Moolenaard799daa2022-06-20 11:17:32 +01004" Maintainer: SungHyun Nam <goweol@gmail.com>
5" Autoload Split: Bram Moolenaar
Christian Brabandtf942db22024-01-17 21:50:16 +01006" Last Change: 2024 Jan 17 (make it work on AIX, see #13847)
Ivan Shapovalov84ca1382024-07-06 16:56:02 +02007" 2024 Jul 06 (honor command modifiers, #15117)
Bram Moolenaard799daa2022-06-20 11:17:32 +01008
9let s:cpo_save = &cpo
10set cpo-=C
11
12let s:man_tag_depth = 0
13
14let s:man_sect_arg = ""
15let s:man_find_arg = "-w"
16try
Christian Brabandtf942db22024-01-17 21:50:16 +010017 if !has("win32") && $OSTYPE !~ 'cygwin\|linux'
18 " cache the value
19 let uname_s = system('uname -s')
20
21 if uname_s =~ "SunOS" && system('uname -r') =~ "^5"
22 " Special Case for Man on SunOS
23 let s:man_sect_arg = "-s"
24 let s:man_find_arg = "-l"
25 elseif uname_s =~? 'AIX'
26 " Special Case for Man on AIX
27 let s:man_sect_arg = ""
28 let s:man_find_arg = ""
29 endif
Bram Moolenaard799daa2022-06-20 11:17:32 +010030 endif
31catch /E145:/
32 " Ignore the error in restricted mode
33endtry
34
Christian Brabandtf942db22024-01-17 21:50:16 +010035unlet! uname_s
36
goweol8cfe52e2023-08-18 06:13:29 +090037func s:ParseIntoPageAndSection()
38 " Accommodate a reference that terminates in a hyphen.
39 "
40 " See init_charset_table() at
41 " https://git.savannah.gnu.org/cgit/groff.git/tree/src/roff/troff/input.cpp?h=1.22.4#n6794
42 "
43 " See can_break_after() at
44 " https://git.savannah.gnu.org/cgit/groff.git/tree/src/roff/troff/charinfo.h?h=1.22.4#n140
45 "
46 " Assumptions and limitations:
47 " 1) Manual-page references (in consequence of command-related filenames)
48 " do not contain non-ASCII HYPHENs (0x2010), any terminating HYPHEN
49 " must have been introduced to mark division of a word at the end of
50 " a line and can be discarded; whereas similar references may contain
51 " ASCII HYPHEN-MINUSes (0x002d) and any terminating HYPHEN-MINUS forms
52 " a compound word in addition to marking word division.
53 " 2) Well-formed manual-page references always have a section suffix, e.g.
54 " "git-commit(1)", therefore suspended hyphenated compounds are not
55 " determined, e.g. [V] (With cursor at _git-merge-_ below...)
56 " ".................... git-merge- and git-merge-base. (See git-cherry-
57 " pick(1) and git-cherry(1).)" (... look up "git-merge-pick(1)".)
58 "
59 " Note that EM DASH (0x2014), a third stooge from init_charset_table(),
60 " neither connects nor divides parts of a word.
61 let str = expand("<cWORD>")
62
63 if str =~ '\%u2010$' " HYPHEN (-1).
64 let str = strpart(str, 0, strridx(str, "\u2010"))
65
66 " Append the leftmost WORD (or an empty string) from the line below.
67 let str .= get(split(get(getbufline(bufnr('%'), line('.') + 1), 0, '')), 0, '')
68 elseif str =~ '-$' " HYPHEN-MINUS.
69 " Append the leftmost WORD (or an empty string) from the line below.
70 let str .= get(split(get(getbufline(bufnr('%'), line('.') + 1), 0, '')), 0, '')
71 endif
72
73 " According to man(1), section name formats vary (MANSECT):
74 " 1 n l 8 3 2 3posix 3pm 3perl 3am 5 4 9 6 7
75 let parts = matchlist(str, '\(\k\+\)(\(\k\+\))')
76 return (len(parts) > 2)
77 \ ? {'page': parts[1], 'section': parts[2]}
78 \ : {'page': matchstr(str, '\k\+'), 'section': ''}
79endfunc
80
Bram Moolenaard799daa2022-06-20 11:17:32 +010081func dist#man#PreGetPage(cnt)
82 if a:cnt == 0
goweol8cfe52e2023-08-18 06:13:29 +090083 let what = s:ParseIntoPageAndSection()
84 let sect = what.section
85 let page = what.page
Bram Moolenaard799daa2022-06-20 11:17:32 +010086 else
goweol8cfe52e2023-08-18 06:13:29 +090087 let what = s:ParseIntoPageAndSection()
Bram Moolenaard799daa2022-06-20 11:17:32 +010088 let sect = a:cnt
goweol8cfe52e2023-08-18 06:13:29 +090089 let page = what.page
Bram Moolenaard799daa2022-06-20 11:17:32 +010090 endif
goweol8cfe52e2023-08-18 06:13:29 +090091
Bram Moolenaard799daa2022-06-20 11:17:32 +010092 call dist#man#GetPage('', sect, page)
93endfunc
94
95func s:GetCmdArg(sect, page)
Bram Moolenaard799daa2022-06-20 11:17:32 +010096 if empty(a:sect)
97 return shellescape(a:page)
98 endif
99
100 return s:man_sect_arg . ' ' . shellescape(a:sect) . ' ' . shellescape(a:page)
101endfunc
102
103func s:FindPage(sect, page)
104 let l:cmd = printf('man %s %s', s:man_find_arg, s:GetCmdArg(a:sect, a:page))
105 call system(l:cmd)
106
107 if v:shell_error
108 return 0
109 endif
110
111 return 1
112endfunc
113
114func dist#man#GetPage(cmdmods, ...)
115 if a:0 >= 2
116 let sect = a:1
117 let page = a:2
118 elseif a:0 >= 1
119 let sect = ""
120 let page = a:1
121 else
122 return
123 endif
124
goweol8cfe52e2023-08-18 06:13:29 +0900125 " To support: nmap K :Man <cWORD><CR>
126 if page ==? '<cword>'
127 let what = s:ParseIntoPageAndSection()
128 let sect = what.section
129 let page = what.page
Bram Moolenaard799daa2022-06-20 11:17:32 +0100130 endif
131
132 if !exists('g:ft_man_no_sect_fallback') || (g:ft_man_no_sect_fallback == 0)
133 if sect != "" && s:FindPage(sect, page) == 0
134 let sect = ""
135 endif
136 endif
137 if s:FindPage(sect, page) == 0
138 let msg = 'man.vim: no manual entry for "' . page . '"'
139 if !empty(sect)
140 let msg .= ' in section ' . sect
141 endif
142 echomsg msg
143 return
144 endif
145 exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%")
146 exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".")
147 exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".")
148 let s:man_tag_depth = s:man_tag_depth + 1
149
150 let open_cmd = 'edit'
151
152 " Use an existing "man" window if it exists, otherwise open a new one.
153 if &filetype != "man"
154 let thiswin = winnr()
155 exe "norm! \<C-W>b"
156 if winnr() > 1
157 exe "norm! " . thiswin . "\<C-W>w"
158 while 1
159 if &filetype == "man"
160 break
161 endif
162 exe "norm! \<C-W>w"
163 if thiswin == winnr()
164 break
165 endif
166 endwhile
167 endif
168 if &filetype != "man"
Ivan Shapovalov84ca1382024-07-06 16:56:02 +0200169 if a:cmdmods =~ '\<\(tab\|vertical\|horizontal\)\>'
170 let open_cmd = a:cmdmods . ' split'
171 elseif exists("g:ft_man_open_mode")
Bram Moolenaard799daa2022-06-20 11:17:32 +0100172 if g:ft_man_open_mode == 'vert'
173 let open_cmd = 'vsplit'
174 elseif g:ft_man_open_mode == 'tab'
175 let open_cmd = 'tabedit'
176 else
177 let open_cmd = 'split'
178 endif
179 else
Ivan Shapovalov84ca1382024-07-06 16:56:02 +0200180 let open_cmd = 'split'
Bram Moolenaard799daa2022-06-20 11:17:32 +0100181 endif
182 endif
183 endif
184
185 silent execute open_cmd . " $HOME/" . page . '.' . sect . '~'
186
187 " Avoid warning for editing the dummy file twice
188 setl buftype=nofile noswapfile
189
190 setl fdc=0 ma nofen nonu nornu
191 %delete _
192 let unsetwidth = 0
193 if empty($MANWIDTH)
194 let $MANWIDTH = winwidth(0)
195 let unsetwidth = 1
196 endif
197
198 " Ensure Vim is not recursively invoked (man-db does this) when doing ctrl-[
199 " on a man page reference by unsetting MANPAGER.
200 " Some versions of env(1) do not support the '-u' option, and in such case
201 " we set MANPAGER=cat.
202 if !exists('s:env_has_u')
203 call system('env -u x true')
204 let s:env_has_u = (v:shell_error == 0)
205 endif
206 let env_cmd = s:env_has_u ? 'env -u MANPAGER' : 'env MANPAGER=cat'
207 let env_cmd .= ' GROFF_NO_SGR=1'
lifecrisis17befac2023-08-20 18:41:25 +0000208 let man_cmd = env_cmd . ' man ' . s:GetCmdArg(sect, page)
209
Bram Moolenaard799daa2022-06-20 11:17:32 +0100210 silent exec "r !" . man_cmd
211
lifecrisis17befac2023-08-20 18:41:25 +0000212 " Emulate piping the buffer through the "col -b" command.
213 " Ref: https://github.com/vim/vim/issues/12301
Yee Cheng Chin249a2082023-09-16 18:09:47 +0200214 exe 'silent! keepjumps keeppatterns %s/\v(.)\b\ze\1?//e' .. (&gdefault ? '' : 'g')
lifecrisis17befac2023-08-20 18:41:25 +0000215
Bram Moolenaard799daa2022-06-20 11:17:32 +0100216 if unsetwidth
217 let $MANWIDTH = ''
218 endif
219 " Remove blank lines from top and bottom.
220 while line('$') > 1 && getline(1) =~ '^\s*$'
221 1delete _
222 endwhile
223 while line('$') > 1 && getline('$') =~ '^\s*$'
224 $delete _
225 endwhile
226 1
227 setl ft=man nomod
228 setl bufhidden=hide
229 setl nobuflisted
230 setl noma
231endfunc
232
233func dist#man#PopPage()
234 if s:man_tag_depth > 0
235 let s:man_tag_depth = s:man_tag_depth - 1
236 exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth
237 exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth
238 exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth
goweol6e5ab2b2023-08-18 06:12:42 +0900239
Bram Moolenaard799daa2022-06-20 11:17:32 +0100240 exec s:man_tag_buf."b"
goweol6e5ab2b2023-08-18 06:12:42 +0900241 call cursor(s:man_tag_lin, s:man_tag_col)
242
Bram Moolenaard799daa2022-06-20 11:17:32 +0100243 exec "unlet s:man_tag_buf_".s:man_tag_depth
244 exec "unlet s:man_tag_lin_".s:man_tag_depth
245 exec "unlet s:man_tag_col_".s:man_tag_depth
246 unlet s:man_tag_buf s:man_tag_lin s:man_tag_col
247 endif
248endfunc
249
250let &cpo = s:cpo_save
251unlet s:cpo_save
252
253" vim: set sw=2 ts=8 noet: