Commits

Meikel Brandmeyer  committed f35fdf5

Rather severe rework of window handling

Refers #99

  • Participants
  • Parent commits 4ddd912

Comments (0)

Files changed (2)

File vim/autoload/vimclojure.vim

 
 function! vimclojure#ReportError(msg)
 	if g:vimclojure#UseErrorBuffer
-		let buf = g:vimclojure#ResultBuffer.New()
+		let buf = g:vimclojure#ResultWindow.New(g:vimclojure#ResultBuffer)
 		call buf.showText(a:msg)
 		wincmd p
 	else
 endfunction
 
 let vimclojure#Buffer = copy(vimclojure#Object)
-let vimclojure#Buffer["__superObjectNew"] = vimclojure#Buffer["New"]
+let vimclojure#Buffer["__superObjectNew"]  = vimclojure#Buffer["New"]
+let vimclojure#Buffer["__superObjectInit"] = vimclojure#Buffer["Init"]
+
+let vimclojure#BufferNr = 0
 
 function! vimclojure#Buffer.New(...) dict
-	if g:vimclojure#SplitPos == "left" || g:vimclojure#SplitPos == "right"
-		let o_sr = &splitright
-		if g:vimclojure#SplitPos == "left"
-			set nosplitright
-		else
-			set splitright
-		end
-		execute printf("%svnew", g:vimclojure#SplitSize)
-		let &splitright = o_sr
-	else
-		let o_sb = &splitbelow
-		if g:vimclojure#SplitPos == "bottom"
-			set splitbelow
-		else
-			set nosplitbelow
-		end
-		execute printf("%snew", g:vimclojure#SplitSize)
-		let &splitbelow = o_sb
-	endif
+	let nr = g:vimclojure#BufferNr
+	let bufname = printf("vimclojure_buffer_%06d", nr)
+	let g:vimclojure#BufferNr += 1
+
+	execute "badd" bufname
+	execute "buffer!" bufname
 
 	return call(self.__superObjectNew, a:000, self)
 endfunction
 
 function! vimclojure#Buffer.Init() dict
-	let self._buffer = bufnr("%")
-endfunction
-
-function! vimclojure#Buffer.goHere() dict
-	execute "buffer! " . self._buffer
-endfunction
-
-function! vimclojure#Buffer.goHereWindow() dict
-	execute "sbuffer! " . self._buffer
-endfunction
-
-function! vimclojure#Buffer.resize() dict
-	call self.goHere()
-	let size = line("$")
-	if size < 3
-		let size = 3
-	endif
-	execute "resize " . size
+	call self.__superObjectInit()
+	let self._buf = bufnr("%")
 endfunction
 
 function! vimclojure#Buffer.showText(text) dict
-	call self.goHere()
 	if type(a:text) == type("")
 		" XXX: Opening the box of the pandora.
 		" 2012-01-09: Adding Carriage Returns here.
 	call append(line("$"), text)
 endfunction
 
-function! vimclojure#Buffer.showOutput(output) dict
-	call self.goHere()
-	if a:output.value == 0
-		if a:output.stdout != ""
-			call self.showText(a:output.stdout)
-		endif
-		if a:output.stderr != ""
-			call self.showText(a:output.stderr)
-		endif
-	else
-		call self.showText(a:output.value)
-	endif
-endfunction
-
 function! vimclojure#Buffer.clear() dict
 	1
 	normal! "_dG
 endfunction
 
+function! vimclojure#Buffer.goHere() dict
+	if bufnr("%") != self._buf
+		execute "buffer!" self._buf
+	endif
+endfunction
+
 function! vimclojure#Buffer.close() dict
-	execute "bdelete! " . self._buffer
+	execute "bdelete!" self._buf
+endfunction
+
+let vimclojure#Window = copy(vimclojure#Object)
+let vimclojure#Window["__superObjectNew"] = vimclojure#Window["New"]
+let vimclojure#Window["__superObjectInit"] = vimclojure#Window["Init"]
+
+function! vimclojure#Window.New(...) dict
+	if g:vimclojure#SplitPos == "left" || g:vimclojure#SplitPos == "right"
+		let o_sr = &splitright
+		if g:vimclojure#SplitPos == "left"
+			set nosplitright
+		else
+			set splitright
+		end
+		execute printf("%svsplit", g:vimclojure#SplitSize)
+		let &splitright = o_sr
+	else
+		let o_sb = &splitbelow
+		if g:vimclojure#SplitPos == "bottom"
+			set splitbelow
+		else
+			set nosplitbelow
+		end
+		execute printf("%ssplit", g:vimclojure#SplitSize)
+		let &splitbelow = o_sb
+	endif
+
+	return call(self.__superObjectNew, a:000, self)
+endfunction
+
+function! vimclojure#Window.Init(buftype) dict
+	call self.__superObjectInit()
+	let self._window = winnr()
+	let self._buffer = a:buftype.New()
+endfunction
+
+function! vimclojure#Window.goHere() dict
+	if winnr() != self._window
+		execute self._window "wincmd w"
+	endif
+	call self._buffer.goHere()
+endfunction
+
+function! vimclojure#Window.resize() dict
+	call self.goHere()
+	let size = line("$")
+	if size < 3
+		let size = 3
+	endif
+	execute "resize " . size
+endfunction
+
+function! vimclojure#Window.showText(text) dict
+	call self.goHere()
+	call self._buffer.showText(a:text)
+endfunction
+
+function! vimclojure#Window.showOutput(output) dict
+	call self.goHere()
+	if a:output.value == 0
+		if a:output.stdout != ""
+			call self._buffer.showText(a:output.stdout)
+		endif
+		if a:output.stderr != ""
+			call self._buffer.showText(a:output.stderr)
+		endif
+	else
+		call self._buffer.showText(a:output.value)
+	endif
+endfunction
+
+function! vimclojure#Window.clear() dict
+	call self.goHere()
+	call self._buffer.clear()
+endfunction
+
+function! vimclojure#Window.close() dict
+	close!
+	call self._buffer.close()
 endfunction
 
 " The transient buffer, used to display results.
-let vimclojure#ResultBuffer = copy(vimclojure#Buffer)
-let vimclojure#ResultBuffer["__superBufferNew"] = vimclojure#ResultBuffer["New"]
-let vimclojure#ResultBuffer["__superBufferInit"] = vimclojure#ResultBuffer["Init"]
-let vimclojure#ResultBuffer.__instance = []
+let vimclojure#ResultWindow = copy(vimclojure#Window)
+let vimclojure#ResultWindow["__superWindowNew"]   = vimclojure#ResultWindow["New"]
+let vimclojure#ResultWindow["__superWindowClose"] = vimclojure#ResultWindow["close"]
 
-function! ClojureResultBufferNewWorker() dict
-	set switchbuf=useopen
-	call self.instance.goHereWindow()
-	call call(self.instance.Init, self.args, self.instance)
+function! vimclojure#ResultWindow.New(buftype, ...) dict
+	if exists("t:vimclojure_result_window")
+		call t:vimclojure_result_window.goHere()
 
-	return self.instance
-endfunction
+		if t:vimclojure_result_window._buffer.prototype != a:buftype
+			let t:vimclojure_result_window._buffer = a:buftype.New()
+		else
+			call t:vimclojure_result_window.clear()
+		endif
 
-function! vimclojure#ResultBuffer.New(...) dict
-	if g:vimclojure#ResultBuffer.__instance != []
-		let oldInstance = g:vimclojure#ResultBuffer.__instance[0]
-
-		if oldInstance.prototype is self
-			let closure = {
-						\ 'instance' : oldInstance,
-						\ 'args'     : a:000,
-						\ 'f'        : function("ClojureResultBufferNewWorker")
-						\ }
-
-			return vimclojure#util#WithSavedOption('switchbuf', closure)
-		else
-			call oldInstance.close()
-		endif
+		return t:vimclojure_result_window
 	endif
 
+	let instance = call(self.__superWindowNew, [a:buftype] + a:000, self)
 	let b:vimclojure_result_buffer = 1
-	let instance = call(self.__superBufferNew, a:000, self)
-	let g:vimclojure#ResultBuffer.__instance = [ instance ]
+	let t:vimclojure_result_window = instance
 
 	return instance
 endfunction
 
+function! vimclojure#ResultWindow.close() dict
+	unlet t:vimclojure_result_window
+	call self.__superWindowClose()
+endfunction
+
+function! vimclojure#ResultWindow.CloseWindow() dict
+	if exists("t:vimclojure_result_window")
+		call t:vimclojure_result_window.close()
+	endif
+endfunction
+
+"function! s:InvalidateResultBufferIfNecessary(buf)
+"	" FIXME: This is incorrect.
+"	if exists("t:vimclojure_result_window")
+"				\ && t:vimclojure_result_window._buffer._buf == a:buf
+"		let t:vimclojure_result_window.close()
+"	endif
+"endfunction
+
+"augroup VimClojureResultWindow
+"	au BufDelete * call s:InvalidateResultWindowIfNecessary(expand("<abuf>"))
+"augroup END
+
+let vimclojure#ResultBuffer = copy(vimclojure#Buffer)
+let vimclojure#ResultBuffer["__superBufferInit"]  = vimclojure#ResultBuffer.Init
+let vimclojure#ResultBuffer["__superBufferClear"] = vimclojure#ResultBuffer.clear
+
 function! vimclojure#ResultBuffer.Init() dict
 	call self.__superBufferInit()
-
 	setlocal noswapfile
 	setlocal buftype=nofile
 	setlocal bufhidden=wipe
 	call vimclojure#MapPlug("n", "p", "CloseResultBuffer")
 
 	call self.clear()
+endfunction
+
+function! vimclojure#ResultBuffer.clear() dict
+	call self.__superBufferClear()
 	let leader = exists("g:maplocalleader") ? g:maplocalleader : "\\"
 	call append(0, "; Use " . leader . "p to close this buffer!")
 endfunction
 
-function! vimclojure#ResultBuffer.CloseBuffer() dict
-	if g:vimclojure#ResultBuffer.__instance != []
-		let instance = g:vimclojure#ResultBuffer.__instance[0]
-		let g:vimclojure#ResultBuffer.__instance = []
-		call instance.close()
-	endif
-endfunction
-
-function! s:InvalidateResultBufferIfNecessary(buf)
-	if g:vimclojure#ResultBuffer.__instance != []
-				\ && g:vimclojure#ResultBuffer.__instance[0]._buffer == a:buf
-		let g:vimclojure#ResultBuffer.__instance = []
-	endif
-endfunction
-
-augroup VimClojureResultBuffer
-	au BufDelete * call s:InvalidateResultBufferIfNecessary(expand("<abuf>"))
-augroup END
-
 " A special result buffer for clojure output.
 let vimclojure#ClojureResultBuffer = copy(vimclojure#ResultBuffer)
 let vimclojure#ClojureResultBuffer["__superResultBufferInit"] =
 			\ vimclojure#ResultBuffer["Init"]
-let vimclojure#ClojureResultBuffer["__superResultBufferShowOutput"] =
-			\ vimclojure#ResultBuffer["showOutput"]
+let vimclojure#ClojureResultBuffer["__superResultBufferShowText"] =
+			\ vimclojure#ResultBuffer["showText"]
 
-function! vimclojure#ClojureResultBuffer.Init(ns) dict
+function! vimclojure#ClojureResultBuffer.Init(...) dict
 	call self.__superResultBufferInit()
 	set filetype=vimclojure.clojure
-	let b:vimclojure_namespace = a:ns
+	if a:0 == 1
+		let b:vimclojure_namespace = a:1
+	else
+		let b:vimclojure_namespace = "user"
+	endif
 endfunction
 
-function! vimclojure#ClojureResultBuffer.showOutput(text) dict
-	call self.__superResultBufferShowOutput(a:text)
+function! vimclojure#ClojureResultBuffer.showText(text) dict
+	call self.__superResultBufferShowText(a:text)
 	normal G
 endfunction
 
 
 	let doc = vimclojure#ExecuteNailWithInput("DocLookup", a:word,
 				\ "-n", b:vimclojure_namespace)
-	let buf = g:vimclojure#ResultBuffer.New()
+	let buf = g:vimclojure#ResultWindow.New(g:vimclojure#ResultBuffer)
 	call buf.showOutput(doc)
 	wincmd p
 endfunction
 function! vimclojure#FindDoc()
 	let pattern = input("Pattern to look for: ")
 	let doc = vimclojure#ExecuteNailWithInput("FindDoc", pattern)
-	let buf = g:vimclojure#ResultBuffer.New()
+	let buf = g:vimclojure#ResultWindow.New(g:vimclojure#ResultBuffer)
 	call buf.showOutput(doc)
 	wincmd p
 endfunction
 				\ "-n", b:vimclojure_namespace)
 
 	if path.stderr != ""
-		let buf = g:vimclojure#ResultBuffer.New()
+		let buf = g:vimclojure#ResultWindow.New(g:vimclojure#ResultBuffer)
 		call buf.showOutput(path)
 		wincmd p
 		return
 function! vimclojure#SourceLookup(word)
 	let source = vimclojure#ExecuteNailWithInput("SourceLookup", a:word,
 				\ "-n", b:vimclojure_namespace)
-	let buf = g:vimclojure#ClojureResultBuffer.New(b:vimclojure_namespace)
+	let ns = b:vimclojure_namespace
+	let buf = g:vimclojure#ResultWindow.New(g:vimclojure#ClojureResultBuffer)
+	let b:vimclojure_namespace = ns
 	call buf.showOutput(source)
 	wincmd p
 endfunction
 function! vimclojure#MetaLookup(word)
 	let meta = vimclojure#ExecuteNailWithInput("MetaLookup", a:word,
 				\ "-n", b:vimclojure_namespace)
