"""
Open_in_Browser.py - A Jython macro for jEdit that opens
the current buffer in the default Web Browser for your
system. *Requires Python.*

Copyright (C) 2002 Ollie Rutherfurd <oliver@rutherfurd.net>

$Id: Open_in_Browser.py 28 2003-03-25 22:40:28Z oliver $
"""

from java.lang import Runnable, Runtime
import java.io.IOException
from javax.swing import SwingUtilities
from org.gjt.sp.jedit import Macros

class Process(Runnable):
	def __init__(self, cmd, view):
		self.cmd = cmd
		self.view = view
	def run(self):
		try:
			process = Runtime.getRuntime().exec(self.cmd)
			process.waitFor()
		except (Exception, java.io.IOException),e:
			msg = 'Error Executing:\n    %s\nException:\n    %s\n\nDo you have Python installed?' % (self.cmd, str(e))
			Macros.error(self.view, msg)
		self.view = None

def openInBrowser(view):
	path = view.getBuffer().getPath()
	path = path.replace('\\','\\\\')
	cmd = '''python -c "import webbrowser; webbrowser.open('%s')"''' % path
	SwingUtilities.invokeLater(Process(cmd,view))

if __name__ in ('__main__','main'):
	openInBrowser(init.view)

# :indentSize=4:lineSeparator=\n:noTabs=false:tabSize=4:

