Personal tools
You are here: Home Blog (English) Emacs perfection - selecting fonts according to screen size


 

Emacs perfection - selecting fonts according to screen size

Posted by Ricardo Bánffy at Aug 07, 2011 12:20 PM |
Filed under: , , , , ,

Most of the time, I work at my desk, where my netbook is hooked up to a reasonably sized monitor (the largest its feeble GPU can handle with acceleration). Since screen real-estate in that situation is abundant, I opted to use a larger font (one I made from the x3270 bitmap font, but that's another, much longer story). Unfortunately, when I am not at my desk, the 1024x600 LCD is quite limiting and the default options don't work for me. When I am away from the big screen monitor, screen real-estate is limited and a small font should be selected by default.

I started from the options the Custom menu gave me. Removing comments, it's a very simple snippet:

(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 140 :width normal :family "IBM 3270"))))
     '(linum ((t (:inherit default :foreground "#777" :height 110)))))

Translating that to English, I have a font called "IBM 3270" at a 14-point height, with line numbers with 11 points. Very readable.

But that doesn't solve the problem when I am on the road.

The friendly guys at Stack Overflow pointed out one way to do it: x-display-pixel-width and x-display-pixel-height.

With that in hand, I can do:

(if (> (x-display-pixel-width) 1280)
    ; screen is big
    (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 140 :width normal :family "IBM 3270"))))
     '(linum ((t (:inherit default :foreground "#777" :height 110)))))
    ; screen is small
    (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 110 :width normal :family "IBM 3270"))))
     '(linum ((t (:inherit default :foreground "#777" :height 80)))))
)

and I can be happy.

But can I?

When I start Emacs without X (as in from a remote terminal), these functions issue an ugly warning telling me that X is not available and that I should --debug-init and fix the problem. That's safely ignorable (as these adjustments are being done when everything non-cosmetic is already in place, at least in my init.el), but, still, annoying.

There is a variable, window-system, that can help - it holds "x" if we are under the X windowing system and nil if we are using a character terminal. With it, I can do:

(if (and (eq 'x window-system) (> (x-display-pixel-width) 1280))
    ; screen is big
    (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 140 :width normal :family "IBM 3270"))))
     '(linum ((t (:inherit default :foreground "#777" :height 110)))))
    ; screen is small
    (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 110 :width normal :family "IBM 3270"))))
     '(linum ((t (:inherit default :foreground "#777" :height 80)))))
)

short-circuiting the x-display-pixel-width and allowing Emacs starts cleanly from a remote terminal session. Since window-system also can tell you if you are on a Mac (or NeXT, if you are into retrocomputing) or under Windows (you can't argue taste), you can take appropriate actions according to your environment.

I am happy for today.