man.vim: accept [count] for section number.

This commit is contained in:
Justin M. Keyes 2015-09-22 03:10:25 -04:00
parent b22b568266
commit 2169721b94
2 changed files with 17 additions and 17 deletions

View File

@ -11,7 +11,7 @@ catch /E145:/
" Ignore the error in restricted mode " Ignore the error in restricted mode
endtry endtry
function man#get_page(...) function man#get_page(...) abort
let invoked_from_man = (&filetype ==# 'man') let invoked_from_man = (&filetype ==# 'man')
if a:0 == 0 if a:0 == 0
@ -21,7 +21,7 @@ function man#get_page(...)
echoerr 'too many arguments' echoerr 'too many arguments'
return return
elseif a:0 == 2 elseif a:0 == 2
let [sect, page] = [a:1, a:2] let [page, sect] = [a:2, 0 + a:1]
elseif type(1) == type(a:1) elseif type(1) == type(a:1)
let [page, sect] = ['<cword>', a:1] let [page, sect] = ['<cword>', a:1]
else else
@ -32,9 +32,9 @@ function man#get_page(...)
let page = expand('<cword>') let page = expand('<cword>')
endif endif
let [page, sect] = s:parse_page_and_section(page) let [page, sect] = s:parse_page_and_section(sect, page)
if sect !=# '' && s:find_page(sect, page) == 0 if 0 + sect > 0 && s:find_page(sect, page) == 0
let sect = '' let sect = ''
endif endif
@ -66,7 +66,7 @@ function man#get_page(...)
endif endif
if !invoked_from_man if !invoked_from_man
tabnew tabnew
call s:set_window_local_options() let invoked_from_man = 1
endif endif
endif endif
@ -75,7 +75,7 @@ function man#get_page(...)
setlocal modifiable setlocal modifiable
silent keepjumps norm! 1G"_dG silent keepjumps norm! 1G"_dG
let $MANWIDTH = winwidth(0) let $MANWIDTH = winwidth(0)
silent exec 'r!/usr/bin/man '.s:get_cmd_arg(sect, page).' | col -b' silent exec 'r!/usr/bin/man '.s:cmd(sect, page).' | col -b'
" Remove blank lines from top and bottom. " Remove blank lines from top and bottom.
while getline(1) =~ '^\s*$' while getline(1) =~ '^\s*$'
silent keepjumps norm! gg"_dd silent keepjumps norm! gg"_dd
@ -91,12 +91,12 @@ function man#get_page(...)
endif endif
endfunction endfunction
function s:set_window_local_options() function s:set_window_local_options() abort
setlocal colorcolumn=0 foldcolumn=0 nonumber setlocal colorcolumn=0 foldcolumn=0 nonumber
setlocal nolist norelativenumber nofoldenable setlocal nolist norelativenumber nofoldenable
endfunction endfunction
function man#pop_page() function man#pop_page() abort
if s:man_tag_depth > 0 if s:man_tag_depth > 0
let s:man_tag_depth = s:man_tag_depth - 1 let s:man_tag_depth = s:man_tag_depth - 1
exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth exec "let s:man_tag_buf=s:man_tag_buf_".s:man_tag_depth
@ -113,14 +113,14 @@ function man#pop_page()
endfunction endfunction
" Expects a string like 'access' or 'access(2)'. " Expects a string like 'access' or 'access(2)'.
function s:parse_page_and_section(str) function s:parse_page_and_section(sect, str) abort
try try
let save_isk = &iskeyword let save_isk = &iskeyword
setlocal iskeyword-=(,) setlocal iskeyword-=(,)
let page = substitute(a:str, '(*\(\k\+\).*', '\1', '') let page = substitute(a:str, '(*\(\k\+\).*', '\1', '')
let sect = substitute(a:str, '\(\k\+\)(\([^()]*\)).*', '\2', '') let sect = substitute(a:str, '\(\k\+\)(\([^()]*\)).*', '\2', '')
if sect == page || -1 == match(sect, '^[0-9 ]\+$') if sect == page || -1 == match(sect, '^[0-9 ]\+$')
let sect = '' let sect = a:sect
endif endif
catch catch
let &l:iskeyword = save_isk let &l:iskeyword = save_isk
@ -130,15 +130,15 @@ function s:parse_page_and_section(str)
return [page, sect] return [page, sect]
endfunction endfunction
function s:get_cmd_arg(sect, page) function s:cmd(sect, page) abort
if a:sect == '' if 0 + a:sect > 0
return a:page
endif
return s:man_sect_arg.' '.a:sect.' '.a:page return s:man_sect_arg.' '.a:sect.' '.a:page
endif
return a:page
endfunction endfunction
function s:find_page(sect, page) function s:find_page(sect, page) abort
let where = system('/usr/bin/man '.s:man_find_arg.' '.s:get_cmd_arg(a:sect, a:page)) let where = system('/usr/bin/man '.s:man_find_arg.' '.s:cmd(a:sect, a:page))
if where !~ "^/" if where !~ "^/"
if matchstr(where, " [^ ]*$") !~ "^ /" if matchstr(where, " [^ ]*$") !~ "^ /"
return 0 return 0

View File

@ -3,4 +3,4 @@ if get(g:, 'loaded_man', 0)
endif endif
let g:loaded_man = 1 let g:loaded_man = 1
command! -nargs=+ Man call man#get_page(<f-args>) command! -count=0 -nargs=+ Man call man#get_page(<count>, <f-args>)