Save Ukraine

Emacs: restore last frame size on startup

Christian Kruse,

Emacs is my favourite editor. I use it for about 10 years now, and there is no editor fullfilling my needs as good as Emacs does. But one thing bothered me always (but not enough to repair it - until now): everytime I start Emacs, I have to resize it to the right size. The .Xressources is not a real option for me, because I use to change my desktop environment from time to time. And this changes the geometry of the desktop, too. Also I have two workstations (one at work, one at home), and the settings differ there, too.

To solve this problem ones and for all, I wrote a small Lisp plugin to save the last frame size in a file and restore it on next startup:

(defun save-framegeometry ()
"Gets the current frame's geometry and saves to /.emacs.d/framegeometry."
(let (
(framegeometry-left (frame-parameter (selected-frame) 'left))
(framegeometry-top (frame-parameter (selected-frame) 'top))
(framegeometry-width (frame-parameter (selected-frame) 'width))
(framegeometry-height (frame-parameter (selected-frame) 'height))
(framegeometry-file (expand-file-name "/.emacs.d/framegeometry"))
)

(when (not (number-or-marker-p framegeometry-left))
  (setq framegeometry-left 0))
(when (not (number-or-marker-p framegeometry-top))
  (setq framegeometry-top 0))
(when (not (number-or-marker-p framegeometry-width))
  (setq framegeometry-width 0))
(when (not (number-or-marker-p framegeometry-height))
  (setq framegeometry-height 0))

(with-temp-buffer
  (insert
   ";;; This is the previous emacs frame's geometry.\n"
   ";;; Last generated " (current-time-string) ".\n"
   "(setq initial-frame-alist\n"
   "      '(\n"
   (format "        (top . %d)\n" (max framegeometry-top 0))
   (format "        (left . %d)\n" (max framegeometry-left 0))
   (format "        (width . %d)\n" (max framegeometry-width 0))
   (format "        (height . %d)))\n" (max framegeometry-height 0)))
  (when (file-writable-p framegeometry-file)
    (write-file framegeometry-file))))

)

(defun load-framegeometry () "Loads /.emacs.d/framegeometry which should load the previous frame's geometry." (let ((framegeometry-file (expand-file-name "/.emacs.d/framegeometry"))) (when (file-readable-p framegeometry-file) (load-file framegeometry-file))) )

;; Special work to do ONLY when there is a window system being used (if window-system (progn (add-hook 'after-init-hook 'load-framegeometry) (add-hook 'kill-emacs-hook 'save-framegeometry)) )

;; eof

Emacs rocks :-)

Update: In some desktop environments it seems as if the Emacs frame may be out of the visible area. In this cases frame-parameter returns something like (+ -8). I added a check if the geometry variables are a number and set them to zero if not.