Mercurial > my-enso-commands
comparison my-enso-commands.py @ 1:3924ba5f3621
Added a bunch of commands, many of which are experimental. Some require PyXPCOM, meaning that Enso must be running atop Firefox or something similar.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sun, 18 May 2008 00:25:03 -0700 |
parents | 429e04abae50 |
children | c28ae8f664ba |
comparison
equal
deleted
inserted
replaced
0:429e04abae50 | 1:3924ba5f3621 |
---|---|
5 import sys | 5 import sys |
6 import subprocess | 6 import subprocess |
7 import webbrowser | 7 import webbrowser |
8 import urllib | 8 import urllib |
9 import threading | 9 import threading |
10 import logging | |
10 from StringIO import StringIO | 11 from StringIO import StringIO |
12 | |
13 def cmd_menu(ensoapi): | |
14 import AppKit | |
15 import Foundation | |
16 | |
17 app = AppKit.NSApplication.sharedApplication() | |
18 | |
19 mainMenu = AppKit.NSMenu.alloc().initWithTitle_("Main") | |
20 ensoMenu = AppKit.NSMenu.alloc().initWithTitle_("Boogy") | |
21 | |
22 item = AppKit.NSMenuItem.alloc().init() | |
23 item.setTitle_("Boogy") | |
24 item.setSubmenu_(ensoMenu) | |
25 mainMenu.insertItem_atIndex_(item, 0) | |
26 | |
27 app.setMainMenu_(mainMenu) | |
28 app.setServicesMenu_(ensoMenu) | |
29 | |
30 app.registerServicesMenuSendTypes_returnTypes_( | |
31 [AppKit.NSStringPboardType], | |
32 [] | |
33 ) | |
34 | |
35 ensoapi.display_message(str(app.mainMenu())) | |
36 | |
37 def cmd_define(ensoapi): | |
38 import AppKit | |
39 | |
40 serviceName = "Look Up in Dictionary" | |
41 | |
42 text = ensoapi.get_selection().get("text", "") | |
43 | |
44 pb = AppKit.NSPasteboard.pasteboardWithName_("EnsoServiceContent") | |
45 | |
46 pb.declareTypes_owner_( [AppKit.NSStringPboardType], None ) | |
47 | |
48 try: | |
49 if not pb.setString_forType_( text, AppKit.NSStringPboardType ): | |
50 raise Exception( "Failed to set pasteboard data." ) | |
51 | |
52 if not AppKit.NSPerformService( serviceName, pb ): | |
53 raise Exception( "Failed to perform service." ) | |
54 finally: | |
55 pass | |
56 | |
57 def cmd_impersonate(ensoapi): | |
58 from xpcom import components | |
59 | |
60 class MyEventHandler: | |
61 _com_interfaces_ = components.interfaces.nsIDOMEventListener | |
62 | |
63 def __init__(self, corpus, author): | |
64 from nltk.probability import ConditionalFreqDist | |
65 | |
66 cfd = ConditionalFreqDist() | |
67 prev_word = None | |
68 for word in corpus.words(): | |
69 cfd[prev_word].inc(word) | |
70 prev_word = word | |
71 | |
72 self._cfd = cfd | |
73 self._author = author | |
74 self._last_word = None | |
75 | |
76 def handleEvent(self, event): | |
77 text = event.target.value[:event.target.selectionStart] | |
78 words = text.split() | |
79 if words and text[-1] == " ": | |
80 last_word = words[-1] | |
81 if last_word == self._last_word: | |
82 return | |
83 self._last_word = last_word | |
84 | |
85 #sorted = self._cfd[last_word].sorted() | |
86 suggestions = self._cfd[last_word].samples() | |
87 suggestions.sort(key=len, reverse=True) | |
88 suggestions = [sugg for sugg in suggestions | |
89 if len(sugg) > 3] | |
90 | |
91 # if suggestions: | |
92 # import random | |
93 # suggestion = random.choice(suggestions) | |
94 # cursor = event.target.selectionStart | |
95 # event.target.value = event.target.value[:cursor] + suggestion + " " + event.target.value[cursor:] | |
96 # cursor += len(suggestion) + 1 | |
97 # event.target.setSelectionRange(cursor, cursor) | |
98 | |
99 # suggestions = None | |
100 if suggestions: | |
101 cc = components.classes | |
102 asvc = cc["@mozilla.org/alerts-service;1"].getService( | |
103 components.interfaces.nsIAlertsService | |
104 ) | |
105 asvc.showAlertNotification( | |
106 "http://www.mozilla.com/favicon.ico", | |
107 "%s would say..." % self._author, | |
108 ", ".join(suggestions[:10]), | |
109 False, | |
110 None, | |
111 None, | |
112 None, | |
113 ) | |
114 | |
115 element = ensoapi.get_selection()["dom.element"] | |
116 | |
117 #from nltk.corpus import gutenberg | |
118 | |
119 #corpus, author = (gutenberg, "Project Gutenberg") | |
120 from nltk.corpus.reader.plaintext import PlaintextCorpusReader | |
121 | |
122 corpus = PlaintextCorpusReader( | |
123 os.getenv("HOME"), | |
124 ["argon.txt"] | |
125 ) | |
126 author = "Jim Theis" | |
127 | |
128 element.addEventListener( | |
129 "keyup", | |
130 MyEventHandler(corpus, author), | |
131 False | |
132 ) | |
133 | |
134 def cmd_extemporize(ensoapi): | |
135 element = ensoapi.get_selection().get("dom.element") | |
136 | |
137 text = "" | |
138 if element: | |
139 text = element.value | |
140 words = text.split() | |
141 if words: | |
142 from nltk.corpus.reader.plaintext import PlaintextCorpusReader | |
143 | |
144 corpus = PlaintextCorpusReader( | |
145 os.getenv("HOME"), | |
146 ["argon.txt"] | |
147 ) | |
148 | |
149 from nltk.probability import ConditionalFreqDist | |
150 import random | |
151 | |
152 cfd = ConditionalFreqDist() | |
153 prev_word = None | |
154 for word in corpus.words(): | |
155 cfd[prev_word].inc(word) | |
156 prev_word = word | |
157 | |
158 last_word = words[-1] | |
159 new_words = [] | |
160 for i in range(10): | |
161 potentials = cfd[last_word].samples() | |
162 if potentials: | |
163 next_word = random.choice(potentials) | |
164 else: | |
165 next_word = "Grignr" | |
166 new_words.append(next_word) | |
167 last_word = next_word | |
168 string = "" | |
169 for word in new_words: | |
170 if len(word) == 1 and not word.isalpha(): | |
171 string += "%s" % word | |
172 else: | |
173 string += " %s" % word | |
174 | |
175 element.value = text + string | |
11 | 176 |
12 def getFuelApplication(): | 177 def getFuelApplication(): |
13 from xpcom import components | 178 from xpcom import components |
14 fuel_cls = components.classes["@mozilla.org/fuel/application;1"] | 179 fuel_cls = components.classes["@mozilla.org/fuel/application;1"] |
15 fuel_abs = fuel_cls.createInstance() | 180 fuel_abs = fuel_cls.createInstance() |
16 fuel_if = components.interfaces.fuelIApplication | 181 fuel_if = components.interfaces.fuelIApplication |
17 return fuel_abs.queryInterface(fuel_if) | 182 return fuel_abs.queryInterface(fuel_if) |
18 | 183 |
19 def getCommandDispatcher(): | 184 def getXpcomObjInfo(thing): |
20 from xpcom import components | |
21 | |
22 ci = components.interfaces | |
23 cc = components.classes | |
24 | |
25 Application = getFuelApplication() | |
26 document = Application.activeWindow.activeTab.document | |
27 | |
28 mediator = cc["@mozilla.org/appshell/window-mediator;1"].getService(ci.nsIWindowMediator) | |
29 window = mediator.getMostRecentWindow("navigator:browser") | |
30 return window.document.commandDispatcher | |
31 | |
32 def cmd_info(ensoapi): | |
33 thing = getCommandDispatcher() | |
34 | |
35 thing._build_all_supported_interfaces_() | 185 thing._build_all_supported_interfaces_() |
36 lines = [] | 186 lines = [] |
37 for iname in thing._interface_names_: | 187 for iname in thing._interface_names_: |
38 methods = [] | 188 methods = [] |
39 properties = [] | 189 properties = [] |
45 lines.append("%s:" % iname) | 195 lines.append("%s:" % iname) |
46 if methods: | 196 if methods: |
47 lines.extend([" METHODS: %s" % ", ".join(methods)]) | 197 lines.extend([" METHODS: %s" % ", ".join(methods)]) |
48 if properties: | 198 if properties: |
49 lines.extend([" PROPERTIES: %s" % ", ".join(properties)]) | 199 lines.extend([" PROPERTIES: %s" % ", ".join(properties)]) |
50 | 200 return "\n\n".join(lines) |
51 ensoapi.set_selection( {"text" : "\n\n".join(lines)} ) | 201 |
202 def cmd_info(ensoapi): | |
203 thing = getFuelApplication() | |
204 ensoapi.set_selection( {"text" : getXpcomObjInfo(thing)} ) | |
52 | 205 |
53 def cmd_highlight(ensoapi): | 206 def cmd_highlight(ensoapi): |
54 """ | 207 """ |
55 Highlights your current selection. | 208 Highlights your current selection. |
56 """ | 209 """ |
57 | 210 |
58 window = getCommandDispatcher().focusedWindow | 211 window = ensoapi.get_selection()["dom.window"] |
59 document = window.document | 212 document = window.document |
60 sel = window.getSelection() | 213 sel = window.getSelection() |
61 if sel.rangeCount >= 1: | 214 if sel.rangeCount >= 1: |
62 range = sel.getRangeAt(0) | 215 range = sel.getRangeAt(0) |
63 newNode = document.createElement("span") | 216 newNode = document.createElement("span") |
827 if popen.returncode == 0: | 980 if popen.returncode == 0: |
828 msg = "done!" | 981 msg = "done!" |
829 else: | 982 else: |
830 msg = "an error occurred." | 983 msg = "an error occurred." |
831 ensoapi.display_message( msg ) | 984 ensoapi.display_message( msg ) |
985 | |
986 def cmd_html_template(ensoapi): | |
987 ensoapi.set_selection({"text" : HTML_TEMPLATE}) | |
988 | |
989 HTML_TEMPLATE = """\ | |
990 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | |
991 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
992 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |
993 <head> | |
994 <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> | |
995 <link rel="stylesheet" type="text/css" media="all" | |
996 href="" /> | |
997 <title></title> | |
998 </head> | |
999 <body> | |
1000 | |
1001 </body> | |
1002 <script type="text/javascript" src=""></script> | |
1003 </html> | |
1004 """ |