How to maximize Emacs on Windows at startup? - emacs

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:
Setting the initial-frame-alist with appropriate height/width
Setting the default-frame-alist
(Windows specific stuff) Sending message to the emacs windows telling it to maximize via (w32-send-sys-command 61488)
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))))))
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).

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"
If you look in the EmacsWiki under W32SendSys command-codes you'll find that 61488 is maximize current frame

This is the simplest fix that worked for me:
(w32-send-sys-command #xf030)
(add-hook 'window-setup-hook (lambda () (tool-bar-mode -1)))

Dudes, what's wrong with
emacs -mm ?
This starts Emacs fully maximized. Put it in your shortcut, it really depends on the window manager you use ; I myself just alias it in my .*shrc.
On a different but not unrelated note, I in fact start Emacs in a way that if it's already started, the selected file is just opened as a new buffer in my existing Emacs session :
if [ "$(pidof emacs)" ] ; then
emacsclient "$#"
else
emacs -mm "$#"
fi
This I put in a ~/bin/e and I just launch Emacs with "e".
I right-clicked a text file in the file manager, selected "open with another program" and entered "e", and now I just double click files and they all open neatly in the same session.
And everything is peachy and lispy :)

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)))

This works reliably on Emacs 24.5.1 (i686-pc-mingw32):
(add-to-list 'initial-frame-alist '(fullscreen . maximized))

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)
)

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)))
)
It is essential to put delay between set-frame-* functions also!

I encountered one command that seems to work (toggle-frame-maximized), which works on both Linux & Windows.
I just put that near the end of my init file after setting up everything, making the assumption that by the end of the setup everything won't be maximized, and voila, it works.

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)
......

If you really want to run Emacs full screen without window chrome (title bar and max/min/close button), then try the mode below. This worked for me with Emacs 23.3.1 on XP SP3.
http://www.martyn.se/code/emacs/darkroom-mode/

