[vim] Add man plugin

This commit is contained in:
Mathieu Maret 2011-05-26 10:10:51 +02:00
parent 0764a40230
commit 27f8bca29a
1 changed files with 106 additions and 0 deletions

106
.vim/plugin/man.vim Normal file
View File

@ -0,0 +1,106 @@
"===========================================================================
" File: man.vim
" Author: Gregory Thiemonge
" License: public domain
"===========================================================================
function! ManSearchSelect(line)
let l:elements = split(a:line, " ")
if len(l:elements) > 2
let l:page = l:elements[0]
let l:section = substitute(l:elements[1], "[()]", "", "g")
call ManWindow(l:page.":".l:section)
endif
endfunction
function! Man(arg, ...)
let l:env = "COLUMNS=".winwidth(0)
if len(a:000) == 0
let l:type = "man"
else
let l:type = a:1
endif
if l:type == "man"
let l:command = "/usr/bin/man"
elseif l:type == "whatis"
let l:command = "/usr/bin/whatis"
elseif l:type == "apropos"
let l:command = "/usr/bin/apropos"
endif
if a:arg =~ "^[1-9][1-9]* .*$"
let l:pos = matchend(a:arg, "^[1-9][1-9]* ")
let l:section = strpart(a:arg, 0, l:pos-1)
let l:page = strpart(a:arg, l:pos)
elseif a:arg =~ "^.*:[1-9][1-9]*.*$"
let l:pos = match(a:arg, ":[1-9][1-9]*.*$")
let l:section = strpart(a:arg, l:pos+1)
let l:page = strpart(a:arg, 0, l:pos)
else
let l:section = ""
let l:page = a:arg
endif
if l:type == "man"
let l:args = [ "-P cat", "-S 3:2:1:4:5:6:7:8", l:section, l:page ]
let l:cmd = [ l:env, l:command ] + l:args
else
if l:section != ""
let l:args = [ "-s", l:section, l:page ]
else
let l:args = [ l:page ]
endif
let l:cmd = [ l:env, l:command ] + l:args
endif
let l:contents = split(system(join(l:cmd, " ") . " 2>/dev/null"), "\n")
if v:shell_error != 0
throw "Not found"
endif
let l:line = 1
for item in l:contents
call setline(l:line, item)
let l:line += 1
endfor
setlocal buftype=nofile
setlocal bufhidden=delete
setlocal noswapfile
setlocal ft=man
setlocal noma nomod ro
set nonumber
if l:type != "man"
nnoremap <buffer> <silent> <Enter> :call ManSearchSelect(getline('.'))<cr>
syntax match manTitle /^\S\S*\s(/he=e-2
endif
endfunction
function! ManWindow(arg, ...)
if len(a:000) == 0
let l:type = "man"
else
let l:type = a:1
endif
try
new
call Man(a:arg, l:type)
catch /Not found/
bd
echohl ErrorMsg
echo "Man page not found"
echohl None
endtry
wincmd p
endfunction
map <silent> K :call ManWindow(expand("<cword>"))<CR>
command! -nargs=+ Man :call ManWindow("<args>")
command! -nargs=+ Apropos :call ManWindow("<args>", "apropos")
command! -nargs=+ Whatis :call ManWindow("<args>", "whatis")
au BufReadCmd man://* call Man(strpart(expand("%"), len("man://")))
au BufReadCmd whatis://* call Man(strpart(expand("%"), len("whatis://")), "whatis")
au BufReadCmd apropos://* call Man(strpart(expand("%"), len("apropos://")), "apropos")