Begin main content

Aquamacs copying styled text

Some time ago I switched from X11 GNU emacs to Aquamacs - the Aqua Mac native port of GNU emacs. The latest version of Aquamacs has a neat Edit menu item "Copy as HTML" which basically makes a convenient way to use the htmlize.el package to put a colourised version, in HTML format, of the current selection on the Mac clipboard.

That's great for posting into, for example, a blog post—but what about into other Cocoa apps? For that you still only need html, but it has to have the correct UTI on the clipboard to be recognised. The Aquamacs/GNU Emacs copy internals for the Mac nearly work flawlessly, and the following slightly hacky function will put a "Copy as Styled Text" into your edit menu:


(defuncopy-as-styled-text(beg end)"Copies the region as HTML styled text into the clipboard."(interactive "r")(when(or (not transient-mark-mode) mark-active)(let((x-select-enable-clipboard t)(buf (aquamacs-convert-to-html-buffer beg end)));; the copy as html is more reliable if we've copied plain text first
;; (which seems to clear the clipboard of all types)
(with-current-buffer buf
    (copy-region-as-kill (point-min)(point-max)))(with-current-buffer buf
        (ns-store-cut-buffer-internal 'PRIMARY (buffer-string) 'html))(kill-buffer buf))))(define-key-after menu-bar-edit-menu [copy-styled]
  '("Copy as Styled Text" . copy-as-styled-text) 'copy-html)

05:33 AM, 24 Sep 2010 by Mark Aufflick Permalink

Add comment