Set Emacs defaut font face per-buffer/mode

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 18:44:51

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.

tseno tanev - цено танев

It's actually straightforward even for emacs version 22.3.1…

Just try the following:

(progn 
  (set-buffer "your buffer name here")
  (overlay-put (make-overlay 0 (buffer-size)) 'face 'your-face))

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)))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!