1" 2" cream-server.vim -- Manage multiple sessions 3" 4" Cream -- An easy-to-use configuration of the famous Vim text editor 5" [ http://cream.sourceforge.net ] Copyright (C) 2001-2011 Steve Hall 6" 7" License: 8" This program is free software; you can redistribute it and/or modify 9" it under the terms of the GNU General Public License as published by 10" the Free Software Foundation; either version 3 of the License, or 11" (at your option) any later version. 12" [ http://www.gnu.org/licenses/gpl.html ] 13" 14" This program is distributed in the hope that it will be useful, but 15" WITHOUT ANY WARRANTY; without even the implied warranty of 16" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17" General Public License for more details. 18" 19" You should have received a copy of the GNU General Public License 20" along with this program; if not, write to the Free Software 21" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 22" 02111-1307, USA. 23" 24" Description: 25" We like single sessions of Vim. If second Vim session opened, open 26" the file in first session, close and move it to forefront 27 28function! Cream_singleserver_init() 29" initialize environment state (default is off) 30 31 " don't use in terminal! 32 if !has("gui_running") 33 return 34 elseif has("mac") 35 return 36 endif 37 38 if exists("g:CREAM_SINGLESERVER") 39 if g:CREAM_SINGLESERVER != 1 40 let g:CREAM_SINGLESERVER = 0 41 set swapfile 42 else 43 let g:CREAM_SINGLESERVER = 1 44 "*** disable swapfiles until we resolve this 45 set noswapfile 46 "*** 47 endif 48 else 49 let g:CREAM_SINGLESERVER = 0 50 set swapfile 51 endif 52endfunction 53 54 55" Singleserver toggle (called by menu) 56function! Cream_singleserver_toggle() 57 58 " don't use in terminal! 59 if !has("gui_running") 60 return 61 elseif has("mac") 62 return 63 endif 64 65 " should already be initialized, but check anyway 66 if exists("g:CREAM_SINGLESERVER") 67 if g:CREAM_SINGLESERVER == 1 68 call confirm( 69 \ "\n" . 70 \ "Single-Session Mode off -- multiple Vim sessions allowed.\n" . 71 \ "\n", "&Ok", 1, "Info") 72 let g:CREAM_SINGLESERVER = 0 73 " save global in viminfo 74 else 75 call confirm( 76 \ "\n" . 77 \ "Single-Session Mode *on* -- multiple files will always\n" . 78 \ "be opened within a single session of Vim.\n" . 79 \ "\n", "&Ok", 1, "Info") 80 let g:CREAM_SINGLESERVER = 1 81 " save global in viminfo 82 endif 83 " init autocommand group 84 call Cream_singleserver_init() 85 " forget last buffer so future sessions have no memory 86 let g:CREAM_LAST_BUFFER = "" 87 wviminfo! 88 else 89 " error 90 call Cream_error_warning("g:CREAM_SINGLESERVER not initialized in Cream_singleserver_toggle()") 91 endif 92 call Cream_menu_settings_preferences() 93endfunction 94 95function! Cream_singleserver() 96" merge a second instance of Vim into the first one 97 98 " don't use in terminal! 99 if !has("gui_running") 100 return 101 elseif has("mac") 102 return 103 endif 104 "" don't use if not initialized 105 "if !exists("g:CREAM_SINGLESERVER") 106 " return 107 "endif 108 "" don't use if off 109 "if g:CREAM_SINGLESERVER != 1 110 " return 111 "endif 112 " 113 "" don't use if separate server over-ride on (see Cream_session_new() ) 114 "if exists("g:CREAM_SERVER_OVERRIDE") 115 " " reset 116 " unlet g:CREAM_SERVER_OVERRIDE 117 " return 118 "endif 119 120 "" option 1 (slow) {{{1 121 "" collect servernames (separated by "\n", should be just one) 122 "let myserverlist = serverlist() 123 "" count 124 "let servercount = MvNumberOfElements(myserverlist, "\n") 125 "" get this server's name 126 "let myserverid = v:servername 127 128 "if servercount > 1 129 " " iterate through servernames until reaching one not equal to mine 130 " let i = 0 131 " while i < servercount 132 " let myserver = MvElementAt(myserverlist, "\n", i) 133 " if v:servername != myserver 134 " let myfile = expand("%:p") 135 " "*** no need to alter, Vim will re-handle 136 " "" windows slash to backslash (if exist, ie ":set shellslash") 137 " "if has("win32") 138 " " let myfile = substitute(myfile,'/','\\','g') 139 " "endif 140 " "*** 141 " if myfile != "" 142 " " escape 143 " let myfile = escape(myfile, " ") 144 " " solve buffer not found errors from second session startup 145 " let g:CREAM_LAST_BUFFER = "" 146 " let mycmd = "\<M-o>\<M-o>:edit " . "+" . line(".") . " " . myfile . "\<CR>" 147 " else 148 " let mycmd = "\<M-o>\<M-o>:call Cream_file_new()\<CR>" 149 " endif 150 " call remote_send(myserver, mycmd) 151 " "if has("win32") 152 " "\ has(" 153 " call remote_expr(myserver, "foreground()") 154 " "else 155 " " call foreground() 156 " "endif 157 " call Cream_exit() 158 " " we're toast at this point, but let's keep good form ;) 159 " break 160 " endif 161 " let i = i + 1 162 " endwhile 163 " ""*** DEBUG: 164 " "call confirm( 165 " " \ "\n" . 166 " " \ " myserverlist = " . myserverlist . "\n" . 167 " " \ " servercount = " . servercount . "\n" . 168 " " \ "\n" . 169 " " \ " myserver = " . myserver . "\n" . 170 " " \ " myfile = " . myfile . "\n" . 171 " " \ " mycmd = " . mycmd . "\n" . 172 " " \ "\n", "&Ok", 1, "Info") 173 " ""*** 174 "endif 175 " 1}}} 176 177 "" option 2 (do better guessing) {{{1 178 "if v:servername !=? "GVIM" 179 " 180 " " get opened file name 181 " let myfile = expand("%:p") 182 " " if have file, open it 183 " if myfile != "" 184 " " reverse backslashes 185 " let myfile = substitute(myfile, '\\', '/', 'g') 186 " " escape 187 " let myfile = escape(myfile, ' ') 188 " " solve buffer not found errors from second session startup 189 " let g:CREAM_LAST_BUFFER = "" 190 " " edit current file, maintain this position 191 " let mycmd = '<C-b>:edit ' . '+' . line('.') . ' ' . myfile . '<CR>' 192 " " open new buffer 193 " else 194 " let mycmd = '<C-b>:call Cream_file_new()<CR>' 195 " endif 196 " call remote_send('GVIM', mycmd) 197 " call remote_expr('GVIM', 'foreground()') 198 " call Cream_exit() 199 " 200 "end 201 " 1}}} 202 203 " option 3 (just become the server) {{{1 204 if v:servername !=? "CREAM" 205 206 if !Cream_buffer_isnewunmod() 207 208 " get opened file name 209 let myfile = expand("%:p") 210 " reverse backslashes 211 let myfile = substitute(myfile, '\\', '/', 'g') 212 " escape 213 let myfile = escape(myfile, ' ') 214 " solve buffer not found errors from second session startup 215 let g:CREAM_LAST_BUFFER = "" 216 ""*** not needed, we're only concerned about variable space 217 "" write the viminfo so it's remembered! 218 "wviminfo! 219 ""*** 220 " edit current file, maintain this position 221 let mycmd = "<C-\\><C-n>:edit +" . line('.') . ' ' . myfile . '<CR>' 222 " open new buffer 223 else 224 let mycmd = "<C-\\><C-n>:call Cream_file_new()<CR>" 225 endif 226 call remote_send('CREAM', mycmd) 227 " Fix tabs (last bit clears the line :) 228 let mycmd = "<C-\\><C-n>:call Cream_tabpages_refresh()<CR><C-\\><C-n>:<CR>" 229 call remote_send('CREAM', mycmd) 230 231 call remote_foreground('CREAM') 232 call Cream_exit() 233 234 call Cream_menu_settings_preferences() 235 236 end 237 " 1}}} 238 239endfunction 240 241" vim:foldmethod=marker 242