Wednesday, March 31, 2010

Re: How to remember end of line when quitting/reopening in insert mode

" Vim plugin -- last-position-jump improved (esp. for Easy Vim)
" File: lastpos.vim
" Created: 2010 Mar 24
" Last Change: 2010 Mar 31
" Rev Days: 3
" Author: Andy Wokula <anwoku@yahoo.de>
"
" TODO
" + how can we be sure that <C-R>=... will be executed in Insert mode?
" ! use intermediate <Plug>LastPos
" + why is :normal! g`" wrapped with :exec?
" ! once the code was a one-liner
" + use this assumption:
" if not present, Vim will initialize the '"'-mark with [0,1,1,0]
" :au BufReadPost * echomsg string(getpos("'\""))
" + don't move the cursor if another plugin already moved it
" + don't move the cursor for special buffers (buftype not empty) [1]

" [1] added, although jumping to help tags seems to work without this
" guard

map <Plug>LastPos <Nop>
cmap <Plug>LastPos <Nop>
ino <silent> <Plug>LastPos <C-R>=''[setpos('.',getpos("'\""))]<CR>

augroup LastPos
au! BufReadPost * call s:LastPos(&insertmode)
augroup END

func! s:LastPos(mode)
if a:mode == 2
au! LastPos InsertEnter
call feedkeys("\<Plug>LastPos")
return
endif
let m = "'\""
if getpos(m) != [0,1,1,0] && line(m) <= line("$")
\ && getpos(".") == [0,1,1,0]
\ && &buftype == ""
if a:mode == 1
au LastPos InsertEnter * call s:LastPos(2)
else
normal! g`"
endif
endif
endfunc

" vim:et tw=72:
Am 31.03.2010 16:42, schrieb Jean Johner:
> On Mar 31, 1:01 am, Tony Mechelynck<antoine.mechely...@gmail.com>
> wrote:
>
>> so Christian's observations leave
>> me baffled.
>
> Please find below a reproduceable situation where help side-effects of
> Andy's code appear:
>
> Use vimrc_example.vim completed with Andy's code as .vimrc
> Go to home directory
> rm .viminfo (DON'T FORGET)
> gvim file1
> :set insertmode
> CTRL-O :h zR
>
> Result: the help window opens at the beginning of fold.txt (not the
> correct place)
>
> If :set insertmode is not typed, the problem does not occur.
>
> Do you reproduce that?

Yes. Vim first jumps to the tag, then lastpos jumps to `" which is
initialized with (line 1, col 1).

I added more guards.

Installation (suggestion only):
- disable existing last-position-jump code in the vimrc
- put the attached file in the plugin folder

--
Andy

--
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

To unsubscribe, reply using "remove me" as the subject.

No comments:

Post a Comment