-	let buf = g:vimclojure#ClojureResultBuffer.New(b:vimclojure_namespace)
+	let ns = b:vimclojure_namespace
+	let buf = g:vimclojure#ResultWindow.New(g:vimclojure#ClojureResultBuffer)
+	let b:vimclojure_namespace = ns
 	call buf.showOutput(meta)
 	wincmd p
 endfunction
 				\ "-n", b:vimclojure_namespace)
 
 	if pos.stderr != ""
-		let buf = g:vimclojure#ResultBuffer.New()
+		let buf = g:vimclojure#ResultWindow.New(g:vimclojure#ResultBuffer)
 		call buf.showOutput(pos)
 		wincmd p
 		return
 
 	let expanded = call(function("vimclojure#ExecuteNailWithInput"), cmd)
 
-	let buf = g:vimclojure#ClojureResultBuffer.New(ns)
+	let buf = g:vimclojure#ResultWindow.New(g:vimclojure#ClojureResultBuffer)
+	let b:vimclojure_namespace = ns
 	call buf.showOutput(expanded)
 	wincmd p
 endfunction
 	let require = "(require :reload" . all . " :verbose '". ns. ")"
 	let result = vimclojure#ExecuteNailWithInput("Repl", require, "-r")
 
-	let resultBuffer = g:vimclojure#ClojureResultBuffer.New(ns)
+	let buf = g:vimclojure#ResultWindow.New(g:vimclojure#ClojureResultBuffer)
+	let b:vimclojure_namespace = ns
 	call resultBuffer.showOutput(result)
 	wincmd p
 endfunction
 
 	let result = call(function("vimclojure#ExecuteNailWithInput"),
 				\ [ "RunTests", "", "-n", ns ] + (a:all ? [ "-a" ] : []))
