diff --git a/.vim/plugin/man.vim b/.vim/plugin/man.vim new file mode 100644 index 0000000..23e2ee4 --- /dev/null +++ b/.vim/plugin/man.vim @@ -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 :call ManSearchSelect(getline('.')) + 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 K :call ManWindow(expand("")) +command! -nargs=+ Man :call ManWindow("") +command! -nargs=+ Apropos :call ManWindow("", "apropos") +command! -nargs=+ Whatis :call ManWindow("", "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")