From f665bde183b5f44d82bd4cb66e0241c242c34766 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Fri, 5 Aug 2016 00:04:48 -0400 Subject: man.vim: default K mapping - Also some small improvements in other parts. --- runtime/autoload/man.vim | 28 +++++++++++++++------------- runtime/ftplugin/man.vim | 4 ++-- runtime/plugin/man.vim | 4 ++-- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/runtime/autoload/man.vim b/runtime/autoload/man.vim index 79931b61b1..1434fc7cf8 100644 --- a/runtime/autoload/man.vim +++ b/runtime/autoload/man.vim @@ -22,7 +22,7 @@ catch /E145:/ " Ignore the error in restricted mode endtry -function! man#open_page_command(...) abort +function! man#open_page_command(count, count1, ...) abort if a:0 > 2 call s:error('too many arguments') return @@ -40,6 +40,10 @@ function! man#open_page_command(...) abort endif try let [sect, name] = s:extract_sect_and_name_ref(ref) + if a:count ==# a:count1 + " user explicitly set a count + let sect = string(a:count) + endif let [sect, name] = s:verify_exists(sect, name) catch call s:error(v:exception) @@ -54,7 +58,7 @@ endfunction " are equal was the count explicitly set. function! man#open_page_mapping(count, count1, ref) abort if empty(a:ref) - call s:error('what manual page do you want?') + call s:error('missing argument') return endif try @@ -164,25 +168,23 @@ function! s:open_page(sect, name) endif if getbufvar(bufname, 'manwidth') ==# manwidth if found_man - silent execute 'buf '.bufnr(bufname) + silent execute 'buf' bufnr(bufname) else - execute 'split '.bufname + execute 'split' bufname + setlocal nobuflisted endif keepjumps 1 return endif - let already_opened = bufexists(bufname) if found_man - execute 'edit '.bufname + execute 'edit' bufname else - execute 'split '.bufname - endif - if already_opened - setlocal modifiable - setlocal noreadonly - keepjumps %delete _ + execute 'split' bufname endif - silent execute 'read!env MANWIDTH='.manwidth.' '.s:man_cmd.' '.s:man_args(a:sect, a:name) + setlocal modifiable + setlocal noreadonly + keepjumps %delete _ + silent execute 'read!env MANWIDTH='.manwidth s:man_cmd s:man_args(a:sect, a:name) let b:manwidth = manwidth " remove all the backspaced text silent keeppatterns keepjumps %substitute,.\b,,ge diff --git a/runtime/ftplugin/man.vim b/runtime/ftplugin/man.vim index a45a380eb2..06b61bd8aa 100644 --- a/runtime/ftplugin/man.vim +++ b/runtime/ftplugin/man.vim @@ -17,9 +17,9 @@ if has('vim_starting') else keepjumps 1 endif - " This is not perfect.See `man glDrawArraysInstanced`. Since the title is + " This is not perfect. See `man glDrawArraysInstanced`. Since the title is " all caps it is impossible to tell what the original capitilization was. - execute 'file '.'man://'.tolower(matchstr(getline(1), '^\S\+')) + execute 'file man://'.tolower(matchstr(getline(1), '^\S\+')) endif setlocal buftype=nofile diff --git a/runtime/plugin/man.vim b/runtime/plugin/man.vim index c47459a551..82ef154725 100644 --- a/runtime/plugin/man.vim +++ b/runtime/plugin/man.vim @@ -5,6 +5,6 @@ if exists('g:loaded_man') endif let g:loaded_man = 1 -command! -complete=customlist,man#complete -nargs=* Man call man#open_page_command() +command! -count=0 -complete=customlist,man#complete -nargs=* Man call man#open_page_command(v:count, v:count1, ) -nnoremap (Man) :call man#open_page_mapping(v:count, v:count1, expand('')) +nnoremap (Man) :call man#open_page_mapping(v:count, v:count1, &filetype ==# 'man' ? expand('') : expand('')) -- cgit From e89eb5d21bf4323bb690bc54d1f035bfd5d6b1e6 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Sat, 6 Aug 2016 05:52:36 -0400 Subject: man.vim: refactoring and autocmd fix - man#open_page_command and man#open_page_mapping are now a single function - New autocmd to fix #5172 --- runtime/autoload/man.vim | 119 ++++++++++++++++++++++------------------------- runtime/plugin/man.vim | 13 +++++- 2 files changed, 67 insertions(+), 65 deletions(-) diff --git a/runtime/autoload/man.vim b/runtime/autoload/man.vim index 1434fc7cf8..332ec63761 100644 --- a/runtime/autoload/man.vim +++ b/runtime/autoload/man.vim @@ -22,7 +22,11 @@ catch /E145:/ " Ignore the error in restricted mode endtry -function! man#open_page_command(count, count1, ...) abort +" We need count and count1 to ensure the section was explicitly set +" by the user. count defaults to 0 which is a valid section and +" count1 defaults to 1 which is also a valid section. Only when they +" are equal was the count explicitly set. +function! man#open_page(count, count1, ...) abort if a:0 > 2 call s:error('too many arguments') return @@ -30,13 +34,13 @@ function! man#open_page_command(count, count1, ...) abort call s:error('missing argument') return elseif a:0 ==# 1 - let ref = a:000[0] + let ref = a:1 else " We combine the name and sect into a manpage reference so that all " verification/extraction can be kept in a single function. - " If a:000[1] is a reference as well, that is fine because it is the only + " If a:2 is a reference as well, that is fine because it is the only " reference that will match. - let ref = a:000[1].'('.a:000[0].')' + let ref = a:2.'('.a:1.')' endif try let [sect, name] = s:extract_sect_and_name_ref(ref) @@ -49,30 +53,49 @@ function! man#open_page_command(count, count1, ...) abort call s:error(v:exception) return endtry - call s:open_page(sect, name) -endfunction - -" We need count and count1 to ensure the section was explicitly set -" by the user. count defaults to 0 which is a valid section and -" count1 defaults to 1 which is also a valid section. Only when they -" are equal was the count explicitly set. -function! man#open_page_mapping(count, count1, ref) abort - if empty(a:ref) - call s:error('missing argument') + call s:push_tag() + let bufname = 'man://'.name.(empty(sect)?'':'('.sect.')') + let found_man = s:find_man() + if getbufvar(bufname, 'manwidth') ==# s:manwidth() + if found_man + silent execute 'buf' bufnr(bufname) + else + execute 'split' bufname + endif + keepjumps 1 return endif + if found_man + noautocmd execute 'edit' bufname + else + noautocmd execute 'split' bufname + endif + call s:read_page(sect, name) +endfunction + +function! man#read_page(ref) abort try let [sect, name] = s:extract_sect_and_name_ref(a:ref) - if a:count ==# a:count1 - " user explicitly set a count - let sect = string(a:count) - endif let [sect, name] = s:verify_exists(sect, name) catch call s:error(v:exception) return endtry - call s:open_page(sect, name) + call s:read_page(sect, name) +endfunction + +function! s:read_page(sect, name) abort + setlocal modifiable + setlocal noreadonly + keepjumps %delete _ + let b:manwidth = s:manwidth() + silent execute 'read!env MANWIDTH='.b:manwidth s:man_cmd s:man_args(a:sect, a:name) + " remove all the backspaced text + silent keeppatterns keepjumps %substitute,.\b,,ge + while getline(1) =~# '^\s*$' + silent keepjumps 1delete _ + endwhile + setlocal filetype=man endfunction " attempt to extract the name and sect out of 'name(sect)' @@ -107,7 +130,6 @@ function! s:verify_exists(sect, name) abort throw 'no manual entry for '.a:name.'('.a:sect.') or '.a:name endif endif - call s:push_tag() if a:name =~# '\/' " We do not need to extract the section/name from the path if the name is " just a path. @@ -151,49 +173,6 @@ function! s:extract_sect_and_name_path(path) abort return [sect, name] endfunction -function! s:open_page(sect, name) - let bufname = 'man://'.a:name.(empty(a:sect)?'':'('.a:sect.')') - let found_man = s:find_man() - " The reason for respecting $MANWIDTH even if it is wider/smaller than the - " current window is that the current window might only be temporarily - " narrow/wide. Since we don't reflow, we should just assume the - " user knows what they're doing and respect $MANWIDTH. - if empty($MANWIDTH) - " If $MANWIDTH is not set, we do not assign directly to $MANWIDTH because - " then $MANWIDTH will always stay the same value as we only use - " winwidth(0) when $MANWIDTH is empty. Instead we set it locally for the command. - let manwidth = winwidth(0) - else - let manwidth = $MANWIDTH - endif - if getbufvar(bufname, 'manwidth') ==# manwidth - if found_man - silent execute 'buf' bufnr(bufname) - else - execute 'split' bufname - setlocal nobuflisted - endif - keepjumps 1 - return - endif - if found_man - execute 'edit' bufname - else - execute 'split' bufname - endif - setlocal modifiable - setlocal noreadonly - keepjumps %delete _ - silent execute 'read!env MANWIDTH='.manwidth s:man_cmd s:man_args(a:sect, a:name) - let b:manwidth = manwidth - " remove all the backspaced text - silent keeppatterns keepjumps %substitute,.\b,,ge - while getline(1) =~# '^\s*$' - silent keepjumps 1delete _ - endwhile - setlocal filetype=man -endfunction - function! s:find_man() abort if &filetype ==# 'man' return 1 @@ -209,6 +188,20 @@ function! s:find_man() abort endwhile endfunction +function! s:manwidth() abort + " The reason for respecting $MANWIDTH even if it is wider/smaller than the + " current window is that the current window might only be temporarily + " narrow/wide. Since we don't reflow, we should just assume the + " user knows what they're doing and respect $MANWIDTH. + if empty($MANWIDTH) + " If $MANWIDTH is not set, we do not assign directly to $MANWIDTH because + " then $MANWIDTH will always stay the same value as we only use + " winwidth(0) when $MANWIDTH is empty. Instead we set it locally for the command. + return winwidth(0) + endif + return $MANWIDTH +endfunction + function! s:man_args(sect, name) abort if empty(a:sect) return shellescape(a:name) diff --git a/runtime/plugin/man.vim b/runtime/plugin/man.vim index 82ef154725..b5b118fbe3 100644 --- a/runtime/plugin/man.vim +++ b/runtime/plugin/man.vim @@ -5,6 +5,15 @@ if exists('g:loaded_man') endif let g:loaded_man = 1 -command! -count=0 -complete=customlist,man#complete -nargs=* Man call man#open_page_command(v:count, v:count1, ) +command! -count=0 -complete=customlist,man#complete -nargs=* Man call man#open_page(v:count, v:count1, ) -nnoremap (Man) :call man#open_page_mapping(v:count, v:count1, &filetype ==# 'man' ? expand('') : expand('')) +nnoremap (Man) :call man#open_page(v:count, v:count1, &filetype ==# 'man' ? expand('') : expand('')) + +augroup man + autocmd! + autocmd BufReadCmd man://* call man#read_page(matchstr(expand(''), 'man://\zs.*')) + " Need this because without it, if you do ':Man printf(3)' and then later, + " open a session that contains a buffer named 'man://printf(3)', the buffer + " will become listed. + autocmd BufEnter man://* set nobuflisted +augroup END -- cgit From e8a3477dc771f6e8f9fa0c8de6d2bdef76fabdc8 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Sat, 6 Aug 2016 16:04:34 -0400 Subject: man.vim: buffers are now listed - Since the names are set and ':vsplit printf(3)' work, there is no need to unlist them. --- runtime/ftplugin/man.vim | 1 - runtime/plugin/man.vim | 4 ---- 2 files changed, 5 deletions(-) diff --git a/runtime/ftplugin/man.vim b/runtime/ftplugin/man.vim index 06b61bd8aa..08d5da0fe4 100644 --- a/runtime/ftplugin/man.vim +++ b/runtime/ftplugin/man.vim @@ -25,7 +25,6 @@ endif setlocal buftype=nofile setlocal noswapfile setlocal bufhidden=hide -setlocal nobuflisted setlocal nomodified setlocal readonly setlocal nomodifiable diff --git a/runtime/plugin/man.vim b/runtime/plugin/man.vim index b5b118fbe3..12762f1854 100644 --- a/runtime/plugin/man.vim +++ b/runtime/plugin/man.vim @@ -12,8 +12,4 @@ nnoremap (Man) :call man#open_page(v:count, v:count1, &filet augroup man autocmd! autocmd BufReadCmd man://* call man#read_page(matchstr(expand(''), 'man://\zs.*')) - " Need this because without it, if you do ':Man printf(3)' and then later, - " open a session that contains a buffer named 'man://printf(3)', the buffer - " will become listed. - autocmd BufEnter man://* set nobuflisted augroup END -- cgit