Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Emacs defaut font face per-buffer/mode

People also ask

How do I set the default font in Emacs?

You can use the menu bar. Go to Options -> Set Default Font... . After you choose a font, don't forget to press Options -> Save Options —otherwise your new font will not be saved after you close Emacs.

How do you change the font on Spacemacs?

Another way is to use the key sequence "SPC z x" and then press "+/=" key to increase font size or "-" key to decrease font size. Other options are shown on the which-key menu. This effects only the present buffer. By the way, you can customize the step size via the variable text-scale-mode-step .

How do I find my Emacs font?

For a given character, you can find out which font was used by moving point to that character than then doing C-u C-x = which will give you all kinds of information about that position in the buffer, including which font was used for it.


How about something like this:

(add-hook 'markdown-mode-hook (lambda () (variable-pitch-mode t))

You can then customize the variable-pitch face, and the other faces in the buffer will inherit from this instead of the default face.

Read the docs for buffer-face-mode for more customization details. (BufFace is also used for text-scale-increase and text-scale-decrease... very useful.)


I have to give a partial answer because this is too complicated to figure out on the spot and I already blew my time budget.

Face is a frame property. A frame can display multiple buffers at the same time. Mode is a buffer property. You ask how to vary the face on a per-mode basis. Combining all this, it seems that the question cannot not have a single fully-correct answer.

You can approximate the desired answer if you assume that a given frame will never display more than one buffer. You can actually accomplish that with something like this, but modified to use special-display-regexps and a set of regexps that match your markdown-mode buffer names.

(append special-display-buffer-names
        '("*VC-log*"
          "*Help*"
          ("*Completions*" 
           (height . 25)
           (font . "8x13"))))

However, this is probably not what you want. Your question seems to imply changing the face properties of a single frame.

Again assuming that a frame will never display more than one buffer at a time, you can try advising switch-to-buffer. But that might not be sufficiently low level and it might be too slow. (untested)

(defadvice switch-to-buffer (after switch-to-buffer activate compile)
  "change the frame's default face after switch-to-buffer"
  (doSomethingToChangePropertiesOfDefaultFace))

And now for my actual (incomplete) answer...

A better, albeit more complicated, approach would instruct markdown-mode to use a new face for all regions that are not already assigned one of the built-in faces. You can create a new face with copy-face and give it interesting properties with set-face-*.

Modify markdown-mode's font-lock-defaults to override the default font-lock-fontify-region-function as described in the comment block near line 946 of font-lock.el that begins, "Fontification functions". You can probably use a very slightly modified font-lock-default-fontify-region that does just one extra step immediately after it does:

  (unless font-lock-keywords-only
    (font-lock-fontify-syntactically-region beg end loudly))

The extra step parses the region similar to what font-lock-fontify-syntactically-region does, breaking the region into "interesting" sub-regions. But this time you find sub-regions that have the default face and you put-text-property those sub-regions to the new face that you previously created.

In all this feels like it should be only a couple lines of elisp in your .emacs file, plus make a copy of font-lock-default-fontify-region that has only a minor diff from the original (call one new function), plus make a copy of font-lock-fontify-syntactically-region and modify it to do your bidding (the most difficult part).

Actually, if you "after" advise font-lock-fontify-syntactically-region then you probably don't even need to modify font-lock-defaults or font-lock-default-fontify-region.


The variable-pitch-mode is awesome. I found out about it through this thread. But it can be made even more awesome:

(dolist (hook '(erc-mode-hook
        LaTeX-mode-hook
        org-mode-hook
        edit-server-start-hook
        markdown-mode-hook))
  (add-hook hook (lambda () (variable-pitch-mode t))))

Just add whatever mode you want sans-serif fonts in to the list.


There is a block of code which I find very convenient, from EmacsWiki. The advantage of this is that you can set not only font face, but conveniently configure :height, :width etc as well

;; 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)

Combined with load-theme-buffer-local package, you can even specify the color theme for the buffer easily:

(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)
  (load-theme-buffer-local 'leuven (current-buffer)))