【问题标题】:Vim - Pydoc plugin not working with operator mappingVim - Pydoc 插件不适用于运算符映射
【发布时间】: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 &lt;buffer&gt;&lt;localleader&gt; 结合使用,以便它只在Python 缓冲区中定义(如:Pydoc 命令)。

标签: vim


【解决方案1】:

:Pydoc 命令不需要shellescape() 函数。 shellescape 在返回的字符串中包含引号,这会导致 :Pydoc 自毁。但是,如果命令是:grep,例如,则需要使用shellescape

相关帮助主题:

:help shellescape()
:help 'operatorfunc'
:help :map-operator

【讨论】:

    猜你喜欢
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 2018-01-03
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    相关资源
    最近更新 更多