I am trying to modify this awesome VIM script , however both the original and my modified versions have a crazy bug in which sometimes the cursor is displayed in the wrong place. The simplest example I could do is text-to-text file 71 below. Note that spaces are important when copying a file.
<?php function someFunction() { global $class, $cv, $go, $pae; global $messages, $counters, $ltn; global $sh, $sub, $temp; $charsets = array( 'us', 'si', 'pr', 'op', 'co', 'pa', 'av', 'pr', 'al', 'pc', 'pe', 'pi', 'pp', 'su', 'qu', 'de', 'ze', 'xo', 'mo', 'wo', 'de', 'mo', 'de', 'mo', 'dr', 'mo', 'de', 'mo', 'ev', 'pa', 'so', 'ms', 'bu', 'at', 'cu', 'pr', 'de', 'mo', 'nv', 'nl', 'nf', 'ne', 'nq', 'nt' ); }
This is the corresponding .vimrc file with the function:
set cul hi CursorLine term=none cterm=none ctermbg=20 set nu set statusline+=%{WhatFunctionAreWeIn()} set laststatus=2 fun WhatFunctionAreWeIn() let strList = ["while", "foreach", "ifelse", "if else", "for", "if", "else", "try", "catch", "case"] let foundcontrol = 1 let position = "" normal mz while (foundcontrol) let foundcontrol = 0 " The idea here is to go back to non-whitespace character before " the last hanging open { and to check if it is a close paran. " If so, then go to the matching open paren and search for the " preceding it. " If not, then go ahead and check the keyword right there. normal [{ ?\S let tempchar = getline(".")[col(".") - 1] if (match(tempchar, ")") >=0 ) normal % ?\S endif let tempstring = getline(".") for item in strList if( match(tempstring,item) >= 0 ) let position = item . " - " . position let foundcontrol = 1 break endif endfor if(foundcontrol == 0) normal `z return tempstring.position endif endwhile normal `z return tempstring.position endfun
Starting at the beginning of the file, press j several times until you reach line 63. Note that the highlighted cursor line remains on the correct line (63), but the cursor appears on line 55. Direct jump to line 63 will not cause an error, only press j several times until you reach this line.
Why is this happening, and how can I fix it? Note that when the cursor is in the wrong place, pressing z actually binds the cursor to the correct location. This is on VIM 7.3.154 on Kubuntu 11.10.
EDIT: I noticed while testing in other installations (Debian, CentOS) that the error was not defined, sometimes it happens, but not in one place on every system! You can test this code by pressing j and paying attention to the cursor location in any PHP files that you could overlay. I would say that approximately one line out of every hundred lines causes an error in which the cursor is in the wrong place.