Skip to content

Commit 2fd4951

Browse files
authored
Merge pull request mhinz#326 from mhinz/toggle
Improve handling of disabled buffers
2 parents dfa1e55 + 7dbf6f0 commit 2fd4951

File tree

6 files changed

+116
-92
lines changed

6 files changed

+116
-92
lines changed

autoload/sy.vim

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ function! sy#start(...) abort
2323
let new_sy = {
2424
\ 'path': path,
2525
\ 'buffer': bufnr,
26-
\ 'active': 0,
2726
\ 'detecting': 0,
2827
\ 'vcs': [],
2928
\ 'hunks': [],
@@ -36,19 +35,11 @@ function! sy#start(...) abort
3635
\ 'file': sy#util#escape(fnamemodify(path, ':t'))
3736
\ }}
3837
call setbufvar(bufnr, 'sy', new_sy)
39-
if get(g:, 'signify_disable_by_default')
40-
call sy#verbose('Disabled by default.')
41-
return
42-
endif
43-
let new_sy.active = 1
44-
call setbufvar(bufnr, 'sy', new_sy)
38+
call sy#set_buflocal_autocmds(bufnr)
4539
call sy#repo#detect(bufnr)
4640
elseif has('vim_starting')
4741
call sy#verbose("Don't run Sy more than once during startup.")
4842
return
49-
elseif !sy.active
50-
call sy#verbose('Inactive buffer.')
51-
return
5243
elseif empty(sy.vcs)
5344
if get(sy, 'retry')
5445
let sy.retry = 0
@@ -59,7 +50,7 @@ function! sy#start(...) abort
5950
call sy#verbose('Detection is already in progress.')
6051
else
6152
call sy#verbose('No VCS found. Disabling.')
62-
call sy#disable(sy.buffer)
53+
call sy#stop(sy.buffer)
6354
endif
6455
endif
6556
else
@@ -76,50 +67,40 @@ function! sy#start(...) abort
7667
endfunction
7768

7869
" #stop {{{1
79-
function! sy#stop(bufnr) abort
80-
if empty(getbufvar(a:bufnr, 'sy'))
81-
return
82-
endif
83-
call sy#sign#remove_all_signs(a:bufnr)
70+
function! sy#stop(...) abort
71+
let bufnr = bufnr('')
72+
if empty(getbufvar(a:0 ? a:1 : bufnr, 'sy')) | return | endif
73+
call sy#sign#remove_all_signs(bufnr)
74+
execute printf('autocmd! signify * <buffer=%d>', bufnr)
75+
call setbufvar(bufnr, 'sy', {})
8476
endfunction
8577

86-
" #enable {{{1
87-
function! sy#enable() abort
88-
if !exists('b:sy')
89-
call sy#start()
90-
return
91-
endif
92-
93-
if !b:sy.active
94-
let b:sy.active = 1
95-
let b:sy.retry = 1
96-
call sy#start()
97-
endif
78+
" #toggle {{{1
79+
function! sy#toggle() abort
80+
call call(empty(getbufvar(bufnr(''), 'sy')) ? 'sy#start' : 'sy#stop', [])
9881
endfunction
9982

100-
" #disable {{{1
101-
function! sy#disable(...) abort
102-
let sy = getbufvar(a:0 ? a:1 : bufnr(''), 'sy')
103-
104-
if !empty(sy) && sy.active
105-
call sy#stop(sy.buffer)
106-
let b:sy.active = 0
107-
let b:sy.stats = [-1, -1, -1]
108-
endif
83+
" #start_all {{{1
84+
function! sy#start_all() abort
85+
for bufnr in range(1, bufnr(''))
86+
call sy#start({'bufnr': bufnr})
87+
endfor
88+
let g:signify_disable_by_default = 0
10989
endfunction
11090

111-
" #toggle {{{1
112-
function! sy#toggle() abort
113-
if !exists('b:sy') || !b:sy.active
114-
call sy#enable()
115-
else
116-
call sy#disable()
117-
endif
91+
" #stop_all {{{1
92+
function! sy#stop_all() abort
93+
for bufnr in range(1, bufnr(''))
94+
if !empty(getbufvar(bufnr, 'sy'))
95+
call sy#stop(bufnr)
96+
endif
97+
endfor
98+
let g:signify_disable_by_default = 1
11899
endfunction
119100

