Thursday, September 29, 2011

Re: Insert a tab or call a function

On 09/29/2011 08:15 AM, Christian Brabandt wrote:
> On Thu, September 29, 2011 1:52 pm, AK wrote:
>> On 09/29/2011 01:58 AM, Benjamin R. Haskell wrote:
>>> On Wed, 28 Sep 2011, AK wrote:
>>>> Hi, I'm trying to write a small function that is called when I press
>>>> tab key and checks if current line is empty; if it is, it should
>>>> insert a tab, and if not, it should call two other functions. Here's
>>>> the example of what I came up with, which does not work right:
>>>
>>> Wanting to either insert something or run another function is a good
>>> case for<expr> mappings. It depends a bit on how TriggerSnippet works,
>>> the following might be all you need:
>>>
>>> fun! DoTab()
>>> if getline('.') =~ '^\s*$'
>>> return "\<tab>"
>>> else
>>> call CloseMenu()
>>> return TriggerSnippet()
>>> endif
>>> endfun
>>> ino<expr> <tab> DoTab()
>>
>> I get 'E523: not allowed here' in TriggerSnippet. I tried :set secure ,
>> but that did not help.
>
> I think what happens is, that one of your functions triggers an
> action, that is not allowed inside an expr-mapping. If you need to do
> something like this, you need to have the functions return the
> commands, that will do that.
>
> See :h :map-<expr>
> and look at the third paragraph.
>
> regards,
> Christian
>


Ok, I think I'm getting there: I've added the check to the
top of TriggerSnippet:

if getline('.') =~ '^\s*$'
" return "\<tab>"
call feedkeys("\<tab>", 'n') | return ''
endif

Now the issue is only that when I go to a new line by pressing
enter, first tab I press is 'eaten'. Nothing happens at all.
Subsequent tabs work fine. If I press 'a' key at the end of
an existing empty line, tab also works fine, i.e. the first
tab does not get 'eaten'.

The same behaviour occurs if I uncomment the 2nd line in
code I pasted.

If I understand right, when a new line is created, it's indent
is 'virtual', and that seems to cause this problem. I still
don't understand why, though, since tab should not be affected
by indent being virtual or not.. I may be completely off track
here..

-ak

--
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:

Post a Comment