Thursday, January 19, 2012

Re: Retrieve the argument of a prior Ex mode command

Chris Jones <cjns1989@gmail.com> a écrit:
>
> Hello Paul,
>
> Sorry for delay.. I didn't have the time to look into this further.

No problem with me.

[...]
> I came up with this:
>
> | let g:indx=histnr("cmd")
> |
> | function! GetArg()
> | let g:indx -= 1
> | let s:cmdl = split(histget("cmd", g:indx))
> | let g:arg = s:cmdl[len(s:cmdl)-1]
> | return g:arg
> | endfunction
> |
> | function! InitIndex()
> | let g:indx=histnr("cmd")
> | return ""
> | endfunction
>
> | cnoremap <M-.> <C-E><C-\>eGetArg()<CR>
> | nnoremap : :<C-\>eInitIndex()<CR>
>
> The main difference with your solution is that I directly access the Vim
> command history via histnr() & histget() rather than maintaining my own
> list of commands.

I didn't know those functions, which make life definitely easier.
(Mapping `:' was also smarter than my way; the best solution would be a
CmdEnter autocommand, but there is none.)

> My effort basically works, except that when I hit 'Alt+.', whatever
> I started to type on the Ex command-line gets overwritten instead of
> being echoed at the current cursor position:

That's because you forgot to retrieve the command typed so far. So
here's a reworking of your solution. It also skips empty arguments:

let s:indx=histnr("cmd")

function! GetArg()
let l:head = matchstr(getcmdline(), '^\S*')
let l:cmdl = ""
while s:indx >= 0 && l:cmdl == ""
let l:cmdl = matchstr(histget("cmd", s:indx), '\s\zs.*')
let s:indx -= 1
endwhile
return l:head . " " . l:cmdl
endfunction

function! InitIndex()
let s:indx=histnr("cmd")
return ""
endfunction

cnoremap <M-.> <C-E><C-\>eGetArg()<CR>
nnoremap : :<C-\>eInitIndex()<CR>

I hope this time it'll work ok.

Best,
Paul

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: