VIM 撤消:为什么撤消`undojoin`时光标会跳到错误的位置?
VIM undo: Why does the cursor jump to the wrong position when undoing `undojoin`?
编辑:
我简化了函数,把问题弄清楚了。
原始问题在页面下方仍然可用。
交叉发布到 vim_dev 邮件列表:
https://groups.google.com/forum/#!topic/vim_dev/_Rz3uVXbwsQ
已向 Neovim 报告错误:
https://github.com/neovim/neovim/issues/6276
为什么下面两个例子中光标的位置不同:
[CORRECT CURSOR POSITION]替换的结果加入到缓冲区中之前的变化(添加第3行),光标位置正确恢复到缓冲区中的第二行。
normal ggiline one is full of aaaa
set undolevels=10 " splits the change into separate undo blocks
normal Goline two is full of bbbb
set undolevels=10
normal Goline three is full of cccc
set undolevels=10
undojoin
keepjumps %s/aaaa/zzzz/
normal u
[INCORRECT CURSOR POSITION] 替换的结果加入到缓冲区中之前的更改(第4行的添加),光标位置不正确恢复到缓冲区的第一行(应该是第3行)。
normal ggiline one is bull of aaaa
set undolevels=10 " splits the change into separate undo blocks
normal Goline two is full of bbbb
set undolevels=10
normal Goline three is full of cccc
set undolevels=10
normal Goline four is full of aaaa's again
set undolevels=10
undojoin
keepjumps %s/aaaa/zzzz/
normal u
原问题
我的 VIM 的设置方式,将缓冲区保存到文件会触发自定义 StripTrailingSpaces() 函数(附在问题末尾):
autocmd BufWritePre,FileWritePre,FileAppendPre,FilterWritePre <buffer>
\ :keepjumps call StripTrailingSpaces(0)
在看到 Restore the cursor position after undoing text change made by a script 之后,我想到了将我的 StripTrailingSpaces() 函数所做的更改从撤消历史记录中排除,方法是将函数创建的撤消记录合并到缓冲区中先前更改的末尾.
这样,当撤消更改时,该函数似乎根本没有创建它自己的撤消记录。
为了验证我的想法,我使用了一个简单的测试用例:创建一个干净的缓冲区并手动输入以下命令,或者将以下块保存为文件并通过以下方式获取它:
vim +"source <saved-filename-here>"
normal ggiline one is full of aaaa
set undolevels=10 " splits the change into separate undo blocks
normal Goline two is full of bbbb
set undolevels=10
normal Goline three is full of cccc
set undolevels=10
undojoin
keepjumps %s/aaaa/zzzz/
normal u
如您所见,撤消缓冲区中的最后一次更改(即创建第三行)后,光标正确返回到文件中的第二行。
由于我的测试成功了,我在我的 StripTrailingSpaces() 中实现了几乎相同的 undojoin
。但是,当我在函数 运行 后撤消最后的更改时,光标返回到文件中最顶部的更改。这通常是一个剥离的 space 并且 不是 我 undojoin
编辑到的更改位置。
谁能想到为什么会这样?更好的是,有人可以提出修复建议吗?
function! StripTrailingSpaces(number_of_allowed_spaces)
" Match all trailing spaces in a file
let l:regex = [
\ '\^\zs\s\{1,\}$',
\ '\S\s\{' . a:number_of_allowed_spaces . '\}\zs\s\{1,\}$',
\ ]
" Join trailing spaces regex into a single, non-magic string
let l:regex_str = '\V\(' . join(l:regex, '\|') . '\)'
" Save current window state
let l:last_search=@/
let l:winview = winsaveview()
try
" Append the comming change onto the end of the previous change
" NOTE: Fails if previous change doesn't exist
undojoin
catch
endtry
" Substitute all trailing spaces
if v:version > 704 || v:version == 704 && has('patch155')
execute 'keepjumps keeppatterns %s/' . l:regex_str . '//e'
else
execute 'keepjumps %s/' . l:regex_str . '//e'
call histdel('search', -1)
endif
" Restore current window state
call winrestview(l:winview)
let @/=l:last_search
endfunction
在我看来,这绝对像是 substitute 命令的错误。据我所知,替换命令偶尔会接管更改位置,以便在包含撤消块时跳转到该位置。我无法隔离模式 - 有时当替换发生时它会这样做 > 一些次数。其他时候,替换的位置似乎会影响发生这种情况的时间。好像很不靠谱。我不认为它实际上与 undojoin 命令有任何关系,因为我已经能够为不使用它的其他功能重现这种效果。如果您有兴趣,请尝试以下操作:
function! Test()
normal ciwfoo
normal ciwbar
%s/one/two/
endfunction
在包含不同数量 "ones" 并放置在不同位置的一些不同文本上进行尝试。您会注意到,之后有时撤消会跳转到第一个替换发生的行,而其他时候它会跳转到第一个普通命令进行更改的地方。
我认为这里适合您的解决方案是执行以下操作:
undo
normal ma
redo
在函数的顶部,然后将 u 绑定到函数中的 u'a 之类的东西,以便在撤消后它会跳回到实际第一次发生变化的地方,而不是任何随机性:s 力量在你身上。当然,它不可能那么简单,因为一旦你完成跳跃等等,你将不得不取消映射你,但这种模式通常应该给你一种方法来保存正确的位置然后跳回去给它。当然,您可能希望使用一些全局变量而不是劫持标记来完成所有这些操作,但您明白了。
编辑:
在花了一些时间研究源代码之后,实际上看起来您所追求的行为就是错误。这是确定撤消后光标应放置位置的代码块:
if (top < newlnum)
{
/* If the saved cursor is somewhere in this undo block, move it to
* the remembered position. Makes "gwap" put the cursor back
* where it was. */
lnum = curhead->uh_cursor.lnum;
if (lnum >= top && lnum <= top + newsize + 1)
{
MSG("Remembered Position.\n");
curwin->w_cursor = curhead->uh_cursor;
newlnum = curwin->w_cursor.lnum - 1;
}
else
{
char msg_buf[1000];
MSG("First change\n");
sprintf(msg_buf, "lnum: %d, top: %d, newsize: %d", lnum, top, newsize);
MSG(msg_buf);
/* Use the first line that actually changed. Avoids that
* undoing auto-formatting puts the cursor in the previous
* line. */
for (i = 0; i < newsize && i < oldsize; ++i)
if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
break;
if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
{
newlnum = top;
curwin->w_cursor.lnum = newlnum + 1;
}
else if (i < newsize)
{
newlnum = top + i;
curwin->w_cursor.lnum = newlnum + 1;
}
}
}
它相当复杂,但基本上它所做的是检查进行更改时光标所在的位置,然后如果它在撤消的更改块内,则将光标重置到 gw 命令的那个位置。否则,它会跳到更改最多的行并将您带到那里。 substitute 发生的事情是它为被替换的每一行激活这个逻辑,因此如果这些替换之一在撤消块中,那么它会跳转到撤消之前光标的位置(您想要的行为)。其他时候,none 的更改将在该块中,因此它会跳到最上面的更改行(可能是它应该做的)。因此,我认为您的问题的答案是 vim 当前不支持您想要的行为(进行更改但将其与之前的更改合并,除了在更改撤消后确定光标的放置位置) .
编辑:
这个特定的代码块位于 undoredo 函数内第 2711 行的 undo.c 中。 u_savecommon 内部是在实际调用撤消之前设置整个事情的地方,也是保存最终用于 gw 命令异常的光标位置的地方(undo.c 第 385 行并保存在第 548 行在同步缓冲区上调用时)。替换命令的逻辑位于第 4268 行的 ex_cmds.c 中,它在第 5208 行间接调用 u_savecommon(调用 u_savesub,后者调用 u_savecommon)。
编辑:
我简化了函数,把问题弄清楚了。
原始问题在页面下方仍然可用。交叉发布到 vim_dev 邮件列表: https://groups.google.com/forum/#!topic/vim_dev/_Rz3uVXbwsQ
已向 Neovim 报告错误:
https://github.com/neovim/neovim/issues/6276
为什么下面两个例子中光标的位置不同:
[CORRECT CURSOR POSITION]替换的结果加入到缓冲区中之前的变化(添加第3行),光标位置正确恢复到缓冲区中的第二行。
normal ggiline one is full of aaaa set undolevels=10 " splits the change into separate undo blocks normal Goline two is full of bbbb set undolevels=10 normal Goline three is full of cccc set undolevels=10 undojoin keepjumps %s/aaaa/zzzz/ normal u
[INCORRECT CURSOR POSITION] 替换的结果加入到缓冲区中之前的更改(第4行的添加),光标位置不正确恢复到缓冲区的第一行(应该是第3行)。
normal ggiline one is bull of aaaa set undolevels=10 " splits the change into separate undo blocks normal Goline two is full of bbbb set undolevels=10 normal Goline three is full of cccc set undolevels=10 normal Goline four is full of aaaa's again set undolevels=10 undojoin keepjumps %s/aaaa/zzzz/ normal u
原问题
我的 VIM 的设置方式,将缓冲区保存到文件会触发自定义 StripTrailingSpaces() 函数(附在问题末尾):
autocmd BufWritePre,FileWritePre,FileAppendPre,FilterWritePre <buffer>
\ :keepjumps call StripTrailingSpaces(0)
在看到 Restore the cursor position after undoing text change made by a script 之后,我想到了将我的 StripTrailingSpaces() 函数所做的更改从撤消历史记录中排除,方法是将函数创建的撤消记录合并到缓冲区中先前更改的末尾.
这样,当撤消更改时,该函数似乎根本没有创建它自己的撤消记录。
为了验证我的想法,我使用了一个简单的测试用例:创建一个干净的缓冲区并手动输入以下命令,或者将以下块保存为文件并通过以下方式获取它:
vim +"source <saved-filename-here>"
normal ggiline one is full of aaaa
set undolevels=10 " splits the change into separate undo blocks
normal Goline two is full of bbbb
set undolevels=10
normal Goline three is full of cccc
set undolevels=10
undojoin
keepjumps %s/aaaa/zzzz/
normal u
如您所见,撤消缓冲区中的最后一次更改(即创建第三行)后,光标正确返回到文件中的第二行。
由于我的测试成功了,我在我的 StripTrailingSpaces() 中实现了几乎相同的 undojoin
。但是,当我在函数 运行 后撤消最后的更改时,光标返回到文件中最顶部的更改。这通常是一个剥离的 space 并且 不是 我 undojoin
编辑到的更改位置。
谁能想到为什么会这样?更好的是,有人可以提出修复建议吗?
function! StripTrailingSpaces(number_of_allowed_spaces)
" Match all trailing spaces in a file
let l:regex = [
\ '\^\zs\s\{1,\}$',
\ '\S\s\{' . a:number_of_allowed_spaces . '\}\zs\s\{1,\}$',
\ ]
" Join trailing spaces regex into a single, non-magic string
let l:regex_str = '\V\(' . join(l:regex, '\|') . '\)'
" Save current window state
let l:last_search=@/
let l:winview = winsaveview()
try
" Append the comming change onto the end of the previous change
" NOTE: Fails if previous change doesn't exist
undojoin
catch
endtry
" Substitute all trailing spaces
if v:version > 704 || v:version == 704 && has('patch155')
execute 'keepjumps keeppatterns %s/' . l:regex_str . '//e'
else
execute 'keepjumps %s/' . l:regex_str . '//e'
call histdel('search', -1)
endif
" Restore current window state
call winrestview(l:winview)
let @/=l:last_search
endfunction
在我看来,这绝对像是 substitute 命令的错误。据我所知,替换命令偶尔会接管更改位置,以便在包含撤消块时跳转到该位置。我无法隔离模式 - 有时当替换发生时它会这样做 > 一些次数。其他时候,替换的位置似乎会影响发生这种情况的时间。好像很不靠谱。我不认为它实际上与 undojoin 命令有任何关系,因为我已经能够为不使用它的其他功能重现这种效果。如果您有兴趣,请尝试以下操作:
function! Test()
normal ciwfoo
normal ciwbar
%s/one/two/
endfunction
在包含不同数量 "ones" 并放置在不同位置的一些不同文本上进行尝试。您会注意到,之后有时撤消会跳转到第一个替换发生的行,而其他时候它会跳转到第一个普通命令进行更改的地方。
我认为这里适合您的解决方案是执行以下操作:
undo
normal ma
redo
在函数的顶部,然后将 u 绑定到函数中的 u'a 之类的东西,以便在撤消后它会跳回到实际第一次发生变化的地方,而不是任何随机性:s 力量在你身上。当然,它不可能那么简单,因为一旦你完成跳跃等等,你将不得不取消映射你,但这种模式通常应该给你一种方法来保存正确的位置然后跳回去给它。当然,您可能希望使用一些全局变量而不是劫持标记来完成所有这些操作,但您明白了。
编辑: 在花了一些时间研究源代码之后,实际上看起来您所追求的行为就是错误。这是确定撤消后光标应放置位置的代码块:
if (top < newlnum)
{
/* If the saved cursor is somewhere in this undo block, move it to
* the remembered position. Makes "gwap" put the cursor back
* where it was. */
lnum = curhead->uh_cursor.lnum;
if (lnum >= top && lnum <= top + newsize + 1)
{
MSG("Remembered Position.\n");
curwin->w_cursor = curhead->uh_cursor;
newlnum = curwin->w_cursor.lnum - 1;
}
else
{
char msg_buf[1000];
MSG("First change\n");
sprintf(msg_buf, "lnum: %d, top: %d, newsize: %d", lnum, top, newsize);
MSG(msg_buf);
/* Use the first line that actually changed. Avoids that
* undoing auto-formatting puts the cursor in the previous
* line. */
for (i = 0; i < newsize && i < oldsize; ++i)
if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
break;
if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
{
newlnum = top;
curwin->w_cursor.lnum = newlnum + 1;
}
else if (i < newsize)
{
newlnum = top + i;
curwin->w_cursor.lnum = newlnum + 1;
}
}
}
它相当复杂,但基本上它所做的是检查进行更改时光标所在的位置,然后如果它在撤消的更改块内,则将光标重置到 gw 命令的那个位置。否则,它会跳到更改最多的行并将您带到那里。 substitute 发生的事情是它为被替换的每一行激活这个逻辑,因此如果这些替换之一在撤消块中,那么它会跳转到撤消之前光标的位置(您想要的行为)。其他时候,none 的更改将在该块中,因此它会跳到最上面的更改行(可能是它应该做的)。因此,我认为您的问题的答案是 vim 当前不支持您想要的行为(进行更改但将其与之前的更改合并,除了在更改撤消后确定光标的放置位置) .
编辑: 这个特定的代码块位于 undoredo 函数内第 2711 行的 undo.c 中。 u_savecommon 内部是在实际调用撤消之前设置整个事情的地方,也是保存最终用于 gw 命令异常的光标位置的地方(undo.c 第 385 行并保存在第 548 行在同步缓冲区上调用时)。替换命令的逻辑位于第 4268 行的 ex_cmds.c 中,它在第 5208 行间接调用 u_savecommon(调用 u_savesub,后者调用 u_savecommon)。