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

"""Replace Commands.openWithTempFilePath so Leo opens temporary
files with a filename that begins with the headline text, and
located in a "username_Leo" subdirectory of the temporary
directory. The "LeoTemp" prefix is omitted.  This makes it easier to
see which temporary file is related to which outline node."""

from leoPlugins import *
from leoGlobals import *

import leoCommands
import getpass,os,tempfile

#@+others
#@+node:onStart
def onStart (tag,keywords):

	# trace("replacing openWithTempFilePath")

	funcToMethod(openWithTempFilePath,leoCommands.Commands,"openWithTempFilePath")
#@-node:onStart
#@+node:openWithTempFilePath
def openWithTempFilePath (self,v,ext):

	"""Return the path to the temp file corresponding to v and ext.

	Replaces the Commands method."""    

	try:
		leoTempDir = getpass.getuser() + "_" + "Leo"
	except:
		leoTempDir = "LeoTemp"
		es("Could not retrieve your user name.")
		es("Temporary files will be stored in: %s" % leoTempDir)

	td = os.path.join(os.path.abspath(tempfile.gettempdir()), leoTempDir)
	if not os.path.exists(td):
		os.mkdir(td)

	name = sanitize_filename(v.headString()) + '_' + str(id(v.t))  + ext
	path = os.path.join(td,name)
	return path
#@nonl
#@-node:openWithTempFilePath
#@-others

# Register the handlers...
registerHandler("start2", onStart)

__version__ = "1.3"
plugin_signon(__name__)
#@nonl
#@-node:@file mod_tempfname.py
#@-leo
