Verified Commit 069e00d6 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Replace home-made vim terminal hotkeys with github.com/voldikss/vim-floaterm

parent ced135fb
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -86,3 +86,6 @@
[submodule ".vim/bundle/vim-yaml"]
	path = .vim/bundle/vim-yaml
	url = https://github.com/stephpy/vim-yaml.git
[submodule ".vim/bundle/vim-floaterm"]
	path = .vim/bundle/vim-floaterm
	url = https://github.com/voldikss/vim-floaterm

.vim/autoload/kodo/toggleterm.vim

deleted100644 → 0
+0 −170
Original line number Diff line number Diff line
""
" Toggle terminal buffers from hotkeys
""

function! kodo#toggleterm#Terminal(new_win, ...)
	let term = {}
	let term.origin_buffer = v:null
	let term.buffer = v:null
	let term.window = v:null
	let term.was_new = v:null
	let term.new_win = a:new_win
	let term.closed = v:false

	let cmd = get(a:, 1, v:null)
	let term.cmd = type(cmd) == v:t_list ? cmd :
	\                   cmd  == v:null   ? [ &shell ] :
	\                                      [ &shell, '-c', string(cmd) ]

	" Expand (show) the terminal, optionally in a new window
	"
	" Terminal.expand(window: window-ID)
	" Terminal.expand(new_win: bool = self.new_win)
	"
	" If a new window is requested it is opened by spliting, which obeys the […] 
	" and […] settings.
	function term.expand(...)
		if self.buffer == v:null
			call self.open()
		endif
		if self.window != v:null
			call self.collapse()
		endif

		let window = get(a:, 1, self.new_win)
		if type(window) == v:t_bool
			let new_win = window
		elseif !win_gotoid(window)
			echoerr "Toggle-terminal window not found, ignoring"
			return
		else
			let new_win = v:false
		endif

		let self.origin_buffer = new_win ? v:null : bufnr('')

		if new_win
			silent execute 'sbuffer +startinsert' self.buffer
		else
			silent execute 'buffer +startinsert' self.buffer
		endif

		let self.window = win_getid()
		let self.was_new = new_win

		augroup toggleterm
			au! BufLeave <buffer>
			au  BufLeave <buffer> let b:toggleterm.window = v:null
		augroup END

		if has('nvim') && !has('nvim-0.4')
			" Hack to fix terminal resizing bug
			silent execute 'buffer' bufnr('#')
			redraw
			silent execute 'buffer +startinsert' self.buffer
		endif
	endfunction

	" Collapse (hide) the terminal by restoring any origin buffer if there is 
	" one, else closing the terminal window.
	function term.collapse()
		" silent execute 'au! toggleterm *' self.buffer
		let window = win_getid()
		if !win_gotoid(self.window)
			echoerr "Toggle-terminal window not found, ignoring"
			let self.window = v:null
			return
		elseif bufnr('') != self.buffer
			echoerr "Buffer in toggle-terminal window changed, ignoring"
		else
			call self.on_collapse()
		endif
		if self.window != window
			call win_gotoid(window)
		endif
		let self.window = v:null
	endfunction

	" Collapse the terminal if expanded and vice-versa
	"
	" Terminal.toggle()
	" Terminal.toggle(window: window-ID)
	"
	" If a window is provided, check for the terminal in that window, otherwise 
	" check in the current window. If the terminal is expanded any specified 
	" window will also be passed as the window to expand into.
	function term.toggle(...)
		let window = get(a:, 1, win_getid())
		if self.window == window
			call self.collapse()
		else
			call call(self.expand, a:000)
		endif
	endfunction

	" termopen() on_exit handler
	" Restores any origin buffer and cleans up the terminal buffer
	function term.on_exit(jobid, data, event)
		if self.origin_buffer != v:null
			silent execute 'buffer' self.origin_buffer
		endif
		silent execute 'bdelete!' self.buffer
		call setbufvar(self.buffer, 'toggleterm', v:null)
		let self.buffer = v:null
		let self.window = v:null
		let self.closed = v:true
	endfunction

	" Perform window cleanup on collapse
	" The current buffer is guranteed to be the terminal buffer, and the current 
	" window is guranteed to be the window it was expanded in, otherwise this 
	" hook is not called.
	function term.on_collapse()
		if self.was_new
			call self.close_window()
		else
			call self.restore_buffer()
		endif
	endfunction

	function term.close_window()
		let self.window = v:null
		close
	endfunction

	function term.restore_buffer()
		if self.origin_buffer != v:null
			silent execute 'buffer' self.origin_buffer
		endif
	endfunction

	function term.open()
		" Create the terminal buffer
		let origin = bufnr('')
		enew
		let self.buffer =  bufnr('')

		if self.buffer == origin && origin != 1
			echoerr "New buffer appears to have replaced old buffer?"
		endif

		" Hide from the buffer list
		setl nobuflisted

		" Start the terminal job
		let self.job = termopen(self.cmd, self)

		silent execute 'buffer' origin

		if self.job == -1
			echoerr "Not found or executable:" self.cmd[0]
		elseif self.job <= 0
			throw "Failed to open a new terminal; returned: " + self.job
		endif

		call setbufvar(self.buffer, 'toggleterm', self)
		let self.closed = v:false
	endfunction

	return term
