"""
Single_Space_Buffer.py - A Jython macro for jEdit that removes
every second line, if they are all blank.  Essentially, it
takes a double-spaced buffer and converts it to single spacing.

Copyright (C) 2002 Ollie Rutherfurd <oliver@rutherfurd.net>

$Id: Single_Space_Buffer.py 28 2003-03-25 22:40:28Z oliver $
"""

from org.gjt.sp.jedit import Macros

__version__ = '$Revision: 1.2 $'[11:-2]
__author__ = 'Ollie Rutherfurd'


def doubleToSingleSpaced(v,b):
	line = 1 	# NOTE: actually starting with second line
	lines = []	# lines to remove

	try:

		while line < b.getLineCount():
			# make sure line is blank, then remove it
			if b.getLineText(line) == '':
				lines.append(line)
			else:
				msg = "%s doesn't look double-spaced.\n" % b.getPath()
				msg += "Line %d isn't blank." % (line + 1,)
				Macros.error(v, msg)
				return
			line += 2 # go to next line

		b.beginCompoundEdit()
		while lines:
			offset = b.getLineStartOffset(lines.pop())
			# remove newline before current line so we
			# don't have problems with an empty last line
			b.remove(offset-1,1)

	finally:
		if b.insideCompoundEdit():
			b.endCompoundEdit()


if __name__ in ('__main__','main'):
	doubleToSingleSpaced(init.view, init.buffer)

#:indentSize=4:lineSeparator=\n:noTabs=false:tabSize=4:

