Emacs: restore last frame size on startup
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"))
)
(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 :-)
Woho! Three comments!
kutchbhi wrote
Thanks but this didn't work on my xp system. Any ideas why ? It seems to saving the data in the file alright, but isn't able to load from it
Christian Kruse wrote
Not with this little info. What errors occur? Can you paste it?
kutchbhi wrote
sorry! it worked just fine. I had some other issues