diff --git a/autoload/gitgutter/diff.vim b/autoload/gitgutter/diff.vim index 1e02662..4cb4fc7 100644 --- a/autoload/gitgutter/diff.vim +++ b/autoload/gitgutter/diff.vim @@ -30,7 +30,19 @@ function! gitgutter#diff#run_diff(realtime, use_external_grep) let buff_file .= '.'.extension endif let cmd .= 'git show '.blob_name.' > '.blob_file.' && ' + + " Writing the whole buffer resets the '[ and '] marks and also the + " 'modified' flag (if &cpoptions includes '+'). These are unwanted + " side-effects so we save and restore the values ourselves. + let modified = getbufvar(bufnr, "&mod") + let op_mark_start = getpos("'[") + let op_mark_end = getpos("']") + execute 'keepalt silent write' buff_file + + call setbufvar(bufnr, "&mod", modified) + call setpos("'[", op_mark_start) + call setpos("']", op_mark_end) endif let cmd .= 'git diff --no-ext-diff --no-color -U0 '.g:gitgutter_diff_args.' -- ' diff --git a/test/keepModified.expected b/test/keepModified.expected new file mode 100644 index 0000000..57a1bbf --- /dev/null +++ b/test/keepModified.expected @@ -0,0 +1,2 @@ +modified: 1 +modified: 1 diff --git a/test/keepOpMarks.expected b/test/keepOpMarks.expected new file mode 100644 index 0000000..ed6b8c3 --- /dev/null +++ b/test/keepOpMarks.expected @@ -0,0 +1,4 @@ +'[ mark: 0,6,1,0 +'] mark: 0,6,2,0 +'[ mark: 0,6,1,0 +'] mark: 0,6,2,0 diff --git a/test/testKeepModified.vim b/test/testKeepModified.vim new file mode 100644 index 0000000..acb687f --- /dev/null +++ b/test/testKeepModified.vim @@ -0,0 +1,8 @@ +source helper.vim +call Setup() + +normal 5Go* +call Dump("modified: ".getbufvar('', '&modified'), 'keepModified') +doautocmd CursorHold +call Dump("modified: ".getbufvar('', '&modified'), 'keepModified') + diff --git a/test/testKeepOpMarks.vim b/test/testKeepOpMarks.vim new file mode 100644 index 0000000..1175c80 --- /dev/null +++ b/test/testKeepOpMarks.vim @@ -0,0 +1,10 @@ +source helper.vim +call Setup() + +normal 5Go* +call Dump("'[ mark: ".join(getpos("'["), ','), 'keepOpMarks') +call Dump("'] mark: ".join(getpos("']"), ','), 'keepOpMarks') +doautocmd CursorHold +call Dump("'[ mark: ".join(getpos("'["), ','), 'keepOpMarks') +call Dump("'] mark: ".join(getpos("']"), ','), 'keepOpMarks') +