Skip to content

Instantly share code, notes, and snippets.

@leftaroundabout
Created May 31, 2015 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leftaroundabout/eb228668391412c35135 to your computer and use it in GitHub Desktop.
Save leftaroundabout/eb228668391412c35135 to your computer and use it in GitHub Desktop.
Edit a gtk-clipboard “in-place”.
#!/usr/bin/env python
import gtk
import chardet
import os
import getopt
import subprocess
dtype = 'text/html'
htmlclip = gtk.Clipboard().wait_for_contents(dtype).data
encoding = chardet.detect(htmlclip)['encoding']
# Shove the clipboard to a temporary file
tmpfn = '/tmp/htmlclip_%i' % os.getpid()
with open (tmpfn, 'w') as editfile:
editfile.write(htmlclip.decode(encoding))
# Manually edit the temporary file
subprocess.call(['vi', tmpfn])
with open (tmpfn, 'r') as editfile:
htmlclip = editfile.read().encode(encoding)
# Put the modified data back to clipboard
gtk.Clipboard().set_with_data(
[(dtype,0,0)],
lambda cb, sd, info, data: sd.set(dtype, 8, htmlclip),
lambda cb, d: None )
gtk.Clipboard().set_can_store([(dtype,0,0)])
gtk.Clipboard().store()
@leftaroundabout
Copy link
Author

This is intended a useful hack to make up for Thunderbird's lack of a proper html source editor. Just select all, copy, execute this script (which invokes vi or whatever editor you specify), manipulate, close and paste back into the Thunderbird composition window.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment