2

Are there any standard techniques, packages or the like for managing font settings in emacs?

I would like to define default fixed and variable width fonts somewhere once, and set the fonts of faces in modes using those defaults. Eg, set org-mode's default font to the variable width default, and its code face to fixed the fixed width default.

Channing Walton
  • 3,977
  • 1
  • 30
  • 59
  • 1
    May be [ColorTheme](http://www.emacswiki.org/emacs?action=browse;oldid=ColorTheme;id=ColorAndCustomThemes) will help. I don't know if you can have a theme for a mode but you can quickly switch theme with icycles. – DJJ Feb 10 '15 at 09:57
  • This is basically a software recommendation. Try asking at emacs.stackexchange.com – Squidly Feb 10 '15 at 10:16
  • Thanks I didn't realise there was an emacs.stackexchange.com – Channing Walton Feb 10 '15 at 12:37
  • I wonder why this was marked down when this very similar question was ok http://stackoverflow.com/questions/3758139/ – Channing Walton Feb 10 '15 at 22:15
  • 1
    Basically, the people who monitor this thread are hard-core programmers and they expect a classic programming question -- e.g., sample code from the original poster of what they have tried, and an explanation of what they expect to happen (but which is not happening). It used to be the situation where general questions were just referred to superuser (where the O.P. ends up answering those questions themselves most of the time), but now the beta Emacs exchange has proven to be extremely useful for both kinds of questions -- i.e., difficult programming questions, *plus* other questions. – lawlist Feb 11 '15 at 00:06

3 Answers3

5

Here's an adaptation of the system that I have. I've since defaulted to using just fixed pitch, but these calls will allow you to define the font to use for modes using the mode hook.

I call the method 'toggle-pitch to switch between fixed and variable pitch fonts on the fly when necessary as well.

(set-face-font 'default "Source Code Pro Semibold-9")
(set-face-font 'variable-pitch "Segoe UI Semibold-9")
(copy-face 'default 'fixed-pitch)

;;============================================================
;; toggle between variable pitch and fixed pitch font for 
;; the current buffer
(defun fixed-pitch-mode ()
  (buffer-face-mode -1))

(defun variable-pitch-mode ()
  (buffer-face-mode t))

(defun toggle-pitch (&optional arg)
  "Switch between the `fixed-pitch' face and the `variable-pitch' face"
  (interactive)
  (buffer-face-toggle 'variable-pitch))

;; enable buffer-face mode to provide buffer-local fonts
(buffer-face-mode)

;; Set the fonts to format correctly
;(add-hook 'text-mode-hook 'fixed-pitch-mode)
;(add-hook 'dired-mode-hook 'fixed-pitch-mode)
;(add-hook 'calendar-mode-hook 'fixed-pitch-mode)
;(add-hook 'org-agenda-mode-hook 'fixed-pitch-mode)
;(add-hook 'shell-mode-hook 'fixed-pitch-mode)
;(add-hook 'eshell-mode-hook 'fixed-pitch-mode)
;(add-hook 'bs-mode-hook 'fixed-pitch-mode)
;(add-hook 'w3m-mode-hook 'variable-pitch-mode)
;(add-hook 'org-mode-hook 'variable-pitch-mode)
(add-hook 'eww-mode-hook 'variable-pitch-mode)
Chris McMahan
  • 2,640
  • 1
  • 17
  • 10
2

A really useful way to set faces is to use the M-x list-faces-display function. This function will pop up a buffer with the face name on the left and an example on the right. This buffer then lets you customize any of the faces and to see what the face definition is. The one possible 'gotcha' is that it will only display the faces it knows about at the time you run the function. This means that if you haven't loaded org-mode for example, you may not see any org-mode face definitions.

One of the useful options when defining a face is the inherit option, which tells the face to inherit the settings of another face. Some of the inherited values can be overridden (such as face colour) in the 'main' face definition.

So, in your case, I would set the appropriate values for the default fixed and variable width fonts, then set the other faces i.e. org-mode faces to inherit from those definitions.

doing it this way shold eliminate the need to do any add-hook jiggery pokery

Tim X
  • 4,158
  • 1
  • 20
  • 26
0

I finally solved it like this:

First set fixed-pitch and variable-pitch fonts for use in general:

(custom-set-faces
 '(fixed-pitch ((t (:family "Input Mono Narrow"))))
 '(variable-pitch ((t (:family "Arial")))))

Then in the hook for your mode:

(add-hook 'org-mode-hook nil :inherit 'variable-pitch)
Channing Walton
  • 3,977
  • 1
  • 30
  • 59
  • That `add-hook` call looks extraordinarily broken. What do you believe it's doing? – phils Feb 11 '15 at 00:23
  • The doc-string for `add-hook` -- i.e., `M-x describe-function RET add-hook RET` tells us that it has 4 arguments -- hook name; function name; whether to append the function to the end of the hook list; and whether to make the hook assignment buffer-local. Your hook statement says that there is no function attached to the hook, and then you assign both an append argument and a buffer-local argument to a function that does not exist within the hook. Normally, the 3rd and 4th arguments are either `nil` or `t`. The 2nd argument is a name of a function, or a lambda (which is discouraged). – lawlist Feb 11 '15 at 08:17
  • Yeah, I think I am screwing this up every way possible. I am not sure where I found that on the web. This looks more promising: http://stackoverflow.com/a/534961/434405 – Channing Walton Feb 11 '15 at 21:51