"""
Select_HyperSearch_Matches.py - A Jython macro for jEdit that
selects all lines in the current buffer which contain matches 
from the most recent HyperSearch.

Copyright (C) 2002 Ollie Rutherfurd <oliver@rutherfurd.net>

$Id: Select_HyperSearch_Matches.py 35 2003-04-03 19:13:26Z oliver $
"""

__version__ = '$Revision: 1.5 $'[11:-2]
__author__ = 'Ollie Rutherfurd'

from org.gjt.sp.jedit import Macros


def selectMatcingLines(view):
	hsearch = view.getDockableWindowManager().getDockableWindow('hypersearch-results')

	if hsearch is None:
		Macros.error(view, 'The "HyperSearch Results" window is not open.')
		return

	root = hsearch.getTreeModel().getRoot()
	if root is None:
		view.getStatus().setMessageAndClear('No HyperSearch matches.')
		return

	bufferPath = view.getBuffer().getPath()
	node = root.getFirstChild()

	major,minor,rev = map(int, jEdit.getBuild().split('.'))[:-1]
	handle_multi_match = major >= 4 and (minor > 1 or (minor == 1 and rev >= 6))
	if handle_multi_match:
		while node.getNextSibling():
			node = node.getNextSibling()
		node = node.getFirstChild()
	

	while node:
		if str(node.getUserObject()) == bufferPath:
			lines = 0
			match = node.getFirstChild()
			while match:
				line = match.getUserObject().line
				start = view.getTextArea().getLineStartOffset(line)
				end = view.getTextArea().getLineEndOffset(line)
				view.getTextArea().extendSelection(start, end - 1)
				lines += 1
				match = match.getNextSibling()
			view.getStatus().setMessageAndClear('%d lines selected' % (lines,))
			break
		node = node.getNextSibling()

	view.getStatus().setMessageAndClear('No matches in %s' % (bufferPath,))


if __name__ in ('__main__','main'):
	selectMatcingLines(init.view)

# :indentSize=4:lineSeparator=\n:noTabs=false:tabSize=4:

