Commits

Anonymous committed 3afaa06

hg paste copies to pastebin now.

Comments (0)

Files changed (1)

 
 from mercurial import commands, cmdutil, patch
 
+def copy_to_clipboard(url):
+    """Copy the url into the clipboard."""
+    # try windows first
+    try:
+        import win32clipboard
+    except ImportError:
+        # then give pbcopy a try.  do that before gtk because
+        # gtk might be installed on os x but nobody is interested
+        # in the X11 clipboard there.
+        from subprocess import Popen, PIPE
+        for prog in 'pbcopy', 'xclip':
+            try:
+                client = Popen([prog], stdin=PIPE)
+            except OSError:
+                continue
+            else:
+                client.stdin.write(url)
+                client.stdin.close()
+                client.wait()
+                break
+        else:
+            try:
+                import pygtk
+                pygtk.require('2.0')
+                import gtk
+                import gobject
+            except ImportError:
+                return
+            gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD).set_text(url)
+            gobject.idle_add(gtk.main_quit)
+            gtk.main()
+    else:
+        win32clipboard.OpenClipboard()
+        win32clipboard.EmptyClipboard()
+        win32clipboard.SetClipboardText(url)
+        win32clipboard.CloseClipboard()
+
 def paste(ui, repo, *pats, **opts):
     import xmlrpclib
     server = ui.config('paste', 'server', 'http://paste.pocoo.org/')
     if not rv:
         ui.warn('Could not create paste; server returned failure.\n')
     else:
-        ui.status('Pasted: %sshow/%s/\n' % (server, rv))
+        url = '%sshow/%s/' % (server, rv)
+        ui.status(url + '\n')
+        copy_to_clipboard(url)
 
 cmdtable = {
     'paste':