Terminator Patched to Support Google Search Through Right Click Menu

Besides xterm which is my all time favourite terminal app, Terminator is the other terminal app that I use on an everyday basis. I do a lot of coding and compiling in Terminator using vim. Its been an annoyance ever since I started using Linux (which is about 10 years now), that everytime I have to google an error, there’s no way of doing it in one singe step from any existing terminal app. Today I had an especially rough day with compilation errors and the repeated copy/pasting into google irritated the hell out me. Thats when I decided to add this support to Terminator knowing that its a python app and should be relatively simple to modify. It was infact quite easy to add the support and you can see the result of the effort below.
terminator-google-search

The deb package with google search support can be downloaded from here.

This has been compiled on Ubuntu Lucid and the base version of terminator is 0.93.

The patch for terminator 0.93 to support the Google Search feature is pasted below.

diff -urNBp terminator-0.93/terminatorlib/terminal_popup_menu.py /home/pratik/work/terminator-0.93/terminatorlib/terminal_popup_menu.py
--- terminator-0.93/terminatorlib/terminal_popup_menu.py  2010-04-16 03:16:18.000000000 +0700
+++ /home/pratik/work/terminator-0.93/terminatorlib/terminal_popup_menu.py  2010-05-17 03:06:10.000000000 +0700
@@ -4,6 +4,7 @@
 """terminal_popup_menu.py - classes necessary to provide a terminal context 
 menu"""
 
+import pygtk
 import gtk
 
 from version import APP_NAME
@@ -33,6 +34,7 @@ class TerminalPopupMenu(object):
         url = None
         button = None
         time = None
+        text = None
 
         if event:
             url = terminal.check_for_url(event)
@@ -77,6 +79,26 @@ class TerminalPopupMenu(object):
         menu.append(item)
 
         menu.append(gtk.MenuItem())
+        
+        
+        google_with_quotes = 'Google Selected Text in Quotes'
+        icon = gtk.image_new_from_stock(gtk.STOCK_JUMP_TO,
+                                            gtk.ICON_SIZE_MENU)
+        item = gtk.ImageMenuItem(google_with_quotes)
+        item.connect('activate', lambda x: terminal.open_google_quoted(text, True))
+        item.set_sensitive(terminal.vte.get_has_selection())
+        menu.append(item)
+        
+        clipboard = gtk.clipboard_get()
+        text = clipboard.wait_for_text()
+        google_without_quotes = 'Google Selected Text'
+        icon = gtk.image_new_from_stock(gtk.STOCK_JUMP_TO,
+                                            gtk.ICON_SIZE_MENU)
+        item = gtk.ImageMenuItem(google_without_quotes)
+        item.connect('activate', lambda x: terminal.open_google(text, True))
+        item.set_sensitive(terminal.vte.get_has_selection())
+        menu.append(item)
+        menu.append(gtk.MenuItem())
 
         if not terminal.is_zoomed():
             item = gtk.ImageMenuItem('Split H_orizontally')
diff -urNBp terminator-0.93/terminatorlib/terminal.py /home/pratik/work/terminator-0.93/terminatorlib/terminal.py
--- terminator-0.93/terminatorlib/terminal.py 2010-04-16 03:16:18.000000000 +0700
+++ /home/pratik/work/terminator-0.93/terminatorlib/terminal.py 2010-05-17 03:06:10.000000000 +0700
@@ -1202,6 +1202,22 @@ for %s (%s)' % (name, urlplugin.__class_
                 import webbrowser
                 webbrowser.open(url)
 
+    def open_google(self, text, prepare=False):
+     """Open a given URL, conditionally unpacking it from a VTE match"""
+     url = "http://www.google.com/search?hl=en&q=" + text
+     print "URL %s" %(url)
+     print "TEXT %s" %(text)
+        import webbrowser
+        webbrowser.open(url)
+
+    def open_google_quoted(self, text, prepare=False):
+     """Open a given URL, conditionally unpacking it from a VTE match"""
+     url = "http://www.google.com/search?hl=en&q=" + '\"' + text + '\"'
+     print "URL %s" %(url)
+     print "TEXT %s" %(text)
+     import webbrowser
+     webbrowser.open(url)
+
     def paste_clipboard(self, primary=False):
         """Paste one of the two clipboards"""
         for term in self.terminator.get_target_terms(self):

The next step is to assign keyboard shortcuts to “Google Selected Text” and “Google Selected Text with Quotes” so that the invocation can be faster.

1 comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.