#@+leo-ver=4
#@+node:@file mod_updown.py
#@@language python

"""
Up Down, show the body as a continum space, the user can go up or down, and will automatically jump from one node to the next.
"""

#@+at
# 04/12/03 File Created. RodrigoB.
# 05/12/03 Leo4.1 support. RodrigoB.
# 06/12/03 Changing to line based. Upload. RodrigoB.
#@-at
#@@c

__version__ = "0.0.3"

from leoPlugins import *
from leoGlobals import *

last_key = None # global var for this module.

try:    import Tkinter
except: Tkinter = None

if Tkinter and app.gui.guiName() == "tkinter":

	#@    @+others
	#@+node:0nStart2
	def OnStart2(tag, keywords):
	
		"""."""
		
		trace("updown started")
		
		c = keywords['c']
		c.frame.body.bodyCtrl.bind("<Key>", OnBodyKey, '+') # append an extra binding.
	#@nonl
	#@-node:0nStart2
	#@+node:OnBodyKey
	def OnBodyKey(event):
		"""."""
		tracing = true
		c = top() ; global last_key
		text = c.frame.body.bodyCtrl
		v = c.currentVnode()
		keycode = event.keycode
		if 0:
			if tracing: # for debugging.
				# trace("event %s %s" % (event, dir(event))) 
				trace("ch %s %s %s" % (event.char, event.keycode, event.num))
		if keycode in [33, 98, 99]: # up, page up
			keycode = "up"
		elif keycode in [34, 104, 105]: # down, page down
			keycode = "down"        
				 
		if last_key == keycode:
			if keycode == "up":
				new = v.threadBack()
				if tracing: trace("Going up")
			elif keycode == "down":
				new = v.threadNext()
				if tracing: trace("Going down")
			else:
				new = None	
			if new:
				c.selectVnode(new) # on Leo4 it will automatically show the selected node.
			last_key = None
			return
		
		insert_line = int(text.index("insert").split('.')[0])
		last_line   = int(text.index("end").split('.')[0]) - 1
	
		if insert_line == 1 and keycode == "up":
			last_key = keycode
		elif insert_line == last_line and keycode == "down":
			last_key = keycode
		else:
			last_key = None
	#@nonl
	#@-node:OnBodyKey
	#@-others

	# Register the handlers...
	registerHandler("start2", OnStart2) #  install the plugin hooks

	plugin_signon(__name__)
#@-node:@file mod_updown.py
#@-leo
