Sunday, February 8, 2015

vimscripting problem w/tabs and expanding width w/numbering option

Ages ago I add a few lines to my .gvimrc file to auto-resize
the width if I turned on line numbering (i.e. since the numbers take
up more width and I still wanted an 80-col display for the contents),
I wanted to expand the width by the # cols needed.
I added a simple function to my .gvimrc:

func! SetNumberAndWidth()
  set number
  let &columns += &numberwidth
endf
au BufReadPost *   let ln = 1+line("'\"") | if search("vim=:SetNumberAndWidth",'n') | call SetNumberAndWidth() | endif

And put this at the top of my source files (with lang appropriate
comment char):

// gvim=:noSetNumberAndWidth   //rm 'no' to activate in gvim, '=:' deliberate

I activated by changing it to:

// gvim=:SetNumberAndWidth

(used =: so no chance of 'vim' thinking it was a standard vim option line),

That's worked fine until I started using tabs for paired files
(source+headers, ex "foo.{cc,h}").  1st file is always coming
up too wide (by #  characters in lineNR) due to the width
getting a double-resize because there were 2 tabs being brought
up at once.

The 1st tab brought up (looks like) (this is all one long line:)
 9 /********#*********#*********#*********#*********#*********#*********#*********/|    ||

The last '/' in the line is in column 80.  It's followed by
an inverse video vertical bar, followed by about 5 spaces (and the '||' represents
the side of my screen (not chars on the screen).

------------------------------------------------------------------------

After tolerating this for several months, I decided to try to take a stab
fixing this, changing my .gvimrc code to handle this:

func! SetNumberAndWidth()
    set number
    if (! exists("g:added_numwth")) | let g:added_numwth=0 | endif
    if (g:added_numwth < &numberwidth)
        let g:added_numwth = &numberwidth
        let &columns = 2 + g:added_numwth + &columns
    endif
endf
au BufReadPost * let ln = line("'\"") | if search("vim=:SetNumberAndWidth",'n') | call SetNumberAndWidth() | endif

-----
Now the 2nd tab (initially not displayed) is 5 too NARROW!!

looking like:
 6
//########//########//########//########//########//########//########//##|     ||
######

    I still have the vertical bar drawn about 5 chars in from the right
of the screen, but it is wrapping at column 74.

    So I'm having problems getting them "synchronized".

    NOTE -- by my *primitive* understanding of vimscript, the 'g:' in front
of a var is supposed to mean it is 'global', so "g:added_numwth" ?should?
be global to both buffers. 

    It sorta looks like both tabs are being brought up at the same time
and a race condition might be happening, but the fact that it
is very deterministic, leads me to believe something else is going on.

    So when tab 1 comes up, it should resize the screen with '&columns' to
be a bit wider, and when tab 2 comes up, it should see the added_numwth has
already been set to correspond to a 3-column numberwidth + the explicit 2
(for the FoldDisplayColumn+pad) and leave it alone -- yet no luck!.


    I thought g: vars were 'global across all buffs, but this seems
to be behaving like g:added_numwth (*or*) &columns are not in-sync
between the tabs?  Is that what is happening or am I missing some
simpler explanation?


Thanks again!
Linda




No comments: