#@+leo
#@+node:0::@file plugins/mod_open_with.py
#@+body
"""Open With handler"""

from leoPlugins import *
from leoGlobals import *


#@+others
#@+node:1::on_idle
#@+body
# frame.OnOpenWith creates the dict as follows:
# dict = {"c":c, "v":v, "f":f, "path":path, "time":time}

def on_idle (tag,keywords):

	import os
	a = app()
	for dict in a.openWithFiles:
		path = dict.get("path")
		c = dict.get("c")
		v = dict.get("v")
		if path and os.path.exists(path):
			try:
				time = os.path.getmtime(path)
				if time and time != dict.get("time"):
					dict["time"] = time
					
					#@<< update v's body text >>
					#@+node:1::<< update v's body text >>
					#@+body
					
					#@<< set s to the new text >>
					#@+node:1::<< set s to the new text >>
					#@+body
					try:
						# Update v from the changed temp file.
						f=open(path)
						s=f.read()
						f.close()
					except:
						es("can not open " + shortFileName(path))
						break
					
					#@-body
					#@-node:1::<< set s to the new text >>

					body = v.bodyString()
					
					#@<< set conflict flag >>
					#@+node:2::<< set conflict flag >>
					#@+body
					try:
						# The OpenWithOldBody attribute does not normally exist in vnodes.
						old_body = v.OpenWithOldBody
						conflict = body != old_body and body != s
					except:
						conflict = v.isDirty() and body != s
					
					#@-body
					#@-node:2::<< set conflict flag >>

					
					if conflict:
						# Report the conflict & set update.
						import leoDialog
						d = leoDialog.leoDialog()
						message = "Conflict in %s.\n\n" % (v.headString())
						message += "Replace outline with external changes?"
						update = d.askYesNo("Conflict!",message) == "yes"
					else:
						update = s != body
					
					if update:
						h = v.headString()
						es("changed:" + h)
						v.setBodyStringOrPane(s)
						c.selectVnode(v)
						v.OpenWithOldBody = s
					
					#@-body
					#@-node:1::<< update v's body text >>

			except: pass
#@-body
#@-node:1::on_idle
#@+node:2::create_open_with_menu
#@+body
def create_open_with_menu (tag,keywords):

	if  (tag in ("start2","open2") or
		(tag=="command2" and keywords.get("label")=="new")):
	
		
		#@<< create the Open With menu >>
		#@+node:1::<< create the Open With menu >>
		#@+body
		#@+at
		#  Entries in the following table are the tuple (commandName,shortcut,data).
		# data is the tuple (command,arg,ext).
		# command is one of "os.system", "os.startfile", "os.spawnl", 
		# "os.spawnv" or "exec".
		# Leo executes command(arg+path) where path is the full path to the 
		# temp file.
		# If ext is not None, the temp file has the extension ext,
		# Otherwise, Leo computes an extension based on what @language 
		# directive is in effect.

		#@-at
		#@@c

		idle_arg = "c:/python22/tools/idle/idle.py -e "
		
		if 1: # Default table.
		 table = (
		  ("&Idle",   "Alt+Shift+I",("os.system",idle_arg,".py")),
		  ("&Word",   "Alt+Shift+W",("os.startfile",None,".doc")),
		  ("Word&Pad","Alt+Shift+T",("os.startfile",None,".txt")))
		else: # David McNab's table.
		 table = (
		  ("X&Emacs", "Ctrl+E", ("os.spawnl","/usr/bin/gnuclient", None)))
		
		top().frame.createOpenWithMenuFromTable(table)
		
		#@-body
		#@-node:1::<< create the Open With menu >>

		# Enable the idle-time hook so we can check temp files created by Open With.
		from leoGlobals import enableIdleTimeHook
		enableIdleTimeHook(idleTimeDelay=500)

#@-body
#@-node:2::create_open_with_menu
#@-others


if 1: # Register the handlers...
	registerHandler("idle", on_idle)
	registerHandler(("start2","open2","command2"), create_open_with_menu)
	
	import mod_open_with
	es("...open with v1.1: " + plugin_date(mod_open_with))

#@-body
#@-node:0::@file plugins/mod_open_with.py
#@-leo
