1

我想为 cscope 替换一个 :tnext 命令,但它没有按我的预期工作。

1) 下图显示了按预期工作的代码。我可以到达符号的第二个实例。

function MyCounter()

    if !exists("s:counter")
        let s:counter = 1
        echo "script executed for the first time"
    else
        let s:counter = s:counter + 1
        echo "script executed " . s:counter . " times now"
    endif
endfunction

nmap <space>w :ls<CR>
nmap <space>i :call MyCounter()
nmap <space>n :cs find s <C-R>=expand("<cword>")<CR><CR>2<CR>

2)下面的代码不起作用

function MyCounter()
    if !exists("s:counter")
        let s:counter = 1
        echo "script executed for the first time"
    else
        let s:counter = s:counter + 1
        echo "script executed " . s:counter . " times now"
    endif
endfunction

nmap <space>w :ls<CR>
nmap <space>i :call MyCounter()
nmap <space>n :cs find s <C-R>=expand("<cword>")<CR><CR><C-R>=str2nr(s:counter)<CR>

代码片段 1 和 2 之间的区别是 =str2nr(s:counter) 即在用户按下 n 时动态计算符号的 n 个实例

在按空格+ni 之前总是按空格+i

请告诉我为什么第二个代码片段不起作用。

4

2 回答 2

0

该问题是由于尝试排队处理作为表达式评估结果而获得的字符引起的。有必要使表达式成为表达式 one(请参阅:help :map-<expr>)或使用 feedkeys()函数。为了便于修改映射,我建议使用第一种方法并按如下方式更改映射。

:nnoremap <expr> <space>n ':cs find s '.expand('<cword>')."\r".s:counter."\r\r"
于 2012-03-10T10:08:23.460 回答
0

你可以试试

nmap <space>n :exec "cs find s" expand("<cword>") "\| norm" str2nr(s:counter)

您似乎<C-R>=str2nr...在正常模式下使用,这是行不通的。

免责声明:我无法测试上述方法是否可行。

编辑

您可能想要使用以下cscopetag设置:

                        *cscopetag* *cst*
If 'cscopetag' set, the commands ":tag" and CTRL-] as well as "vim -t" will
always use |:cstag| instead of the default :tag behavior.  Effectively, by
setting 'cst', you will always search your cscope databases as well as your
tag files.  The default is off.  Examples: >
    :set cst
    :set nocst

这应该为您提供一组使用标签的丰富命令,这可能为您提供执行所需操作的方法(:tjump:tnext

于 2012-03-09T21:45:30.977 回答