blob: 281b751583b2b1a41e29fdc52624c55a08c054e8 [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)
David Mandelbergb34ce3e2025-03-05 20:38:20 +01008" 2025 Mar 05 (add :keepjumps, #16791)
Bram Moolenaard799daa2022-06-20 11:17:32 +01009
10let s:cpo_save = &cpo
11set cpo-=C
12
13let s:man_tag_depth = 0
14
15let s:man_sect_arg = ""
16let s:man_find_arg = "-w"
17try
Christian Brabandtf942db22024-01-17 21:50:16 +010018 if !has("win32") && $OSTYPE !~ 'cygwin\|linux'
19 " cache the value
20 let uname_s = system('uname -s')
21
22 if uname_s =~ "SunOS" && system('uname -r') =~ "^5"
23 " Special Case for Man on SunOS
24 let s:man_sect_arg = "-s"
25 let s:man_find_arg = "-l"
26 elseif uname_s =~? 'AIX'
27 " Special Case for Man on AIX
28 let s:man_sect_arg = ""
29 let s:man_find_arg = ""
30 endif
Bram Moolenaard799daa2022-06-20 11:17:32 +010031 endif
32catch /E145:/
33 " Ignore the error in restricted mode
34endtry
35
Christian Brabandtf942db22024-01-17 21:50:16 +010036unlet! uname_s
37
goweol8cfe52e2023-08-18 06:13:29 +090038func s:ParseIntoPageAndSection()
39 " Accommodate a reference that terminates in a hyphen.
40 "
41 " See init_charset_table() at
42 " https://git.savannah.gnu.org/cgit/groff.git/tree/src/roff/troff/input.cpp?h=1.22.4#n6794
43 "
44 " See can_break_after() at
45 " https://git.savannah.gnu.org/cgit/groff.git/tree/src/roff/troff/charinfo.h?h=1.22.4#n140
46 "
47 " Assumptions and limitations:
48 " 1) Manual-page references (in consequence of command-related filenames)
49 " do not contain non-ASCII HYPHENs (0x2010), any terminating HYPHEN
50 " must have been introduced to mark division of a word at the end of
51 " a line and can be discarded; whereas similar references may contain
52 " ASCII HYPHEN-MINUSes (0x002d) and any terminating HYPHEN-MINUS forms
53 " a compound word in addition to marking word division.
54 " 2) Well-formed manual-page references always have a section suffix, e.g.
55 " "git-commit(1)", therefore suspended hyphenated compounds are not
56 " determined, e.g. [V] (With cursor at _git-merge-_ below...)
57 " ".................... git-merge- and git-merge-base. (See git-cherry-
58 " pick(1) and git-cherry(1).)" (... look up "git-merge-pick(1)".)
59 "
60 " Note that EM DASH (0x2014), a third stooge from init_charset_table(),
61 " neither connects nor divides parts of a word.
62 let str = expand("<cWORD>")
63
64 if str =~ '\%u2010$' " HYPHEN (-1).
65 let str = strpart(str, 0, strridx(str, "\u2010"))
66
67 " Append the leftmost WORD (or an empty string) from the line below.
68 let str .= get(split(get(getbufline(bufnr('%'), line('.') + 1), 0, '')), 0, '')
69 elseif str =~ '-$' " HYPHEN-MINUS.
70 " Append the leftmost WORD (or an empty string) from the line below.
71 let str .= get(split(get(getbufline(bufnr('%'), line('.') + 1), 0, '')), 0, '')
72 endif
73
74 " According to man(1), section name formats vary (MANSECT):
75 " 1 n l 8 3 2 3posix 3pm 3perl 3am 5 4 9 6 7
76 let parts = matchlist(str, '\(\k\+\)(\(\k\+\))')
77 return (len(parts) > 2)
78 \ ? {'page': parts[1], 'section': parts[2]}
79 \ : {'page': matchstr(str, '\k\+'), 'section': ''}
80endfunc
81
Bram Moolenaard799daa2022-06-20 11:17:32 +010082func dist#man#PreGetPage(cnt)
83 if a:cnt == 0
goweol8cfe52e2023-08-18 06:13:29 +090084 let what = s:ParseIntoPageAndSection()
85 let sect = what.section
86 let page = what.page
Bram Moolenaard799daa2022-06-20 11:17:32 +010087 else
goweol8cfe52e2023-08-18 06:13:29 +090088 let what = s:ParseIntoPageAndSection()
Bram Moolenaard799daa2022-06-20 11:17:32 +010089 let sect = a:cnt
goweol8cfe52e2023-08-18 06:13:29 +090090 let page = what.page
Bram Moolenaard799daa2022-06-20 11:17:32 +010091 endif
goweol8cfe52e2023-08-18 06:13:29 +090092
Bram Moolenaard799daa2022-06-20 11:17:32 +010093 call dist#man#GetPage('', sect, page)
94endfunc
95
96func s:GetCmdArg(sect, page)
Bram Moolenaard799daa2022-06-20 11:17:32 +010097 if empty(a:sect)
98 return shellescape(a:page)
99 endif
100
101 return s:man_sect_arg . ' ' . shellescape(a:sect) . ' ' . shellescape(a:page)
102endfunc
103
104func s:FindPage(sect, page)
105 let l:cmd = printf('man %s %s', s:man_find_arg, s:GetCmdArg(a:sect, a:page))
106 call system(l:cmd)
107
108 if v:shell_error
109 return 0
110 endif
111
112 return 1
113endfunc
114
115func dist#man#GetPage(cmdmods, ...)
116 if a:0 >= 2
117 let sect = a:1
118 let page = a:2
119 elseif a:0 >= 1
120 let sect = ""
121 let page = a:1
122 else
123 return
124 endif
125
goweol8cfe52e2023-08-18 06:13:29 +0900126 " To support: nmap K :Man <cWORD><CR>
127 if page ==? '<cword>'
128 let what = s:ParseIntoPageAndSection()
129 let sect = what.section
130 let page = what.page
Bram Moolenaard799daa2022-06-20 11:17:32 +0100131 endif
132
133 if !exists('g:ft_man_no_sect_fallback') || (g:ft_man_no_sect_fallback == 0)
134 if sect != "" && s:FindPage(sect, page) == 0
135 let sect = ""
136 endif
137 endif
138 if s:FindPage(sect, page) == 0
139 let msg = 'man.vim: no manual entry for "' . page . '"'
140 if !empty(sect)
141 let msg .= ' in section ' . sect
142 endif
143 echomsg msg
144 return
145 endif
146 exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%")
147 exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".")
148 exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".")
149 let s:man_tag_depth = s:man_tag_depth + 1
150
151 let open_cmd = 'edit'
152
153 " Use an existing "man" window if it exists, otherwise open a new one.
154 if &filetype != "man"
155 let thiswin = winnr()
156 exe "norm! \<C-W>b"
157 if winnr() > 1
158 exe "norm! " . thiswin . "\<C-W>w"
159 while 1
160 if &filetype == "man"
161 break
162 endif
163 exe "norm! \<C-W>w"
164 if thiswin == winnr()
165 break
166 endif
167 endwhile
168 endif
169 if &filetype != "man"
Ivan Shapovalov84ca1382024-07-06 16:56:02 +0200170 if a:cmdmods =~ '\<\(tab\|vertical\|horizontal\)\>'
171 let open_cmd = a:cmdmods . ' split'
172 elseif exists("g:ft_man_open_mode")
Bram Moolenaard799daa2022-06-20 11:17:32 +0100173 if g:ft_man_open_mode == 'vert'
174 let open_cmd = 'vsplit'
175 elseif g:ft_man_open_mode == 'tab'
176 let open_cmd = 'tabedit'
177 else
178 let open_cmd = 'split'
179 endif
180 else
Ivan Shapovalov84ca1382024-07-06 16:56:02 +0200181 let open_cmd = 'split'
Bram Moolenaard799daa2022-06-20 11:17:32 +0100182 endif
183 endif
184 endif
185
186 silent execute open_cmd . " $HOME/" . page . '.' . sect . '~'
187
188 " Avoid warning for editing the dummy file twice
189 setl buftype=nofile noswapfile
190
191 setl fdc=0 ma nofen nonu nornu
David Mandelbergb34ce3e2025-03-05 20:38:20 +0100192 keepjumps %delete _
Bram Moolenaard799daa2022-06-20 11:17:32 +0100193 let unsetwidth = 0
194 if empty($MANWIDTH)
195 let $MANWIDTH = winwidth(0)
196 let unsetwidth = 1
197 endif
198
199 " Ensure Vim is not recursively invoked (man-db does this) when doing ctrl-[
200 " on a man page reference by unsetting MANPAGER.
201 " Some versions of env(1) do not support the '-u' option, and in such case
202 " we set MANPAGER=cat.
203 if !exists('s:env_has_u')
204 call system('env -u x true')
205 let s:env_has_u = (v:shell_error == 0)
206 endif
207 let env_cmd = s:env_has_u ? 'env -u MANPAGER' : 'env MANPAGER=cat'
208 let env_cmd .= ' GROFF_NO_SGR=1'
lifecrisis17befac2023-08-20 18:41:25 +0000209 let man_cmd = env_cmd . ' man ' . s:GetCmdArg(sect, page)
210
Bram Moolenaard799daa2022-06-20 11:17:32 +0100211 silent exec "r !" . man_cmd
212
lifecrisis17befac2023-08-20 18:41:25 +0000213 " Emulate piping the buffer through the "col -b" command.
214 " Ref: https://github.com/vim/vim/issues/12301
Yee Cheng Chin249a2082023-09-16 18:09:47 +0200215 exe 'silent! keepjumps keeppatterns %s/\v(.)\b\ze\1?//e' .. (&gdefault ? '' : 'g')
lifecrisis17befac2023-08-20 18:41:25 +0000216
Bram Moolenaard799daa2022-06-20 11:17:32 +0100217 if unsetwidth
218 let $MANWIDTH = ''
219 endif
220 " Remove blank lines from top and bottom.
221 while line('$') > 1 && getline(1) =~ '^\s*$'
David Mandelbergb34ce3e2025-03-05 20:38:20 +0100222 keepjumps 1delete _
Bram Moolenaard799daa2022-06-20 11:17:32 +0100223 endwhile
224 while line('$') > 1 && getline('$') =~ '^\s*$'
David Mandelbergb34ce3e2025-03-05 20:38:20 +0100225 keepjumps $delete _
Bram Moolenaard799daa2022-06-20 11:17:32 +0100226 endwhile
227 1
228 setl ft=man nomod
229 setl bufhidden=hide
230 setl nobuflisted
231 setl noma
232endfunc
233
234func dist#man#PopPage()
235 if s:man_tag_depth > 0
236 let s:man_tag_depth = s:man_tag_depth - 1
237 exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth
238 exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth
239 exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth
goweol6e5ab2b2023-08-18 06:12:42 +0900240
Bram Moolenaard799daa2022-06-20 11:17:32 +0100241 exec s:man_tag_buf."b"
goweol6e5ab2b2023-08-18 06:12:42 +0900242 call cursor(s:man_tag_lin, s:man_tag_col)
243
Bram Moolenaard799daa2022-06-20 11:17:32 +0100244 exec "unlet s:man_tag_buf_".s:man_tag_depth
245 exec "unlet s:man_tag_lin_".s:man_tag_depth
246 exec "unlet s:man_tag_col_".s:man_tag_depth
247 unlet s:man_tag_buf s:man_tag_lin s:man_tag_col
248 endif
249endfunc
250
251let &cpo = s:cpo_save
252unlet s:cpo_save
253
254" vim: set sw=2 ts=8 noet: