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.
59 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.
6 Maximize on Startup, Part 1 | M-x all-things-emacs // Mar 10, 2007 at 2:56 am
[...] 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
16 Gergely Hodicska - » Escaping problem with WP-Syntax WordPress plugin // Oct 30, 2007 at 12:49 am
[...] 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.
34 memory ’s blog » Blog Archive » WP-Syntax // Nov 23, 2008 at 3:13 am
[...] plugin was originally written for use with EmacsBlog. To see it in action, scroll through this particular post or visit the [...]
I found in this forum: http://ubuntuforums.org/showthread.php?t=782196
the following code:
putting it in .emacs lets maximize the window on startup (at least on my debian), like you explained in the first part for microsoft windows.
Thanks.
I use this with Emacs 23 on the Mac (built from CVS sources using the —with-ns (NextStep) option to ./configure) simply by adding ns in the window system lists found in maximize-frame and restore-frame.
I also find this to be handy:
I have been using your 1st method in Win32 (Emacs 22) with only one problem:
I use emacs as a diff tool from SVN. That involves emacs being run with command line arguments:
—eval “(ediff-files %s %s)”
When ediff pops up, it has 2 frames:
1) diff display frame
2) control frame
The control frame is supposed to be tiny, but the window-setup-hook end up being run for that frame instead of the diff-display frame. So I get a giant control frame that completely hides the diff display.
38 WP-Syntax Plugin | WordPress Plugins Database - WordPressPluginsDatabase.com // Feb 24, 2009 at 6:46 am
[...] plugin was originally written for use with EmacsBlog. To see it in action, scroll through this particular post or visit the [...]
Really nice and useful. Love it!
Ryan, thanks for your post. I was fed up with maximizing the frame by hand. However, Trianam’s comment caught me before I got to choose one of your methods.
I reproduce the code here in the hope that this markup will work. If the code blows up in your emacs, make sure to translate back the quote to their “not-fancy” versions (single quotes and opening/closing double quotes).
This works perfectly on my Ubuntu 8.10 + snapshot-emacs.
Oh Crap, looks like my block was not honored. Do be careful with the quotes when pasting the code.
Ryan it would really help if code blocks where allowed as well as comment preview. I do not use wordpress but I am sure plugins exist.
Arnaud, I fixed your code snippet.
Ryan thanks for the post, I’m on Fedora 10 and was having the same problem a couple others were with your code missing a couple vertical lines on the bottom. I tried Arnaud’s/Trianam’s code and that worked great. So it also works on non-debian builds. Using Emacs 22.3.1 from the Fedora repos on x86_64 in case anyone else wants to try.
Thanks for the package; I just started learning emacs and disliked having to manually maximize. It works cleanly on this old Windows XP box.
Ryan —
In order to support Emacs built for Mac OS X from sources on Savannah, you need to change maxframe.el in two places. In both maximize-frame and restore-frame, replace ‘(x mac) with ‘(x ns mac) .
David, Yup, agreed. It’s been updated on github
Thanks for this package. I find myself using a mac for testing, and doing a lot of editting, and the maximize issues finally annoyed me enough to do something about it.
In the past I usually hacked something up, but this is much nicer. On linux I use stumpwm so these are not issues.
I was thinking it is too bad there is no way to dynamically get the size of the doc so the maximize could automatically scale intelligently. No biggy, as I don’t resize my doc, but thought it would be nice if mac/elisp provided a way to find that sort of information out.
Thanks again, great package.
On Linux, I installed a program called “wmctrl” and added the following to my .emacs:
(shell-command (format “wmctrl -i -r %s -b add,maximized_vert,maximized_horz”
(frame-parameter nil ‘outer-window-id)))
This is great! It should be on the emacswiki, but I don’t see it there
Currently using this to get maximal maximization under X. nice and distraction proof.
(defun toc:make-fullscreen-X ()
“forces the currently selected frame to consume the entire display”
(interactive)
(let ((f (selected-frame)))
(modify-frame-parameters f ‘((fullscreen . fullboth)))))
(add-hook ‘window-setup-hook ‘toc:make-fullscreen-X t)
Thanks for the post, really useful!
Hi rohan nicholls,
Are you looking for something like the function count-lines-page which tells the number of lines the current buffer has? If yes it should be easy to include a return of the total.
Thanks for your add-on. Now I can maximize easily in carbon emacs-os x.
54 WP-Syntax – Code HighLighting ?????? for Wordpress « Kebot // Sep 26, 2010 at 8:42 am
[...] plugin was originally written for use with EmacsBlog. To see it in action, scroll through this particular post or visit [...]
Thanks, it “just works”!
It’s a nice plugin worth trying out.
I will give it a try and see how it works.
Thanks
Hey there,
Thank you for this module. Loved it.
However, when I try to use it on OS X Lion
with Emacs 24, it seems to “hide” the first line
of every buffer. Are there any workarounds to this?
Cheers,
Yesudeep.
Yesudeep,
There’s little reason to use this library on OS X and Emacs 24 anymore, but is your issue the same as what is described here?
https://github.com/rmm5t/maxframe.el/issues/2
If so, feel free to chime in and participate, but also read my reply. I suggest using
ns-toggle-fullscreenon Emacs 24 on OS X instead.59 Inforscience // Aug 7, 2012 at 11:08 pm
[...] plugin was originally written for use with EmacsBlog. To see it in action, scroll through this particular post or visit the [...]
Leave a Comment