@@ -27,6 +27,10 @@ let s:blamer_relative_time = get(g:, 'blamer_relative_time', 0)
2727let s: is_windows = has (' win16' ) || has (' win32' ) || has (' win64' ) || has (' win95' )
2828let s: missing_popup_feature = ! has (' nvim' ) && ! exists (' *popup_create' )
2929
30+ let s: blamer_buffer_enabled = 0
31+ let s: blamer_show_enabled = 0
32+
33+
3034function ! s: GetRelativeTime (commit_timestamp) abort
3135 let l: current_timestamp = localtime ()
3236 let l: elapsed = l: current_timestamp - a: commit_timestamp
@@ -139,7 +143,8 @@ function! blamer#GetMessages(file, line_number, line_count) abort
139143
140144 if l: hash_is_empty
141145 if l: result = ~? ' fatal' && l: result = ~? ' not a git repository'
142- let g: blamer_enabled = 0
146+ " Not a git repository
147+ let g: blamer_buffer_enabled = 0
143148 echo ' [blamer.nvim] Not a git repository'
144149 return ' '
145150 endif
@@ -246,8 +251,6 @@ function! blamer#Show() abort
246251 return
247252 endif
248253
249-
250-
251254 let l: buffer_number = bufnr (' ' )
252255 let l: line_numbers = s: GetLines ()
253256
@@ -256,10 +259,6 @@ function! blamer#Show() abort
256259 return
257260 endif
258261
259- " if mode() == 'i' && s:blamer_show_in_insert_modes == 0
260- " return
261- " endif
262-
263262 let l: line_count = len (l: line_numbers )
264263 let l: messages = blamer#GetMessages (l: file_path , l: line_numbers [0 ], l: line_count )
265264 let l: index = 0
@@ -296,60 +295,80 @@ function! blamer#UpdateGitUserConfig() abort
296295 let s: blamer_user_email = s: Head (split (system (' git -C ' . l: dir_path . ' config --get user.email' ), ' \n' ))
297296endfunction
298297
298+
299+ function ! blamer#IsBufferGitTracked () abort
300+ let l: file_path = shellescape (s: substitute_path_separator (expand (' %:p' )))
301+ if empty (l: file_path )
302+ return 0
303+ endif
304+
305+ let l: dir_path = shellescape (s: substitute_path_separator (expand (' %:h' )))
306+ let l: result = system (' git -C ' . l: dir_path . ' ls-files --error-unmatch ' . l: file_path )
307+ if l: result [0 :4 ] == # ' fatal'
308+ return 0
309+ endif
310+
311+ return 1
312+ endfunction
313+
299314function ! blamer#BufferEnter () abort
300315 if g: blamer_enabled == 0
301316 return
302317 endif
303318
304- call blamer#UpdateGitUserConfig ()
319+ let l: is_tracked = blamer#IsBufferGitTracked ()
320+ if l: is_tracked
321+ let s: blamer_buffer_enabled = 1
322+ call blamer#UpdateGitUserConfig ()
323+ call blamer#EnableShow ()
324+ else
325+ let s: blamer_buffer_enabled = 0
326+ endif
305327endfunction
306328
307- function ! blamer#Refresh () abort
329+ function ! blamer#BufferLeave () abort
308330 if g: blamer_enabled == 0
309331 return
310332 endif
311333
334+ call blamer#DisableShow ()
335+ endfunction
336+
337+ function ! blamer#Refresh () abort
338+ if g: blamer_enabled == 0 || s: blamer_buffer_enabled == 0 || s: blamer_show_enabled == 0
339+ return
340+ endif
341+
312342 call timer_stop (s: blamer_timer_id )
313343 call blamer#Hide ()
314344 let s: blamer_timer_id = timer_start (s: blamer_delay , { tid - > blamer#Show () })
315345endfunction
316346
317347function ! blamer#Enable () abort
318- if g: blamer_enabled == 1
319- return
320- endif
321-
322348 let g: blamer_enabled = 1
323- call blamer#Init ()
324349endfunction
325350
326351function ! blamer#Disable () abort
327- if g: blamer_enabled == 0
328- return
329- endif
330-
331352 let g: blamer_enabled = 0
332- call timer_stop (s: blamer_timer_id )
333- let s: blamer_timer_id = -1
334353endfunction
335354
336- function ! blamer#EnableOnInsertLeave () abort
337- if g: blamer_show_on_insert_leave == 0
355+ function ! blamer#EnableShow () abort
356+ if g: blamer_enabled == 0 || s: blamer_buffer_enabled == 0 || s: blamer_show_enabled == 1
338357 return
339358 endif
340359
341- let g: blamer_show_on_insert_leave = 0
342- call blamer#Enable ()
360+ let s: blamer_show_enabled = 1
343361 call blamer#Show ()
344362endfunction
345363
346- function ! blamer#DisableOnInsertEnter () abort
347- if g: blamer_enabled == 0
364+ function ! blamer#DisableShow () abort
365+ if g: blamer_enabled == 0 || s: blamer_buffer_enabled == 0 || s: blamer_show_enabled == 0
348366 return
349367 endif
350368
351- let g: blamer_show_on_insert_leave = 1
352- call blamer#Disable ()
369+ let s: blamer_show_enabled = 0
370+ call timer_stop (s: blamer_timer_id )
371+ let s: blamer_timer_id = -1
353372 call blamer#Hide ()
354373endfunction
355374
@@ -373,10 +392,11 @@ function! blamer#Init() abort
373392 augroup blamer
374393 autocmd !
375394 autocmd BufEnter * :call blamer#BufferEnter ()
395+ autocmd BufLeave * :call blamer#BufferLeave ()
376396 autocmd BufEnter ,BufWritePost ,CursorMoved * :call blamer#Refresh ()
377397 if s: blamer_show_in_insert_modes == 0
378- autocmd InsertEnter * :call blamer#DisableOnInsertEnter ()
379- autocmd InsertLeave * :call blamer#EnableOnInsertLeave ()
398+ autocmd InsertEnter * :call blamer#DisableShow ()
399+ autocmd InsertLeave * :call blamer#EnableShow ()
380400 endif
381401 augroup END
382402endfunction
0 commit comments