changeset 2:4f22928c7a2c

Added firefox selection context, so it's now possible to use enso in firefox (otherwise, the command-c injection wasn't working b/c I guess you can't inject events into your own process).
author Atul Varma <varmaa@toolness.com>
date Thu, 08 May 2008 13:00:26 -0700
parents 6e4335fa3321
children 21f5e1dc0a4c
files enso_pyxpcom.py
diffstat 1 files changed, 54 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/enso_pyxpcom.py	Thu May 08 11:16:05 2008 -0700
+++ b/enso_pyxpcom.py	Thu May 08 13:00:26 2008 -0700
@@ -8,6 +8,57 @@
 ENSO_PATH = os.path.join(os.getenv("HOME"), "Documents/enso-svn")
 LOGGING_FILE = os.path.join(os.getenv("HOME"), "enso_pyxpcom.log")
 
+def getFuelApplication():
+    from xpcom import components
+    fuel_cls = components.classes["@mozilla.org/fuel/application;1"]
+    fuel_abs = fuel_cls.createInstance()
+    fuel_if = components.interfaces.fuelIApplication
+    return fuel_abs.queryInterface(fuel_if)
+
+def getCommandDispatcher():
+    ci = components.interfaces
+    cc = components.classes
+
+    Application = getFuelApplication()
+    document = Application.activeWindow.activeTab.document
+
+    mediator = cc["@mozilla.org/appshell/window-mediator;1"].getService(ci.nsIWindowMediator)
+    window = mediator.getMostRecentWindow("navigator:browser")
+    return window.document.commandDispatcher
+
+def firefoxSelectionHook():
+    import AppKit
+
+    app = AppKit.NSWorkspace.sharedWorkspace().activeApplication()
+    if app["NSApplicationProcessIdentifier"] == os.getpid():
+        return getFirefoxSelection()
+    else:
+        from enso.platform.osx import selection
+
+        return selection._old_get()
+
+def getFirefoxSelection():
+    text = ""
+    dispatcher = getCommandDispatcher()
+
+    if dispatcher.focusedElement:
+        start = dispatcher.focusedElement.selectionStart
+        end = dispatcher.focusedElement.selectionEnd
+        if start != end:
+            text = dispatcher.focusedElement.value[start:end]
+    elif dispatcher.focusedWindow:
+        sel = dispatcher.focusedWindow.getSelection()
+        if sel.rangeCount >= 1:
+            text = sel.toString()
+
+    return {"text" : text}
+
+def installFirefoxSelectionHook():
+    from enso.platform.osx import selection
+
+    selection._old_get = selection.get
+    selection.get = firefoxSelectionHook
+
 def getAbout():
     global _ensoStarted
 
@@ -16,6 +67,9 @@
         sys.path.append(ENSO_PATH)
 
         import enso
+
+        logging.info("Installing Firefox selection hook.")
+        installFirefoxSelectionHook()
         logging.info("Launching Enso.")
         _ensoStarted = True
         enso.run()