Commits

Evgeny Podjachev committed 1a11115

More accurate selection of window to open file in.

Comments (0)

Files changed (1)

 "
 " Version:		1.4.1
 "
-" ChangeLog:	1.4.1:	Removed yate buffer from buffers list.
+" ChangeLog:	1.4.1:	More accurate selection of window to open file in.
+" 						Removed yate buffer from buffers list.
 "						Better work with history popup menu.
 "
 " 				1.4.0:	Added command YATEStationary to look into several tags
 	let index=str2nr(str)
 
 	let pos_in_yate = getpos(".") " save cursor position may halp later
-	exe ':wincmd p'
+
+	call <SID>GoToPrevWindow()
+
+	"exe ':wincmd p'
 	if !a:stationary
 		exe ':'.s:yate_winnr.'bd!'
 		let s:yate_winnr=-1
 	call <SID>PrintTagsList()
 endfun
 
+" This function is taken from NERD_tree.vim
+fun <SID>FirstUsableWindow()
+	let i = 1
+	while i <= winnr("$")
+		let bnum = winbufnr(i)
+		if bnum != -1 && getbufvar(bnum, '&buftype') ==# ''
+					\ && !getwinvar(i, '&previewwindow')
+					\ && (!getbufvar(bnum, '&modified') || &hidden)
+			return i
+		endif
+
+		let i += 1
+	endwhile
+	return -1
+endfun
+
+" This function is taken from NERD_tree.vim
+fun <SID>IsWindowUsable(winnumber)
+	"gotta split if theres only one window (i.e. the NERD tree)
+	if winnr("$") ==# 1
+		return 0
+	endif
+
+	let oldwinnr = winnr()
+	exe a:winnumber . "wincmd p"
+	let specialWindow = getbufvar("%", '&buftype') != '' || getwinvar('%', '&previewwindow')
+	let modified = &modified
+	exe oldwinnr . "wincmd p"
+
+	"if its a special window e.g. quickfix or another explorer plugin then we
+	"have to split
+	if specialWindow
+		return 0
+	endif
+
+	if &hidden
+		return 1
+	endif
+
+	return !modified || <SID>BufInWindows(winbufnr(a:winnumber)) >= 2
+endfun
+
+" This function is taken from NERD_tree.vim
+fun <SID>BufInWindows(bnum)
+	let cnt = 0
+	let winnum = 1
+	while 1
+		let bufnum = winbufnr(winnum)
+		if bufnum < 0
+			break
+		endif
+		if bufnum ==# a:bnum
+			let cnt = cnt + 1
+		endif
+		let winnum = winnum + 1
+	endwhile
+
+	return cnt
+endfun
+
+" This function is taken from NERD_tree.vim
+fun <SID>GoToPrevWindow()
+	if !<SID>IsWindowUsable(winnr("#"))
+		exe <SID>FirstUsableWindow() . "wincmd w"
+	else
+		exe 'wincmd p'
+	endif
+endfun
+
 fun! <SID>ToggleTagExplorerBuffer(stationary)
 	if !exists("s:yate_winnr") || s:yate_winnr==-1
 		let buffer_name = 'YATE'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.