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) |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 8 | |
| 9 | let s:cpo_save = &cpo |
| 10 | set cpo-=C |
| 11 | |
| 12 | let s:man_tag_depth = 0 |
| 13 | |
| 14 | let s:man_sect_arg = "" |
| 15 | let s:man_find_arg = "-w" |
| 16 | try |
Christian Brabandt | f942db2 | 2024-01-17 21:50:16 +0100 | [diff] [blame] | 17 | 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 Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 30 | endif |
| 31 | catch /E145:/ |
| 32 | " Ignore the error in restricted mode |
| 33 | endtry |
| 34 | |
Christian Brabandt | f942db2 | 2024-01-17 21:50:16 +0100 | [diff] [blame] | 35 | unlet! uname_s |
| 36 | |
goweol | 8cfe52e | 2023-08-18 06:13:29 +0900 | [diff] [blame] | 37 | func 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': ''} |
| 79 | endfunc |
| 80 | |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 81 | func dist#man#PreGetPage(cnt) |
| 82 | if a:cnt == 0 |
goweol | 8cfe52e | 2023-08-18 06:13:29 +0900 | [diff] [blame] | 83 | let what = s:ParseIntoPageAndSection() |
| 84 | let sect = what.section |
| 85 | let page = what.page |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 86 | else |
goweol | 8cfe52e | 2023-08-18 06:13:29 +0900 | [diff] [blame] | 87 | let what = s:ParseIntoPageAndSection() |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 88 | let sect = a:cnt |
goweol | 8cfe52e | 2023-08-18 06:13:29 +0900 | [diff] [blame] | 89 | let page = what.page |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 90 | endif |
goweol | 8cfe52e | 2023-08-18 06:13:29 +0900 | [diff] [blame] | 91 | |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 92 | call dist#man#GetPage('', sect, page) |
| 93 | endfunc |
| 94 | |
| 95 | func s:GetCmdArg(sect, page) |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 96 | if empty(a:sect) |
| 97 | return shellescape(a:page) |
| 98 | endif |
| 99 | |
| 100 | return s:man_sect_arg . ' ' . shellescape(a:sect) . ' ' . shellescape(a:page) |
| 101 | endfunc |
| 102 | |
| 103 | func 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 |
| 112 | endfunc |
| 113 | |
| 114 | func 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 | |
goweol | 8cfe52e | 2023-08-18 06:13:29 +0900 | [diff] [blame] | 125 | " 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 Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 130 | 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 Shapovalov | 84ca138 | 2024-07-06 16:56:02 +0200 | [diff] [blame] | 169 | if a:cmdmods =~ '\<\(tab\|vertical\|horizontal\)\>' |
| 170 | let open_cmd = a:cmdmods . ' split' |
| 171 | elseif exists("g:ft_man_open_mode") |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 172 | 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 Shapovalov | 84ca138 | 2024-07-06 16:56:02 +0200 | [diff] [blame] | 180 | let open_cmd = 'split' |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 181 | 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' |
lifecrisis | 17befac | 2023-08-20 18:41:25 +0000 | [diff] [blame] | 208 | let man_cmd = env_cmd . ' man ' . s:GetCmdArg(sect, page) |
| 209 | |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 210 | silent exec "r !" . man_cmd |
| 211 | |
lifecrisis | 17befac | 2023-08-20 18:41:25 +0000 | [diff] [blame] | 212 | " Emulate piping the buffer through the "col -b" command. |
| 213 | " Ref: https://github.com/vim/vim/issues/12301 |
Yee Cheng Chin | 249a208 | 2023-09-16 18:09:47 +0200 | [diff] [blame] | 214 | exe 'silent! keepjumps keeppatterns %s/\v(.)\b\ze\1?//e' .. (&gdefault ? '' : 'g') |
lifecrisis | 17befac | 2023-08-20 18:41:25 +0000 | [diff] [blame] | 215 | |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 216 | 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 |
| 231 | endfunc |
| 232 | |
| 233 | func 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 |
goweol | 6e5ab2b | 2023-08-18 06:12:42 +0900 | [diff] [blame] | 239 | |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 240 | exec s:man_tag_buf."b" |
goweol | 6e5ab2b | 2023-08-18 06:12:42 +0900 | [diff] [blame] | 241 | call cursor(s:man_tag_lin, s:man_tag_col) |
| 242 | |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 243 | 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 |
| 248 | endfunc |
| 249 | |
| 250 | let &cpo = s:cpo_save |
| 251 | unlet s:cpo_save |
| 252 | |
| 253 | " vim: set sw=2 ts=8 noet: |