M-x all-things-emacs

Maximize on Startup, Part 2

February 22nd, 2007 by Ryan McGeary · 33 Comments

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

  1. Download the full package. You can obtain the latest version (on github) or the version referenced in this article.
  2. Put maxframe.el in your load-path.
  1. 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.

Tags: elisp · osx · tips · windows · x

33 responses so far ↓

  • 1 RedBlue // Feb 22, 2007 at 7:31 am

    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.

  • 2 Derek // Feb 22, 2007 at 6:16 pm

    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.

    (setq screen-dpi
          (string-to-number (nth 9 (split-string (car (x-list-fonts "*" nil nil 1)) "-"))))
    (setq high-dpi (> screen-dpi 96))
  • 3 Ryan McGeary // Feb 22, 2007 at 7:28 pm

    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 t argument to append the function to the hook list. Anyone else have suggestions?

  • 4 Ryan McGeary // Feb 22, 2007 at 7:41 pm

    Derek, I also tried grepping/grokking the C code in hopes of finding something to call from elisp to reset the display-pixel-width return value. I didn’t find anything either, but I hope someone will point me in the right direction.

    The x-list-fonts call returns the following on Carbon Emacs (Mac OS X):

    ("-apple-abadi mt condensed extra bold-medium-r-normal--0-0-0-0-m-0-iso10646-1")

    So the value of screen-dpi is always 0. 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?

  • 5 Corwin // Feb 24, 2007 at 6:59 pm

    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 [...]

  • 7 Thomas Narten // Aug 24, 2007 at 1:08 pm

    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.

  • 8 Ryan McGeary // Aug 24, 2007 at 3:24 pm

    Thomas, Good idea. I just added restore-frame support to the maxframe package. Try the latest version. You can either call it via M-x restore-frame or bind it to a key like so:

    (global-set-key [(f14)] 'restore-frame)
    (global-set-key [(f15)] 'maximize-frame)

    For those that don’t have F14 or F15 on their keyboard, be sure to pick another key binding :-)

  • 9 Thomas Narten // Aug 24, 2007 at 6:05 pm

    Looks like your changes haven’t actually made it out, I’m still seeing the older version of the code…

  • 10 Ryan McGeary // Aug 24, 2007 at 7:52 pm

    Thomas, The changes are definitely there. Be sure to clear your browser cache. Here’s a link that should avoid any browser caching.

  • 11 Thomas Narten // Aug 24, 2007 at 9:06 pm

    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?

  • 12 Ryan McGeary // Aug 25, 2007 at 1:06 am

    Thomas, restore-frame should (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-frame isn’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.

  • 13 Larme // Aug 31, 2007 at 3:58 pm

    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.

  • 14 Ryan McGeary // Aug 31, 2007 at 6:40 pm

    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:

    (setq mf-display-padding-height (+ mf-display-padding-height 48))

    This assumes a dock height of 48 pixels (adjust as needed).

    If you want full control specify an more absolute value like so:

    (setq mf-display-padding-height (+ 22 23 48))

    This manually accounts for the menubar, titlebar, and dock respectively.

  • 15 Jacob Tjornholm // Oct 22, 2007 at 7:41 pm

    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 [...]

  • 17 Joseph Zitt // Nov 19, 2007 at 12:36 pm

    Following these instructions precisely had no effect on the emacs running on my Xandros-based Asus Eee. Suggestions?

  • 18 Ryan McGeary // Nov 19, 2007 at 3:08 pm

    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.

    (display-pixel-width)  ;; should return the monitor's width
    (display-pixel-height)  ;; monitor's height

    If all else fails, I’d love if you could help debug the package; otherwise, try option 2 explained here.

  • 19 Adam Compton // Nov 27, 2007 at 6:45 pm

    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.

  • 20 Ryan McGeary // Nov 30, 2007 at 2:45 am

    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-frame after switching to the scratch buffer, does it fix the problem temporarily? If so, I’d recommend one of two things:

    1. Make your fonts more consistent across buffers
    1. or Add a switch buffer hook that calls maximize-frame for the buffers that run with different size fonts
  • 21 Jakub Piotr C?apa // Mar 16, 2008 at 2:33 pm

    I’ve got some better results with this code:

    (defun mac-display-pixel-width ()
      (- (caddr (mac-display-available-pixel-bounds))
         (car (mac-display-available-pixel-bounds))))
     
    (defun mac-display-pixel-height ()
      (- (cadddr (mac-display-available-pixel-bounds))
         (cadr (mac-display-available-pixel-bounds))))
     
    (defun mf-max-display-pixel-width ()
      (min (mac-display-pixel-width)
           (display-pixel-width)
           (or mf-max-width (display-pixel-width))))
     
    (defun mf-max-display-pixel-height ()
      (min (mac-display-pixel-height)
           (display-pixel-height)
           (or mf-max-height (display-pixel-height))))

    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.

  • 22 Jakub Piotr C?apa // Mar 23, 2008 at 9:18 pm

    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…

  • 23 Ryan McGeary // Mar 24, 2008 at 1:16 pm

    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-width variable:

    ;; Biggest monitor width I currently have.  This is necessary when multiple
    ;; monitors exist; otherwise, the frame will span multiple displays.
    (setq mf-max-width 1920)
  • 24 Jakub Piotr C?apa // Mar 28, 2008 at 12:57 am

    The mac-display-available-pixel-bounds function works great in my dual-head setup so you could also consider using it in maxframe.el

  • 25 Ryan McGeary // Mar 28, 2008 at 2:49 am

    Jakub, I will investigate that. Thanks. One less thing to configure for us Mac geeks.

  • 26 Austin Frank // Apr 2, 2008 at 7:18 pm

    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.

    (setq au-frame-maxp nil)
     
    (defun au-max-frame ()
    "Maximize the current frame and toggle au-frame-maxp"
      (interactive)
      (maximize-frame)
      (setq au-frame-maxp t))
     
    (defun au-restore-frame ()
    "Restore the current frame to its previous size and toggle au-frame-maxp"
      (interactive)
      (restore-frame)
      (setq au-frame-maxp nil))
     
    (defun au-toggle-max-frame ()
    "Check the status of au-max-framep and change to whichever mode we're not in now."
      (interactive)
      (if (eq au-frame-maxp nil) (au-max-frame) (au-restore-frame)))
  • 27 Austin Frank // Apr 3, 2008 at 3:24 am

    I have the following code in a file sourced by my .emacs:

    ;;from steve yegge's article
    ;;http://opal.cabochon.com/~stevey/blog-rants/effective-emacs.html
    (if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))
    (if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
    (if (fboundp 'menu-bar-mode) (menu-bar-mode -1))

    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

  • 28 Ryan McGeary // Apr 3, 2008 at 6:41 pm

    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.

  • 29 Michael Long // Apr 12, 2008 at 9:44 pm

    I’m using the following to maximize to my primary monitor (1920×1200):

    ;; Enable maximize on startup
    (require 'maxframe)
    (setq mf-max-width 1920)  ;; Pixel width of main monitor.
    (setq mf-display-padding-width 5)
    (setq mf-display-padding-height 0)
    (add-hook 'window-setup-hook 'maximize-frame t)

    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.

  • 30 Ryan McGeary // Apr 13, 2008 at 12:07 am

    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.

  • 31 Michael Long // Apr 13, 2008 at 1:39 am

    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.

  • 32 Jacobo García // Oct 20, 2008 at 10:35 am

    Thanks for your add-on. Now I can maximize easily in carbon emacs-os x.

    Regards

  • 33 Unreal Media // Nov 4, 2008 at 5:17 pm

    Thanks, this helped alot.

Leave a Comment