In part 1 of the series, we discussed two ways to resize the emacs frame on startup. Here, we will use the display-pixel-width and display-pixel-height functions to automatically determine the proper size of the emacs frame.
Option 3 (the package)
As I tried to come up with a generic way to maximize the emacs frame on startup, the more the elisp moved out of my .emacs and into its own package. Soon enough, maxframe.el was born. First, I’ll describe how to install the package. Then I’ll try to describe how it works. Finally, I’ll discuss some of the caveats and gotchas.
Installation
- Download the full package. You can obtain the latest version (on github) or the version referenced in this article.
- Put
maxframe.elin yourload-path.
- Add the following to your
.emacs:
(require 'maxframe) (add-hook 'window-setup-hook 'maximize-frame t)
Maximizing the frame in the window-setup-hook helps prevent font size and window customizations from affecting the size of the newly sized frame. There are a couple optional customization variables, but we’ll explain those soon. Upon startup, emacs will resize itself to fit your display’s full resolution. Voil� .
How it works
Let’s walk through the package and explain what’s going on. Line numbers match up with the version of maxframe.el tagged for this article.
Customization variables
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | (defgroup maxframe nil "Handle maximizing frames.") (defcustom mf-display-padding-width 0 "*Any extra display padding that you want to account for while determining the maximize number of columns to fit on a display" :type 'integer :group 'maxframe) ;; The default accounts for a Mac OS X display with a menubar ;; height of 22 pixels, a titlebar of 23 pixels, and no dock. (defcustom mf-display-padding-height (+ 22 23) "*Any extra display padding that you want to account for while determining the maximize number of rows to fit on a display" :type 'integer :group 'maxframe) |
First, we define a new customization group and add two new variables to allow for extra display padding if desired. The defaults were designed for Mac OS X, with the menu-bar and title-bar, but without the dock visible. I hide my toolbar, but that doesn’t seem to affect the calculations anyway. Run M-x customize-group RET maxframe RET or add the appropriate setq calls in your .emacs to fit to your liking.
Windows specific functions
62 63 64 65 66 67 68 69 70 | (defun w32-maximize-frame () "Maximize the current frame (windows only)" (interactive) (w32-send-sys-command 61488)) (defun w32-restore-frame () "Restore a minimized/maximized frame (windows only)" (interactive) (w32-send-sys-command 61728)) |
These were taken nearly verbatim from the GNU Emacs FAQ. They directly access the Windows system commands to maximize/restore the containing window. Later we’ll see that when Windows is detected, w32-maximize-frame is used to short-circuit the other maximize frame functions.
Columns and rows
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | (defun mf-max-columns (width) "Calculates the maximum number of columns that can fit in pixels specified by WIDTH." (let ((scroll-bar (or (frame-parameter nil 'scroll-bar-width) 0)) (left-fringe (or left-fringe-width (nth 0 (window-fringes)) 0)) (right-fringe (or right-fringe-width (nth 1 (window-fringes)) 0))) (/ (- width scroll-bar left-fringe right-fringe mf-display-padding-width) (frame-char-width)))) (defun mf-max-rows (height) "Calculates the maximum number of rows that can fit in pixels specified by HEIGHT." (/ (- height mf-display-padding-height) (frame-char-height))) |
These two functions take pixel dimensions and calculate the max number of characters that can fit in those dimensions. Both functions account for the padding values we talked about above, but the columns are also affected by the scroll-bar and fringes (lines 75-77). The widths of these values are subtracted from the base pixel width before dividing by the character pixel width. The mf-max-rows function is a bit simpler, because less window decoration affects it.
Resize frame using pixels
89 90 91 | (defun mf-set-frame-pixel-size (frame width height) "Sets size of FRAME to WIDTH by HEIGHT, measured in pixels." (set-frame-size frame (mf-max-columns width) (mf-max-rows height))) |
This function was designed to mimic the built-in set-frame-size function but to instead accept pixel dimensions instead of column and row arguments. It merely calls set-frame-size given the return values from the mf-max-columns and mf-max-rows functions.
X-Window/Mac specific function
93 94 95 96 97 98 99 | (defun x-maximize-frame () "Maximize the current frame (x or mac only)" (interactive) (mf-set-frame-pixel-size (selected-frame) (display-pixel-width) (display-pixel-height)) (set-frame-position (selected-frame) 0 0)) |
This tries to mimic the behavior of w32-maximize-frame found on line 62 but for x-window and mac systems. It uses display-pixel-width and display-pixel-height to resize the current frame to fit the display and moves the frame to the top left corner of the display which is situated at (0,0).
Maximize frame
101 102 103 104 105 106 | (defun maximize-frame () "Maximizes the frame to fit the display if under a windowing system." (interactive) (cond ((eq window-system 'w32) (w32-maximize-frame)) ((memq window-system '(x mac)) (x-maximize-frame)))) |
This is where the butter meets the bread. If under a Windows windowing system, w32-maximize-frame is called (line 105) and Windows handles the rest . If under an X-Window or Mac windowing system, we call our custom x-maximize-frame function (line 106) to to sort out the rest.
The rest
108 109 110 | (defalias 'mf 'maximize-frame) (provide 'maxframe) |
Line 108 defines a new alias that makes it quicker to manually maximize your frame (i.e. M-x mf instead of M-x maximize-frame). Like any good package, on line 110, we announce our new maxframe feature to emacs with the provide function. This allows an associated (require 'maxframe) call to load the functionality into emacs.
Gripes
One issue I ran into during this exercise is that emacs does not recognize when the display’s resolution is changed. This is a problem because I would like to be able to re-maximize the frame (M-x mf) after I connect my laptop to an external monitor. Unfortunately, display-pixel-width and display-pixel-height yield the display resolution values from when emacs was started instead of the current display values. Perhaps there’s a way to have emacs re-sniff these values, but I don’t know how. If anyone has suggestions, please post a comment.
[Update: This was fixed in the release version of Emacs 22.]
This was designed for Emacs 22. The fringe related functions and variables probably won’t work on Emacs 21. The Windows specific functions should still work on Emacs 21 though.
While I’m glad I was able to achieve this kind of functionality with elisp, I am disapointed it required the extra work. Having to manually account for scroll-bars, fringes, menu-bars, title-bars, etc is a little crazy in my opinion. I’d like to see more of this behavior accounted for in the core. I’d prefer if there existed a function (e.g. set-frame-pixel-size) that correctly sized the frame to the proper pixel dimensions.
33 responses so far ↓
Have you tried the w32-maximize-frame on emacsW32 ? If I run it hooked to window-setup-hook it maximizes the window, but then it gets restored in a second.
This is a bit ugly, but it does seem to work. I grepped the C code, and this appears to be the only way to find the screen resolution without adding another native function.
RedBlue, No I haven’t tried this on EmacsW32. My guess is that you’ll need to add the maximize call to a hook that runs later than window-setup-hook. I’m assuming you included the
targument to append the function to the hook list. Anyone else have suggestions?Derek, I also tried grepping/grokking the C code in hopes of finding something to call from elisp to reset the
display-pixel-widthreturn value. I didn’t find anything either, but I hope someone will point me in the right direction.The
x-list-fontscall returns the following on Carbon Emacs (Mac OS X):So the value of
screen-dpiis always0. It also looks like this will yield the dots per inch of the display, but how do you suggest determining the resolution? Is there something else that will yield the display’s dimensions?Red Blue, I had the same problem adding just (w32-send-sys-command 61488) line in .emacs as was suggested at some other sites
Adding the code described in the first option here solved the problem.
I’m new to emacs and I’m customizing it on windows at the moment- thanks to guys like Ryan for publishing such a useful information.
[...] discuss some straight forward options for maximizing and resizing the emacs window on startup. In part two, we’ll explore a more advanced [...]
This is cool, but what I really want is an easy way to toggle back and forth between full screen and reduced screen size. I.e., I like to switch back and forth from within emacs, using only keys rather than having to use the mouse.
I really want the equivalent of the two w32 functions (w32-maximize-frame and w32-restore-frame), but for non MS environments.
Thomas, Good idea. I just added
restore-framesupport to the maxframe package. Try the latest version. You can either call it viaM-x restore-frameor bind it to a key like so:For those that don’t have F14 or F15 on their keyboard, be sure to pick another key binding
Looks like your changes haven’t actually made it out, I’m still seeing the older version of the code…
Thomas, The changes are definitely there. Be sure to clear your browser cache. Here’s a link that should avoid any browser caching.
Got it now. Thanks. Ah, what I really want is the equivalent of clicking on the fullscreen/restor iconn on the upper corner of the window. I.e., when I restore the frame, it’s positioned in a different location on the screen from where it was before. Somewhat annoying, e.g., if the mouse is no longer over the window.
I’ve looked a bit in documentation and am really surprised there is no way to invoke the equivalent even from within emacs.
Is it (easily) possible to restore the previous positioning as well?
Thomas,
restore-frameshould (does) restore the previous positioning. The maxframe package has no control over the window manager’s maximize and restore buttons in the top corner of the window. That’s handled by the OS/window manager typically.If you’re saying that
restore-frameisn’t working for you and isn’t positioning the emacs frame back where it was, what OS and what version of emacs are you using?For my own usage, I almost never want to make my emacs frame smaller, so I guess I’m having a hard time understanding your workflow.
The maxframe.el doesn’t work well in Carbon Emacs running on Mac OS X with dock automatically hiding disabled. The problem is that the calculation of pixels count the dock area. Hence after maximization the bottom area of the emacs window is covered by the dock.
Hence I use the second option (hard-coded one). Now here comes another issue: I use both the carbon emacs and the console-only emacs provided by OS X. The initial size for carbon emacs is not good for the console emacs. My solution is using the variable window-system to determine the windows system (mac for carbon emacs and nil for console emacs) and run the initial script conditionally.
Larme, It sounds like you overlooked the customization variables provided by maxframe. It was designed on Carbon Emacs. You just need to specify extra height padding using
mf-display-padding-height. I hide my dock, so the defaults for maxframe are set to only account for the height of the menubar and titlebar. If you also want to account for the dock height, try adding something like this to your .emacs:This assumes a dock height of 48 pixels (adjust as needed).
If you want full control specify an more absolute value like so:
This manually accounts for the menubar, titlebar, and dock respectively.
Ryan,
Very useful package, both for maximizing from .emacs (on OSX in my case), but also for learning more elisp.
Thanks a lot for your thorough posts!
/Jacob
[...] some positive experience with the GeSHi (Generic Syntax Highlighter) library, and I liked the demo on the EmacsBlog so I thought this is what I [...]
Following these instructions precisely had no effect on the emacs running on my Xandros-based Asus Eee. Suggestions?
Joseph, That’s a hard thing to debug because I don’t test on Linux as often as I should. Without seeing the machine,try running some of these functions and see what they return. If they don’t return values that you’d expect, the problem probably lies with the interaction between emacs and the window manager.
If all else fails, I’d love if you could help debug the package; otherwise, try option 2 explained here.
When I switch to the “scratch“ buffer (or change an existing buffer to text-mode), the display resizes due to a font change and the minibuffer is scrolled right off the bottom of the screen.
Have you seen this behavior before, and/or can you think of anything I might be able to do to fix or avoid it?
I’m running Aquamacs 1.2a on 10.4.10, with the latest version of your maxframe.el.
Adam, Hi. It sounds like your scratch buffer has a different font size. The maxframe package relies on font size to determine the max dimensions because emacs requires sizing itself by rows and columns. If you run
M-x maximize-frameafter switching to the scratch buffer, does it fix the problem temporarily? If so, I’d recommend one of two things:maximize-framefor the buffers that run with different size fontsI’ve got some better results with this code:
Set mf-display-padding-height to 0 afterwards since mac-display-available-pixel-bounds already accounts for the dock and the menubar.
Unfortunately I don’t really know how to make this portable since I’m new to elisp.
Oh… I forgot to mention why this works better. ;]
It uses the main monitor size (and not the whole dual-monitor framebuffer size). On Aquamacs at least…
Jakub, I agree that it’s a pain to handle the dual-monitor framebuffer issue. Older version of emacs did not have this isse, but a workaround while still using the maxframe package is to just set the
mf-max-widthvariable:The mac-display-available-pixel-bounds function works great in my dual-head setup so you could also consider using it in maxframe.el
Jakub, I will investigate that. Thanks. One less thing to configure for us Mac geeks.
Thanks for this code, I’m using it all the time now.
I wrote a convenience function to toggle back and forth between maximized and restored frame sizes.
I have the following code in a file sourced by my .emacs:
It seems that you factor the height of the toolbar into your calculation of the appropriate height for the maximized frame. This means that when I maximize the frame it ends up being too short by the height of the tool-bar. If I subsequently display the tool-bar, the frame takes the whole screen as it should.
Any chance you could check the value of tool-bar-mode, and adjust the height of the maximized display accordingly?
Thanks,
/au
Austin, By default, maxframe assumes that the toolbar is hidden. It only pads for the menubar and titlebar. In fact, on Carbon Emacs on OS X, the height of my emacs frame remains the same whether the toolbar is on or off.
What version/distribution of emacs are you using? I can’t reproduce the problem you’re describing. Either that, or I’m misunderstanding.
I’m using the following to maximize to my primary monitor (1920×1200):
It works, except it doesn’t maximum heightwise, but is short by 3 lines. I’ve played around with several variables but nothing seems to get it maximize fully.
Doing maximize-frame manually doesn’t help. This is GNU emacs 22.1.1 on Fedora 8.
Michael, Hmm, that’s strange. I don’t get much opportunity to test on Linux, so I wouldn’t be surprised if I’m missing some height measurement. If anyone has a patch, I’d be happy to apply it.
I’m not sure its a problem with your package. Explicitly passing a given number of rows to set-frame-size doesn’t seem to help, it appears to be a issue (either limited by design or a bug) with set-frame-size. Unfortunately I’m not familiar enough with emacs lisp to know where exactly the problem is.
Thanks for your add-on. Now I can maximize easily in carbon emacs-os x.
Regards
Thanks, this helped alot.
Leave a Comment