Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim filetype plugin file |
| 2 | " Language: man |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 3 | " Maintainer: SungHyun Nam <goweol@gmail.com> |
Bram Moolenaar | 85eee13 | 2018-05-06 17:57:30 +0200 | [diff] [blame] | 4 | " Last Change: 2018 May 2 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5 | |
| 6 | " To make the ":Man" command available before editing a manual page, source |
| 7 | " this script from your startup vimrc file. |
| 8 | |
| 9 | " If 'filetype' isn't "man", we must have been called to only define ":Man". |
| 10 | if &filetype == "man" |
| 11 | |
| 12 | " Only do this when not done yet for this buffer |
| 13 | if exists("b:did_ftplugin") |
| 14 | finish |
| 15 | endif |
| 16 | let b:did_ftplugin = 1 |
| 17 | |
| 18 | " allow dot and dash in manual page name. |
| 19 | setlocal iskeyword+=\.,- |
| 20 | |
| 21 | " Add mappings, unless the user didn't want this. |
| 22 | if !exists("no_plugin_maps") && !exists("no_man_maps") |
| 23 | if !hasmapto('<Plug>ManBS') |
| 24 | nmap <buffer> <LocalLeader>h <Plug>ManBS |
| 25 | endif |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 26 | nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:setl nomod<CR>'' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 27 | |
| 28 | nnoremap <buffer> <c-]> :call <SID>PreGetPage(v:count)<CR> |
| 29 | nnoremap <buffer> <c-t> :call <SID>PopPage()<CR> |
Bram Moolenaar | d042dc8 | 2015-11-24 19:18:36 +0100 | [diff] [blame] | 30 | nnoremap <buffer> <silent> q :q<CR> |
| 31 | endif |
| 32 | |
| 33 | if exists('g:ft_man_folding_enable') && (g:ft_man_folding_enable == 1) |
| 34 | setlocal foldmethod=indent foldnestmax=1 foldenable |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 35 | endif |
| 36 | |
Bram Moolenaar | 84f7235 | 2012-03-11 15:57:40 +0100 | [diff] [blame] | 37 | let b:undo_ftplugin = "setlocal iskeyword<" |
| 38 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 39 | endif |
| 40 | |
| 41 | if exists(":Man") != 2 |
Bram Moolenaar | 40962ec | 2018-01-28 22:47:25 +0100 | [diff] [blame] | 42 | com -nargs=+ -complete=shellcmd Man call s:GetPage(<f-args>) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 43 | nmap <Leader>K :call <SID>PreGetPage(0)<CR> |
Bram Moolenaar | 6856393 | 2017-01-10 13:31:15 +0100 | [diff] [blame] | 44 | nmap <Plug>ManPreGetPage :call <SID>PreGetPage(0)<CR> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 45 | endif |
| 46 | |
| 47 | " Define functions only once. |
| 48 | if !exists("s:man_tag_depth") |
| 49 | |
| 50 | let s:man_tag_depth = 0 |
| 51 | |
Bram Moolenaar | 864207d | 2008-06-24 22:14:38 +0000 | [diff] [blame] | 52 | let s:man_sect_arg = "" |
| 53 | let s:man_find_arg = "-w" |
| 54 | try |
| 55 | if !has("win32") && $OSTYPE !~ 'cygwin\|linux' && system('uname -s') =~ "SunOS" && system('uname -r') =~ "^5" |
| 56 | let s:man_sect_arg = "-s" |
| 57 | let s:man_find_arg = "-l" |
| 58 | endif |
| 59 | catch /E145:/ |
| 60 | " Ignore the error in restricted mode |
| 61 | endtry |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 62 | |
| 63 | func <SID>PreGetPage(cnt) |
| 64 | if a:cnt == 0 |
| 65 | let old_isk = &iskeyword |
Bram Moolenaar | c229967 | 2014-11-13 14:25:38 +0100 | [diff] [blame] | 66 | if &ft == 'man' |
| 67 | setl iskeyword+=(,) |
| 68 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 69 | let str = expand("<cword>") |
| 70 | let &l:iskeyword = old_isk |
| 71 | let page = substitute(str, '(*\(\k\+\).*', '\1', '') |
| 72 | let sect = substitute(str, '\(\k\+\)(\([^()]*\)).*', '\2', '') |
| 73 | if match(sect, '^[0-9 ]\+$') == -1 |
| 74 | let sect = "" |
| 75 | endif |
| 76 | if sect == page |
| 77 | let sect = "" |
| 78 | endif |
| 79 | else |
| 80 | let sect = a:cnt |
| 81 | let page = expand("<cword>") |
| 82 | endif |
| 83 | call s:GetPage(sect, page) |
| 84 | endfunc |
| 85 | |
| 86 | func <SID>GetCmdArg(sect, page) |
| 87 | if a:sect == '' |
| 88 | return a:page |
| 89 | endif |
| 90 | return s:man_sect_arg.' '.a:sect.' '.a:page |
| 91 | endfunc |
| 92 | |
| 93 | func <SID>FindPage(sect, page) |
Bram Moolenaar | 690afe1 | 2017-01-28 18:34:47 +0100 | [diff] [blame] | 94 | let where = system("man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 95 | if where !~ "^/" |
| 96 | if matchstr(where, " [^ ]*$") !~ "^ /" |
| 97 | return 0 |
| 98 | endif |
| 99 | endif |
| 100 | return 1 |
| 101 | endfunc |
| 102 | |
| 103 | func <SID>GetPage(...) |
| 104 | if a:0 >= 2 |
| 105 | let sect = a:1 |
| 106 | let page = a:2 |
| 107 | elseif a:0 >= 1 |
| 108 | let sect = "" |
| 109 | let page = a:1 |
| 110 | else |
| 111 | return |
| 112 | endif |
| 113 | |
| 114 | " To support: nmap K :Man <cword> |
| 115 | if page == '<cword>' |
| 116 | let page = expand('<cword>') |
| 117 | endif |
| 118 | |
| 119 | if sect != "" && s:FindPage(sect, page) == 0 |
| 120 | let sect = "" |
| 121 | endif |
| 122 | if s:FindPage(sect, page) == 0 |
| 123 | echo "\nCannot find a '".page."'." |
| 124 | return |
| 125 | endif |
| 126 | exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%") |
| 127 | exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".") |
| 128 | exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".") |
| 129 | let s:man_tag_depth = s:man_tag_depth + 1 |
| 130 | |
| 131 | " Use an existing "man" window if it exists, otherwise open a new one. |
| 132 | if &filetype != "man" |
| 133 | let thiswin = winnr() |
| 134 | exe "norm! \<C-W>b" |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 135 | if winnr() > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 136 | exe "norm! " . thiswin . "\<C-W>w" |
| 137 | while 1 |
| 138 | if &filetype == "man" |
| 139 | break |
| 140 | endif |
| 141 | exe "norm! \<C-W>w" |
| 142 | if thiswin == winnr() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 143 | break |
| 144 | endif |
| 145 | endwhile |
| 146 | endif |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 147 | if &filetype != "man" |
Bram Moolenaar | ddf8d1c | 2016-06-20 11:22:54 +0200 | [diff] [blame] | 148 | if exists("g:ft_man_open_mode") |
| 149 | if g:ft_man_open_mode == "vert" |
| 150 | vnew |
| 151 | elseif g:ft_man_open_mode == "tab" |
| 152 | tabnew |
| 153 | else |
| 154 | new |
| 155 | endif |
| 156 | else |
| 157 | new |
| 158 | endif |
Bram Moolenaar | c81e5e7 | 2007-05-05 18:24:42 +0000 | [diff] [blame] | 159 | setl nonu fdc=0 |
| 160 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 161 | endif |
| 162 | silent exec "edit $HOME/".page.".".sect."~" |
| 163 | " Avoid warning for editing the dummy file twice |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 164 | setl buftype=nofile noswapfile |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 165 | |
Bram Moolenaar | 9ba7e17 | 2013-07-17 22:37:26 +0200 | [diff] [blame] | 166 | setl ma nonu nornu nofen |
Bram Moolenaar | 85eee13 | 2018-05-06 17:57:30 +0200 | [diff] [blame] | 167 | silent exec "norm! 1GdG" |
Bram Moolenaar | ddf8d1c | 2016-06-20 11:22:54 +0200 | [diff] [blame] | 168 | let unsetwidth = 0 |
Bram Moolenaar | cbebd48 | 2016-02-07 23:02:56 +0100 | [diff] [blame] | 169 | if empty($MANWIDTH) |
| 170 | let $MANWIDTH = winwidth(0) |
Bram Moolenaar | ddf8d1c | 2016-06-20 11:22:54 +0200 | [diff] [blame] | 171 | let unsetwidth = 1 |
Bram Moolenaar | cbebd48 | 2016-02-07 23:02:56 +0100 | [diff] [blame] | 172 | endif |
Bram Moolenaar | 7f2e9d7 | 2017-11-11 20:58:53 +0100 | [diff] [blame] | 173 | |
| 174 | " Ensure Vim is not recursively invoked (man-db does this) when doing ctrl-[ |
| 175 | " on a man page reference by unsetting MANPAGER. |
Bram Moolenaar | 40962ec | 2018-01-28 22:47:25 +0100 | [diff] [blame] | 176 | " Some versions of env(1) do not support the '-u' option, and in such case |
| 177 | " we set MANPAGER=cat. |
| 178 | if !exists('s:env_has_u') |
| 179 | call system('env -u x true') |
| 180 | let s:env_has_u = (v:shell_error == 0) |
| 181 | endif |
| 182 | let env_cmd = s:env_has_u ? 'env -u MANPAGER' : 'env MANPAGER=cat' |
| 183 | let man_cmd = env_cmd . ' man ' . s:GetCmdArg(sect, page) . ' | col -b' |
| 184 | silent exec "r !" . man_cmd |
Bram Moolenaar | 7f2e9d7 | 2017-11-11 20:58:53 +0100 | [diff] [blame] | 185 | |
Bram Moolenaar | ddf8d1c | 2016-06-20 11:22:54 +0200 | [diff] [blame] | 186 | if unsetwidth |
| 187 | let $MANWIDTH = '' |
| 188 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 189 | " Remove blank lines from top and bottom. |
| 190 | while getline(1) =~ '^\s*$' |
Bram Moolenaar | 85eee13 | 2018-05-06 17:57:30 +0200 | [diff] [blame] | 191 | silent keepj norm! ggdd |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 192 | endwhile |
| 193 | while getline('$') =~ '^\s*$' |
Bram Moolenaar | 85eee13 | 2018-05-06 17:57:30 +0200 | [diff] [blame] | 194 | silent keepj norm! Gdd |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 195 | endwhile |
| 196 | 1 |
| 197 | setl ft=man nomod |
| 198 | setl bufhidden=hide |
| 199 | setl nobuflisted |
Bram Moolenaar | ddf8d1c | 2016-06-20 11:22:54 +0200 | [diff] [blame] | 200 | setl noma |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 201 | endfunc |
| 202 | |
| 203 | func <SID>PopPage() |
| 204 | if s:man_tag_depth > 0 |
| 205 | let s:man_tag_depth = s:man_tag_depth - 1 |
| 206 | exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth |
| 207 | exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth |
| 208 | exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth |
| 209 | exec s:man_tag_buf."b" |
| 210 | exec s:man_tag_lin |
Bram Moolenaar | 85eee13 | 2018-05-06 17:57:30 +0200 | [diff] [blame] | 211 | exec "norm! ".s:man_tag_col."|" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 212 | exec "unlet s:man_tag_buf_".s:man_tag_depth |
| 213 | exec "unlet s:man_tag_lin_".s:man_tag_depth |
| 214 | exec "unlet s:man_tag_col_".s:man_tag_depth |
| 215 | unlet s:man_tag_buf s:man_tag_lin s:man_tag_col |
| 216 | endif |
| 217 | endfunc |
| 218 | |
| 219 | endif |
| 220 | |
Bram Moolenaar | ddf8d1c | 2016-06-20 11:22:54 +0200 | [diff] [blame] | 221 | " vim: set sw=2 ts=8 noet: |