Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Okay, here's the best I could come up with myself. This function starts at the top of the current buffer and using the <code>]c</code> motion it moves through the changes until <code>]c</code> no longer has an effect. It returns the number of changes (or <code>0</code> if the cursor if it is not a diff buffer).</p> <pre><code>function! CountDiffs() let winview = winsaveview() let num_diffs = 0 if &amp;diff let pos = getpos(".") keepj sil exe 'normal! G' let lnum = 1 let moved = 1 while moved let startl = line(".") keepj sil exe 'normal! [c' let moved = line(".") - startl if moved let num_diffs+=1 endif endwhile call winrestview(winview) call setpos(".",pos) endif return num_diffs endfunction </code></pre> <p>It seems to work okay, and isn't a noticeable performance hit when included in my statusline.</p> <p>As for finding the "number" of the current change, here is a function that uses the backward <code>[c</code> motion to count the number of changes before the position of the cursor. The returned value isn't quite right... I think maybe it should only return a number if the cursor is "within" the changed text, rather than after the first line of the change.</p> <pre><code>function! CurrentDiff() if &amp;diff let num_diff = 0 let winview = winsaveview() let pos = getpos(".") let moved = 1 while moved let startl = line(".") keepj sil exe 'normal! [c' let moved = line(".") - startl if moved let num_diff+=1 endif endwhile call winrestview(winview) call setpos(".",pos) return num_diff endif endfunction </code></pre> <p>Again, it seems to behave itself in my statusline and doesn't affect the movement of the cursor. The numbers update properly as changes are copied too/from the buffer.</p> <p>Once the problems have been ironed out I might consider uploading this as a plugin on the Vim website.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload