From cea8855836335942886b4e2a3fea4a3d7d549b3c Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 3 Jun 2019 21:58:21 +0200 Subject: diff_corrections: Add `cx` mapping to close tab Add a mapping to quickly close a tab. Makes it easier to close files that I've finished reviewing. Thanks to user9433424 (https://vi.stackexchange.com/users/6960/user9433424) on the Vi Stack Exchange for the very detailed explanation of how to restore a mapping: https://vi.stackexchange.com/questions/7734/how-to-save-and-restore-a-mapping/7735#7735 Using user9433424's mapping restore code, and ignoring the case where a mapping is defined both globally and local to a buffer. --- .../diff-corrections/autoload/diff_corrections.vim | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'bundle/diff-corrections') diff --git a/bundle/diff-corrections/autoload/diff_corrections.vim b/bundle/diff-corrections/autoload/diff_corrections.vim index 620c1b0..c3003e9 100644 --- a/bundle/diff-corrections/autoload/diff_corrections.vim +++ b/bundle/diff-corrections/autoload/diff_corrections.vim @@ -1,4 +1,6 @@ let s:old_cursorline = &cursorline +let s:restore_cx_mapping = '' + function! diff_corrections#Run() if &diff @@ -7,12 +9,18 @@ function! diff_corrections#Run() endif set nocursorline + + nnoremap cx :tabclose else if exists('g:colors_name') && g:colors_name ==# 'twilight256' execute 'highlight ' . s:old_highlight_comment endif let &cursorline = s:old_cursorline + + if strlen(s:restore_cx_mapping) > 0 + execute s:restore_cx_mapping + endif endif endfunction @@ -32,4 +40,22 @@ function! s:SaveCommentColor() endfunction +function! s:SaveMapping() + let mapping = maparg('cx', 'n', 0, 1) + + if !empty(mapping) + let s:restore_cx_mapping .= (mapping.noremap ? 'nnoremap' : 'nmap') . + \ join( + \ map( + \ ['buffer', 'expr', 'nowait', 'silent'], + \ 'mapping[v:val] ? "<" . v:val . ">" : ""' + \ ) + \ ) . + \ mapping.lhs . ' ' . + \ substitute(mapping.rhs, '', '' . mapping.sid . '_', 'g') + endif +endfunction + + let s:old_highlight_comment = s:SaveCommentColor() +call s:SaveMapping() -- cgit v1.2.3