Question

In emacs, I can change the font-size of current buffer by text-scale-adjust. However, to change the font, I only find functions like set-face-font or set-frame-font, which will change the font globally in emacs (or change all buffers' font within current frame).

Is there a function in emacs that only changes the font (default font) of current buffer, while not modifying the fonts in any other buffers?

Was it helpful?

Solution

I found a way, though it has some side effects as it changes the global variable buffer-face-mode-face

For example, I want to set current buffer, I can eval this function definition and run it

 (defun my-buffer-face-mode-serif ()
   "Sets a fixed width (monospace) font in current buffer"
   (interactive)
   (setq buffer-face-mode-face '(:family "Times New Roman"))
   (buffer-face-mode))

OTHER TIPS

As of Emacs 23, you can change the face per buffer via M-x buffer-face-set. See http://www.emacswiki.org/emacs/FacesPerBuffer . So, taking Firegun's answer, you can use buffer-set-face like so:

(defun jmi/set-buffer-local-family (font-family)
  "Sets font in current buffer"
  (interactive "sFont Family: ")
  (defface tmp-buffer-local-face 
    '((t :family font-family))
    "Temporary buffer-local face")
  (buffer-face-set 'tmp-buffer-local-face))

(NB: This still suffers from the problem that face names themselves are global)

In GNU Emacs, a font is part of a face. Faces are generally global, so changing the font of a face affects all buffers, by default.

But you can give a face a buffer-local setting, using the functions in face-remap.el - see the Elisp manual, node Face Remapping. Or you can use function buffer-set-face, as @jmibanez's answer says.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top