#@+leo-ver=4
#@+node:@file script_io_to_body.py
"""Send output from the Execute Script command to the end of the body pane"""

from leoPlugins import *
from leoGlobals import *

try:
	import Tkinter
except ImportError:
	Tkinter = None

if Tkinter: # Register the handlers...

	#@	@+others
	#@+node:onStart
	def onStart (tag,keywords):
		
		# Replace frame.put with newPut.
		import leoTkinterFrame
		funcToMethod(newExecuteScript,leoTkinterFrame.leoTkinterFrame,"OnExecuteScript")
	#@-node:onStart
	#@+node:newExecuteScript
	# Execute the selected body text as a Python script and sends the output to the end of the body pane.
	
	def newExecuteScript(self,event=None,v=None):
		
		c = self.c ; body = self.body ; s = None
		if v == None:
			v = c.currentVnode() 
	
		# Assume any selected body text is a script.
		start,end = body.getTextSelection() # EKR: 11/04/03
		if start and end and start != end: # 7/7/03
			s = body.bodyCtrl.get(start,end)
		else:
			s = body.bodyCtrl.get("1.0","end")
		s = s.strip()
		if s and len(s) > 0:
			s += '\n' # Make sure we end the script properly.
			# Switch output.
			import leoTkinterFrame,leoGlobals
			oldput = leoTkinterFrame.leoTkinterLog.put # 11/7/03
			oldputnl = leoTkinterFrame.leoTkinterLog.putnl # 11/7/03
			oldes = leoGlobals.es
			oldenl = leoGlobals.enl
			oldecnl = leoGlobals.ecnl
			oldecnls = leoGlobals.ecnls
			leoGlobals.es = newEs
			leoGlobals.enl = newEnl
			leoGlobals.ecnl = newEcnl
			leoGlobals.ecnls = newEcnls
			funcToMethod(newPut,leoTkinterFrame.leoTkinterLog,"put") #  11/7/03
			funcToMethod(newPutNl,leoTkinterFrame.leoTkinterLog,"putNl") # 11/7/03
			redirectStderr()
			redirectStdout()
			try:
				exec s in {} # Use {} to get a pristine environment!
				ok = true
			except:
				ok = false
			# Restore output.
			funcToMethod(oldput,leoTkinterFrame.leoTkinterLog,"put") # 11/7/03
			funcToMethod(oldputnl,leoTkinterFrame.leoTkinterLog,"putNl") # 11/7/03
			leoGlobals.es = oldes
			leoGlobals.enl = oldenl
			leoGlobals.ecnl = oldecnl
			leoGlobals.ecnls = oldecnls
			restoreStderr()
			restoreStdout()
			if not ok:
				es("newExecuteScript: exception executing script")
				es_exception(full=false)
		else:
			es("newExecuteScript: empty script")
	#@nonl
	#@-node:newExecuteScript
	#@+node:newPut and newPutNl
	# Same as frame.put except sends output to the end of the body text.
	def newPut (self,s):
		c = self.frame.c
		bodyCtrl = self.frame.body.bodyCtrl
		if bodyCtrl:
			bodyCtrl.insert("end",s)
			# bodyCtrl.see("end")
			v = c.currentVnode()
			self.frame.tree.onBodyChanged(v,"Typing")
		else: print s,
	
	# Same as frame.putnl exceptsends output to the end of the body text.
	def newPutNl (self):
		newPut (self,'\n')
	#@nonl
	#@-node:newPut and newPutNl
	#@+node:newEs, etc.
	def newEnl():
		print
		
	def newEcnl():
		print
		
	def newEcnls(n):
		while n > 0:
			n -= 1
			print
	
	def newEs(s,*args,**keys):
		newline = keys.get("newline",true)
		if type(s) != type("") and type(s) != type(u""):
			s = repr(s)
		for arg in args:
			if type(arg) != type("") and type(arg) != type(u""):
				arg = repr(arg)
			s = s + ", " + arg
		if newline:
			print s
		else:
			print s,
	#@-node:newEs, etc.
	#@-others

	if app.gui is None:
		app.createTkGui(__file__)

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

		registerHandler("start1", onStart)
	
		__version__ = "1.3" # Contains Tk-specific code.
		plugin_signon(__name__)
#@nonl
#@-node:@file script_io_to_body.py
#@-leo
