How to disable bold fonts by overriding set-face-attribute in Emacs?

2.5k views Asked by At

The idea is to redefine set-face-attribute so that it sets face attributes normally except for the :weight attribute, which shall always be set to normal (the default value, I think). With this I hope to disable bold fonts in Emacs once and for all.

I got this:

(fset 'original-set-face-attribute (symbol-function 'set-face-attribute))

(defun set-face-attribute (face frame &rest args)
  (progn
    (original-set-face-attribute face frame args)))

So far, it doesn't work. If I do (make-face-bold 'default) I get Wrong type argument: symbolp, (:weight bold). I think what I have to do is remove elements that contain :weight from the list of arguments args.

3

There are 3 answers

1
Ernest A On

Alright! I improved on abo-abo's solution and this is what I've come up with:

(defadvice set-face-attribute
  (before ignore-attributes (face frame &rest args) activate)
  (setq args
        (apply 'nconc
               (mapcar (lambda (i)
                         (let ((attribute (nth i args))
                               (value (nth (1+ i) args)))
                           (if (not (memq attribute
                                          set-face-ignore-attributes))
                               (list attribute value))))
                       (number-sequence 0 (1- (length args)) 2)))))

(setq set-face-ignore-attributes '(:weight :height :box))

It disables :height, :weight and :box attributes (this is configurable via the set-face-ignore-attributes variable) for most fonts. For this to work, it has to go at the very beginning of init.el before the font attributes are set.

1
Ernest A On

Following Aaron's suggestion, here's another solution using face-remap-add-relative.

(defun remap-faces-default-attributes ()
  (let ((family (face-attribute 'default :family))
        (height (face-attribute 'default :height)))
    (mapcar (lambda (face)
              (face-remap-add-relative
               face :family family :weight 'normal :height height))
          (face-list))))

(when (display-graphic-p)
  (add-hook 'minibuffer-setup-hook 'remap-faces-default-attributes)
  (add-hook 'change-major-mode-after-body-hook 'remap-faces-default-attributes))

This one gets rid of bold fonts everywhere and also of variable-width fonts and sets all faces to the same height. Basically it's like running Emacs in a terminal window except with more colours.

1
abo-abo On

Here's some code to start you off:

(defadvice set-face-attribute
    (before no-bold (face frame &rest args) activate)
  (setq args
        (mapcar (lambda(x) (if (eq x 'bold) 'normal x))
                args)))

I've seen this work for most of the cases, except for basic-faces that don't call set-face-attribute, for instance the error face.