1

Vim functions do not seem to be able to fail without spitting out 3 unnecessary lines. For example, take this set of functions and maps:

" NEXT / PREVIOUS
" ~~~~~~~~~~~~~~~
" n and N always search for matched pattern forwards and backwards (resp.)
" no more guessing!
function! s:searchNext(forward)
    try
        execute 'normal! '.(a:forward ? 'n' : 'N')
    catch /^Vim\%((\a\+)\)\=:E486/
        echohl ErrorMsg
        echon "\rE486: Pattern not found: ".@/
        echohl None
        " echoerr 'E486: Pattern not found: '.@/
        return
    endtry
    normal! zv
    redraw
    echon (a:forward ? '/' : '?').@/
endfunction
nnoremap n :call <sid>searchNext(v:searchforward ? 1 : 0)<bar>set hlsearch<cr>
nnoremap N :call <sid>searchNext(v:searchforward ? 0 : 1)<bar>set hlsearch<cr>

Now try opening a file with this in it:

foo
bar keepme 1

foo
bar

foo
bar keepme 2

foo
bar

foo
bar

foo
keepme bar 3

foo
bar

1 | let @a = '' | let @/ = 'keep'.'me' | let @q = 'n"Ad2k'
keepme .. run on previous line: ^y$:<c-r>"<cr>  then press @q at least 5 times

on the 5th time, the macro does not fail on the "n", so the text that would not
be yanked into @a with norm! n IS cut/yanked into @a

Follow the instructions.


The issue is that :echon doesn't fail. So, I tried to return 0 and return 1. This doesn't seem to make the function fail. I tried replacing the 3 lines (echohl .., echon .., echohl ..) with echoerr .. (the commented out bit), but this takes up three lines with two error messages:

Error detected while processing function <SNR>24_searchNext:
line    7:
E486: Pattern not found: keepme

and that--to me--is just unacceptable.


So, is there:

  1. a way to make a function fail without :echoerr
    • it thouroughly annoys me that :echoerr throws an error itself .. ridiculous .. there should at least be an :echoerr that doesn't throw an error

or

  1. a way to :echoerr without throwing an error or some way to achieve something similar

or

  1. any other way of making the macro (or another nmap) fail when using a map that is bound to a function like this (restriction: you can only make it fail from within this function, itself).

Thanks,

1 Answer 1

1

...:echoerr without throwing an error or some way to achieve something similar?

Call it outside of a try block. Per :h :echoerr you're seeing this behavior...

When used inside a try conditional, the message is raised as an error exception


...make a function fail without :echoerr?

Use :throw?

Have you read all of the applicable Vim documentation? Besides the help for individual commands like :echoerr, :throw, :try, :catch, etc. there's this whole section: :h exception-handling.

8
  • :throw doesn't work. It prints 3 lines still like :echoerr. yes I read them
    – dylnmc
    Oct 5, 2018 at 23:43
  • I didn't say anything about 3 lines. The question just reads "is there a way to make a function fail without :echoerr?.
    – B Layer
    Oct 5, 2018 at 23:49
  • I'm not going to accept 3 lines of unnecessary crap every time I press n and pattern not found. That does technically answer one of the bulleted points, but I also made a big stink about not wanting 3 error lines. I can easily fix this one problem gitlab.com/dylnmc/vim-config/blob/master/plugin/search.vim#L104 but there should still be a way to make a function fail silently. thanks
    – dylnmc
    Oct 6, 2018 at 0:03
  • Relax, man. Don't let the "crap" get to you so much. lol. My advice would be to try to tighten up the question. It's kind of a lot of steps you're asking people to go through. You'll probably have better odds of getting additional input if you can distill things down...especially if you can reduce or eliminate the need to enter code in order to understand what you're looking for.
    – B Layer
    Oct 6, 2018 at 4:14
  • Oh, and you didn't mention my other answer. Did you try to "call echoerr outside a try block"? Finally, what is the link you pasted in? I didn't catch your drift there...a bunch of code...what am I supposed to look at.
    – B Layer
    Oct 6, 2018 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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