every one.
emacs 23.3.1,blow code is invalid. because (menu-bar-mode -1) can cause windows not
full-screen too.
(w32-send-sys-command #xf030)
(add-hook 'window-setup-hook (lambda () (tool-bar-mode -1)))
I search Eamcs wiki,find a useable method.
http://www.emacswiki.org/emacs/FullScreen#toc3
MS Windows
If you’re using MS Windows, and want to use “real fullscreen”, i.e, getting rid of the top titlebar and all, see w32-fullscreen at the site for darkroom-mode
Alternatively, a patch is available here that makes the fullscreen frame parameter really fullscreen on Windows.
To get a maximized window you can use: (w32-send-sys-command #xf030)
Attention! If you want that emacs starts maximized, you have to put this code into your .emacs file:
(defun jbr-init ()
"Called from term-setup-hook after the default
terminal setup is
done or directly from startup if term-setup-hook not
used. The value
0xF030 is the command for maximizing a window."
(interactive)
(w32-send-sys-command #xf030)
(ecb-redraw-layout)
(calendar)
)
(setq term-setup-hook 'jbr-init)
(setq window-setup-hook 'jbr-init)

(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.

To disable the toolbar, add the line
(tool-bar-mode nil)
to your customization file (usually .emacs in your root directory).

emacs-full-screen-win32 project will help you :)

The following snippet from emacswiki worked fine from me:
(add to your startup config file)
(defun jbr-init ()
"Called from term-setup-hook after the default
terminal setup is
done or directly from startup if term-setup-hook not
used. The value
0xF030 is the command for maximizing a window."
(interactive)
(w32-send-sys-command #xf030)
(ecb-redraw-layout)
(calendar)
)
(setq term-setup-hook 'jbr-init)
(setq window-setup-hook 'jbr-init)

Related

Emacs Window Resize on Startup

So I have been using emacs a lot lately. And I have been noticing that the window resizes for a second when it starts up. Is there a way to fix that?
Here is the GIF of what I'm talking about.
To prevent Emacs from resizing its window after startup, put all geometry and font options on the command line or .Xdefaults file rather than in .emacs or other lisp init files.
The initial Emacs frame is drawn before running the lisp startup files, but the X config and command line options have already been read.
As your GIF is mainly showing a width change, with only a minor change in height and no change in the position of the frame, I suspect it is most likely font settings rather than size settings that you need to look for.
My Sample Code (Ensure to put these codes at the first line of your init.el file)
(setq frame-inhibit-implied-resize t) ;; prevent resize window on startup
(setq default-frame-alist '((width . 120) (height . 42)))
(defun x/disable-scroll-bars (frame)
(modify-frame-parameters frame '((horizontal-scroll-bars . nil)
(vertical-scroll-bars . nil))))
(if (display-graphic-p)
(progn
(scroll-bar-mode -1)
(tool-bar-mode -1)
(fringe-mode '(8 . 0))
(add-hook 'after-make-frame-functions 'x/disable-scroll-bars))
(progn
(menu-bar-mode -1)
(setq-default
left-margin-width 1
right-margin-width 0)))
The core is (setq frame-inhibit-implied-resize t).
I've got this line in my .emacs to go fullscreen on startup:
(add-to-list 'default-frame-alist '(fullscreen . maximized))
I run Emacs from WSL with an X11 server on Windows 10. I could set some -geometry to make it not resize into a minimal initial window, but that seemed to be flaky and random. This happened with both Xming and Vcxsrv. It happened even with -Q so in my case it was not related to anything in my startup files.
I haven't tried Cygwins X11 server, but when I tried the evaluation version X410 (available in the Windows Store) it did not have the same problem.

emacs init - splitting window and opening files next to each other

I am trying to customize my Emacs init file in such a way that Emacs opens with two windows split and the ansi-term opened in one side and my init file on the other side. Now, the function I wrote (switch-to-next-window) works perfectly if Emacs is open already.
I was hoping to make the cursor switch to the other window and then open my init file there. However, if I try to run this upon start-up (actually after start up, at least this is what I am thinking) I get the following error: window-live-p, nil
I am gessing that there is no "next window". But I just don't know a work around here since I do think that I am only calling my function after Emacs has fully started up? If anyone could point me to where I am going wrong in my logic, that would be great!
(split-window-horizontally)
(setq initial-buffer-choice "*ansi-term*")
(defun switch-to-next-window ()
(interactive)
(let* ((next-window (get-buffer-window (other-buffer (current-buffer) t))))
(select-window next-window)))
(add-hook 'emacs-startup-hook (lambda ()(ansi-term "/bin/bash")))
(with-eval-after-load "~/.emacs.d/init.el"
(switch-to-next-window)
(setq initial-buffer-choice "~/.emacs.d/init.el"))
Changing initial-buffer-choice after the initial buffer has been opened won't have any effect.
What helps is putting everything into the emacs-startup-hook and using the find-file-other-window function:
(add-hook 'emacs-startup-hook
(lambda ()
(ansi-term "/bin/bash")
(split-window-horizontally)
(find-file-other-window "~/.emacs.d/init.el")))

How to start emacs with ipython shell running in one side of a divided window?

I´d like to have the following workflow using emacs 23.4 as a python (2.7) IDE on Debian:
Emacs initiates with 2 windows side-by-side when opening a file like $ emacs file.py
There´s already a shell in the left window and the buffer file.py in the right window.
A shortcut executes the code (and another shortcut for parts of it) and the result can be seen in the left window (ipython shell). The focus remains at the right window and the buffers don´t change when the command is executed.
A shortcut easily switches the focus from left to right and the other way around.
I could, so far, accomplish everything except the second item (I have to make the buffer visible manually), which seems simple. I´ve been reading the emacs lips reference manual, so that I can customize emacs myself, but I´m still a beginner in emacs. I also found some similar questions, but not fully helpful. Here are some relevant parts of my .emacs.
;; Initial frame size and position (1280x1024)
(setq default-frame-alist
'((top . 45) (left . 45)
(width . 142) (height . 54)))
(if (window-system)
(split-window-horizontally (floor (* 0.49 (window-width))))
)
; python-mode
(setq py-install-directory "~/.emacs.d/python-mode.el-6.1.3")
(add-to-list 'load-path py-install-directory)
(require 'python-mode)
; use IPython
(setq-default py-shell-name "ipython")
(setq-default py-which-bufname "IPython")
; use the wx backend, for both mayavi and matplotlib
(setq py-python-command-args
'("--gui=wx" "--pylab=wx" "--colors=linux"))
(setq py-force-py-shell-name-p t)
; switch to the interpreter after executing code
(setq py-shell-switch-buffers-on-execute-p t)
;(setq py-switch-buffers-on-execute-p t)
; don't split windows
(setq py-split-windows-on-execute-p nil)
; try to automagically figure out indentation
(setq py-smart-indentation t)
(defun goto-python-shell ()
"Go to the python command window (start it if needed)"
(interactive)
(setq current-python-script-buffer (current-buffer))
(py-shell)
(end-of-buffer)
)
(goto-python-shell)
I believe the solution is simple and lies on the functions/variables: switch-to-buffer, initial-buffer-choice, other-window, py-shell-switch-buffers-on-execute-p, py-switch-buffers-on-execute-p.
However, I still couldn't find a solution that makes it all work.
EDIT:
I was able to have my desired behavior, substituting the last part for:
(switch-to-buffer (py-shell))
(end-of-buffer)
(other-window 3)
(switch-to-buffer (current-buffer))
, since I found out with get-buffer-window that the left window appears as 3 and right window as 6.
When py-split-windows-on-execute-p, py-switch-buffers-on-execute-p are set, it should work as expected - no need to hand-write splitting.
Remains the horizontal/vertical question.
For this python-mode.el provides a customization of py-split-windows-on-execute-function in current trunk:
https://code.launchpad.net/python-mode

How do I configure Emacs to dedicate the Calculator window?

I'm using emacs 24.3 from emacsformacosx.com on OS X 10.9 (Mavericks). The behavior is the same on emacs 23.4.1 on Debian Wheezy.
I want to automate applying set-window-dedicated-p so switching/opening a buffer won't use certain windows. For example, if I'm in the Calculator and manually use Meta-: and enter (set-window-dedicated-p (get-buffer-window) t) then it works great - my Calculator window doesn't get hijacked by other buffers. I want it to work like that automatically.
I put this in my .emacs file:
(add-hook 'calc-mode-hook
(lambda ()
(message "Dedicating %s" (buffer-name))
(set-window-dedicated-p (get-buffer-window) t)
(message "Dedication %s" (window-dedicated-p (get-buffer-window "*Calculator*")))))
Then I start up emacs, switch to the *Messages* window, and Meta-x calc. The *Messages* buffer shows
Dedicating *Calculator*
Dedication t
so I know my hook was called and what buffer it operated on. But the *Calculator* buffer is not dedicated - it doesn't behave properly and Meta-: (window-dedicated-p) returns nil. The *Messages* buffer is dedicated instead.
Why is the *Calculator* window shown as dedicated in the hook but not afterwards? What am I doing wrong here?
Unfortunately the *Calculator* buffer is not displayed in any window at the point your code runs.
Your 'validation' messages were misleading you. (buffer-name) is certainly the buffer you want, but it's not in any window, and so you're actually passing a nil argument for the window in all situations. i.e. You're setting the current window dedicated, and then confirming that it's dedicated (which it should indeed be).
I think after advice to calc is what you need here. e.g.:
(defadvice calc (after my-dedicated-calc-window)
"Make the *Calculator* window dedicated."
(let ((win (get-buffer-window "*Calculator*")))
(when win
(set-window-dedicated-p win t))))
(ad-activate 'calc)
n.b. I'm not sure exactly how the arguments to calc affect the window display, but I think with the test for the window wrapping the call to set-window-dedicated-p this code is probably fine in all cases.

Emacs shell-mode display is too wide after splitting window

If I run M-x shell in emacs to get a terminal, it knows where to wrap lines automatically. For example, the output of ls is formatted into columns that fit the window properly.
My problem is if I then split the window vertically with C-x 3, shell-mode still thinks the window fills the whole frame. The result is ugly wrapping of command output. Is there a way to let shell-mode know it has to update the screen width?
EDIT:
Using HN's answer below, I came up with this fix:
(defun my-resize-window ()
"Reset the COLUMNS environment variable to the current width of the window."
(interactive)
(let ((proc (get-buffer-process (current-buffer)))
(str (format "export COLUMNS=%s" (window-width))))
(funcall comint-input-sender proc str)))
(defun my-shell-mode-hook ()
(local-set-key "\C-cw" 'my-resize-window))
I'm a bit late to the party, but COLUMNS isn't the way to do this. Here is an excerpt from my .emacs:
(defun comint-fix-window-size ()
"Change process window size."
(when (derived-mode-p 'comint-mode)
(set-process-window-size (get-buffer-process (current-buffer))
(window-height)
(window-width))))
(defun my-shell-mode-hook ()
;; add this hook as buffer local, so it runs once per window.
(add-hook 'window-configuration-change-hook 'comint-fix-window-size nil t))
(add-hook 'shell-mode-hook 'my-shell-mode-hook)
Unlike exporting COLUMNS each time, this method doesn't require you to be in
bash at the moment and it doesn't spam your session with blank prompts. This
code should probably be in comint itself, maybe I will file a bug report.
EDIT: If you modify window-configuration-change-hook in a buffer-local-way
the hook runs once per window, as opposed once per frame.
Here is a slightly improved resize function from #Christopher Monsanto's answer. The original one will cause a problem due to nil process. (e.g exit in shell-mode)
(defun comint-fix-window-size ()
"Change process window size."
(when (derived-mode-p 'comint-mode)
(let ((process (get-buffer-process (current-buffer))))
(unless (eq nil process)
(set-process-window-size process (window-height) (window-width))))))
This display is dictated by the COLUMNS environment variable. In my setup COLUMNS has a value of 202, after a vertical split ls displays correctly on shell-mode if I set columns to 80 via
export COLUMNS=80
There must be a way to code this up but I don't have enough elisp-fu to do that. If you'd rather avoid the hassle multi-term manages this automagically.
http://www.emacswiki.org/emacs/MultiTerm
Try M-x eshell; it doesn't have this problem.