Skip to content

Commit 1ed0e7e

Browse files
committed
Support project-local viminfo
1 parent 20fe2d0 commit 1ed0e7e

File tree

2 files changed

+70
-17
lines changed

2 files changed

+70
-17
lines changed

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ let g:vim_project_config = {
402402
\'config_home': '~/.vim/vim-project-config',
403403
\'project_base': ['~'],
404404
\'use_session': 0,
405+
\'use_viminfo': 0,
405406
\'open_root_when_use_session': 0,
406407
\'check_branch_when_use_session': 0,
407408
\'project_root': './',
@@ -483,7 +484,8 @@ let g:vim_project_config.list_mappings_git_branch = {
483484
|-------------------------------|-------------------------------------------------------------------------------|
484485
| config_home | The directory for all config files |
485486
| project_base | A list of base directories used for path in `:Project path` |
486-
| use_session | Read and write separate session for each project |
487+
| use_session | Save and load project-local session |
488+
| use_viminfo | Save and load project-local viminfo (or shada for neovim) |
487489
| open_root_when_use_session | When session used, always open project root at the beginning |
488490
| check_branch_when_use_session | When session used, keep one for each branch |
489491
| project_root | Starting directory or file when fist launching project |
@@ -521,20 +523,25 @@ The config files are located at `~/.vim/vim-project-config/`, where `~/.vim/vim-
521523
| sessions/ " session for each branch when use_session is '1'
522524
| master.vim
523525
| dev.vim
526+
| viminfo/ " viminfo file when use_viminfo is '1'
527+
| main.shada " for neovim
528+
| .viminfo " for vim
524529
525530
```
526531

527532
Open
528533

529-
- Load session
530-
- Source project's `init.vim`
534+
- Load project viminfo (when enabled)
535+
- Load project session (when enabled)
536+
- Source project `init.vim`
531537

532538
Quit
533539

534-
- Save session
535-
- Source project's `quit.vim`
540+
- Save project viminfo (when enabled)
541+
- Save project session (when enabled)
542+
- Source project `quit.vim`
536543

537-
`init.vim` will get reloaded once you change and save it.
544+
`init.vim` will get automatically reloaded after you change it.
538545

539546
### Project local config
540547

autoload/project.vim

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function! s:Prepare()
3939
\'config_home': '~/.vim/vim-project-config',
4040
\'project_base': ['~'],
4141
\'use_session': 0,
42+
\'use_viminfo': 0,
4243
\'open_root_when_use_session': 0,
4344
\'check_branch_when_use_session': 0,
4445
\'project_root': './',
@@ -76,6 +77,7 @@ function! s:Prepare()
7677
\'file_mappings',
7778
\'tasks',
7879
\'use_session',
80+
\'use_viminfo',
7981
\'open_root_when_use_session',
8082
\'check_branch_when_use_session',
8183
\'commit_message',
@@ -209,6 +211,7 @@ function! s:InitConfig()
209211
let s:open_root_when_use_session = s:config.open_root_when_use_session
210212
let s:check_branch_when_use_session = s:config.check_branch_when_use_session
211213
let s:use_session = s:config.use_session
214+
let s:use_viminfo = s:config.use_viminfo
212215
let s:project_root = s:config.project_root
213216
let s:project_base = s:RemoveListTrailingSlash(s:config.project_base)
214217
let s:include = s:config.include
@@ -550,6 +553,7 @@ function! s:InitProjectConfig(project)
550553
\'" \],',
551554
\'" \''project_root'': ''./'',',
552555
\'" \''use_session'': 0,',
556+
\'" \''use_viminfo'': 0,',
553557
\'" \''open_root_when_use_session'': 0,',
554558
\'" \''check_branch_when_use_session'': 0,',
555559
\'" \}',
@@ -1833,6 +1837,7 @@ function! s:LoadProject()
18331837
call s:SourceInitFile()
18341838
call s:WatchOnInitFileChange()
18351839
call s:FindBranch()
1840+
call s:LoadViminfo()
18361841
call s:LoadSession()
18371842
endfunction
18381843

@@ -1941,6 +1946,7 @@ endfunction
19411946
function! s:QuitProject()
19421947
if project#ProjectExist()
19431948
call s:Info('Quitted ['.s:project.name.']')
1949+
call s:SaveViminfo()
19441950
call s:SaveSession()
19451951
call s:SourceQuitFile()
19461952
call s:UnwatchOnInitFileChange()
@@ -2192,24 +2198,64 @@ function! s:FindBranch()
21922198
endfunction
21932199

21942200
function! s:GetSessionFolder()
2195-
if project#ProjectExist()
2196-
let config = s:GetProjectConfigPath(s:config_home, s:project)
2197-
return config.'/sessions'
2198-
else
2199-
return ''
2201+
let config = s:GetProjectConfigPath(s:config_home, s:project)
2202+
return config.'/sessions'
2203+
endfunction
2204+
2205+
function! s:GetSessionFile()
2206+
let config = s:GetProjectConfigPath(s:config_home, s:project)
2207+
return config.'/sessions/'.s:branch.'.vim'
2208+
endfunction
2209+
2210+
function! s:LoadViminfo()
2211+
if !s:use_viminfo
2212+
return
2213+
endif
2214+
2215+
let file = s:GetViminfoFile()
2216+
if filereadable(file)
2217+
call s:Debug('Load viminfo file: '.file)
2218+
if has('nvim')
2219+
execute 'rshada! '.file
2220+
else
2221+
execute 'rviminfo! '.file
2222+
endif
22002223
endif
22012224
endfunction
22022225

2226+
function! s:SaveViminfo()
2227+
if !s:use_viminfo
2228+
return
2229+
endif
22032230

2204-
function! s:GetSessionFile()
2205-
if project#ProjectExist()
2206-
let config = s:GetProjectConfigPath(s:config_home, s:project)
2207-
return config.'/sessions/'.s:branch.'.vim'
2231+
let folder = s:GetViminfoFolder()
2232+
if !isdirectory(folder) && exists('*mkdir')
2233+
call mkdir(folder, 'p')
2234+
endif
2235+
2236+
let file = s:GetViminfoFile()
2237+
call s:Debug('Save viminfo to: '.file)
2238+
if has('nvim')
2239+
execute 'wshada! '.file
22082240
else
2209-
return ''
2241+
execute 'wviminfo! '.file
22102242
endif
22112243
endfunction
22122244

2245+
function! s:GetViminfoFile()
2246+
let config = s:GetProjectConfigPath(s:config_home, s:project)
2247+
if has('nvim')
2248+
return config.'/viminfo/main.shada'
2249+
else
2250+
return config.'/viminfo/.viminfo'
2251+
endif
2252+
endfunction
2253+
2254+
function! s:GetViminfoFolder()
2255+
let config = s:GetProjectConfigPath(s:config_home, s:project)
2256+
return config.'/viminfo'
2257+
endfunction
2258+
22132259
function! s:LoadSession()
22142260
if !s:use_session
22152261
return
@@ -2220,7 +2266,7 @@ function! s:LoadSession()
22202266
call s:Debug('Load session file: '.file)
22212267
execute 'source '.file
22222268
else
2223-
call s:Debug('Not session file found: '.file)
2269+
call s:Debug('No session file found: '.file)
22242270
endif
22252271
endfunction
22262272

0 commit comments

Comments
 (0)