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 | 8feef4f | 2015-01-07 16:57:10 +0100 | [diff] [blame] | 4 | " Last Change: 2014 Dec 29 |
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> |
| 36 | endif |
| 37 | |
Bram Moolenaar | 84f7235 | 2012-03-11 15:57:40 +0100 | [diff] [blame] | 38 | let b:undo_ftplugin = "setlocal iskeyword<" |
| 39 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 40 | endif |
| 41 | |
| 42 | if exists(":Man") != 2 |
| 43 | com -nargs=+ Man call s:GetPage(<f-args>) |
| 44 | nmap <Leader>K :call <SID>PreGetPage(0)<CR> |
| 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) |
| 94 | let where = system("/usr/bin/man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page)) |
| 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" |
| 148 | new |
| 149 | setl nonu fdc=0 |
| 150 | endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 151 | endif |
| 152 | silent exec "edit $HOME/".page.".".sect."~" |
| 153 | " Avoid warning for editing the dummy file twice |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 154 | setl buftype=nofile noswapfile |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 155 | |
Bram Moolenaar | 9ba7e17 | 2013-07-17 22:37:26 +0200 | [diff] [blame] | 156 | setl ma nonu nornu nofen |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 157 | silent exec "norm 1GdG" |
| 158 | let $MANWIDTH = winwidth(0) |
| 159 | silent exec "r!/usr/bin/man ".s:GetCmdArg(sect, page)." | col -b" |
| 160 | " Remove blank lines from top and bottom. |
| 161 | while getline(1) =~ '^\s*$' |
Bram Moolenaar | 8feef4f | 2015-01-07 16:57:10 +0100 | [diff] [blame] | 162 | silent keepj norm ggdd |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 163 | endwhile |
| 164 | while getline('$') =~ '^\s*$' |
Bram Moolenaar | 8feef4f | 2015-01-07 16:57:10 +0100 | [diff] [blame] | 165 | silent keepj norm Gdd |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 166 | endwhile |
| 167 | 1 |
| 168 | setl ft=man nomod |
| 169 | setl bufhidden=hide |
| 170 | setl nobuflisted |
| 171 | endfunc |
| 172 | |
| 173 | func <SID>PopPage() |
| 174 | if s:man_tag_depth > 0 |
| 175 | let s:man_tag_depth = s:man_tag_depth - 1 |
| 176 | exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth |
| 177 | exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth |
| 178 | exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth |
| 179 | exec s:man_tag_buf."b" |
| 180 | exec s:man_tag_lin |
| 181 | exec "norm ".s:man_tag_col."|" |
| 182 | exec "unlet s:man_tag_buf_".s:man_tag_depth |
| 183 | exec "unlet s:man_tag_lin_".s:man_tag_depth |
| 184 | exec "unlet s:man_tag_col_".s:man_tag_depth |
| 185 | unlet s:man_tag_buf s:man_tag_lin s:man_tag_col |
| 186 | endif |
| 187 | endfunc |
| 188 | |
| 189 | endif |
| 190 | |
| 191 | " vim: set sw=2: |