Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Both `fullheight` and `width` in Emacs on OS X

I find the default size of the Emacs frame a little too small. From reading around I know that I can set the height and width quite easily with something like the following:

;;; 140 x 60 window size
(setq default-frame-alist '((width . 140) (height . 60)))

Which works great on my external monitor, however it is a litte too big for the laptop display. I can solve the height problem by changing to the follwing:

;;; automatically set the height
(setq default-frame-alist '((fullscreen . fullheight)))

Which sets the frame to be as tall as possible for the current screen. I can't however set the width of the frame if I use this method. Adding (width . 140) to the above alist sets the width to the right value but also sets the height to the default height again.

When I see the frame appear it sets itself to the full height, and then sets the width to the value I requested, and shrinks in height.

I can overcome this problem with the following code:

;;; Full height for the default window
(setq default-frame-alist
      '((fullscreen . fullheight)))
;; Set the width in a hook and have all windows inherit
(setq frame-inherited-parameters
      '(width height))
(add-hook 'after-init-hook
          (lambda ()
                  (set-frame-parameter nil 'width 140)))

Which uses a hook to set the width of the first frame to the value I want, and then sets all other windows to inherit this value.

This isn't very elegant however, so the question is "how can I accomplish this in a simpler (or less hackish) way?".

If you want to see my exact init.el script, take a look at this gist

TL;DR

How can I set both the width of a frame, and set the frame to be as tall as possible on the current monitor, on OS X? It seems you can't specify (width . 140) and (fullscreen . fullheight) in the default-frame-alist.

like image 674
Will Avatar asked Jun 28 '13 10:06

Will


1 Answers

I have come up with a solution to this. I explicitly calculate the height of the window rather than relying on (fullscreen . fullheight) to do it for me.

The updated code to set the values for the height and width is quite simple:

;;; Nice size for the default window
(defun get-default-height ()
       (/ (- (display-pixel-height) 120)
          (frame-char-height)))

(add-to-list 'default-frame-alist '(width . 140))
(add-to-list 'default-frame-alist (cons 'height (get-default-height)))

In this code the subtraction of 120 from the height of the screen makes sure that the height of the window takes into account the height of the dock and the menubar. For correct results you will have to make sure that this code is executed after you have chosen the font face to use, otherwise the computed height value will not be valid.

Placing the height calculation in its own function should allow special casing certain operating systems and versions. This method also has the added advantage that is faster to open the window as it doesn't "animate" the height to the full height value.

like image 89
Will Avatar answered Nov 03 '22 17:11

Will