2 " Author: Jason Heddings (vim at heddway dot com)
4 " Last Modified: 20 October, 2005
6 if exists('g:SessionMgr_Loaded')
9 let g:SessionMgr_Loaded = 1
12 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
13 " used for my debugging - adapted from the taglist plugin
14 let g:SessionMgr_DebugLog = ""
15 if !exists("g:SessionMgr_Debug")
16 let g:SessionMgr_Debug = 0
19 command! -nargs=0 SMLog call confirm("__SessionMgr Log__\n\n" . g:SessionMgr_DebugLog)
20 function! SessionMgr_Debug(msg)
22 let l:len = strlen(g:SessionMgr_DebugLog)
24 let g:SessionMgr_DebugLog = strpart(g:SessionMgr_DebugLog, l:len - 4096)
26 let l:msg = strftime('%H:%M:%S') . ': ' . a:msg
27 let g:SessionMgr_DebugLog = g:SessionMgr_DebugLog . l:msg . "\n"
30 call SessionMgr_Debug("BEGIN LOADING")
33 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
34 " set the default options for the plugin
35 if !exists("g:SessionMgr_AutoManage")
36 let g:SessionMgr_AutoManage = 1
38 call SessionMgr_Debug("_AutoManage: " . g:SessionMgr_AutoManage)
40 if !exists("g:SessionMgr_DefaultSession")
41 let g:SessionMgr_DefaultSession = "session"
43 call SessionMgr_Debug("_DefaultSession: " . g:SessionMgr_DefaultSession)
45 if !exists("g:SessionMgr_Dir") || !isdirectory(g:SessionMgr_Dir)
46 " try to use the first instance of "sessions" in runtime path
47 let dirsearch = globpath(&runtimepath, "sessions/")
48 if strlen(dirsearch) > 0
49 let g:SessionMgr_Dir = substitute(dirsearch, "\n.*", "", "g")
51 let g:SessionMgr_Dir = "."
55 call SessionMgr_Debug("_Dir: " . g:SessionMgr_Dir)
58 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
59 " used to update the tag status
60 if g:SessionMgr_AutoManage
61 augroup SessionMgr_AutoManage
63 autocmd VimEnter * call SessionMgr_OnEnter()
64 autocmd VimLeavePre * call SessionMgr_OnLeave()
69 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
70 " creates a new session with an optional name
71 command! -nargs=? -complete=custom,SessionMgr_Complete SS call SessionMgr_Save(<f-args>)
72 function! SessionMgr_Save(...)
76 let l:name = SessionMgr_GetCurrentSession()
78 let l:session = g:SessionMgr_Dir . "/" . l:name . ".vim"
79 let g:SessionMgr_CurrentSession = l:name
80 call SessionMgr_Debug("Save(" . l:name . "): " . l:session)
82 silent! execute "mksession! " . l:session
83 echohl ModeMsg | echo "Session Saved: " . l:name | echohl None
86 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
87 " loads a previously saved session, or the most recent session if not specified
88 command! -nargs=? -complete=custom,SessionMgr_Complete SR call SessionMgr_Restore(<f-args>)
89 function! SessionMgr_Restore(...)
93 let l:name = SessionMgr_GetCurrentSession()
95 let l:session = g:SessionMgr_Dir . "/" . l:name . ".vim"
96 call SessionMgr_Debug("Restore(" . l:name . "): " . l:session)
98 if filereadable(l:session)
99 silent! execute "source " . l:session
100 echohl ModeMsg | echo "Session Restored: " . l:name | echohl None
101 let g:SessionMgr_CurrentSession = l:name
105 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
106 " deletes the specified session (or the active session)
107 command! -nargs=? -complete=custom,SessionMgr_Complete SD call SessionMgr_Delete(<f-args>)
108 function! SessionMgr_Delete(...)
109 " use only either the specified name or active session (not the default name)
113 if exists("g:SessionMgr_CurrentSession")
114 let l:name = g:SessionMgr_CurrentSession
116 call SessionMgr_Debug("Delete(): NO SESSION")
117 echohl ErrorMsg | echo "No Active Session" | echohl None
121 call SessionMgr_Debug("Delete(" . l:name . ")")
123 " end the session if it is the current one
124 if exists("g:SessionMgr_CurrentSession") && l:name == g:SessionMgr_CurrentSession
128 " go ahead and delete it
129 call delete(g:SessionMgr_Dir . "/" . l:name . ".vim")
130 echohl ModeMsg | echo "Session Deleted: " . l:name | echohl None
133 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
134 " quit recording the current session
135 command! -nargs=0 SQ call SessionMgr_Quit()
136 function! SessionMgr_Quit()
137 call SessionMgr_Debug("Quit()")
139 if exists("g:SessionMgr_CurrentSession")
140 call SessionMgr_Debug("Quit(): " . g:SessionMgr_CurrentSession)
141 unlet g:SessionMgr_CurrentSession
142 echohl ModeMsg | echo "Session Ended: " . g:SessionMgr_CurrentSession | echohl None
144 call SessionMgr_Debug("Quit(): NO SESSION")
145 echohl ErrorMsg | echo "No Active Session" | echohl None
149 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
150 " prints a list of all stored sessions
151 command! -nargs=0 SL call SessionMgr_List()
152 function! SessionMgr_List()
153 let l:sessions = SessionMgr_GetSessions()
154 if strlen(l:sessions) == 0
155 call SessionMgr_Debug("List(): NO SESSIONS")
156 echohl ErrorMsg | echo "No Sessions" | echohl None
158 call SessionMgr_Debug("List()")
159 echohl ModeMsg | echo "__Sessions__" | echohl None
160 echohl SpecialKey | echo l:sessions | echohl None
164 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
165 " returns the names of all available sessions (in session directory)
166 function! SessionMgr_GetSessions()
167 if !isdirectory(g:SessionMgr_Dir)
171 let l:curdir = escape(getcwd(), "\" ()")
172 execute "cd " . g:SessionMgr_Dir
173 let l:dir = glob("*.vim")
174 let l:sessions = substitute(l:dir, "\\.vim", "", "g")
175 let l:sessions = substitute(l:sessions, "\n$", "", "g")
176 execute "cd " . l:curdir
180 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
181 " toggles the available sessions window
182 command! -nargs=0 SE call SessionMgr_Echo()
183 function! SessionMgr_Echo()
184 if exists("g:SessionMgr_CurrentSession")
185 echohl ModeMsg | echo "Current Session: " . SessionMgr_GetCurrentSession() | echohl None
187 echohl ErrorMsg | echo "No Active Session" | echohl None
191 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
192 " returns the name of the current session, or the default
193 function! SessionMgr_GetCurrentSession()
194 if exists("g:SessionMgr_CurrentSession")
195 return g:SessionMgr_CurrentSession
197 return g:SessionMgr_DefaultSession
200 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
201 " used for auto-completing session names
202 function! SessionMgr_Complete(ArgLead, CmdLine, CursorPos)
203 let l:sessions = SessionMgr_GetSessions()
207 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
208 " called when vim starts up
209 function! SessionMgr_OnEnter()
210 call SessionMgr_Debug("OnEnter [" . argc() . "]")
213 execute "SR " . SessionMgr_GetCurrentSession()
214 let g:SessionMgr_CurrentSession = SessionMgr_GetCurrentSession()
218 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
219 " called when vim is about to exit
220 function! SessionMgr_OnLeave()
221 if exists("g:SessionMgr_CurrentSession")
222 execute "SS " . SessionMgr_GetCurrentSession()
227 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
229 call SessionMgr_Debug("END LOADING")