#@+leo-ver=4
#@+node:@file startfile.py
"""Lauches (starts) a file with the name of the headline on double-clicking it.
Uses the @folder path if the headline is under an @folder headline.
Otherwise the path is relative to the Leo file.
Headlines starting with an '@' are ignored.
This does not work on Linux (yet)."""

# By Josef Dalcolmo: contributed under the same license as Leo.py itself.

from leoPlugins import *
from leoGlobals import *
import os

#@<< about this plugin >>
#@+node:<<about this plugin >>
#@+at 
#@nonl
# This plugin starts a file with the name of a headline.
# 
# 
#@-at
#@-node:<<about this plugin >>
#@nl
#@<< change log >>
#@+node:<< change log >>
#@+at 
#@nonl
# Change log
# 
# - JD: 2003-03-11 separated out from rst plugin
# 
# 
# 
#@-at
#@-node:<< change log >>
#@nl

#@+others
#@+node:onIconDoubleClick
# @folder behavior after an idea and sample code by:
# korakot ( Korakot Chaovavanich ) @folder for files annotation 2002-11-27 02:39
# 
# open file (double-click = startfile) behavior added 
# nodes with @url, @folder, @rst are treated special
#
# by Josef Dalcolmo 2003-01-13
#
# this does not check for proper filename syntax.
# path is the current dir, or the place @folder points to
# this should probably be changed to @path or so.

def onIconDoubleClick(tag,keywords):

	v = keywords.get("v")
	c = keywords.get("c")
	h = v.headString().strip()
	if len(h)==0 or h[0]=='@':
		return # Let other plugins handle these
	else:
		# open file with associated application
		#@		<< find path and start file >>
		#@+node:<< find path and start file >>
		# Set the base directory by searching for @folder directives in ancestors.
		try:
			basedir = os.curdir	# use current dir as default.
			parv = v.parent()	# start with parent
			while parv:	# stop when no more parent found
				p = parv.headString().strip()
				if match_word(p,0,'@folder'):
					basedir = p[8:]	# take rest of headline as pathname
					break	# we found the closest @folder
				else:
					parv = parv.parent()	# try the parent of the parent
			fname = os.path.join(basedir,h) # join path and filename
			os.startfile(fname)	# Try to open the file; it may not work for all file types.
		except:
			es(os.path.join(os.getcwd(),fname)+' - file or application not found')
			es_exception()
		#@-node:<< find path and start file >>
		#@nl
#@nonl
#@-node:onIconDoubleClick
#@-others

# Register the handlers...
registerHandler("icondclick1",onIconDoubleClick)

__version__ = "1.1"
plugin_signon(__name__)

def unitTest ():
	pass
#@-node:@file startfile.py
#@-leo
