-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathvim-lastplace.vim
More file actions
90 lines (73 loc) · 2.64 KB
/
vim-lastplace.vim
File metadata and controls
90 lines (73 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
" ============================================================================
" File: vim-lastplace.vim
" Description: Reopen files where you left off. Configurable.
" Author: Gregory L. Dietsche <greg@gregd.org>
" Licence: MIT
" Website: https://www.gregd.org/
" Version: 4.6.0
" ============================================================================
if exists("b:loaded_lastplace_plugin") || &cp
finish
endif
let b:loaded_lastplace_plugin = 1
scriptencoding utf-8
if !exists('g:lastplace_ignore')
let g:lastplace_ignore = "gitcommit,gitrebase,hgcommit,svn,xxd"
endif
if !exists('g:lastplace_ignore_buftype')
let g:lastplace_ignore_buftype = "help,nofile,quickfix"
endif
if !exists('g:lastplace_open_folds')
let g:lastplace_open_folds = 1
endif
fu! s:lastplace_can_run()
if index(split(g:lastplace_ignore_buftype, ","), &buftype) != -1
return 0
endif
if index(split(g:lastplace_ignore, ","), &filetype) != -1
return 0
endif
try
"if the file does not exist on disk (a new, unsaved file) then do nothing
if empty(glob(@%))
return 0
endif
catch
return 0
endtry
return 1
endf
fu! s:lastplace_jump()"{{{
if !s:lastplace_can_run()
return
endif
if line("'\"") > 0 && line("'\"") <= line("$")
"if the last edit position is set and is less than the number of lines in this buffer.
if line("w$") == line("$")
"if the last line in the current buffer is also the last line visible in this window
execute "normal! g`\""
elseif line("$") - line("'\"") > ((line("w$") - line("w0")) / 2) - 1
"if we're not at the bottom of the file, center the cursor on the screen after we make the jump
execute "normal! g`\"zz"
else
"otherwise, show as much context as we can by jumping to the end of the file and then to the mark.
"if we pressed zz here, there would be blank lines at the bottom of the screen.
"we intentionally leave the last line blank by pressing <c-e> so the user can see that they are near the end of the file.
execute "keepjumps normal! \G`\"\<c-e>"
endif
endif
endf"}}}
fu! s:lastplace_open_folds()
if !s:lastplace_can_run()
return
endif
if foldclosed(".") != -1 && g:lastplace_open_folds
"if we're in a fold, make the current line visible and recenter screen
execute "normal! zvzz"
endif
endf
augroup lastplace_plugin
autocmd!
autocmd BufRead * call s:lastplace_jump()
autocmd BufWinEnter * call s:lastplace_open_folds()
augroup END