On 05-Feb-2011 19:44, James Vega wrote:
> On Sat, Feb 05, 2011 at 07:57:00PM +0300, ZyX wrote:
>> Reply to message «Re: ctrl-o with a mapping», 
>> sent 19:38:30 05 February 2011, Saturday
>> by Ben Fritz:
>>
>> Perhaps a bug somewhere? It should be redirected to vim_dev, I think.
>>
>> Original message:
>>> On Feb 5, 4:05 am, ZyX <[email protected]> wrote:
>>>
>>> It seems strange to me that recorded macros behave differently from
>>> normal mode mappings, especially if you interpret them as above. :help
>>> @ even says "the register is executed like a mapping".
> 
> Hmm, that was a very recent addition to the help and the behavior
> doesn't agree with that.  I wonder what the impetus was behind the
> addition of that description.
> 
>>> For example:
>>>
>>> qa0:echo getfontname()<Enter>q
>>> i<C-O>@a
>>>
>>> This does not insert the :echo getfontname(), it does exactly as is
>>> done in normal mode.
> 
> This makes sense.  <C-o> lets you execute one normal mode command.  In
> this case that command is @a.
> 
>>> However,
>>>
>>> :nnoremap <F9> 0:echo getfontname()<CR>
>>>
>>> i<C-O><F9>
>>>
>>> This inserts the :echo command in the text.
> 
> And in this case the one normal mode command is 0, since mappings are
> treated just like a user typing.

Not every normal mode-mapping is automatically suitable for execution via
i_CTRL-O; you need to explicitly design your mappings for that purpose. I
wouldn't call it a bug, rather a weakness in Vi(m)'s model of mode-based 
operation.


Here are some rules and guidelines from my personal "Vim scripting guide":

The |i_CTRL-O| command allows execution of one normal mode command, then
returns to insert mode. If a normal mode mapping concatenates multiple normal
mode commands, this breaks down in temporary normal mode and literally inserts
the second part of the command into the buffer instead.
To support execution of normal mode mappings from within insert mode (via
|i_CTRL-O|), these strategies can be used:
a) Instead of concatenating multiple normal mode commands, use one :normal
   mapping: >
   "nnoremap zC zCVzC
   nnoremap <silent> zC :<C-U>normal! zCVzC<CR>
b) Concatenate multiple Ex commands via <Bar>, not >
    :<C-U>call FirstCommand()<CR>:<C-U>call SecondCommand()<CR>
c) Shadow normal mode mappings are by insert mode mappings that re-enter
   normal mode, then invoke the normal mode mapping. >
    inoremap <silent> <script> <SID>MyMapping <C-\><C-O><SID>MyMapping
d) Normal mode mappings that consist of multiple Ex command lines (and where
   Ex commands cannot be concatenated via <Bar>) use <SID>NM instead of
   ':<C-U>'; the insert mode variant of <SID>NM re-enter command mode for one
   ex command line. >
    " Handle execution from insert mode via |i_CTRL-O|.
    nnoremap <silent> <SID>NM :<C-U>
    inoremap <silent> <SID>NM <C-\><C-O>:
    "nnoremap <silent> <script> <SID>MyMap1:<C-U>call MySecondCommand()<CR>
    nnoremap <silent> <script> <SID>MyMap1<SID>NMcall MySecondCommand()<CR>
e)  If none of the above is possible, at least force normal mode for
    subsequent commands via |CTRL-\_CTRL-N| to avoid accidental insertion of
    the remainder of the mapping. >
    nnoremap zC zC<C-\><C-N>VzCzz

If you rewrite your mapping as follows, it'll also work in temporary insert 
mode:
    :nnoremap <F9> :execute 'normal! 0'<Bar>echo getfontname()<CR>

-- regards, ingo

-- 
You received this message from the "vim_dev" 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

Raspunde prin e-mail lui