From 7d484eaf520efc7677e71277d4057d507d27975f Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Tue, 13 Aug 2019 23:53:16 -0400 Subject: [PATCH] Use s:Map() for nearly all maps --- autoload/fugitive.vim | 191 +++++++++++++++++++++--------------------- 1 file changed, 97 insertions(+), 94 deletions(-) diff --git a/autoload/fugitive.vim b/autoload/fugitive.vim index 0be692a..a859de9 100644 --- a/autoload/fugitive.vim +++ b/autoload/fugitive.vim @@ -173,10 +173,6 @@ function! s:Map(mode, lhs, rhs, ...) abort endif endfunction -function! s:map(...) abort - return call('s:Map', a:000) -endfunction - " Section: Quickfix function! s:QuickfixGet(nr, ...) abort @@ -1811,56 +1807,54 @@ function! fugitive#BufReadStatus() abort let b:dispatch = ':Gfetch --all' call fugitive#MapJumps() let nowait = v:version >= 704 ? '' : '' - nunmap P - nunmap ~ - exe "nnoremap " nowait "- :execute Do('Toggle',0)" - exe "xnoremap " nowait "- :execute Do('Toggle',1)" - exe "nnoremap " nowait "s :execute Do('Stage',0)" - exe "xnoremap " nowait "s :execute Do('Stage',1)" - exe "nnoremap " nowait "u :execute Do('Unstage',0)" - exe "xnoremap " nowait "u :execute Do('Unstage',1)" - nnoremap U :exe EchoExec('reset', '-q') + call s:Map('n', '-', ":execute Do('Toggle',0)", '') + call s:Map('x', '-', ":execute Do('Toggle',1)", '') + call s:Map('n', 's', ":execute Do('Stage',0)", '') + call s:Map('x', 's', ":execute Do('Stage',1)", '') + call s:Map('n', 'u', ":execute Do('Unstage',0)", '') + call s:Map('x', 'u', ":execute Do('Unstage',1)", '') + call s:Map('n', 'n', 'U', ":exe EchoExec('reset', '-q')", '') call s:MapEx('gu', "exe StageJump(v:count, 'Untracked', 'Unstaged')") call s:MapEx('gU', "exe StageJump(v:count, 'Unstaged', 'Untracked')") call s:MapEx('gs', "exe StageJump(v:count, 'Staged')") call s:MapEx('gp', "exe StageJump(v:count, 'Unpushed')") call s:MapEx('gP', "exe StageJump(v:count, 'Unpulled')") call s:MapEx('gr', "exe StageJump(v:count, 'Rebasing')") - nnoremap C :Gcommit:echohl WarningMsgecho ':Gstatus C is deprecated in favor of cc'echohl NONE - nnoremap a :execute Do('Toggle',0) - nnoremap i :execute NextExpandedHunk(v:count1) - exe 'nnoremap ' nowait "= :execute StageInline('toggle',line('.'),v:count)" - exe 'nnoremap ' nowait "< :execute StageInline('hide', line('.'),v:count)" - exe 'nnoremap ' nowait "> :execute StageInline('show', line('.'),v:count)" - exe 'xnoremap ' nowait "= :execute StageInline('toggle',line(\"'<\"),line(\"'>\")-line(\"'<\")+1)" - exe 'xnoremap ' nowait "< :execute StageInline('hide', line(\"'<\"),line(\"'>\")-line(\"'<\")+1)" - exe 'xnoremap ' nowait "> :execute StageInline('show', line(\"'<\"),line(\"'>\")-line(\"'<\")+1)" - nnoremap D :execute StageDiff('Gdiffsplit!')redrawechohl WarningMsg echo ':Gstatus D is deprecated in favor of dd'echohl NONE - nnoremap dd :execute StageDiff('Gdiffsplit!') - nnoremap dh :execute StageDiff('Ghdiffsplit!') - nnoremap ds :execute StageDiff('Ghdiffsplit!') - nnoremap dp :execute StageDiffEdit() - nnoremap dv :execute StageDiff('Gvdiffsplit!') - nnoremap d? :help fugitive_d - nnoremap P :execute StagePatch(line('.'),line('.')+v:count1-1) - xnoremap P :execute StagePatch(line("'<"),line("'>")) + call s:Map('n', 'C', ":Gcommit:echohl WarningMsgecho ':Gstatus C is deprecated in favor of cc'echohl NONE", '') + call s:Map('n', 'a', ":execute Do('Toggle',0)", '') + call s:Map('n', 'i', ":execute NextExpandedHunk(v:count1)", '') + call s:Map('n', "=", ":execute StageInline('toggle',line('.'),v:count)", '') + call s:Map('n', "<", ":execute StageInline('hide', line('.'),v:count)", '') + call s:Map('n', ">", ":execute StageInline('show', line('.'),v:count)", '') + call s:Map('x', "=", ":execute StageInline('toggle',line(\"'<\"),line(\"'>\")-line(\"'<\")+1)", '') + call s:Map('x', "<", ":execute StageInline('hide', line(\"'<\"),line(\"'>\")-line(\"'<\")+1)", '') + call s:Map('x', ">", ":execute StageInline('show', line(\"'<\"),line(\"'>\")-line(\"'<\")+1)", '') + call s:Map('n', 'D', ":execute StageDiff('Gdiffsplit!')redrawechohl WarningMsg echo ':Gstatus D is deprecated in favor of dd'echohl NONE", '') + call s:Map('n', 'dd', ":execute StageDiff('Gdiffsplit!')", '') + call s:Map('n', 'dh', ":execute StageDiff('Ghdiffsplit!')", '') + call s:Map('n', 'ds', ":execute StageDiff('Ghdiffsplit!')", '') + call s:Map('n', 'dp', ":execute StageDiffEdit()", '') + call s:Map('n', 'dv', ":execute StageDiff('Gvdiffsplit!')", '') + call s:Map('n', 'd?', ":help fugitive_d", '') + call s:Map('n', 'P', ":execute StagePatch(line('.'),line('.')+v:count1-1)", '') + call s:Map('x', 'P', ":execute StagePatch(line(\"'<\"),line(\"'>\"))", '') call s:Map('n', 'p', ":if v:countsilent exe GF('pedit')elseechoerr 'Use = for inline diff, P for :Git add/reset --patch, 1p for :pedit'endif", '') call s:Map('x', 'p', ":execute StagePatch(line(\"'<\"),line(\"'>\"))", '') - nnoremap I :execute StagePatch(line('.'),line('.')) - xnoremap I :execute StagePatch(line("'<"),line("'>")) + call s:Map('n', 'I', ":execute StagePatch(line('.'),line('.'))", '') + call s:Map('x', 'I', ":execute StagePatch(line(\"'<\"),line(\"'>\"))", '') if empty(mapcheck('q', 'n')) nnoremap q :if bufnr('$') == 1quitelsebdeleteendif endif - exe 'nnoremap ' s:nowait "gq :if bufnr('$') == 1quitelsebdeleteendif" - nnoremap R :echohl WarningMsgecho 'Reloading is automatic. Use :e to force'echohl NONE - nnoremap g :echoerr 'Changed to X' - xnoremap g :echoerr 'Changed to X' - nnoremap X :execute StageDelete(line('.'), 0, v:count) - xnoremap X :execute StageDelete(line("'<"), line("'>"), v:count) - nnoremap gI :execute StageIgnore(line('.'), line('.'), v:count) - xnoremap gI :execute StageIgnore(line("'<"), line("'>"), v:count) - nnoremap . : =StageArgs(0) - xnoremap . : =StageArgs(1) + call s:Map('n', 'gq', ":if bufnr('$') == 1quitelsebdeleteendif", '') + call s:Map('n', 'R', ":echohl WarningMsgecho 'Reloading is automatic. Use :e to force'echohl NONE", '') + call s:Map('n', 'g', ":echoerr 'Changed to X'", '') + call s:Map('x', 'g', ":echoerr 'Changed to X'", '') + call s:Map('n', 'X', ":execute StageDelete(line('.'), 0, v:count)", '') + call s:Map('x', 'X', ":execute StageDelete(line(\"'<\"), line(\"'>\"), v:count)", '') + call s:Map('n', 'gI', ":execute StageIgnore(line('.'), line('.'), v:count)", '') + call s:Map('x', 'gI', ":execute StageIgnore(line(\"'<\"), line(\"'>\"), v:count)", '') + call s:Map('n', '.', ': =StageArgs(0)') + call s:Map('x', '.', ': =StageArgs(1)') setlocal filetype=fugitive for [lnum, section] in [[staged_end, 'Staged'], [unstaged_end, 'Unstaged']] @@ -2030,8 +2024,8 @@ function! fugitive#BufReadCmd(...) abort let &l:modifiable = modifiable if b:fugitive_type !=# 'blob' setlocal filetype=git foldmethod=syntax - nnoremap a :let b:fugitive_display_format += v:count1exe fugitive#BufReadCmd(@%) - nnoremap i :let b:fugitive_display_format -= v:count1exe fugitive#BufReadCmd(@%) + call s:Map('n', 'a', ":let b:fugitive_display_format += v:count1exe fugitive#BufReadCmd(@%)", '') + call s:Map('n', 'i', ":let b:fugitive_display_format -= v:count1exe fugitive#BufReadCmd(@%)", '') endif call fugitive#MapJumps() endtry @@ -2097,7 +2091,7 @@ function! s:TempReadPost(file) abort if empty(mapcheck('q', 'n')) nnoremap q :bdeleteechohl WarningMsgecho "Temp file q is deprecated in favor of the built-in C-W>q"echohl NONE endif - exe 'nnoremap ' s:nowait "gq :bdelete" + call s:Map('n', 'gq', ":bdelete", '') endif return '' endfunction @@ -4413,19 +4407,19 @@ function! s:Diff(autodir, keepfocus, mods, ...) abort let mods = (a:autodir ? s:diff_modifier(3) : '') . s:Mods(mods, 'leftabove') let nr = bufnr('') execute mods 'split' s:fnameescape(s:Generate(s:Relative(':2:'))) - execute 'nnoremap dp :diffput '.nr.'diffupdate' + call s:Map('n', 'dp', ':diffput '.nr.'diffupdate', '') let nr2 = bufnr('') call s:diffthis() exe back let mods = substitute(mods, '\Cleftabove\|rightbelow\|aboveleft\|belowright', '\=submatch(0) =~# "f" ? "rightbelow" : "leftabove"', '') execute mods 'split' s:fnameescape(s:Generate(s:Relative(':3:'))) - execute 'nnoremap dp :diffput '.nr.'diffupdate' + call s:Map('n', 'dp', ':diffput '.nr.'diffupdate', '') let nr3 = bufnr('') call s:diffthis() exe back call s:diffthis() - execute 'nnoremap d2o :diffget '.nr2.'diffupdate' - execute 'nnoremap d3o :diffget '.nr3.'diffupdate' + call s:Map('n', 'd2o', ':diffget '.nr2.'diffupdate', '') + call s:Map('n', 'd3o', ':diffget '.nr3.'diffupdate', '') return post elseif len(args) let arg = join(args, ' ') @@ -4828,9 +4822,9 @@ function! s:BlameSubcommand(line1, count, range, bang, mods, args) abort setlocal norelativenumber endif execute "vertical resize ".(s:linechars('.\{-\}\ze\s\+\d\+)')+1) - nnoremap A :exe "vertical resize ".(linechars('.\{-\}\ze [0-9:/+-][0-9:/+ -]* \d\+)')+1+v:count) - nnoremap C :exe "vertical resize ".(linechars('^\S\+')+1+v:count) - nnoremap D :exe "vertical resize ".(linechars('.\{-\}\ze\d\ze\s\+\d\+)')+1-v:count) + call s:Map('n', 'A', ":exe 'vertical resize '.(linechars('.\\{-\\}\\ze [0-9:/+-][0-9:/+ -]* \\d\\+)')+1+v:count)", '') + call s:Map('n', 'C', ":exe 'vertical resize '.(linechars('^\\S\\+')+1+v:count)", '') + call s:Map('n', 'D', ":exe 'vertical resize '.(linechars('.\\{-\\}\\ze\\d\\ze\\s\\+\\d\\+)')+1-v:count)", '') redraw syncbind endif @@ -4896,7 +4890,7 @@ function! s:BlameCommit(cmd, ...) abort return '' endfunction -function! s:BlameJump(suffix) abort +function! s:BlameJump(suffix, ...) abort let suffix = a:suffix let [commit, path, lnum] = s:BlameCommitFileLnum() if empty(path) @@ -4908,6 +4902,13 @@ function! s:BlameJump(suffix) abort endif let offset = line('.') - line('w0') let flags = get(s:TempState(), 'blame_flags', []) + if a:0 && a:1 + if s:HasOpt(flags, '--reverse') + call remove(flags, '--reverse') + else + call add(flags, '--reverse') + endif + endif let blame_bufnr = s:BlameBufnr() if blame_bufnr > 0 let bufnr = bufnr('') @@ -5033,20 +5034,20 @@ function! s:BlameFileType() abort if &modifiable return '' endif - nnoremap :help fugitive-:Gblame - nnoremap g? :help fugitive-:Gblame + call s:Map('n', '', ':help fugitive-:Gblame', '') + call s:Map('n', 'g?', ':help fugitive-:Gblame', '') if mapcheck('q', 'n') =~# '^$\|bdelete' - nnoremap q :exe BlameQuit()echohl WarningMsgecho ":Gblame q is deprecated in favor of gq"echohl NONE + call s:Map('n', 'q', ':exe BlameQuit()echohl WarningMsgecho ":Gblame q is deprecated in favor of gq"echohl NONE', '') endif - exe 'nnoremap ' s:nowait "gq :exe BlameQuit()" - nnoremap :exe BlameCommit("exe BlameLeave()edit") - nnoremap - :exe BlameJump('') - nnoremap P :exe BlameJump('^'.v:count1) - nnoremap ~ :exe BlameJump('~'.v:count1) - nnoremap i :exe BlameCommit("exe BlameLeave()edit") - nnoremap o :exe BlameCommit("split") - nnoremap O :exe BlameCommit("tabedit") - nnoremap p :exe BlameCommit("pedit") + call s:Map('n', 'gq', ':exe BlameQuit()', '') + call s:Map('n', '', ':exe BlameCommit("exe BlameLeave()edit")', '') + call s:Map('n', '-', ':exe BlameJump("")', '') + call s:Map('n', 'P', ':exe BlameJump("^".v:count1)', '') + call s:Map('n', '~', ':exe BlameJump("~".v:count1)', '') + call s:Map('n', 'i', ':exe BlameCommit("exe BlameLeave()edit")', '') + call s:Map('n', 'o', ':exe BlameCommit("split")', '') + call s:Map('n', 'O', ':exe BlameCommit("tabedit")', '') + call s:Map('n', 'p', ':exe BlameCommit("pedit")', '') endfunction augroup fugitive_blame @@ -5285,11 +5286,11 @@ function! fugitive#MapCfile(...) abort exe 'cnoremap ' (a:0 ? a:1 : 'fugitive#Cfile()') let b:undo_ftplugin = get(b:, 'undo_ftplugin', 'exe') . '|sil! exe "cunmap "' if !exists('g:fugitive_no_maps') - call s:map('n', 'gf', ':find ', '', 1) - call s:map('n', 'f', ':sfind ', '', 1) - call s:map('n', '', ':sfind ', '', 1) - call s:map('n', 'gf', ':tabfind ', '', 1) - call s:map('c', '', '', '', 1) + call s:Map('n', 'gf', ':find ', '', 1) + call s:Map('n', 'f', ':sfind ', '', 1) + call s:Map('n', '', ':sfind ', '', 1) + call s:Map('n', 'gf', ':tabfind ', '', 1) + call s:Map('c', '', '', '', 1) endif endfunction @@ -5334,9 +5335,9 @@ function! s:NavigateUp(count) abort endfunction function! s:MapEx(lhs, rhs) abort - execute "nnoremap " s:nowait a:lhs ":" . a:rhs . "" - execute "onoremap " s:nowait a:lhs ":" . a:rhs . "" - execute "xnoremap " s:nowait a:lhs ":exe 'normal! gv'" . a:rhs . "" + call s:Map('n', a:lhs, ":" . a:rhs . "", "") + call s:Map('o', a:lhs, ":" . a:rhs . "", "") + call s:Map('x', a:lhs, ":exe 'normal! gv'" . a:rhs . "", "") endfunction function! fugitive#MapJumps(...) abort @@ -5355,12 +5356,14 @@ function! fugitive#MapJumps(...) abort call s:Map('n', 'O', ':exe GF("tabedit")', '') call s:Map('n', 'p', ':exe GF("pedit")', '') - if exists(':CtrlP') && get(g:, 'ctrl_p_map') =~? '^$' - nnoremap :execute line('.') == 1 ? 'CtrlP ' . fnameescape(Tree()) : PreviousItem(v:count1) - else - nnoremap :execute PreviousItem(v:count1) + if !exists('g:fugitive_no_maps') + if exists(':CtrlP') && get(g:, 'ctrl_p_map') =~? '^$' + nnoremap :execute line('.') == 1 ? 'CtrlP ' . fnameescape(Tree()) : PreviousItem(v:count1) + else + nnoremap :execute PreviousItem(v:count1) + endif + nnoremap :execute NextItem(v:count1) endif - nnoremap :execute NextItem(v:count1) call s:MapEx('(', 'exe PreviousItem(v:count1)') call s:MapEx(')', 'exe NextItem(v:count1)') call s:MapEx('K', 'exe PreviousHunk(v:count1)') @@ -5377,15 +5380,15 @@ function! fugitive#MapJumps(...) abort call s:MapEx('][', 'exe NextSectionEnd(v:count1)') endif call s:Map('n', 'S', ':echoerr "Use gO"', '') - exe "nnoremap " s:nowait "- :exe 'Gedit ' . fnameescape(NavigateUp(v:count1)) if getline(1) =~# '^tree \x\{40,\}$' && empty(getline(2))call search('^'.escape(expand('#:t'),'.*[]~\').'/\=$','wc')endif" - nnoremap P :exe 'Gedit ' . fnameescape(ContainingCommit().'^'.v:count1.Relative(':')) - nnoremap ~ :exe 'Gedit ' . fnameescape(ContainingCommit().'~'.v:count1.Relative(':')) - nnoremap C :exe 'Gedit ' . fnameescape(ContainingCommit()) - nnoremap cp :echoerr 'Use gC' - nnoremap gC :exe 'Gpedit ' . fnameescape(ContainingCommit()) - nnoremap gc :exe 'Gpedit ' . fnameescape(ContainingCommit()) - nnoremap gi :exe 'Gsplit' (v:count ? '.gitignore' : '.git/info/exclude') - xnoremap gi :exe 'Gsplit' (v:count ? '.gitignore' : '.git/info/exclude') + call s:Map('n', '-', ":exe 'Gedit ' . fnameescape(NavigateUp(v:count1)) if getline(1) =~# '^tree \x\{40,\}$' && empty(getline(2))call search('^'.escape(expand('#:t'),'.*[]~\').'/\=$','wc')endif", '') + call s:Map('n', 'P', ":exe 'Gedit ' . fnameescape(ContainingCommit().'^'.v:count1.Relative(':'))", '') + call s:Map('n', '~', ":exe 'Gedit ' . fnameescape(ContainingCommit().'~'.v:count1.Relative(':'))", '') + call s:Map('n', 'C', ":exe 'Gedit ' . fnameescape(ContainingCommit())", '') + call s:Map('n', 'cp', ":echoerr 'Use gC'", '') + call s:Map('n', 'gC', ":exe 'Gpedit ' . fnameescape(ContainingCommit())", '') + call s:Map('n', 'gc', ":exe 'Gpedit ' . fnameescape(ContainingCommit())", '') + call s:Map('n', 'gi', ":exe 'Gsplit' (v:count ? '.gitignore' : '.git/info/exclude')", '') + call s:Map('x', 'gi', ":exe 'Gsplit' (v:count ? '.gitignore' : '.git/info/exclude')", '') nnoremap c :Gcommit nnoremap c :Gcommit @@ -5454,10 +5457,10 @@ function! fugitive#MapJumps(...) abort nnoremap ra :Grebase --abort nnoremap r? :help fugitive_r - nnoremap . : =fnameescape(fugitive#Real(@%)) - xnoremap . : =fnameescape(fugitive#Real(@%)) - nnoremap g? :help fugitive-mappings - nnoremap :help fugitive-mappings + call s:Map('n', '.', ": =fnameescape(fugitive#Real(@%))") + call s:Map('x', '.', ": =fnameescape(fugitive#Real(@%))") + call s:Map('n', 'g?', ":help fugitive-mappings", '') + call s:Map('n', '', ":help fugitive-mappings", '') endif endfunction @@ -5799,8 +5802,8 @@ function! fugitive#Init() abort endtry endif if !exists('g:fugitive_no_maps') - call s:map('c', '', 'fnameescape(fugitive#Object(@%))', '') - call s:map('n', 'y', ':call setreg(v:register, fugitive#Object(@%))', '') + call s:Map('c', '', 'fnameescape(fugitive#Object(@%))', '') + call s:Map('n', 'y', ':call setreg(v:register, fugitive#Object(@%))', '') endif if expand('%:p') =~# ':[\/][\/]' let &l:path = s:sub(&path, '^\.%(,|$)', '')