endfunction
Original line number Diff line number Diff line
Subproject commit 4e28c8dd0271e10a5f55142fb6fe9b1599ee6160
+33 −13
Original line number Diff line number Diff line
if exists('##WinNew')

function! s:setorientation()
	" If an 80×24 terminal is approx. a 4:3 pixel ratio, assume a 1:1 pixel 
	" ratio is approx. a 1:2.5 cell ratio.
	augroup orientation
	if 1.0 * &columns / &lines > 2.5
		" Wide terminal
			execute 'au WinNew * wincmd' (&splitright ? 'L' : 'H')
		let g:floaterm_position = "center"
	else
			" Narrow terminal, normal behaviour fine
			au! WinNew
		" Narrow terminal
		let g:floaterm_position = "bottom"
	endif
	augroup END

	if &columns <= 80
		let g:floaterm_width = 1.0
	elseif &columns <= 134
		let g:floaterm_width = 80
	else
		let g:floaterm_width = 0.6
	endif

	if &lines <= 30
		let g:floaterm_height = 1.0
	elseif &lines <= 50
		let g:floaterm_height = 30
	else
		let g:floaterm_height = 0.6
	endif

	let l:config = {
		\ "width": g:floaterm_width,
		\ "height": g:floaterm_height,
		\ "position": g:floaterm_position,
	\ }
	for l:bufnr in floaterm#buflist#gather()
		call floaterm#config#set_all(l:bufnr, l:config)
	endfor
endfunction

augroup orientation
	au!
	au VimEnter * call s:setorientation()
	au VimResized * call s:setorientation()
	au OptionSet splitright call s:setorientation()
augroup END

endif
call s:setorientation()

.vim/plugin/toggleterm.vim

deleted100644 → 0
+0 −46
Original line number Diff line number Diff line
let g:TermToggleOpenNewWindow = 1
let g:TermToggleDebug = [ &shell ]
let s:hot_terms = {}
let s:debugger_term = {}

function s:toggle_debug()
	if get(s:debugger_term, 'closed', v:true)
		let s:debugger_term = kodo#toggleterm#Terminal(v:false, g:TermToggleDebug)
	endif
	call s:debugger_term.toggle()
endfunction

function s:toggle(num)
	if !has_key(s:hot_terms, a:num)
		let l:term = kodo#toggleterm#Terminal(v:false)
		let s:hot_terms[a:num] = l:term

		function! l:term.on_collapse()
			call self.close_window()
			unlet t:term_window
		endfunction
	else
		let l:term = s:hot_terms[a:num]
	endif
	if exists("t:term_window") && 0 != win_id2win(t:term_window)
		call l:term.toggle(t:term_window)
	else
		call l:term.expand(v:true)
		let t:term_window = l:term.window
	endif
endfunction

command        DebuggerTerm :call s:toggle_debug()
command -nargs=1 TermToggle :call s:toggle(<args>)

nnoremap <silent> <F5> <Esc>:DebuggerTerm<CR>
tnoremap <silent> <F5> <C-\><C-n>:DebuggerTerm<CR>

nnoremap <silent> <F6> <Esc>:TermToggle 0<CR>
tnoremap <silent> <F6> <C-\><C-n>:TermToggle 0<CR>

nnoremap <silent> <F7> <Esc>:TermToggle 1<CR>
tnoremap <silent> <F7> <C-\><C-n>:TermToggle 1<CR>

nnoremap <silent> <F8> <Esc>:TermToggle 2<CR>
tnoremap <silent> <F8> <C-\><C-n>:TermToggle 2<CR>
Loading