120101
" #buffer_is_active {{{1
121102
function! sy#buffer_is_active()
122-
return exists('b:sy') && b:sy.active
103+
return !empty(getbufvar(bufnr(''), 'sy'))
123104
endfunction
124105

125106
" #verbose {{{1
@@ -135,6 +116,35 @@ function! sy#verbose(msg, ...) abort
135116
endif
136117
endfunction
137118

119+
" #set_buflocal_autocmds {{{1
120+
function! sy#set_buflocal_autocmds(bufnr) abort
121+
augroup signify
122+
execute printf('autocmd! * <buffer=%d>', a:bufnr)
123+
124+
execute printf('autocmd BufEnter <buffer=%d> call sy#start()', a:bufnr)
125+
execute printf('autocmd WinEnter <buffer=%d> call sy#start()', a:bufnr)
126+
execute printf('autocmd BufWritePost <buffer=%d> call sy#start()', a:bufnr)
127+
128+
execute printf('autocmd CursorHold <buffer=%d> call sy#start()', a:bufnr)
129+
execute printf('autocmd CursorHoldI <buffer=%d> call sy#start()', a:bufnr)
130+
131+
execute printf('autocmd FocusGained <buffer=%d> SignifyRefresh', a:bufnr)
132+
133+
execute printf('autocmd CmdwinEnter <buffer=%d> let g:signify_cmdwin_active = 1', a:bufnr)
134+
execute printf('autocmd CmdwinLeave <buffer=%d> let g:signify_cmdwin_active = 0', a:bufnr)
135+
136+
execute printf('autocmd ShellCmdPost <buffer=%d> call sy#start()', a:bufnr)
137+
138+
if exists('##VimResume')
139+
execute printf('autocmd VimResume <buffer=%d> call sy#start()', a:bufnr)
140+
endif
141+
augroup END
142+
143+
if exists('#User#SignifyAutocmds')
144+
doautocmd <nomodeline> User SignifyAutocmds
145+
endif
146+
endfunction
147+
138148
" s:get_path {{{1
139149
function! s:get_path(bufnr)
140150
let path = resolve(fnamemodify(bufname(a:bufnr), ':p'))

autoload/sy/debug.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function! sy#debug#list_active_buffers() abort
1414

1515
echo "\n". path ."\n". repeat('=', strlen(path))
1616

17-
for k in ['active', 'buffer', 'vcs', 'stats', 'signid']
17+
for k in ['buffer', 'vcs', 'stats', 'signid']
1818
if k == 'stats'
1919
echo printf("%10s = %d added, %d changed, %d removed\n",
2020
\ k,

autoload/sy/repo.vim

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function! s:handle_diff(options, exitval) abort
129129
elseif !empty(sy.updated_by) && sy.updated_by != a:options.vcs
130130
call sy#verbose(printf('Signs already got updated by %s.', sy.updated_by), a:options.vcs)
131131
return
132-
elseif empty(sy.vcs) && sy.active
132+
elseif empty(sy.vcs)
133133
let sy.detecting -= 1
134134
endif
135135

@@ -257,7 +257,7 @@ endfunction
257257

258258
" #debug_detection {{{1
259259
function! sy#repo#debug_detection()
260-
if !exists('b:sy')
260+
if empty(getbufvar(bufnr(''), 'sy'))
261261
echomsg 'signify: I cannot detect any changes!'
262262
return
263263
endif
@@ -365,8 +365,10 @@ endfunction
365365

366366
" #diff_hunk {{{1
367367
function! sy#repo#diff_hunk() abort
368-
if exists('b:sy') && !empty(b:sy.updated_by)
369-
call sy#repo#get_diff(bufnr(''), b:sy.updated_by, function('s:diff_hunk'))
368+
let bufnr = bufnr('')
369+
let sy = getbufvar(bufnr, 'sy')
370+
if !empty(sy) && !empty(sy.updated_by)
371+
call sy#repo#get_diff(bufnr, sy.updated_by, function('s:diff_hunk'))
370372
endif
371373
endfunction
372374

@@ -396,8 +398,10 @@ endfunction
396398

397399
" #undo_hunk {{{1
398400
function! sy#repo#undo_hunk() abort
399-
if exists('b:sy') && !empty(b:sy.updated_by)
400-
call sy#repo#get_diff(bufnr(''), b:sy.updated_by, function('s:undo_hunk'))
401+
let bufnr = bufnr('')
402+
let sy = getbufvar(bufnr, 'sy')
403+
if !empty(sy) && !empty(sy.updated_by)
404+
call sy#repo#get_diff(bufnr, sy.updated_by, function('s:undo_hunk'))
401405
endif
402406
endfunction
403407

autoload/sy/util.vim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ endfunction
9393

9494
" #return_if_no_changes {{{1
9595
function! sy#util#return_if_no_changes() abort
96-
if !exists('b:sy') || empty(b:sy.hunks)
96+
let sy = getbufvar(bufnr(''), 'sy')
97+
if empty(sy) || empty(sy.hunks)
9798
echomsg 'signify: There are no changes.'
9899
return 'return'
99100
endif

doc/signify.txt

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,11 @@ Works the same as |g:signify_vcs_cmds|.
231231
*g:signify_disable_by_default* >
232232
let g:signify_disable_by_default = 0
233233
<
234-
This makes Sy not looking for changes for each new buffer. You can easily
235-
enable it later using |signify-:SignifyToggle|.
234+
Disable Sy by default. You can still enable it later via:
235+
236+
|signify-:SignifyToggle|
237+
|signify-:SignifyEnable|
238+
|signify-:SignifyEnableAll|
236239

237240
------------------------------------------------------------------------------
238241
*g:signify_skip_filename_pattern*
@@ -340,12 +343,24 @@ Enable the plugin for the current buffer only.
340343
Can also be used to when a repository was initialized while Sy was already
341344
loaded.
342345

346+
------------------------------------------------------------------------------
347+
*signify-:SignifyEnableAll* >
348+
:SignifyEnableAll
349+
<
350+
Enable the plugin for all buffers. Sets |g:signify_disable_by_default| to 0.
351+
343352
------------------------------------------------------------------------------
344353
*signify-:SignifyDisable* >
345354
:SignifyDisable
346355
<
347356
Disable the plugin for the current buffer only.
348357

358+
------------------------------------------------------------------------------
359+
*signify-:SignifyDisableAll* >
360+
:SignifyDisableAll
361+
<
362+
Disable the plugin for all buffers. Sets |g:signify_disable_by_default| to 1.
363+
349364
------------------------------------------------------------------------------
350365
*signify-:SignifyToggle* >
351366
:SignifyToggle
@@ -451,12 +466,12 @@ See all of them with:
451466
<
452467
You can disable sign updating for certain events:
453468
>
454-
autocmd User SignifySetup autocmd! signify CursorHold,CursorHoldI
469+
autocmd User SignifyAutocmds autocmd! signify CursorHold,CursorHoldI
455470
<
456471
If you don't need immediate feedback or responses from your VCS are slow, then
457472
use this to only update signs when writing the buffer:
458473
>
459-
autocmd User SignifySetup
474+
autocmd User SignifyAutocmds
460475
\ exe 'au! signify' | au signify BufWritePost * call sy#start()
461476
<
462477
==============================================================================
@@ -467,7 +482,13 @@ Signify fires these user events:
467482
User SignifySetup~
468483

469484
This event fires at the end of `plugin/signify.vim`, in case you want to
470-
change any of the default autocmds, commands, or mappings.
485+
change any of the default commands, or mappings.
486+
487+
User SignifyAutocmds~
488+
489+
This event fires every time Sy sets autocmds. E.g. when opening a
490+
version-controlled file or when using |signify-:SignifyToggle| a lot.
491+
Useful to change the default |signify-autocmds|.
471492

472493
User Signify~
473494

@@ -657,6 +678,9 @@ EXAMPLE *signify-example*
657678

658679
An example configuration for Sy:
659680
>
681+
" Faster sign updates on CursorHold/CursorHoldI
682+
set updatetime=100
683+
660684
nnoremap <leader>gd :SignifyDiff<cr>
661685
nnoremap <leader>gp :SignifyHunkDiff<cr>
662686
nnoremap <leader>gu :SignifyHunkUndo<cr>

plugin/signify.vim

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,9 @@ if exists('g:loaded_signify') || !has('signs') || &compatible
66
finish
77
endif
88

9-
" Variables {{{1
109
let g:loaded_signify = 1
1110
let g:signify_locked = 0
1211

13-
" Autocmds {{{1
14-
augroup signify
15-
autocmd!
16-
17-
autocmd BufEnter * call sy#start()
18-
autocmd WinEnter * call sy#start()
19-
autocmd BufWritePost * call sy#start()
20-
21-
autocmd CursorHold * call sy#start()
22-
autocmd CursorHoldI * call sy#start()
23-
24-
autocmd FocusGained * SignifyRefresh
25-
26-
autocmd QuickFixCmdPre *vimgrep* let g:signify_locked = 1
27-
autocmd QuickFixCmdPost *vimgrep* let g:signify_locked = 0
28-
29-
autocmd CmdwinEnter * let g:signify_cmdwin_active = 1
30-
autocmd CmdwinLeave * let g:signify_cmdwin_active = 0
31-
32-
autocmd ShellCmdPost * call sy#start()
33-
34-
if exists('##VimResume')
35-
autocmd VimResume * call sy#start()
36-
endif
37-
38-
if has('gui_running') && has('win32') && argc()
39-
" Fix 'no signs at start' race.
40-
autocmd GUIEnter * redraw
41-
endif
42-
augroup END
43-
4412
" Commands {{{1
4513
command! -nargs=0 -bar SignifyList call sy#debug#list_active_buffers()
4614
command! -nargs=0 -bar SignifyDebug call sy#repo#debug_detection()
@@ -49,10 +17,13 @@ command! -nargs=0 -bar -bang SignifyDiff call sy#repo#diffmode(<bang>
4917
command! -nargs=0 -bar SignifyHunkDiff call sy#repo#diff_hunk()
5018
command! -nargs=0 -bar SignifyHunkUndo call sy#repo#undo_hunk()
5119
command! -nargs=0 -bar SignifyRefresh call sy#util#refresh_windows()
52-
command! -nargs=0 -bar SignifyEnable call sy#enable()
53-
command! -nargs=0 -bar SignifyDisable call sy#disable()
20+
21+
command! -nargs=0 -bar SignifyEnable call sy#start()
22+
command! -nargs=0 -bar SignifyDisable call sy#stop()
5423
command! -nargs=0 -bar SignifyToggle call sy#toggle()
5524
command! -nargs=0 -bar SignifyToggleHighlight call sy#highlight#line_toggle()
25+
command! -nargs=0 -bar SignifyEnableAll call sy#start_all()
26+
command! -nargs=0 -bar SignifyDisableAll call sy#stop_all()
5627

5728
" Mappings {{{1
5829
let s:cpoptions = &cpoptions
@@ -87,6 +58,20 @@ xnoremap <silent> <plug>(signify-motion-outer-visual) :<c-u>call sy#util#hunk_t
8758
8859
let &cpoptions = s:cpoptions
8960
unlet s:cpoptions
61+
62+
" Autocmds {{{1
63+
if has('gui_running') && has('win32') && argc()
64+
" Fix 'no signs at start' race.
65+
autocmd GUIEnter * redraw
66+
endif
67+
68+
autocmd QuickFixCmdPre *vimgrep* let g:signify_locked = 1
69+
autocmd QuickFixCmdPost *vimgrep* let g:signify_locked = 0
70+
71+
autocmd BufNewFile,BufRead * nested
72+
\ if !get(g:, 'signify_disable_by_default') |
73+
\ call sy#start({'bufnr': bufnr('')}) |
74+
\ endif
9075
" 1}}}
9176

9277
if exists('#User#SignifySetup')

0 commit comments

Comments
 (0)