"""
Underline_Current_Line.py - A Jython macro for jEdit that
underlines the current line using a character given by the
user.

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

$Id: Underline_Current_Line.py 28 2003-03-25 22:40:28Z oliver $
"""

from org.gjt.sp.jedit import Macros
from org.gjt.sp.jedit import MiscUtilities

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


def underlineCurrentLine(v,b):

	textArea = v.getTextArea()

	# get tabSize property
	try:
		tabSize = int(b.getProperty('tabSize'))
	except ValueError,e:
		Macros.error(v, 'Error getting tabSize: ' + e)
		return

	# get line caret is on
	line = textArea.getCaretLine()
	# get line text
	line_text = textArea.getLineText(line)

	# if line is blank, try the previous line
	if line > 0 and len(line_text) == 0:
		line = line - 1
		line_text = textArea.getLineText(line)

	# get number of leading whitespace characters
	leading_ws_len = MiscUtilities.getLeadingWhiteSpace(line_text)
	# get length of leading portion of line, with expanded tabs
	leading_ws_len = len(line_text[:leading_ws_len].expandtabs(tabSize))
	# measure length of 'text' in line, with tabs expanded
	text_len = len(line_text.strip().expandtabs(tabSize))

	# abort if nothing to underline
	if text_len == 0:
		Macros.error(v, 'Nothing to underline on current line.')
		return

	# prompt for an underline char
	char = Macros.input(v, 'Underline Char', '=')
	# in case user cancels
	if char is None:
		return
	else:
		char = char[0]	# make sure we've only got one char

	# string to insert into buffer
	underline = (char * text_len)
	tabs = '\t' * (leading_ws_len / tabSize)
	spaces = ' ' * (leading_ws_len % tabSize)
	underline = tabs + spaces + underline

	# insert the underline on the following line
	b.insert(textArea.getLineEndOffset(line) - 1, '\n' + underline)


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

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

