【发布时间】:2015-02-07 19:56:30
【问题描述】:
我有一个用于Pydoc plugin 的小型运算符映射。它的代码如下:
nnoremap <buffer> <localleader>d :set operatorfunc=<SID>PydocOperator<cr>g@
vnoremap <buffer> <localleader>d :<c-u>call <SID>PydocOperator(visualmode())<cr>
function! s:PydocOperator(type)
let l:orig_register = @@
if a:type ==# 'v'
normal! `<v`>y
elseif a:type ==# 'char'
normal! `[v`]y
else
return
endif
execute 'Pydoc ' . shellescape(@@)
let @@ = l:orig_register
endfunction
但是,vim 会抛出错误:
E116: Invalid arguments for function <SNR>117_ShowPyDoc
如果我手动复制一些文本并运行此命令,也会发生同样的错误:
execute 'Pydoc ' . shellescape(@@)
这很奇怪,考虑到:Pydoc 应该像普通命令一样工作,将一个参数作为输入。我查看了定义:Pydoc 命令的代码(该代码行是here),发现将参数传递给引号中的:Pydoc 命令可能会导致问题。所以我运行:Pydoc 'sys' 看看它是否会抛出与运算符映射相同的错误,它确实如此。因此,如果参数周围的引号有问题,我该如何格式化 execute 命令,使其不会给出无效参数?
【问题讨论】:
-
另外,您应该将
:map <buffer>与<localleader>结合使用,以便它只在Python 缓冲区中定义(如:Pydoc命令)。
标签: vim