Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim filetype plugin file |
| 2 | " Language: man |
| 3 | " Maintainer: Nam SungHyun <namsh@kldp.org> |
| 4 | " Last Change: 2003 Dec 24 |
| 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 |
| 26 | nnoremap <buffer> <Plug>ManBS :%s/.\b//g<CR>:set nomod<CR>'' |
| 27 | |
| 28 | nnoremap <buffer> <c-]> :call <SID>PreGetPage(v:count)<CR> |
| 29 | nnoremap <buffer> <c-t> :call <SID>PopPage()<CR> |
| 30 | endif |
| 31 | |
| 32 | endif |
| 33 | |
| 34 | if exists(":Man") != 2 |
| 35 | com -nargs=+ Man call s:GetPage(<f-args>) |
| 36 | nmap <Leader>K :call <SID>PreGetPage(0)<CR> |
| 37 | endif |
| 38 | |
| 39 | " Define functions only once. |
| 40 | if !exists("s:man_tag_depth") |
| 41 | |
| 42 | let s:man_tag_depth = 0 |
| 43 | |
| 44 | if !has("win32") && $OSTYPE !~ 'cygwin\|linux' && system('uname -s') =~ "SunOS" && system('uname -r') =~ "^5" |
| 45 | let s:man_sect_arg = "-s" |
| 46 | let s:man_find_arg = "-l" |
| 47 | else |
| 48 | let s:man_sect_arg = "" |
| 49 | let s:man_find_arg = "-w" |
| 50 | endif |
| 51 | |
| 52 | func <SID>PreGetPage(cnt) |
| 53 | if a:cnt == 0 |
| 54 | let old_isk = &iskeyword |
| 55 | setl iskeyword+=(,) |
| 56 | let str = expand("<cword>") |
| 57 | let &l:iskeyword = old_isk |
| 58 | let page = substitute(str, '(*\(\k\+\).*', '\1', '') |
| 59 | let sect = substitute(str, '\(\k\+\)(\([^()]*\)).*', '\2', '') |
| 60 | if match(sect, '^[0-9 ]\+$') == -1 |
| 61 | let sect = "" |
| 62 | endif |
| 63 | if sect == page |
| 64 | let sect = "" |
| 65 | endif |
| 66 | else |
| 67 | let sect = a:cnt |
| 68 | let page = expand("<cword>") |
| 69 | endif |
| 70 | call s:GetPage(sect, page) |
| 71 | endfunc |
| 72 | |
| 73 | func <SID>GetCmdArg(sect, page) |
| 74 | if a:sect == '' |
| 75 | return a:page |
| 76 | endif |
| 77 | return s:man_sect_arg.' '.a:sect.' '.a:page |
| 78 | endfunc |
| 79 | |
| 80 | func <SID>FindPage(sect, page) |
| 81 | let where = system("/usr/bin/man ".s:man_find_arg.' '.s:GetCmdArg(a:sect, a:page)) |
| 82 | if where !~ "^/" |
| 83 | if matchstr(where, " [^ ]*$") !~ "^ /" |
| 84 | return 0 |
| 85 | endif |
| 86 | endif |
| 87 | return 1 |
| 88 | endfunc |
| 89 | |
| 90 | func <SID>GetPage(...) |
| 91 | if a:0 >= 2 |
| 92 | let sect = a:1 |
| 93 | let page = a:2 |
| 94 | elseif a:0 >= 1 |
| 95 | let sect = "" |
| 96 | let page = a:1 |
| 97 | else |
| 98 | return |
| 99 | endif |
| 100 | |
| 101 | " To support: nmap K :Man <cword> |
| 102 | if page == '<cword>' |
| 103 | let page = expand('<cword>') |
| 104 | endif |
| 105 | |
| 106 | if sect != "" && s:FindPage(sect, page) == 0 |
| 107 | let sect = "" |
| 108 | endif |
| 109 | if s:FindPage(sect, page) == 0 |
| 110 | echo "\nCannot find a '".page."'." |
| 111 | return |
| 112 | endif |
| 113 | exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%") |
| 114 | exec "let s:man_tag_lin_".s:man_tag_depth." = ".line(".") |
| 115 | exec "let s:man_tag_col_".s:man_tag_depth." = ".col(".") |
| 116 | let s:man_tag_depth = s:man_tag_depth + 1 |
| 117 | |
| 118 | " Use an existing "man" window if it exists, otherwise open a new one. |
| 119 | if &filetype != "man" |
| 120 | let thiswin = winnr() |
| 121 | exe "norm! \<C-W>b" |
| 122 | if winnr() == 1 |
| 123 | new |
| 124 | else |
| 125 | exe "norm! " . thiswin . "\<C-W>w" |
| 126 | while 1 |
| 127 | if &filetype == "man" |
| 128 | break |
| 129 | endif |
| 130 | exe "norm! \<C-W>w" |
| 131 | if thiswin == winnr() |
| 132 | new |
| 133 | break |
| 134 | endif |
| 135 | endwhile |
| 136 | endif |
| 137 | endif |
| 138 | silent exec "edit $HOME/".page.".".sect."~" |
| 139 | " Avoid warning for editing the dummy file twice |
| 140 | set buftype=nofile noswapfile |
| 141 | |
| 142 | set ma |
| 143 | silent exec "norm 1GdG" |
| 144 | let $MANWIDTH = winwidth(0) |
| 145 | silent exec "r!/usr/bin/man ".s:GetCmdArg(sect, page)." | col -b" |
| 146 | " Remove blank lines from top and bottom. |
| 147 | while getline(1) =~ '^\s*$' |
| 148 | silent norm ggdd |
| 149 | endwhile |
| 150 | while getline('$') =~ '^\s*$' |
| 151 | silent norm Gdd |
| 152 | endwhile |
| 153 | 1 |
| 154 | setl ft=man nomod |
| 155 | setl bufhidden=hide |
| 156 | setl nobuflisted |
| 157 | endfunc |
| 158 | |
| 159 | func <SID>PopPage() |
| 160 | if s:man_tag_depth > 0 |
| 161 | let s:man_tag_depth = s:man_tag_depth - 1 |
| 162 | exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth |
| 163 | exec "let s:man_tag_lin=s:man_tag_lin_".s:man_tag_depth |
| 164 | exec "let s:man_tag_col=s:man_tag_col_".s:man_tag_depth |
| 165 | exec s:man_tag_buf."b" |
| 166 | exec s:man_tag_lin |
| 167 | exec "norm ".s:man_tag_col."|" |
| 168 | exec "unlet s:man_tag_buf_".s:man_tag_depth |
| 169 | exec "unlet s:man_tag_lin_".s:man_tag_depth |
| 170 | exec "unlet s:man_tag_col_".s:man_tag_depth |
| 171 | unlet s:man_tag_buf s:man_tag_lin s:man_tag_col |
| 172 | endif |
| 173 | endfunc |
| 174 | |
| 175 | endif |
| 176 | |
| 177 | " vim: set sw=2: |