"""
Emacs_Previous_Line.py - A Jython macro for jEdit that 
implements `Emacs-like` scrolling. If the cursor is at the 
top of the text area (or electric-scroll boundary), instead 
of just moving to the prev line, the text area will first be 
scrolled so the cursor is in the middle of the screen.  This 
may be used as a replacement for the built-in 'Go To Previous 
Line' command.

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

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

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

from org.gjt.sp.jedit import Macros


def Emacs_Previous_Line(view):
	textArea = view.getTextArea()

	if not getattr(textArea, 'physicalToVirtual', None):
		textArea.goToPrevLine(0)
		Macros.error(view, "If you're using jEdit 4.2 pre1 or greater, this macro is redundant.")
		return

	first = textArea.getFirstLine()
	current = textArea.getCaretLine()
	virtual = textArea.physicalToVirtual(current)
	pad = textArea.getElectricScroll()

	# move current line to center, if at top edge
	if virtual <= (first + pad):
		visible = textArea.getVisibleLines()
		newFirst = first - ((visible - pad) / 2)
		if newFirst < 0:
			newFirst = 0
		textArea.setFirstLine(newFirst)

	textArea.goToPrevLine(0)


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

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

