rbanffy
rbanffy

Reputation: 2521

Interesting error with Emacs Lisp

While trying to streamline my init.el, I decided to move some functionality out of an ugly cond tree. In order to move some decisions out of it, I built two helper functions:

(defun abstract-screen-width ()
  (cond ((eq 'x window-system) (x-display-pixel-width))
        ((eq 'ns window-system) (display-pixel-width))
        ))

(defun perfect-font-size (pixels)
  (cond ((eq 'x window-system) (cond ((<= pixels 1024) 100)
                                     ((<= pixels 1366) 110)
                                     ((> pixels 1366) 120)))
        ((eq 'ns window-system) (cond ((<= pixels 1024) 110)
                                      ((<= pixels 1280) 120)
                                      ((> pixels 1280) 140)))))

And they combine nicely and invoking them the way they were intended to be invoked works fine.

(perfect-font-size (abstract-screen-width))
130

The custom-set-faces invocation as it was works

(custom-set-faces
        '(default ((t (:inherit nil :stipple nil :inverse-video nil :box nil
                                :strike-through nil :overline nil
                                :underline nil :slant normal :weight normal
                                :height 130 :width normal
                                :family "IBM 3270"))))
        '(linum ((t (:inherit default :foreground "#777" :height 130)))))

but my "better" version

(custom-set-faces
        '(default ((t (:inherit nil :stipple nil :inverse-video nil :box nil
                                :strike-through nil :overline nil
                                :underline nil :slant normal :weight normal
                                :height (perfect-font-size (abstract-screen-width)) :width normal
                                :family "IBM 3270"))))
        '(linum ((t (:inherit default :foreground "#777" :height 120)))))

doesn't. It gives a "Default face height not absolute and positive" error. The source in faces.el and cus-face.el didn't help much. Any hints?

Upvotes: 3

Views: 369

Answers (1)

Dirk
Dirk

Reputation: 31053

The expression

'(default ((t (:inherit nil :stipple nil :inverse-video nil :box nil
                            :strike-through nil :overline nil
                            :underline nil :slant normal :weight normal
                            :height (perfect-font-size (abstract-screen-width)) :width normal
                            :family "IBM 3270"))))

is quoted in its entirety, i.e., (perfect-font-size (abstract-screen-width)) won't be evaluated. Try backquote instead:

`(default ((t (:inherit nil :stipple nil :inverse-video nil :box nil
                            :strike-through nil :overline nil
                            :underline nil :slant normal :weight normal
                            :height ,(perfect-font-size (abstract-screen-width)) :width normal
                            :family "IBM 3270"))))

(note the backtick and the comma). The error is just emacs way of telling you, that it had preferred a number where it got the list (perfect-font-size (abstract-screen-width))

Upvotes: 9

Related Questions