Vim - Pydoc 插件无法使用运算符映射
Vim - Pydoc plugin not working with operator mapping
我有一个用于 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
命令,以便它不会给出无效参数?
shellescape()
函数对于 :Pydoc
命令不是必需的。 shellescape
在返回的字符串中包含引号,这会导致 :Pydoc
自毁。但是,如果命令是 :grep
,例如,将需要使用 shellescape
。
相关帮助主题:
:help shellescape()
:help 'operatorfunc'
:help :map-operator
我有一个用于 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
命令,以便它不会给出无效参数?
shellescape()
函数对于 :Pydoc
命令不是必需的。 shellescape
在返回的字符串中包含引号,这会导致 :Pydoc
自毁。但是,如果命令是 :grep
,例如,将需要使用 shellescape
。
相关帮助主题:
:help shellescape()
:help 'operatorfunc'
:help :map-operator