This repository has been archived by the owner on Sep 21, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.vim
132 lines (123 loc) · 3.55 KB
/
util.vim
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
let s:V = gita#vital()
let s:File = s:V.import('System.File')
let s:Prompt = s:V.import('Vim.Prompt')
function! s:diffoff() abort
if !&diff
return
endif
augroup gita_internal_util_diffthis
autocmd! * <buffer>
augroup END
if maparg('<C-l>', 'n') ==# '<Plug>(gita-C-l)'
unmap <buffer> <C-l>
endif
nunmap <buffer> <Plug>(gita-C-l)
diffoff
endfunction
function! s:syncbind() abort
augroup gita_internal_util_syncbind
autocmd! *
augroup END
syncbind
endfunction
function! gita#util#clip(content) abort
let @" = a:content
if has('clipboard')
call setreg(v:register, a:content)
endif
endfunction
function! gita#util#browse(uri) abort
call s:File.open(a:uri)
endfunction
function! gita#util#doautocmd(name, ...) abort
let pattern = get(a:000, 0, '')
let expr = empty(pattern)
\ ? '#' . a:name
\ : '#' . a:name . '#' . pattern
let eis = split(&eventignore, ',')
if index(eis, a:name) >= 0 || index(eis, 'all') >= 0 || !exists(expr)
" the specified event is ignored or not exists
return
endif
let is_pseudo_required = empty(pattern) && !exists('#' . a:name . '#*')
if is_pseudo_required
" NOTE:
" autocmd XXXXX <pattern> exists but not sure if current buffer name
" match with the <pattern> so register empty autocmd to prevent
" 'No matching autocommands' warning
augroup gita_internal_util_doautocmd
autocmd! *
execute 'autocmd ' . a:name . ' * :'
augroup END
endif
let nomodeline = has('patch-7.4.438') && a:name ==# 'User'
\ ? '<nomodeline> '
\ : ''
try
execute 'doautocmd ' . nomodeline . a:name . ' ' . pattern
finally
if is_pseudo_required
augroup gita_internal_util_doautocmd
autocmd! *
augroup END
endif
endtry
endfunction
function! gita#util#diffthis() abort
if maparg('<C-l>', 'n') ==# ''
nmap <buffer> <C-l> <Plug>(gita-C-l)
endif
nnoremap <buffer><silent> <Plug>(gita-C-l)
\ :<C-u>diffupdate<BAR>redraw<CR>
augroup gita_internal_util_diffthis
autocmd! * <buffer>
autocmd BufWinLeave <buffer> call s:diffoff()
autocmd BufHidden <buffer> call s:diffoff()
augroup END
diffthis
keepjump normal! zM
endfunction
function! gita#util#syncbind() abort
" NOTE:
" Somehow syncbind does not just after opening a buffer so use
" CursorHold and CursorMoved to call a bit later again
augroup gita_internal_util_syncbind
autocmd!
autocmd CursorHold * call s:syncbind()
autocmd CursorHoldI * call s:syncbind()
autocmd CursorMoved * call s:syncbind()
autocmd CursorMovedI * call s:syncbind()
augroup END
syncbind
endfunction
function! gita#util#handle_exception() abort
let known_attention_patterns = [
\ '^\%(vital: Git[:.]\|vim-gita:\) Cancel$',
\ '^\%(vital: Git[:.]\|vim-gita:\) Cancel: ',
\ '^\%(vital: Git[:.]\|vim-gita:\) Attention: ',
\]
for pattern in known_attention_patterns
if v:exception =~# pattern
call s:Prompt.attention(
\ 'gita:',
\ substitute(v:exception, pattern, '', ''),
\)
return
endif
endfor
let known_warning_patterns = [
\ '^\%(vital: Git[:.]\|vim-gita:\) \zeWarning: ',
\ '^\%(vital: Git[:.]\|vim-gita:\) \zeValidationError: ',
\]
for pattern in known_warning_patterns
if v:exception =~# pattern
call s:Prompt.warn(
\ 'gita:',
\ substitute(v:exception, pattern, '', ''),
\)
return
endif
endfor
call s:Prompt.error(v:exception)
call s:Prompt.debug(v:throwpoint)
endfunction