tags:

views:

2617

answers:

8

This is driving me crazy: I simply want Emacs to maximize to whatever screen resolution I have at startup. Ideally I like a cross-platform (Windows & Linux) solution that works on any screen resolution, but I can't even get it to work on just Window XP with even hard-coded sizes.

Here are what I tried:

  1. Setting the initial-frame-alist with appropriate height/width
  2. Setting the default-frame-alist
  3. (Windows specific stuff) Sending message to the emacs windows telling it to maximize via (w32-send-sys-command 61488)
  4. Tried this function which I found somewhere:
(defun toggle-fullscreen ()
  "toggles whether the currently selected frame consumes the entire display
or is decorated with a window border"
  (interactive)
  (let ((f (selected-frame)))
    (modify-frame-parameters 
     f
     `((fullscreen . ,(if (eq nil (frame-parameter f 'fullscreen)) 
                          'fullboth
                        nil))))))
  1. Tried the above methods in both beginning and end of my init file to try to eliminate interference from other init things.

Unfortunately, none of the above works!! For some of the above, I can see my emacs windows resizes correctly for a split second before reverting back to the smallish default size. And if I run the methods above after the initialization, the emacs windows DOES resize correctly. What in the world is going on here?

[p.s. there are other SO questions on this but none of the answers work]


Update:

The answers make me think that something else in my init file is causing the problem. And indeed it is! After some try-and-error, I found the culprit. If I commented out the following line, everything works perfectly:

 (tool-bar-mode -1)

What in the world does the toolbar have to do with maximizing windows?

So the question now is: how can I disable toolbar (after all, emacs's toolbar is ugly and takes up precious screen real-estate) AND maximize the windows both in my init file? It is possibly a bug that toolbar interferes with the windows size?

Clarification: (tool-bar-mode -1) turns the toolbar off, but this line interferes with maximizing the Emacs windows. So if I try put functions to maximize windows and turn off the toolbar, the maximize part will fail; if the toolbar part is commented out, then the maximize part will work ok. It does not even matter what solutions I use (among the 4 that I listed).


Solution: (or at least what work for me now)

This is probably a bug in Emacs. The workaround is to disable the toolbar through the Registry, not in .emacs. Save the following as a .reg file, and execute that file in Windows Explorer:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\GNU\Emacs]
"Emacs.Toolbar"="-1"

(This solution is a working version of what OtherMichael suggested).

A: 

Hey - nice function! Thanks for posting

I think it may not be working for you because you have code that looks like the following somewhere else in your init file. The default-frame-alist is being applied after the frame is created. I removed the size and position elements and you function works great on bootup of emacs.

   (setq default-frame-alist
     (list
      (cons 'left                 350)
      (cons 'top                  0)
      (cons 'width                80)
      (cons 'height               45)
     ......
jottos
A: 
(defun resize-frame ()
"Set size"
(interactive)
(set-frame-width (selected-frame) 110)
(set-frame-height (selected-frame) 33)
(set-frame-position (selected-frame) 0 1))

following is the last function called in my .emacs files it sets the height and width of the screen it does work on both emacs 22 and emacs 23 on debian and mac os x. set the height and width numbers according to your screen.

Hamza Yerlikaya
A: 

THANK YOU!!!

I ran this particular set of traps a while back, but never found the (tool-bar-mode) fix.

The reason the toolbar mode is interfering with maximizing Emacs is that the idiot^H^H^H^H^Hperson who added the toolbar failed to account for the vertical space occupied by the toolbar. This results in Emacs trying to maximize the window larger than the physical screen.

A: 

To disable the toolbar, add the line

(tool-bar-mode nil)

to your customization file (usually .emacs in your root directory).

I think you misunderstood my question. (tool-bar-mode nil) and (tool-bar-mode -1) does the same thing, and (tool-bar-mode -1) is exactly what I have in my .emacs file AND it is the cause of Emacs not correctly maximizing.
polyglot
+5  A: 

I found an answer a year-or-so back that explains you have to manipulate the registry to do things right:

To start Emacs maximized put this line at the end of your ~/.emacs file:

(w32-send-sys-command 61488)

If you don't want the Emacs tool bar you can add the line (tool-bar-mode -1) [NOTE: value is 0 on original page] to your ~/.emacs file but Emacs won't fully maximize in this case - the real estate occupied by the tool bar is lost. You have to disable the tool bar in the registry to get it back:

[HKEY_LOCAL_MACHINE\SOFTWARE\GNU\Emacs\Emacs.Toolbar]
@="0"
Michael Paulukonis
I already have the w32-send-sys-command in my .emacs file, and it works ONLY IF I did not specify (tool-bar-mode -1) in my .emacs file somewhere. The registry trick to turn off toolbar does not work for me, though...
polyglot
After some tinkering, I find out that instead of creating a key, Emacs will response to Emacs.Toolbar as a value inside the Emacs key. Also the value should be -1. Then voila, finally! I posted a working solution in my question as an update.
polyglot
I double-checked, and while the original page does say (tool-bar-mode 0), my .emacs has (tool-bar-mode -1); now reflected above. However, my registry value is still 0, not -1. hunh.
Michael Paulukonis
+4  A: 

On X.org the system call is (since you asked originally for a cross-platform solution):

(defun x11-maximize-frame ()
  "Maximize the current frame (to full screen)"
  (interactive)
  (x-send-client-message nil 0 nil "_NET_WM_STATE" 32 '(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0))
  (x-send-client-message nil 0 nil "_NET_WM_STATE" 32 '(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0)))
A: 

Another way to resolve such problem is put delay between

(menu-bar-mode -1)
(tool-bar-mode -1)
(tooltip-mode -1)
(scroll-bar-mode 1)

and set-frame-* functions. For example:

 (tool-bar-mode -1)
 (when window-system
  (run-at-time (format "%d sec" 1) nil '(lambda () (set-frame-position (selected-frame) 1 1)))
  (run-at-time (format "%d sec" 2) nil '(lambda () (set-frame-width (selected-frame) 150 t)))
  (run-at-time (format "%d sec" 3) nil '(lambda () (set-frame-height (selected-frame) 60 t)))
  )

Essential put delay between set-frame-* functions also!

gavenkoa
A: 

First, thanks for the tip on the bug with (tool-bar-mode -1); saved me a lot of trouble! I'd rather keep everything within the .emacs file (no registry mods for me), so if you turn on the toolbar before maximizing, and then turn it off again, you'll can maximize nicely:

(defun maximize-current-frame () "Resizes the current frame to fill the screen"
    (interactive)
    ;; There is a bug in tool-bar-mode that prevents it from being
    ;; maximized, so turn it on before maximizing.
    ;; See http://stackoverflow.com/questions/815239/how-to-maximize-emacs-on-windows-at-startup)
    (tool-bar-mode 1)
    (w32-send-sys-command 61488)
    (tool-bar-mode -1)
)
prewett