1

I want to make a custom search, accessible with operator-pending commands; I use onoremap to make it work in this mode.

It's working well, and I can use my command (let's say it's mapped to zz) with czz, yzz...

But when the function called by the mapping does not match anything, I would like to make it react exactly like when I type the following normal mode command:

c/never\&match<cr>

E486: Pattern not found: never\&match

An error is gently displayed, and it goes back to normal mode, because an error happened.

For now, when my custom search fails with the example czz command, it enters into insert mode, as if the search was successful. That's my problem:

How can I cancel the czz command and go back to normal mode, when the search inside the zz mapping fails?

To solve it I tried to put the following in the right place, but in vain: exe "normal! \<esc>"

Here is an example code:

" Create the o-mapping:
onoremap zz :<c-u>call MyQTest()<cr>

function! MyQTest()
    " Just below, performs a custom search that will set two 
    " vars, 'l:start' and 'l:end', the bounds of the area to
    " work with:

    " [ ... ]

    " Finally we get some values, for example:
    " let l:start = 5
    " let l:end = 10

    " But if the search fails, instead of the two lines above,
    " the values could be:
    let l:start = -1
    let l:end = -1

    if l:start == -1 || l:end == -1
        " HERE is the problem:
        " If the search fails, how can I completely cancel the operator-pending 
        " command and return to normal mode, instead of start editing (with
        " the 'c' normal command) ?
        return
    endif

    " Make a selection, for operator-pending mode:
    call cursor(0, l:start)
    normal v
    call cursor(0, l:end)
endf
  • 1
    You could throw an error or add an echoerr. But it won't be as "nice" as your c/... example, because it will lead to two error messages and so will require a press enter to continue. – Karl Yngve Lervåg Oct 15 '16 at 6:55
  • 2
    Try return feedkeys("\e") that seems to work from my little test here. – Christian Brabandt Oct 15 '16 at 7:54
1

It works by using:

call feedkeys("\e")

even if an error message is hard to display in all the cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.