FacesPerBuffer

GNU Emacs

Starting with Emacs 23, you can set the face for the current buffer, using ‘M-x buffer-face-set’. You can toggle this on/off using ‘M-x buffer-face-mode’. Internally, this uses ‘face-map-add-relative’ to remap faces. For example, (face-remap-add-relative 'default :family "Source Code Pro" :height 140)

Prior to Emacs 23, you can use the fact that font-lock uses a variable with the same name as the font-lock face. For example, font-lock does not necessarily use ‘font-lock-comment-face’ for comments. It uses the face that is the value of variable ‘font-lock-comment-face’, which by default is face ‘font-lock-comment-face’.

 (make-face 'php-comment-face)
 (set-face-foreground 'php-comment-face "LightGrey")
 (add-hook 'php-mode-hook 
           (lambda ()
            ;; ...
            (set (make-local-variable 'font-lock-comment-face)
                 'php-comment-face)
            ;; ...

XEmacs

XEmacs supports per-frame, per-window, and per-buffer face settings, using the confusing “specifier” functions:

 From: QuoteMstr - Danny Colascione
 Subject: Re: Switch buffers and faces
 Newsgroups: comp.emacs
 Date: Mon, 04 Jun 2001 17:38:10 GMT
 In XEmacs, on the other hand, the following will work:
 (set-specifier (face-background (get-face 'default)) "blue"
                (get-buffer "*scratch*"))
 Obviously, replace *scratch* and "blue" with whatever you want. (In a
 hook, you could just use (current-buffer)

Automatically setting default faces per buffer

If you want to use different fonts per buffer (say monospaced font by default and variable size font in Info, ERC, etc.) you can use something like this:

 ;; Use variable width font faces in current buffer
 (defun my-buffer-face-mode-variable ()
   "Set font to a variable width (proportional) fonts in current buffer"
   (interactive)
   (setq buffer-face-mode-face '(:family "DejaVu Sans" :height 100 :width semi-condensed))
   (buffer-face-mode))
 ;; Use monospaced font faces in current buffer
 (defun my-buffer-face-mode-fixed ()
   "Sets a fixed width (monospace) font in current buffer"
   (interactive)
   (setq buffer-face-mode-face '(:family "Consolas" :height 100))
   (buffer-face-mode))
 ;; Set default font faces for Info and ERC modes
 (add-hook 'erc-mode-hook 'my-buffer-face-mode-variable)
 (add-hook 'Info-mode-hook 'my-buffer-face-mode-variable)
 
 ;; Control + scroll to change font type
 (global-set-key [C-mouse-4] 'my-buffer-face-mode-fixed)
 (global-set-key [C-mouse-5] 'my-buffer-face-mode-variable)
 
 ;; Shift + scroll to change font size
 (global-set-key [S-mouse-4] 'text-scale-increase)
 (global-set-key [S-mouse-5] 'text-scale-decrease)
 

CategoryFaces