3

我需要将所有没有空字符串的寄存器放入列表中。

首先,它们是否已经存在于某个列表中?

其次,如果他们没有,是否有一种更简单的方法可以将它们放入列表中,而不是手动浏览它们并检查它们是否为空?

也许我可以创建一个已定义寄存器的列表并在该列表上使用 getreg() 但我想我希望该列表已经存在所以我不必创建它。

4

2 回答 2

2

这是我想出的:

" Description:
"   Get a list of all the non-empty register indexes
" Example Usage:
"   :call GetNonEmptyRegsIndex( 'print' )    """ To print the registers
"   for n in GetNonEmptyRegsIndex() | echo nr2char(n)." = ".getreg(nr2char(n)) | endfor
" Resources:
"   http://www.ibm.com/developerworks/linux/library/l-vim-script-3/index.html
"   irs channel #vim <jamessan>
function! GetNonEmptyRegsIndex( ... )
  " Create the list of register 'indexes' where the the elements are in char2nr form
  let regnum =  range(char2nr('a'), char2nr('z'))
  let regnum += range(char2nr('0'), char2nr('9'))
  let regstr =  ['"','-','*','%','/','.','#',':']
  let regnum += map(regstr, 'char2nr(v:val)')

  " Remove the registers that are empty
  "let regnum = filter( regnum, 'getreg(nr2char(v:val)) != ""' )
  let regnum = filter( regnum, 'getreg(nr2char(v:val)) !~ "^$"' )

  " Remove the registers that are just spaces
  let regnum = filter( regnum, 'getreg(nr2char(v:val)) !~ "^\s\+$"' )

  " Remove the registers that have no alpha-num
  "let regnum = filter( regnum, 'getreg(nr2char(v:val)) !~ "^\W\+$"' )
  return regnum
endfunction

必须从 string 转换为 num 并再次返回使代码有点难看,但它可以工作。

这是自动完成注册信息的完整工作解决方案。稍后我可能会对其进行更多调整,但它为我提供了我想要的基础知识。

inoremap <F5> <C-R>=AutoCompleteRegs()<CR>

function! AutoCompleteRegs()
  call complete( col('.'), GetNonEmptyRegs() )
  return ''
endfunction

" Description:
"   Get a list of all the non-empty registers.
function! GetNonEmptyRegs()
  " Create the list of register 'indexes' where the the elements are in char2nr form
  let regIndexNum = GetNonEmptyRegsIndex()

  " Convert the list of indexes to values
  let regs = map(regIndexNum, 'getreg(nr2char(v:val))')
  "let regs = map(regs, 'substitute(v:val,  "^(.*)", "\\U\\1", "g")')
  "let regs = map(regs, "echo substitute(v:val,  '^(.*)', '\U\1', 'g')")
  return regs
endfunction

" Description:
"   Get a list of all the non-empty register indexes
" Example Usage:
"   :call GetNonEmptyRegsIndex( 'print' )    """ To print the registers
"   for n in GetNonEmptyRegsIndex() | echo nr2char(n)." = ".getreg(nr2char(n)) | endfor
" Resources:
"   http://www.ibm.com/developerworks/linux/library/l-vim-script-3/index.html
"   irs channel #vim <jamessan>
function! GetNonEmptyRegsIndex( ... )
  " Create the list of register 'indexes' where the the elements are in char2nr form
  let regnum =  range(char2nr('a'), char2nr('z'))
  let regnum += range(char2nr('0'), char2nr('9'))
  let regstr =  ['"','-','*','%','/','.','#',':']
  let regnum += map(regstr, 'char2nr(v:val)')

  " Remove the registers that are empty
  "let regnum = filter( regnum, 'getreg(nr2char(v:val)) != ""' )
  let regnum = filter( regnum, 'getreg(nr2char(v:val)) !~ "^$"' )

  " Remove the registers that are just spaces
  let regnum = filter( regnum, 'getreg(nr2char(v:val)) !~ "^\s\+$"' )

  " Remove the registers that have no alpha-num
  "let regnum = filter( regnum, 'getreg(nr2char(v:val)) !~ "^\W\+$"' )
  return regnum
endfunction
于 2012-06-18T00:30:31.847 回答
1

没有这样的列表,至少不能用 vimscript 访问。您可以解析由 捕获的:silent regsilent没有可见输出,但仍可捕获)的输出,:redir但我不太喜欢这个命令(由于“没有嵌套重定向”以及 while 目标变量将在该位置创建的事实在第一个 redir 发生的地方,它填充在第二个 redir 发生的地方,并在那里按名称引用,从而可以将数据附加到任何变量或抛出错误,如E121: Undefined variable)。我只是不喜欢这种不可预测的命令。

于 2012-06-17T21:15:44.130 回答