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