http://bjoernstechblog.rueffer.info/posts/lisp/emacs/html/2012/11/28/black-on-white-light-on-dark-HTML-from-light-on-black-emacs-buffers/
last updated on 25 May 2018

28 November 2012

black on white / light-on-dark HTML from light on black emacs buffers

In my emacs I use a dark background with a light color for text and have the impression that it is less painful on the eyes that the more common black-on-white. So far so nice, but when I want to export buffer contents to HTML using htmlize-buffer to post it here, I get the same light on black which seems incompatible with the design of this blog. The solution is to advise the function htmlize-buffer to temporarily change the default face (that’s where I changed the colors to light on black) when the conversion is done. The actual code for the .emacs-file is as follows:

(defadvice htmlize-buffer-1 (around black-on-white first activate)
 "Advise a core function used by htmlize-buffer to use black on
white for the main text. This is useful if you mainly use bright
foreground colors on dark background.

(c) Bjoern Rueffer 2010
"
 (let ((fg (face-attribute 'default :foreground))
	   (bg (face-attribute 'default :background)))   
   (set-face-foreground 'default "black")   
   (set-face-background 'default "white")   
   ad-do-it
   (set-face-foreground 'default fg)   
   (set-face-background 'default bg)))

EDIT 2010-12-28: If your aquamacs is using htmlfontify instead of htmlize then you can still use the code above. All you have to do is change htmlize-buffer-1 into hfy-fontify-buffer.

Comments (through old commenting system)

marcop, 17 May 2012:  Works like a charm, elegant and simple. Made my day! I struggled a bit to find it though... go light-on-dark! (even on the Mac :)

Björn Rüffer — Copyright © 2009–2018 — bjoern.rueffer.info