0

I am trying to modify this terrific VIM script however both the original and my modified version have a maddening bug in which sometimes the cursor is shown in the wrong place. The simplest example that I could make is the 71 line text file below. Note that whitespace is important when copying the 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 relevant .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 from the beginning of the file, press j repeatedly until you get to line 63. Note that the highlighted cursorline stays on the correct line (63) but the cursor is shown on line 55. Jumping directly to line 63 won't trigger the bug, only pressing j repeatedly until you get to that line will.

Why does that happen, and how can I fix it? Note that when the cursor appears to be in the wrong place, pressing ``z` does in fact snap the cursor to the correct location. This is on VIM 7.3.154 on Kubuntu 11.10.

EDIT: I notice by testing in other installs (Debian, CentOS) that the bug is not determinate, it happens occasionally but not in the same place on every system! You can test this code by pressing j and paying attention to the cursor location in whatever PHP files that you might have strung about. I would say that about one line out of every hundred lines triggers the bug in which the cursor appears to be in the wrong place.

2
  • Try placing comments at the relevant lines instead of using line numbers to refer to them. Line numbers don't show up in StackOverflow code.
    – user849425
    Mar 15, 2012 at 21:38
  • The comments make the bug disappear. That is why I mentioned set nu in the .vimrc file.
    – dotancohen
    Mar 15, 2012 at 21:57

1 Answer 1

3

I'm slightly confused by the logic of this function, but I suspect it is the ?\S which is causing the problems. It is searching backwards for a non-whitespace character, and wrapping around to the bottom of the file once it has reached the top.

Try replacing both occurrences of ?\S with

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

(Here the b flag searches backwards, and W prevents wrapping around the file.)

EDIT (2nd attempt)

The function also causes lots of jumping around of the view. The root of this is continually setting the mark mz and jumping to and fro. A better 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

You can then use these 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) just before the return commands. This ensures that the function doesn't modify the appearance of the window, and makes for more pleasant usage.

Hope this helps!

3
  • Thank you Prince Goulash. However changing the ?\S to call search('\S','bW') did not help. I do not believe that wrapping around to the end of the file was the issue, as there are most certainly non-whitespace characters available in the areas where the problems lie. But thank you for trying to help!
    – dotancohen
    Mar 15, 2012 at 22:32
  • I am not sure if I am reproducing the problem correctly, but I've had another shot at a solution. Mar 15, 2012 at 22:35
  • Thank you, your code works perfectly! I just threw it at a whole bunch of corner cases and it works fine. I was also uncomfortable using mz but that is how the code that I purloined worked and I didn't even know enough to ask if there is a better way! Your help and your willingness to teach are very much appreciated. Thank you!
    – dotancohen
    Mar 15, 2012 at 23:48

Your Answer

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

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