Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs dynamically change font-size based on window width and fill-column

Tags:

emacs

fonts

size

I'm using Emacs on a small netbook to write code, and I've set the font size so that approximately fill-column(79) characters fill the width of a single 'maximized' window (ie pretty big font size). However, if I open two windows side by side, I would like the font size in those windows to automatically shrink so that each window's width accommodates at least 79 characters.

Before I start looking deep into this (I'm not good at Lisp), is it possible to get the window's width on each resize, divide it by fill-columns, and based on that result select a monospaced font size?

like image 543
armones Avatar asked Jun 08 '12 14:06

armones


1 Answers

It seems to me the right place to do this would be window-size-change-functions:

Functions called before redisplay, if window sizes have changed. The value should be a list of functions that take one argument. Just before redisplay, for each frame, if any of its windows have changed size since the last redisplay, or have been split or deleted, all the functions in the list are called, with the frame as argument.

Specifically, stick something like this in your .emacs:

(defun window-width-to-font-size (window-width)
  ;; Insert a calculation to turn window width into 79 chars.
  )

(add-to-list
 'window-size-change-functions
 (lambda (frame)
   (dolist (window (window-list frame))
     (set-face-attribute
      'default nil
      :width (window-width-to-font-size (window-body-width window))))))
like image 157
Mikey Boldt Avatar answered Sep 21 '22 21:09

Mikey Boldt