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

"""Opens up cmd and explorer window to same directory 
as @file nodes or children ..."""

#@<< about the open shell plugin >>
#@+node:<< about the open shell plugin >>
#@+at 
#@nonl
# Written by Ed Taekema.  Modified by E.K.Ream
# 
# Please submit bugs / feature requests to etaekema@earthlink.net"""
# 
# This is a simple plugin for leo 3.12 that allows the user to open either an 
# xterm on linux or a cmd windows/explorer window on win32 in the directory of 
# the current @file.  This allows quick navigation to facilitate testing and 
# navigating large systems with complex direcgtories.
# 
# Current limitations ...
# 
# 1. Not tested on Mac OS X ...
# 2. On win32, the cmd window will not open in the right directory if the 
# @file location is on a different drive than the .leo file that is being 
# edited.
# 3. On linux, xterm must be in your path.
#@-at
#@-node:<< about the open shell plugin >>
#@nl

import leo,os,os.path,sys
from leoPlugins import *
from leoGlobals import *

pathToExplorer = 'c:/windows/explorer.exe'
pathToCmd = 'c:/windows/system32/cmd.exe'

#@+others
#@+node:load_menu
def load_menu(tag,keywords):
	
	if sys.platform=="win32":
		table = (
			("&Open Console Window",None,launchCmd),
			("Open &Explorer",None,launchExplorer)) 
	else:
		table = ( ("Open &xterm",None,launchxTerm), ) 
	
	top().frame.menu.createNewMenu("E&xtensions","top")
	top().frame.menu.createMenuItemsFromTable("Extensions",table)
#@-node:load_menu
#@+node:_getpath
def _getpath(c,v):
	dict = scanDirectives(c,v)
	d = dict.get("path")

	if d == None:

		if v.isAtFileNode():
			filename = v.atFileNodeName()
		if v.isAtNoSentinelsFileNode():
			filename = v.atNoSentinelsFileNodeName()
		if v.isAtRawFileNode():
			filename = v.atRawFileNodeName()
		if v.isAtSilentFileNode():
			filename = v.atSilentFileNodeName()

		d = os.path.dirname(filename)

	d = os.path.normpath(d)
	return d
#@-node:_getpath
#@+node:_getcurrentnodepath
def _getCurrentNodePath():
	c = leo.top()
	v = c.currentVnode()
	f = v.atFileNodeName()
	d = _getpath(c,v)
	return d
#@-node:_getcurrentnodepath
#@+node:launchCmd
def launchCmd(event=None):
	
	global pathToCmd

	d = _getCurrentNodePath()
	myCmd = 'cd ' + d
	os.spawnl(os.P_NOWAIT, pathToCmd, '/k ', myCmd)
#@nonl
#@-node:launchCmd
#@+node:launchExplorer
def launchExplorer(event=None):
	
	global pathToExplorer

	d = _getCurrentNodePath()
	os.spawnl(os.P_NOWAIT,pathToExplorer, ' ', d)

#@-node:launchExplorer
#@+node:launchxTerm
def launchxTerm(not_used):

	d = _getCurrentNodePath()
	curdir = os.getcwd()
	os.chdir(d)
	os.spawnlp(os.P_NOWAIT, 'xterm', '-title Leo')
	os.chdir(curdir)
#@nonl
#@-node:launchxTerm
#@-others

# Register the handlers...
registerHandler("after-create-leo-frame", load_menu)

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