From 321f63ff198eef4cc95d9b022f06f502583a51d9 Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Thu, 26 Jul 2018 21:53:43 -0400 Subject: [PATCH] Compartmentalize filetype support --- autoload/fugitive.vim | 72 +++++++++++++++++++++++-------------------- plugin/fugitive.vim | 14 +++++++-- 2 files changed, 49 insertions(+), 37 deletions(-) diff --git a/autoload/fugitive.vim b/autoload/fugitive.vim index d3fabfc..a408153 100644 --- a/autoload/fugitive.vim +++ b/autoload/fugitive.vim @@ -208,6 +208,10 @@ function! s:map(mode, lhs, rhs, ...) abort endwhile if flags !~# '' || empty(mapcheck(head.tail, a:mode)) exe a:mode.'map ' flags head.tail a:rhs + if a:0 > 1 + let b:undo_ftplugin = get(b:, 'undo_ftplugin', 'exe') . + \ '|sil! exe "' . a:mode . 'unmap ' . head.tail . '"' + endif endif endfunction @@ -2956,13 +2960,14 @@ augroup END nnoremap : :=v:count ? v:count : '' function! fugitive#MapCfile(...) abort - cnoremap fugitive#Cfile() + 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 ', '') - call s:map('n', 'f', ':sfind ', '') - call s:map('n', '', ':sfind ', '') - call s:map('n', 'gf', ':tabfind ', '') - call s:map('c', '', '', '') + 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 @@ -3019,6 +3024,32 @@ function! fugitive#MapJumps(...) abort endif endfunction +function! s:StatusCfile(...) abort + let pre = '' + if getline('.') =~# '^.\=\trenamed:.* -> ' + return '/'.matchstr(getline('.'),' -> \zs.*') + elseif getline('.') =~# '^.\=\t\(\k\| \)\+\p\?: *.' + return '/'.matchstr(getline('.'),': *\zs.\{-\}\ze\%( ([^()[:digit:]]\+)\)\=$') + elseif getline('.') =~# '^.\=\t.' + return '/'.matchstr(getline('.'),'\t\zs.*') + elseif getline('.') =~# ': needs merge$' + return '/'.matchstr(getline('.'),'.*\ze: needs merge$') + elseif getline('.') =~# '^\%(. \)\=Not currently on any branch.$' + return 'HEAD' + elseif getline('.') =~# '^\%(. \)\=On branch ' + return 'refs/heads/'.getline('.')[12:] + elseif getline('.') =~# "^\\%(. \\)\=Your branch .*'" + return matchstr(getline('.'),"'\\zs\\S\\+\\ze'") + else + return '' + endif +endfunction + +function! fugitive#StatusCfile() abort + let file = s:StatusCfile() + return empty(file) ? "\\" : s:fnameescape(s:Generate(file)) +endfunction + function! s:cfile() abort try let myhash = s:DirRev(@%)[1] @@ -3058,29 +3089,6 @@ function! s:cfile() abort let ref = matchstr(getline('.'),'\x\{40\}') let file = ':'.s:sub(matchstr(getline('.'),'\d\t.*'),'\t',':') return [file] - elseif &filetype ==# 'gitcommit' - if getline('.') =~# '^.\=\trenamed:.* -> ' - let file = '/'.matchstr(getline('.'),' -> \zs.*') - return [file] - elseif getline('.') =~# '^.\=\t\(\k\| \)\+\p\?: *.' - let file = '/'.matchstr(getline('.'),': *\zs.\{-\}\ze\%( ([^()[:digit:]]\+)\)\=$') - return [file] - elseif getline('.') =~# '^.\=\t.' - let file = '/'.matchstr(getline('.'),'\t\zs.*') - return [file] - elseif getline('.') =~# ': needs merge$' - let file = '/'.matchstr(getline('.'),'.*\ze: needs merge$') - return [file, 'Gdiff!'] - - elseif getline('.') =~# '^\%(. \)\=Not currently on any branch.$' - return ['HEAD'] - elseif getline('.') =~# '^\%(. \)\=On branch ' - let file = 'refs/heads/'.getline('.')[12:] - return [file] - elseif getline('.') =~# "^\\%(. \\)\=Your branch .*'" - let file = matchstr(getline('.'),"'\\zs\\S\\+\\ze'") - return [file] - endif endif let showtree = (getline(1) =~# '^tree ' && getline(2) == "") @@ -3206,7 +3214,7 @@ endfunction function! s:GF(mode) abort try - let results = s:cfile() + let results = &filetype ==# 'gitcommit' ? [s:StatusCfile()] : s:cfile() catch /^fugitive:/ return 'echoerr v:errmsg' endtry @@ -3236,10 +3244,6 @@ function! fugitive#Cfile() abort return pre . s:fnameescape(s:Generate(results[0])) endfunction -function! fugitive#cfile() abort - return fugitive#Cfile() -endfunction - " Section: Statusline function! fugitive#Statusline(...) abort diff --git a/plugin/fugitive.vim b/plugin/fugitive.vim index 1b31a36..00b6910 100644 --- a/plugin/fugitive.vim +++ b/plugin/fugitive.vim @@ -183,11 +183,19 @@ augroup fugitive autocmd FileType git \ if exists('b:git_dir') | \ call fugitive#MapJumps() | - \ endif - autocmd FileType git,gitcommit,gitrebase - \ if exists('b:git_dir') | \ call fugitive#MapCfile() | \ endif + autocmd FileType gitcommit + \ if exists('b:git_dir') | + \ call fugitive#MapCfile('fugitive#StatusCfile()') | + \ endif + autocmd FileType gitrebase + \ let &l:include = '^\%(pick\|squash\|edit\|reword\|fixup\|drop\|[pserfd]\)\>' | + \ if exists('b:git_dir') | + \ let &l:includeexpr = 'v:fname =~# ''^\x\{4,40\}$'' ? FugitiveGenerate(v:fname) : ' . + \ (len(&l:includeexpr) ? &l:includeexpr : 'v:fname') | + \ endif | + \ let b:undo_ftplugin = get(b:, 'undo_ftplugin', 'exe') . '|setl inex= inc=' autocmd BufReadCmd index{,.lock} \ if FugitiveIsGitDir(expand(':p:h')) |