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