Why does this function skip on line 63?

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 /** * Some silly method * * @param string Some silly string */ 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.

0
source share
1 answer

I am a bit confused by the logic of this function, but I suspect that this is a problem ?\S It searches backward for a character with no spaces and wraps it around the bottom of the file when it reaches the top.

Try replacing both occurrences ?\S with

 call search('\S','bW') 

(Here, the b flag looks back, and W prevents wrapping around the file.)

EDIT (second attempt)

The function also causes many jumps around the view. The root of this constantly sets the mz mark and jumps to and fro. The best approach in vimscripts is to use the following commands to save the current view (instead of normal mz ):

 let pos=getpos(".") " This saves the cursor position let view=winsaveview() " This saves the window view 

Then you can use them to restore the view:

 call cursor(pos) " This restores the cursor position to that of "pos" call winrestview(view) " This restores the window view to that of "view" 

So, I would use call cursor(pos) instead of `z and call winrestview(view) immediately before return commands. This ensures that the function does not change the appearance of the window and makes use more pleasant.

Hope this helps!

+3
source

Source: https://habr.com/ru/post/1398499/


All Articles