add vim conf files
[arrow.git] / conf_slk120 / vim / _vim / plugin / sessionmgr.vim
blobe6047e197f86db9617a68ea89f36ad49432073b9
1 " File: sessionmgr.vim
2 " Author: Jason Heddings (vim at heddway dot com)
3 " Version: 1.1
4 " Last Modified: 20 October, 2005
6 if exists('g:SessionMgr_Loaded')
7   finish
8 endif
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
17 endif
19 command! -nargs=0 SMLog call confirm("__SessionMgr Log__\n\n" . g:SessionMgr_DebugLog)
20 function! SessionMgr_Debug(msg)
21   if g:SessionMgr_Debug
22     let l:len = strlen(g:SessionMgr_DebugLog)
23     if l:len > 4096
24       let g:SessionMgr_DebugLog = strpart(g:SessionMgr_DebugLog, l:len - 4096)
25     endif
26     let l:msg = strftime('%H:%M:%S') . ': ' .  a:msg
27     let g:SessionMgr_DebugLog = g:SessionMgr_DebugLog . l:msg . "\n"
28   endif
29 endfunction
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
37 endif
38 call SessionMgr_Debug("_AutoManage: " . g:SessionMgr_AutoManage)
40 if !exists("g:SessionMgr_DefaultSession")
41   let g:SessionMgr_DefaultSession = "session"
42 endif
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")
50   else
51     let g:SessionMgr_Dir = "."
52   endif
54 endif
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
62     autocmd!
63     autocmd VimEnter * call SessionMgr_OnEnter()
64     autocmd VimLeavePre * call SessionMgr_OnLeave()
65   augroup END
66 endif
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(...)
73   if a:0 > 0
74     let l:name = a:1
75   else
76     let l:name = SessionMgr_GetCurrentSession()
77   endif
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
84 endfunction
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(...)
90   if a:0 > 0
91     let l:name = a:1
92   else
93     let l:name = SessionMgr_GetCurrentSession()
94   endif
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
102   endif
103 endfunction
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)
110   if a:0 > 0
111     let l:name = a:1
112   else
113     if exists("g:SessionMgr_CurrentSession")
114       let l:name = g:SessionMgr_CurrentSession
115     else
116       call SessionMgr_Debug("Delete(): NO SESSION")
117       echohl ErrorMsg | echo "No Active Session" | echohl None
118       return
119     endif
120   endif
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
125     SQ
126   endif
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
131 endfunction
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
143   else
144     call SessionMgr_Debug("Quit(): NO SESSION")
145     echohl ErrorMsg | echo "No Active Session" | echohl None
146   endif
147 endfunction
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
157   else
158     call SessionMgr_Debug("List()")
159     echohl ModeMsg | echo "__Sessions__" | echohl None
160     echohl SpecialKey | echo l:sessions | echohl None
161   endif
162 endfunction
164 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
165 " returns the names of all available sessions (in session directory)
166 function! SessionMgr_GetSessions()
167   if !isdirectory(g:SessionMgr_Dir)
168     return ""
169   endif
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
177   return l:sessions
178 endfunction
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
186   else
187     echohl ErrorMsg | echo "No Active Session" | echohl None
188   endif
189 endfunction
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
196   endif
197   return g:SessionMgr_DefaultSession
198 endfunction
200 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
201 " used for auto-completing session names
202 function! SessionMgr_Complete(ArgLead, CmdLine, CursorPos)
203   let l:sessions = SessionMgr_GetSessions()
204   return l:sessions
205 endfunction
207 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
208 " called when vim starts up
209 function! SessionMgr_OnEnter()
210   call SessionMgr_Debug("OnEnter [" . argc() . "]")
212   if argc() == 0
213     execute "SR " . SessionMgr_GetCurrentSession()
214     let g:SessionMgr_CurrentSession = SessionMgr_GetCurrentSession()
215   endif
216 endfunction
218 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
219 " called when vim is about to exit
220 function! SessionMgr_OnLeave()
221   if exists("g:SessionMgr_CurrentSession")
222     execute "SS " . SessionMgr_GetCurrentSession()
223   endif
224 endfunction
227 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
228 " always be last
229 call SessionMgr_Debug("END LOADING")