kevin
kevin

Reputation: 785

the function doesn't work correctly

Today, I upgraded my Vim from 7.3 to 7.4. However, the below function, CleverTab(), doesn’t seem to work. (I am not sure if the matter is the update.)

function! CleverTab()
    if strpart( getline('.'), 0, col('.')-1 ) =~ '^\s*$'
        return "\<Tab>"
    endif
    if pumvisible()
        return "\<C-N>"
    endif
    return "hello"
endfunction

inoremap <silent><tab> <C-R>=CleverTab()<CR>

The purpose of this function is to do something different depending on the current condition when Tab is pressed:

  1. If the current line is filled with space, then just add a tab.
  2. If a popup menu appears, move down.
  3. Otherwise, add the string “hello”.

(To be honest, what I want to do in scenario 3 is to call another function. But whatever — to make the problem easy, I’m just trying to show a string in that case.)

Suppose I’m editing my file via Vim and have typed the string “123” (for instance). At the moment, when I press Tab, the actual result is that a <tab> is added. The expected result is that the flow should go to condition 3 and add the string “hello”. I have spent this afternoon on it.

So, can anyone help me to figure it out? Appreciated!

Upvotes: 1

Views: 283

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172768

Your CleverTab() function is global in scope. If another plugin defines the same function, the former will be overridden. To avoid such conflicts, global functions are to be avoided. Rather, use script-local functions s:CleverTab(), to be invoked from a mapping via <SID>CleverTab(), or autoload functions myplugin#CleverTab().

If the issue is that another plugin overrides your <Tab> mapping, you have to choose another mapping key, or decide for one or the other. Only when your function is an extension of the original (and provides compatible return types), you can invoke the other function from inside yours (provided you've used different function names, as described in my first paragraph).

Upvotes: 2

Related Questions