/
200-session.vim
135 lines (124 loc) · 4.67 KB
/
200-session.vim
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
UsePlugin 'vim-session'
UsePlugin 'vim-misc'
" gitリポジトリ配下ならgitのrootを返す。そうでないなら空を返す
function! s:GetGitDir()
" Set 'gitdir' to be the folder containing .git
let gitdir = system("git rev-parse --show-toplevel")
" See if the command output starts with 'fatal' (if it does, not in a git repo)
let isnotgitdir=matchstr(gitdir, '^fatal:.*')
" If it empty, there was no error. Let's cd
if empty(isnotgitdir)
return substitute(gitdir, '\n', '', 'g')
else
return ""
endif
endfunction
function! s:GetGitCurrentBranch()
" Set 'gitdir' to be the folder containing .git
let branch = system("git rev-parse --abbrev-ref HEAD")
" See if the command output starts with 'fatal' (if it does, not in a git repo)
let isnotgitdir=matchstr(branch, '^fatal:.*')
" If it empty, there was no error. Let's cd
if empty(isnotgitdir)
return substitute(branch, '\n', '', 'g')
else
return ""
endif
endfunction
"""" 以下の順番でsessionディレクトリを探索
"""" 1. 現在のディレクトリ直下の .vimsessions フォルダ
"""" 2. gitリポジトリ配下ならリポジトリroot直下の .vimsessionsフォルダ
"""" 3. ~/.config/nvim/default-sessions (共通)
" https://www.g104robo.com/entry/vim-session
" 現在のディレクトリ直下の .vimsessions/ を取得
let s:local_session_directory = xolox#misc#path#merge(getcwd(), '.vimsessions')
if isdirectory(s:local_session_directory)
" 存在すれば
" session保存ディレクトリをそのディレクトリの設定
let g:session_directory = s:local_session_directory
" vimを辞める時に自動保存
let g:session_autosave = 'yes'
" 引数なしでvimを起動した時にsession保存ディレクトリのdefault.vimを開かない
let g:session_autoload = 'no'
" 1分間に1回自動保存
let g:session_autosave_periodic = 1
let g:session_autosave_silent = 1
else
" 存在しなければ
" 現在のリポジトリroot配下の .vimsessions/ を取得
let s:git_dir = s:GetGitDir()
if strlen(s:git_dir) > 0
" gitリポジトリ配下なら
let s:local_session_directory = xolox#misc#path#merge(s:git_dir, '.vimsessions')
" 存在しなければディレクトリ作成する
if !isdirectory(s:local_session_directory)
call mkdir(s:local_session_directory)
endif
" session保存ディレクトリをそのディレクトリの設定
let g:session_directory = s:local_session_directory
" vimを辞める時に自動保存
let g:session_autosave = 'yes'
" 引数なしでvimを起動した時にsession保存ディレクトリのdefault.vimを開かない
let g:session_autoload = 'no'
" 1分間に1回自動保存
let g:session_autosave_periodic = 1
let g:session_autosave_silent = 1
else
" neovimのディレクトリにsession作成
let s:local_session_directory = expand("~/.config/nvim/default-sessions")
" 存在しなければディレクトリ作成する
if !isdirectory(s:local_session_directory)
call mkdir(s:local_session_directory)
endif
" デフォルトの保存ディレクトリを変更する
let g:session_directory = s:local_session_directory
" vimを辞める時に自動保存しない
let g:session_autosave = 'no'
" 引数なしでvimを起動した時にsession保存ディレクトリのdefault.vimを開かない
let g:session_autoload = 'no'
endif
unlet s:git_dir
endif
unlet s:local_session_directory
" セッション保存と復帰のコマンド
" https://qiita.com/ToruIwashita/items/8c5039655ab73ea9261e
function! s:save_session(bang, ...)
if a:0
let session_name = a:1
else
let session_name = s:GetGitCurrentBranch()
end
if strlen(session_name)
execute 'SaveSession' . ToBang(a:bang) . ' ' . session_name . '.branch'
else
echo 'not a git repository'
endif
endfunction
function! s:load_session(bang, ...)
if a:0
let session_name = a:1
else
let session_name = s:GetGitCurrentBranch()
end
if strlen(session_name)
execute 'OpenSession' . ToBang(a:bang) . ' ' . session_name . '.branch'
else
echo 'no target session found'
endif
endfunction
command! -nargs=? -bang SaveBranchSession call s:save_session(<bang>v:false, <f-args>)
command! -nargs=? -bang LoadBranchSession call s:load_session(<bang>v:false, <f-args>)
" for ale
function! MySessionName() abort
let l:session_name = xolox#session#find_current_session()
if empty(l:session_name)
return ''
else
if strridx(l:session_name, '.branch')
let l:session_display_name = ''
else
let l:session_display_name = l:session_name
endif
return '' . l:session_display_name
endif
endfunction