WThirtyTwoFontifiedRegionToClipboard

Difference between revision 41 and current revision

Summary: Rollback to 2013-07-27 07:54 UTC

No diff available.

(defun w32-fontified-region-to-clipboard (START END)
  "Htmlizes region, saves it as a html file, scripts Microsoft Word to
open in the background and to copy all text to the clipboard, then
quits. Useful if you want to send fontified source code snippets to
your friends using RTF-formatted e-mails.

Version: 0.1

Author:

Mathias Dahl, <mathias@cucumber.dahl.net>. Remove the big, green
vegetable from my e-mail address...

Requirements:

* htmlize.el
* wscript.exe must be installed and enabled
* Microsoft Word must be installed

Usage:

Mark a region of fontified text, run this function and in a number of
seconds you have the whole colorful text on your clipboard, ready to
be pasted into a RTF-enabled application.

"
  (interactive "r")
    (let ((snippet (buffer-substring START END))
          (buf (get-buffer-create "*htmlized_to_clipboard*"))
          (script-file-name (expand-file-name "~/htmlized_to_clipboard.vbs"))
          (htmlized-file-name (expand-file-name "~/htmlized.html")))
      (set-buffer buf)
      (delete-region (point-min) (point-max))
      (insert snippet)
      (htmlize-buffer)
      (write-file htmlized-file-name)
      (delete-region (point-min) (point-max))
      (setq htmlized-file-name 
            (substitute ?\\ ?/ htmlized-file-name))
      (insert
       (concat
        "Set oWord = CreateObject(\"Word.Application\")\n"
        "oWord.Documents.Open(\"" htmlized-file-name "\")\n"
        "oWord.Selection.HomeKey 6\n"
        "oWord.Selection.EndKey 6,1\n"
        "oWord.Selection.Copy\n"
        "oWord.Quit\n"
        "Set oWord = Nothing\n"))
      (write-file script-file-name)
      (kill-buffer "htmlized_to_clipboard.vbs")
      (setq script-file-name
            (substitute ?\\ ?/ script-file-name))
      (w32-shell-execute nil "wscript.exe" 
                         script-file-name)))

See also PostScriptToPDF for similar functionality.

It seems that there is a bug: after htmlize-buffer, it didn’t set-buffer to the result of htmlize-buffer. Following is my (lgfang) fix for it.

    *** c:/lgfang/myhome/tmp/orginal.el	Tue Dec 26 18:14:15 2006
    --- c:/lgfang/myhome/tmp/fixed.el	Tue Dec 26 18:14:24 2006
    ***************
    *** 4,10 ****
      quits. Useful if you want to send fontified source code snippets to
      your friends using RTF-formatted e-mails.
    
    ! Version: 0.1
    
      Author:
    
    --- 4,10 ----
      quits. Useful if you want to send fontified source code snippets to
      your friends using RTF-formatted e-mails.
    
    ! Version: 0.2
    
      Author:
    
    ***************
    *** 32,39 ****
          (set-buffer buf)
          (delete-region (point-min) (point-max))
          (insert snippet)
    !     (htmlize-buffer)
    !     (write-file htmlized-file-name)
          (delete-region (point-min) (point-max))
          (setq htmlized-file-name
                (substitute ?\\ ?/ htmlized-file-name))
    --- 32,42 ----
          (set-buffer buf)
          (delete-region (point-min) (point-max))
          (insert snippet)
    !     (let ( (tmp-html-buf (htmlize-buffer)) )
    !       (set-buffer tmp-html-buf)
    !       (write-file htmlized-file-name)
    !       (kill-buffer tmp-html-buf))
    !     (set-buffer buf)
          (delete-region (point-min) (point-max))
          (setq htmlized-file-name
                (substitute ?\\ ?/ htmlized-file-name))

I also tried htmlize-region. But that would cause Microsoft VBScript compilation error. Why?


CategoryWThirtyTwo