Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 1 | " Vim filetype plugin autoload file |
| 2 | " Language: man |
| 3 | " Maintainer: Jason Franklin <vim@justemail.net> |
| 4 | " Maintainer: SungHyun Nam <goweol@gmail.com> |
| 5 | " Autoload Split: Bram Moolenaar |
goweol | 6e5ab2b | 2023-08-18 06:12:42 +0900 | [diff] [blame^] | 6 | " Last Change: 2023 Jun 28 |
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 |
| 16 | if !has("win32") && $OSTYPE !~ 'cygwin\|linux' && system('uname -s') =~ "SunOS" && system('uname -r') =~ "^5" |
| 17 | let s:man_sect_arg = "-s" |
| 18 | let s:man_find_arg = "-l" |
| 19 | endif |
| 20 | catch /E145:/ |
| 21 | " Ignore the error in restricted mode |
| 22 | endtry |
| 23 | |
| 24 | func dist#man#PreGetPage(cnt) |
| 25 | if a:cnt == 0 |
| 26 | let old_isk = &iskeyword |
| 27 | if &ft == 'man' |
| 28 | setl iskeyword+=(,) |
| 29 | endif |
| 30 | let str = expand("<cword>") |
| 31 | let &l:iskeyword = old_isk |
| 32 | let page = substitute(str, '(*\(\k\+\).*', '\1', '') |
| 33 | let sect = substitute(str, '\(\k\+\)(\([^()]*\)).*', '\2', '') |
| 34 | if match(sect, '^[0-9 ]\+$') == -1 |
| 35 | let sect = "" |
| 36 | endif |
| 37 | if sect == page |
| 38 | let sect = "" |
| 39 | endif |
| 40 | else |
| 41 | let sect = a:cnt |
| 42 | let page = expand("<cword>") |
| 43 | endif |
| 44 | call dist#man#GetPage('', sect, page) |
| 45 | endfunc |
| 46 | |
| 47 | func s:GetCmdArg(sect, page) |
| 48 | |
| 49 | if empty(a:sect) |
| 50 | return shellescape(a:page) |
| 51 | endif |
| 52 | |
| 53 | return s:man_sect_arg . ' ' . shellescape(a:sect) . ' ' . shellescape(a:page) |
| 54 | endfunc |
| 55 | |
| 56 | func s:FindPage(sect, page) |
| 57 | let l:cmd = printf('man %s %s', s:man_find_arg, s:GetCmdArg(a:sect, a:page)) |
| 58 | call system(l:cmd) |
| 59 | |
| 60 | if v:shell_error |
| 61 | return 0 |
| 62 | endif |
| 63 | |
| 64 | return 1 |
| 65 | endfunc |
| 66 | |
| 67 | func dist#man#GetPage(cmdmods, ...) |
| 68 | if a:0 >= 2 |
| 69 | let sect = a:1 |
| 70 | let page = a:2 |
| 71 | elseif a:0 >= 1 |
| 72 | let sect = "" |
| 73 | let page = a:1 |
| 74 | else |
| 75 | return |
| 76 | endif |
| 77 | |
| 78 | " To support: nmap K :Man <cword> |
| 79 | if page == '<cword>' |
| 80 | let page = expand('<cword>') |
| 81 | endif |
| 82 | |
| 83 | if !exists('g:ft_man_no_sect_fallback') || (g:ft_man_no_sect_fallback == 0) |
| 84 | if sect != "" && s:FindPage(sect, page) == 0 |
| 85 | let sect = "" |
| 86 | endif |
| 87 | endif |
| 88 | if s:FindPage(sect, page) == 0 |
| 89 | let msg = 'man.vim: no manual entry for "' . page . '"' |
| 90 | if !empty(sect) |
| 91 | let msg .= ' in section ' . sect |
| 92 | endif |
| 93 | echomsg msg |
| 94 | return |
| 95 | endif |
| 96 | exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%") |
| 97 | exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".") |
| 98 | exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".") |
| 99 | let s:man_tag_depth = s:man_tag_depth + 1 |
| 100 | |
| 101 | let open_cmd = 'edit' |
| 102 | |
| 103 | " Use an existing "man" window if it exists, otherwise open a new one. |
| 104 | if &filetype != "man" |
| 105 | let thiswin = winnr() |
| 106 | exe "norm! \<C-W>b" |
| 107 | if winnr() > 1 |
| 108 | exe "norm! " . thiswin . "\<C-W>w" |
| 109 | while 1 |
| 110 | if &filetype == "man" |
| 111 | break |
| 112 | endif |
| 113 | exe "norm! \<C-W>w" |
| 114 | if thiswin == winnr() |
| 115 | break |
| 116 | endif |
| 117 | endwhile |
| 118 | endif |
| 119 | if &filetype != "man" |
| 120 | if exists("g:ft_man_open_mode") |
| 121 | if g:ft_man_open_mode == 'vert' |
| 122 | let open_cmd = 'vsplit' |
| 123 | elseif g:ft_man_open_mode == 'tab' |
| 124 | let open_cmd = 'tabedit' |
| 125 | else |
| 126 | let open_cmd = 'split' |
| 127 | endif |
| 128 | else |
| 129 | let open_cmd = a:cmdmods . ' split' |
| 130 | endif |
| 131 | endif |
| 132 | endif |
| 133 | |
| 134 | silent execute open_cmd . " $HOME/" . page . '.' . sect . '~' |
| 135 | |
| 136 | " Avoid warning for editing the dummy file twice |
| 137 | setl buftype=nofile noswapfile |
| 138 | |
| 139 | setl fdc=0 ma nofen nonu nornu |
| 140 | %delete _ |
| 141 | let unsetwidth = 0 |
| 142 | if empty($MANWIDTH) |
| 143 | let $MANWIDTH = winwidth(0) |
| 144 | let unsetwidth = 1 |
| 145 | endif |
| 146 | |
| 147 | " Ensure Vim is not recursively invoked (man-db does this) when doing ctrl-[ |
| 148 | " on a man page reference by unsetting MANPAGER. |
| 149 | " Some versions of env(1) do not support the '-u' option, and in such case |
| 150 | " we set MANPAGER=cat. |
| 151 | if !exists('s:env_has_u') |
| 152 | call system('env -u x true') |
| 153 | let s:env_has_u = (v:shell_error == 0) |
| 154 | endif |
| 155 | let env_cmd = s:env_has_u ? 'env -u MANPAGER' : 'env MANPAGER=cat' |
| 156 | let env_cmd .= ' GROFF_NO_SGR=1' |
| 157 | let man_cmd = env_cmd . ' man ' . s:GetCmdArg(sect, page) . ' | col -b' |
| 158 | silent exec "r !" . man_cmd |
| 159 | |
| 160 | if unsetwidth |
| 161 | let $MANWIDTH = '' |
| 162 | endif |
| 163 | " Remove blank lines from top and bottom. |
| 164 | while line('$') > 1 && getline(1) =~ '^\s*$' |
| 165 | 1delete _ |
| 166 | endwhile |
| 167 | while line('$') > 1 && getline('$') =~ '^\s*$' |
| 168 | $delete _ |
| 169 | endwhile |
| 170 | 1 |
| 171 | setl ft=man nomod |
| 172 | setl bufhidden=hide |
| 173 | setl nobuflisted |
| 174 | setl noma |
| 175 | endfunc |
| 176 | |
| 177 | func dist#man#PopPage() |
| 178 | if s:man_tag_depth > 0 |
| 179 | let s:man_tag_depth = s:man_tag_depth - 1 |
| 180 | exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth |
| 181 | exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth |
| 182 | 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^] | 183 | |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 184 | exec s:man_tag_buf."b" |
goweol | 6e5ab2b | 2023-08-18 06:12:42 +0900 | [diff] [blame^] | 185 | call cursor(s:man_tag_lin, s:man_tag_col) |
| 186 | |
Bram Moolenaar | d799daa | 2022-06-20 11:17:32 +0100 | [diff] [blame] | 187 | exec "unlet s:man_tag_buf_".s:man_tag_depth |
| 188 | exec "unlet s:man_tag_lin_".s:man_tag_depth |
| 189 | exec "unlet s:man_tag_col_".s:man_tag_depth |
| 190 | unlet s:man_tag_buf s:man_tag_lin s:man_tag_col |
| 191 | endif |
| 192 | endfunc |
| 193 | |
| 194 | let &cpo = s:cpo_save |
| 195 | unlet s:cpo_save |
| 196 | |
| 197 | " vim: set sw=2 ts=8 noet: |