-	let resultBuffer = g:vimclojure#ClojureResultBuffer.New(ns)
+	let buf = g:vimclojure#ResultWindow.New(g:vimclojure#ClojureResultBuffer)
+	let b:vimclojure_namespace = ns
 	call resultBuffer.showOutput(result)
 	wincmd p
 endfunction
 	let result = vimclojure#ExecuteNailWithInput("Repl", content,
 				\ "-r", "-n", ns, "-f", file)
 
-	let resultBuffer = g:vimclojure#ClojureResultBuffer.New(ns)
+	let buf = g:vimclojure#ResultWindow.New(g:vimclojure#ClojureResultBuffer)
+	let b:vimclojure_namespace = ns
 	call resultBuffer.showOutput(result)
 	wincmd p
 endfunction
 	let result = vimclojure#ExecuteNailWithInput("Repl", content,
 				\ "-r", "-n", ns, "-f", file, "-l", theLine)
 
-	let resultBuffer = g:vimclojure#ClojureResultBuffer.New(ns)
+	let buf = g:vimclojure#ResultWindow.New(g:vimclojure#ClojureResultBuffer)
+	let b:vimclojure_namespace = ns
 	call resultBuffer.showOutput(result)
 	wincmd p
 endfunction
 	let result = vimclojure#ExecuteNailWithInput("Repl", content,
 				\ "-r", "-n", ns, "-f", file, "-l", line("'<") - 1)
 
