aboutsummaryrefslogtreecommitdiffstats
path: root/bundle/diff-corrections/autoload/diff_corrections.vim
blob: c3003e98eecb6eadfe6a54fe38edba31f359d9eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
let s:old_cursorline = &cursorline
let s:restore_cx_mapping = ''


function! diff_corrections#Run()
	if &diff
		if exists('g:colors_name') && g:colors_name ==# 'twilight256'
			highlight Comment ctermfg=7
		endif

		set nocursorline

		nnoremap cx :<C-u>tabclose<CR>
	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


function! s:SaveCommentColor()
	redir => old_highlight
	silent highlight Comment
	redir END

	let parts = split(old_highlight, ' ')
	call filter(parts, {_idx, val -> val !=? "" && val !=? "xxx"})

	let restore = join(parts, ' ')

	" Remove ^@ character from the beginning that messes up the `execute` call
	return restore[1:]
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, '<SID>', '<SNR>' . mapping.sid . '_', 'g')
	endif
endfunction


let s:old_highlight_comment = s:SaveCommentColor()
call s:SaveMapping()