1+ scriptencoding utf- 8
2+
13if exists (' g:blamer_autoloaded' )
24 finish
35endif
46let g: blamer_autoloaded = 1
57
6- let s: save_cpo = &cpo
7- set cpo &vim
8+ let s: save_cpo = &cpoptions
9+ set cpoptions &vim
810
9- let s: git_root = ' '
1011let s: blamer_prefix = get (g: , ' blamer_prefix' , ' ' )
1112let s: blamer_template = get (g: , ' blamer_template' , ' <author>, <author-time> • <summary>' )
1213let s: blamer_date_format = get (g: , ' blamer_date_format' , ' %d/%m/%y %H:%M' )
@@ -28,6 +29,10 @@ let s:blamer_relative_time = get(g:, 'blamer_relative_time', 0)
2829let s: is_windows = has (' win16' ) || has (' win32' ) || has (' win64' ) || has (' win95' )
2930let s: missing_popup_feature = ! has (' nvim' ) && ! exists (' *popup_create' )
3031
32+ let s: blamer_buffer_enabled = 0
33+ let s: blamer_show_enabled = 0
34+
35+
3136function ! s: GetRelativeTime (commit_timestamp) abort
3237 let l: current_timestamp = localtime ()
3338 let l: elapsed = l: current_timestamp - a: commit_timestamp
@@ -71,14 +76,6 @@ function! s:Head(array) abort
7176 return a: array [0 ]
7277endfunction
7378
74- function ! s: IsFileInPath (file_path, path ) abort
75- if a: file_path = ~? a: path
76- return 1
77- else
78- return 0
79- endif
80- endfunction
81-
8279function ! s: GetLines () abort
8380 let l: visual_line_number = line (' v' )
8481 let l: cursor_line_number = line (' .' )
@@ -126,20 +123,20 @@ function! blamer#ParseCommitDataLine(line) abort
126123endfunction
127124
128125function ! blamer#GetMessages (file , line_number, line_count) abort
126+ let l: dir_path = shellescape (s: substitute_path_separator (expand (' %:h' )))
129127 let l: end_line = a: line_number + a: line_count - 1
130128 let l: file_path_escaped = shellescape (a: file )
131- let l: command = ' git --no-pager blame --line-porcelain -L ' . a: line_number . ' ,' . l: end_line . ' -- ' . l: file_path_escaped
129+ let l: command = ' git -C ' . l: dir_path . ' - -no-pager blame --line-porcelain -L ' . a: line_number . ' ,' . l: end_line . ' -- ' . l: file_path_escaped
132130 let l: result = system (l: command )
133131 let l: lines = split (l: result , ' \n' )
134132
135- let l: info = {}
136- let l: info [' commit-short' ] = split (l: lines [0 ], ' ' )[0 ][:7 ]
137- let l: info [' commit-long' ] = split (l: lines [0 ], ' ' )[0 ]
138- let l: hash_is_empty = empty (matchstr (info[' commit-long' ],' \c[0-9a-f]\{40}' ))
133+ let hash = split (l: lines [0 ], ' ' )[0 ]
134+ let l: hash_is_empty = empty (matchstr (hash,' \c[0-9a-f]\{40}' ))
139135
140136 if l: hash_is_empty
141137 if l: result = ~? ' fatal' && l: result = ~? ' not a git repository'
142- let g: blamer_enabled = 0
138+ " Not a git repository
139+ let g: blamer_buffer_enabled = 0
143140 echo ' [blamer.nvim] Not a git repository'
144141 return ' '
145142 endif
@@ -163,8 +160,6 @@ function! blamer#GetMessages(file, line_number, line_count) abort
163160 endif
164161
165162 let l: TAB_ASCII = 9
166-
167- let l: reading_commit_data = 0
168163 let l: commit_data = {}
169164 let l: commit_data_per_line = []
170165
@@ -182,7 +177,7 @@ function! blamer#GetMessages(file, line_number, line_count) abort
182177 elseif l: has_line_tab
183178 " line type TAB
184179 " Change messsage when changes are not commited
185- if l: commit_data .author == ? " Not Committed Yet"
180+ if l: commit_data .author == ? ' Not Committed Yet'
186181 let l: commit_data .author = ' You'
187182 let l: commit_data .committer = ' You'
188183 let l: commit_data .summary = ' Uncommitted changes'
@@ -195,7 +190,7 @@ function! blamer#GetMessages(file, line_number, line_count) abort
195190 endif
196191 endfor
197192
198- return map (l: commit_data_per_line ," blamer#CommitDataToMessage(v:val)" )
193+ return map (l: commit_data_per_line ,' blamer#CommitDataToMessage(v:val)' )
199194endfunction
200195
201196function ! blamer#SetVirtualText (buffer_number, line_number, message) abort
@@ -219,7 +214,7 @@ function! blamer#CreatePopup(buffer_number, line_number, message) abort
219214 \ ' id' : l: propid ,
220215 \} )
221216
222- let l: popup_winid = popup_create (s: blamer_prefix . a: message , {
217+ call popup_create (s: blamer_prefix . a: message , {
223218 \ ' textprop' : ' blamer_popup_marker' ,
224219 \ ' textpropid' : l: propid ,
225220 \ ' line' : -1 ,
@@ -235,13 +230,14 @@ function! blamer#Show() abort
235230 return
236231 endif
237232
238- let l: is_buffer_special = &buftype != ' ' ? 1 : 0
239- if is_buffer_special
233+ let l: is_buffer_special = &buftype !=# ' ' ? 1 : 0
234+ if l: is_buffer_special
240235 return
241236 endif
242237
243238 let l: file_path = s: substitute_path_separator (expand (' %:p' ))
244- if s: IsFileInPath (l: file_path , s: git_root ) == 0
239+
240+ if empty (l: file_path )
245241 return
246242 endif
247243
@@ -253,10 +249,6 @@ function! blamer#Show() abort
253249 return
254250 endif
255251
256- " if mode() == 'i' && s:blamer_show_in_insert_modes == 0
257- " return
258- " endif
259-
260252 let l: line_count = len (l: line_numbers )
261253 let l: messages = blamer#GetMessages (l: file_path , l: line_numbers [0 ], l: line_count )
262254 let l: index = 0
@@ -287,52 +279,86 @@ function! blamer#Hide() abort
287279 endif
288280endfunction
289281
290- function ! blamer#Refresh () abort
282+ function ! blamer#UpdateGitUserConfig () abort
283+ let l: dir_path = shellescape (s: substitute_path_separator (expand (' %:h' )))
284+ let s: blamer_user_name = s: Head (split (system (' git -C ' . l: dir_path . ' config --get user.name' ), ' \n' ))
285+ let s: blamer_user_email = s: Head (split (system (' git -C ' . l: dir_path . ' config --get user.email' ), ' \n' ))
286+ endfunction
287+
288+
289+ function ! blamer#IsBufferGitTracked () abort
290+ let l: file_path = shellescape (s: substitute_path_separator (expand (' %:p' )))
291+ if empty (l: file_path )
292+ return 0
293+ endif
294+
295+ let l: dir_path = shellescape (s: substitute_path_separator (expand (' %:h' )))
296+ let l: result = system (' git -C ' . l: dir_path . ' ls-files --error-unmatch ' . l: file_path )
297+ if l: result [0 :4 ] == # ' fatal'
298+ return 0
299+ endif
300+
301+ return 1
302+ endfunction
303+
304+ function ! blamer#BufferEnter () abort
291305 if g: blamer_enabled == 0
292306 return
293307 endif
294308
295- call timer_stop (s: blamer_timer_id )
296- call blamer#Hide ()
297- let s: blamer_timer_id = timer_start (s: blamer_delay , { tid - > blamer#Show () })
309+ let l: is_tracked = blamer#IsBufferGitTracked ()
310+ if l: is_tracked
311+ let s: blamer_buffer_enabled = 1
312+ call blamer#UpdateGitUserConfig ()
313+ call blamer#EnableShow ()
314+ else
315+ let s: blamer_buffer_enabled = 0
316+ endif
298317endfunction
299318
300- function ! blamer#Enable () abort
301- if g: blamer_enabled == 1
319+ function ! blamer#BufferLeave () abort
320+ if g: blamer_enabled == 0
302321 return
303322 endif
304323
305- let g: blamer_enabled = 1
306- call blamer#Init ()
324+ call blamer#DisableShow ()
307325endfunction
308326
309- function ! blamer#Disable () abort
310- if g: blamer_enabled == 0
327+ function ! blamer#Refresh () abort
328+ if g: blamer_enabled == 0 || s: blamer_buffer_enabled == 0 || s: blamer_show_enabled == 0
311329 return
312330 endif
313331
314- let g: blamer_enabled = 0
315332 call timer_stop (s: blamer_timer_id )
316- let s: blamer_timer_id = -1
333+ call blamer#Hide ()
334+ let s: blamer_timer_id = timer_start (s: blamer_delay , { tid - > blamer#Show () })
335+ endfunction
336+
337+ function ! blamer#Enable () abort
338+ let g: blamer_enabled = 1
317339endfunction
318340
319- function ! blamer#EnableOnInsertLeave () abort
320- if g: blamer_show_on_insert_leave == 0
341+ function ! blamer#Disable () abort
342+ let g: blamer_enabled = 0
343+ endfunction
344+
345+ function ! blamer#EnableShow () abort
346+ if g: blamer_enabled == 0 || s: blamer_buffer_enabled == 0 || s: blamer_show_enabled == 1
321347 return
322348 endif
323349
324- let g: blamer_show_on_insert_leave = 0
325- call blamer#Enable ()
350+ let s: blamer_show_enabled = 1
326351 call blamer#Show ()
327352endfunction
328353
329- function ! blamer#DisableOnInsertEnter () abort
330- if g: blamer_enabled == 0
354+ function ! blamer#DisableShow () abort
355+ if g: blamer_enabled == 0 || s: blamer_buffer_enabled == 0 || s: blamer_show_enabled == 0
331356 return
332357 endif
333358
334- let g: blamer_show_on_insert_leave = 1
335- call blamer#Disable ()
359+ let s: blamer_show_enabled = 0
360+ call timer_stop (s: blamer_timer_id )
361+ let s: blamer_timer_id = -1
336362 call blamer#Hide ()
337363endfunction
338364
@@ -353,27 +379,14 @@ function! blamer#Init() abort
353379 return
354380 endif
355381
356- if s: is_windows
357- let l: result = split (system (' git rev-parse --show-toplevel 2>NUL' ), ' \n' )
358- else
359- let l: result = split (system (' git rev-parse --show-toplevel 2>/dev/null' ), ' \n' )
360- endif
361- let s: git_root = s: Head (l: result )
362-
363- if s: git_root == ' '
364- let g: blamer_enabled = 0
365- return
366- endif
367-
368- let s: blamer_user_name = s: Head (split (system (' git config --get user.name' ), ' \n' ))
369- let s: blamer_user_email = s: Head (split (system (' git config --get user.email' ), ' \n' ))
370-
371382 augroup blamer
372383 autocmd !
384+ autocmd BufEnter * :call blamer#BufferEnter ()
385+ autocmd BufLeave * :call blamer#BufferLeave ()
373386 autocmd BufEnter ,BufWritePost ,CursorMoved * :call blamer#Refresh ()
374387 if s: blamer_show_in_insert_modes == 0
375- autocmd InsertEnter * :call blamer#DisableOnInsertEnter ()
376- autocmd InsertLeave * :call blamer#EnableOnInsertLeave ()
388+ autocmd InsertEnter * :call blamer#DisableShow ()
389+ autocmd InsertLeave * :call blamer#EnableShow ()
377390 endif
378391 augroup END
379392endfunction
@@ -383,5 +396,5 @@ function! s:substitute_path_separator(path) abort
383396 return s: is_windows ? substitute (a: path , ' \\' , ' /' , ' g' ) : a: path
384397endfunction
385398
386- let &cpo = s: save_cpo
399+ let &cpoptions = s: save_cpo
387400unlet s: save_cpo
0 commit comments