Monday, November 27, 2023

Preventing Emacs Window Resizing on Startup

If you set the geometry of your Emacs window in elisp, you may find that the window redraws itself upon resizing. Note that I try to avoid using Xresources to set the geometry which is another solution to this problem. I'd like to control all of my settings in Emacs elisp.

To do this, we will replace default-frame-alist by setting it in the set-initial-frame function which will be associated with the before-init-hook. This hook is run before the frame is drawn preventing the window to resize.

In this example, I set the font to RobotoMono. The width is 170 characters wide and the height is y resolution of the screen less 200 pixels. I also disable other parts of the window like the menu bar, tool bar, and scroll bars.

It would be great if I can control the default background color before my theme kicks in.

(defun set-initial-frame ()
  "Defines and center the frame window"
  (let* ((width-chars 170)
         (height-buffer 200)
         (setq my-font "RobotoMono Nerd Font")
         (monitor-width (x-display-pixel-width))
         (monitor-height (- (x-display-pixel-height) height-buffer)))
    (setq default-frame-alist
          `((width . ,width-chars)
            (height . (text-pixels . ,monitor-height))
            (font . ,my-font)
            ;; Prevent the glimpse of un-styled Emacs by disabling these UI elements early.
            (menu-bar-lines . 0)
            (tool-bar-lines . 0)
            (horizontal-scroll-bars . nil)
            (vertical-scroll-bars . nil)))))

(add-hook 'before-init-hook #'set-initial-frame)

No comments: