Saturday, November 15, 2014

Re: How to scroll then click in inactive window without jumping back?

On 11/15/2014 04:23 AM, Tony Mechelynck wrote:
> On 15/11/14 00:18, Michael Henry wrote:
>> Is there any way to prevent Gvim from scrolling the inactive
>> window back to the original position before accepting the click?
>
> What does Vim answer to
> :verbose set mouse? winheight? winminheight? winwidth?
winminwidth? equalalways?

The output from the above command is:

mouse=a
winheight=1
winminheight=1
winwidth=20
winminwidth=1
equalalways

These are all the defaults. But the underlying implication of
your question is that the behavior I've been seeing isn't
typical, which somehow hadn't occurred to me. I wish I'd
thought of it that way earlier, but thanks to your answer I
tried bypassing my configuration to see how Gvim behaves
out-of-the-box::

gvim -u NONE '+set nocp'

Sure enough, something in my configuration was causing the
problem, since stock Gvim doesn't have the behavior I'd been
seeing. After a bit of looking around, I determined the cause
was due to a work-around I'd added long ago based on this Vim
tip:
http://vim.wikia.com/wiki/Avoid_scrolling_when_switch_buffers

The tip suggests saving and restoring a buffer's view to work
around the way Vim likes to center the current cursor position
in the window when switching buffers. In my vimrc, I'd had the
following::

" Save current view settings.
function! AutoSaveWinView()
let b:winview = winsaveview()
endfunction

" Restore current view settings.
function! AutoRestoreWinView()
if exists('b:winview')
if !&diff
call winrestview(b:winview)
endif
unlet b:winview
endif
endfunction

autocmd BufLeave * call AutoSaveWinView()
autocmd BufEnter * call AutoRestoreWinView()

The BufEnter event causes AutoRestoreWinView() to restore the
cursor position of the inactive window, overriding the new
position indicated by the mouse click.

It took a while to figure out how to correct the mouse problem
without losing the work-around for Vim's scrolling behavior. It
turns out that when switching buffers within one window, Vim
first sets the cursor position to the start of the file, then
fires a BufEnter event; afterward, if no autocmd has changed the
cursor position, Vim restores the cursor position based on
values saved in its internal data structures. In contrast, when
switching to another window the cursor position is not forced to
the start of the file; this case includes the "click in the
inactive window" case I was asking about. So I've reworked the
logic to restore the view settings only when the cursor is at
start-of-file. In addition, I'm now saving the view settings on
a per-buffer, per-window basis, which allows independent cursor
positions when using two windows to edit the same buffer::

" Save current view settings on a per-window, per-buffer basis.
function! AutoSaveWinView()
if !exists("w:SavedBufView")
let w:SavedBufView = {}
endif
let w:SavedBufView[bufnr("%")] = winsaveview()
endfunction

" Restore current view settings.
function! AutoRestoreWinView()
let buf = bufnr("%")
if exists("w:SavedBufView") && has_key(w:SavedBufView, buf)
let v = winsaveview()
let atStartOfFile = v.lnum == 1 && v.col == 0
if atStartOfFile && !&diff
call winrestview(w:SavedBufView[buf])
endif
unlet w:SavedBufView[buf]
endif
endfunction

Thanks for your help,
Michael Henry

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: