medit/moo/moopython/plugins/python.py

169 lines
5.3 KiB
Python
Raw Normal View History

2005-11-07 19:24:34 -08:00
import moo
import gtk
import gobject
2005-11-08 11:00:32 -08:00
import pango
2005-11-15 13:08:06 -08:00
import re
2006-04-20 12:13:42 -07:00
import sys
import os
2006-08-05 00:07:34 -07:00
from moo.utils import _
2005-11-15 13:08:06 -08:00
2006-04-20 12:13:42 -07:00
if os.name == 'nt':
PYTHON_COMMAND = '"' + sys.exec_prefix + '\\python.exe" -u'
2006-04-20 12:13:42 -07:00
else:
PYTHON_COMMAND = 'python -u'
2006-04-20 12:13:42 -07:00
2005-11-08 11:00:32 -08:00
try:
import pyconsole
have_pyconsole = True
except ImportError:
have_pyconsole = False
2005-11-07 19:24:34 -08:00
2006-05-23 13:11:20 -07:00
PLUGIN_ID = "Python"
2005-11-07 19:24:34 -08:00
2005-11-15 13:08:06 -08:00
class FileLine(object):
def __init__(self, filename, line):
object.__init__(self)
self.filename = filename
self.line = line
2005-11-09 05:12:55 -08:00
class Plugin(moo.edit.Plugin):
def do_init(self):
editor = moo.edit.editor_instance()
xml = editor.get_ui_xml()
self.ui_merge_id = xml.new_merge_id()
2005-11-07 19:24:34 -08:00
2005-11-08 11:00:32 -08:00
if have_pyconsole:
moo.utils.window_class_add_action(moo.edit.EditWindow, "PythonConsole",
2006-08-05 00:07:34 -07:00
display_name=_("Python Console"),
label=_("Python Console"),
callback=self.show_console)
xml.add_item(self.ui_merge_id, "ToolsMenu",
"PythonConsole", "PythonConsole", -1)
2005-11-15 13:08:06 -08:00
""" Run file """
2006-05-12 13:07:07 -07:00
self.patterns = [
[re.compile(r'\s*File\s*"([^"]+)",\s*line\s*(\d+).*'), 1, 2],
[re.compile(r'\s*([^:]+):(\d+):.*'), 1, 2]
]
moo.utils.window_class_add_action(moo.edit.EditWindow, "RunFile",
2006-08-05 00:07:34 -07:00
display_name=_("Run File"),
label=_("Run File"),
stock_id=moo.utils.STOCK_EXECUTE,
accel="<shift>F9",
callback=self.run_file)
2006-05-05 14:17:32 -07:00
moo.edit.window_set_action_langs("RunFile", "sensitive", ["python"])
xml.add_item(self.ui_merge_id, "ToolsMenu",
"RunFile", "RunFile", -1)
2005-11-07 19:24:34 -08:00
return True
def do_deinit(self):
editor = moo.edit.editor_instance()
xml = editor.get_ui_xml()
xml.remove_ui(self.ui_merge_id)
moo.utils.window_class_remove_action(moo.edit.EditWindow, "PythonConsole")
moo.utils.window_class_remove_action(moo.edit.EditWindow, "RunFile")
moo.utils.window_class_remove_action(moo.edit.EditWindow, "ReloadPythonPlugins")
2006-05-26 22:40:03 -07:00
2005-11-08 11:00:32 -08:00
def show_console(self, window):
window = gtk.Window()
swin = gtk.ScrolledWindow()
swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
window.add(swin)
console_type = pyconsole.ConsoleType(moo.edit.TextView)
console = console_type(use_rlcompleter=False, start_script=
"import moo\nimport gtk\n")
2005-11-08 11:00:32 -08:00
console.set_property("highlight-current-line", False)
editor = moo.edit.editor_instance()
mgr = editor.get_lang_mgr()
lang = mgr.get_lang("PythonConsole")
console.set_lang(lang)
console.modify_font(pango.FontDescription("Courier New 11"))
swin.add(console)
2005-11-09 04:19:46 -08:00
window.set_default_size(400,300)
2006-08-05 00:07:34 -07:00
window.set_title(_("pythony"))
2005-11-08 11:00:32 -08:00
window.show_all()
2005-11-07 19:24:34 -08:00
2005-11-15 13:08:06 -08:00
def ensure_output(self, window):
pane = window.get_pane(PLUGIN_ID)
if not pane:
2006-08-05 00:07:34 -07:00
label = moo.utils.PaneLabel(icon_stock_id=moo.utils.STOCK_EXECUTE, label=_("Output"))
2005-11-15 13:08:06 -08:00
output = moo.edit.CmdView()
output.set_property("highlight-current-line", True)
output.connect("activate", self.output_activate)
output.connect("stderr-line", self.stderr_line)
pane = gtk.ScrolledWindow()
pane.set_shadow_type(gtk.SHADOW_ETCHED_IN)
pane.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
pane.add(output)
pane.show_all()
pane.output = output
window.add_pane(PLUGIN_ID, pane, label, moo.utils.PANE_POS_BOTTOM)
2006-05-02 11:21:53 -07:00
window.add_stop_client(output)
2005-11-15 13:08:06 -08:00
return pane
def output_activate(self, output, line):
data = output.get_line_data(line)
if not data:
return False
editor = moo.edit.editor_instance()
editor.open_file(None, output, data.filename)
doc = editor.get_doc(data.filename)
if not doc:
return True
editor.set_active_doc(doc)
doc.grab_focus()
2006-04-29 23:02:19 -07:00
if data.line > 0:
doc.move_cursor(data.line, -1, False, True)
2005-11-15 13:08:06 -08:00
return True
def stderr_line(self, output, line):
2006-05-12 13:07:07 -07:00
data = None
2005-11-15 13:08:06 -08:00
2006-05-12 13:07:07 -07:00
for p in self.patterns:
match = p[0].match(line)
if match:
data = FileLine(match.group(p[1]), int(match.group(p[2])) - 1)
break
2005-11-15 13:08:06 -08:00
2006-05-12 13:07:07 -07:00
if not data:
return False
2005-11-15 13:08:06 -08:00
line_no = output.start_line()
output.write(line, -1, output.lookup_tag("error"))
output.end_line()
output.set_line_data(line_no, data)
return True
def run_file(self, window):
doc = window.get_active_doc()
if not doc or not doc.save():
return
pane = self.ensure_output(window)
2006-05-20 00:48:24 -07:00
if pane.output.running():
return
2005-11-15 13:08:06 -08:00
pane.output.clear()
window.paned.present_pane(pane)
pane.output.run_command(PYTHON_COMMAND + ' "%s"' % doc.get_filename())
2005-11-15 13:08:06 -08:00
def do_detach_win(self, window):
2005-11-15 13:08:06 -08:00
window.remove_pane(PLUGIN_ID)
2006-05-29 00:43:09 -07:00
2006-08-03 13:14:32 -07:00
gobject.type_register(Plugin)
__plugin__ = Plugin