Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 1 | " Vim filetype plugin autoload file |
| 2 | " Language: man |
lifecrisis | 17befac | 2023-08-20 18:41:25 +0000 | [diff] [blame] | 3 | " Maintainer: Jason Franklin <jason@oneway.dev> |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 4 | " Maintainer: SungHyun Nam <goweol@gmail.com> |
| 5 | " Autoload Split: Bram Moolenaar |
Christian Brabandt | f942db2 | 2024-01-17 21:50:16 +0100 | [diff] [blame] | 6 | " Last Change: 2024 Jan 17 (make it work on AIX, see #13847) |
Ivan Shapovalov | 84ca138 | 2024-07-06 16:56:02 +0200 | [diff] [blame] | 7 | " 2024 Jul 06 (honor command modifiers, #15117) |
David Mandelberg | b34ce3e | 2025-03-05 20:38:20 +0100 | [diff] [blame] | 8 | " 2025 Mar 05 (add :keepjumps, #16791) |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 9 | |
| 10 | let s:cpo_save = &cpo |
| 11 | set cpo-=C |
| 12 | |
| 13 | let s:man_tag_depth = 0 |
| 14 | |
| 15 | let s:man_sect_arg = "" |
| 16 | let s:man_find_arg = "-w" |
| 17 | try |
Christian Brabandt | f942db2 | 2024-01-17 21:50:16 +0100 | [diff] [blame] | 18 | 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 Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 31 | endif |
| 32 | catch /E145:/ |
| 33 | " Ignore the error in restricted mode |
| 34 | endtry |
| 35 | |
Christian Brabandt | f942db2 | 2024-01-17 21:50:16 +0100 | [diff] [blame] | 36 | unlet! uname_s |
| 37 | |
goweol | 8cfe52e | 2023-08-18 06:13:29 +0900 | [diff] [blame] | 38 | func 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': ''} |
| 80 | endfunc |
| 81 | |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 82 | func dist#man#PreGetPage(cnt) |
| 83 | if a:cnt == 0 |
goweol | 8cfe52e | 2023-08-18 06:13:29 +0900 | [diff] [blame] | 84 | let what = s:ParseIntoPageAndSection() |
| 85 | let sect = what.section |
| 86 | let page = what.page |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 87 | else |
goweol | 8cfe52e | 2023-08-18 06:13:29 +0900 | [diff] [blame] | 88 | let what = s:ParseIntoPageAndSection() |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 89 | let sect = a:cnt |
goweol | 8cfe52e | 2023-08-18 06:13:29 +0900 | [diff] [blame] | 90 | let page = what.page |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 91 | endif |
goweol | 8cfe52e | 2023-08-18 06:13:29 +0900 | [diff] [blame] | 92 | |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 93 | call dist#man#GetPage('', sect, page) |
| 94 | endfunc |
| 95 | |
| 96 | func s:GetCmdArg(sect, page) |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 97 | if empty(a:sect) |
| 98 | return shellescape(a:page) |
| 99 | endif |
| 100 | |
| 101 | return s:man_sect_arg . ' ' . shellescape(a:sect) . ' ' . shellescape(a:page) |
| 102 | endfunc |
| 103 | |
| 104 | func 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 |
| 113 | endfunc |
| 114 | |
| 115 | func 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 | |
goweol | 8cfe52e | 2023-08-18 06:13:29 +0900 | [diff] [blame] | 126 | " 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 Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 131 | 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 Shapovalov | 84ca138 | 2024-07-06 16:56:02 +0200 | [diff] [blame] | 170 | if a:cmdmods =~ '\<\(tab\|vertical\|horizontal\)\>' |
| 171 | let open_cmd = a:cmdmods . ' split' |
| 172 | elseif exists("g:ft_man_open_mode") |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 173 | 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 Shapovalov | 84ca138 | 2024-07-06 16:56:02 +0200 | [diff] [blame] | 181 | let open_cmd = 'split' |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 182 | 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 Mandelberg | b34ce3e | 2025-03-05 20:38:20 +0100 | [diff] [blame] | 192 | keepjumps %delete _ |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 193 | 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' |
lifecrisis | 17befac | 2023-08-20 18:41:25 +0000 | [diff] [blame] | 209 | let man_cmd = env_cmd . ' man ' . s:GetCmdArg(sect, page) |
| 210 | |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 211 | silent exec "r !" . man_cmd |
| 212 | |
lifecrisis | 17befac | 2023-08-20 18:41:25 +0000 | [diff] [blame] | 213 | " Emulate piping the buffer through the "col -b" command. |
| 214 | " Ref: https://github.com/vim/vim/issues/12301 |
Yee Cheng Chin | 249a208 | 2023-09-16 18:09:47 +0200 | [diff] [blame] | 215 | exe 'silent! keepjumps keeppatterns %s/\v(.)\b\ze\1?//e' .. (&gdefault ? '' : 'g') |
lifecrisis | 17befac | 2023-08-20 18:41:25 +0000 | [diff] [blame] | 216 | |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 217 | if unsetwidth |
| 218 | let $MANWIDTH = '' |
| 219 | endif |
| 220 | " Remove blank lines from top and bottom. |
| 221 | while line('$') > 1 && getline(1) =~ '^\s*$' |
David Mandelberg | b34ce3e | 2025-03-05 20:38:20 +0100 | [diff] [blame] | 222 | keepjumps 1delete _ |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 223 | endwhile |
| 224 | while line('$') > 1 && getline('$') =~ '^\s*$' |
David Mandelberg | b34ce3e | 2025-03-05 20:38:20 +0100 | [diff] [blame] | 225 | keepjumps $delete _ |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 226 | endwhile |
| 227 | 1 |
| 228 | setl ft=man nomod |
| 229 | setl bufhidden=hide |
| 230 | setl nobuflisted |
| 231 | setl noma |
| 232 | endfunc |
| 233 | |
| 234 | func 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 |
goweol | 6e5ab2b | 2023-08-18 06:12:42 +0900 | [diff] [blame] | 240 | |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 241 | exec s:man_tag_buf."b" |
goweol | 6e5ab2b | 2023-08-18 06:12:42 +0900 | [diff] [blame] | 242 | call cursor(s:man_tag_lin, s:man_tag_col) |
| 243 | |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 244 | 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 |
| 249 | endfunc |
| 250 | |
| 251 | let &cpo = s:cpo_save |
| 252 | unlet s:cpo_save |
| 253 | |
| 254 | " vim: set sw=2 ts=8 noet: |