-	let resultBuffer = g:vimclojure#ClojureResultBuffer.New(ns)
+	let buf = g:vimclojure#ResultWindow.New(g:vimclojure#ClojureResultBuffer)
+	let b:vimclojure_namespace = ns
 	call resultBuffer.showOutput(result)
 	wincmd p
 endfunction
 	let result = vimclojure#ExecuteNailWithInput("Repl", expr,
 				\ "-r", "-n", ns, "-f", file, "-l", pos[0] - 1)
 
-	let resultBuffer = g:vimclojure#ClojureResultBuffer.New(ns)
+	let buf = g:vimclojure#ResultWindow.New(g:vimclojure#ClojureResultBuffer)
+	let b:vimclojure_namespace = ns
 	call resultBuffer.showOutput(result)
 	wincmd p
 endfunction
 	let result = vimclojure#ExecuteNailWithInput("Repl", content,
 				\ "-r", "-n", ns, "-f", file, "-l", startPosition - 1)
 
-	let resultBuffer = g:vimclojure#ClojureResultBuffer.New(ns)
+	let buf = g:vimclojure#ResultWindow.New(g:vimclojure#ClojureResultBuffer)
+	let b:vimclojure_namespace = ns
 	call resultBuffer.showOutput(result)
 	wincmd p
 endfunction
 	if result.value == 0 && result.stderr == ""
 		call vimclojure#ReplDoEnter()
 	elseif result.stderr != ""
-		let buf = g:vimclojure#ResultBuffer.New()
+		let buf = g:vimclojure#ResultWindow.New(g:vimclojure#ResultBuffer)
 		call buf.showOutput(result)
 	else
 		let result = vimclojure#ExecuteNailWithInput("Repl", cmd,

File vim/plugin/clojure.vim

 inoremap <Plug>ClojureReplUpHistory. <C-O>:call b:vimclojure_repl.upHistory()<CR>
 inoremap <Plug>ClojureReplDownHistory. <C-O>:call b:vimclojure_repl.downHistory()<CR>
 
-nnoremap <Plug>ClojureCloseResultBuffer. :call vimclojure#ResultBuffer.CloseBuffer()<CR>
+nnoremap <Plug>ClojureCloseResultBuffer. :call vimclojure#ResultWindow.CloseWindow()<CR>
 
 let &cpo = s:cpo_save