Emacs Window Resize on Startup - emacs

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.

Related

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

GNU emacs editor specifying window height and width (Windows8)

I am running GNU Emacs 24.3.1 on Windows 8. I put the following 2 lines in my .emacs file to specify the height and width I want for the editor window:
(add-to-list 'default-frame-alist (cons 'height 63))
(add-to-list 'default-frame-alist (cons 'width 125))
That works fine. When I open another window from the main editor window using C-x 5 2, it starts up with the same width, but for some reason the height is 66, not 63.
Does anyone have thoughts on why this would happen?
How are you determining the actual size of the resulting (new) frame? If you are using function frame-parameters then that's the right approach.
Are you starting from emacs -Q, so that you are sure that you are not loading some code from your init file that might interfere?
If so, and if you see a discrepancy between what default-frame-alist says a new frame should be like and what the new frame is actually like, then this is most likely a bug. You can report it, giving your recipe starting with emacs -Q, by doing M-x report-emacs-bug.

Emacs: disable theme background color in terminal

I'd like to have emacs not to have a background color when I open a frame in the terminal. I'm using a terminal with a translucent background, and characters with a background color are not "see-through". TERM is set to "xterm-256color".
How do I get emacs to use the default background color (no color at all), when the frame is not graphical?
Edit:
I've got it, sort of:
(add-to-list 'custom-theme-load-path "~/.emacs.d/themes")
(load-theme 'my-awesome-theme t)
(defun on-frame-open (frame)
(if (not (display-graphic-p frame))
(set-face-background 'default "unspecified-bg" frame)))
(on-frame-open (selected-frame))
(add-hook 'after-make-frame-functions 'on-frame-open)
I put the above code in my init file, but only suppresses the background when opening an emacsclient in a terminal, and not emacs itself (i.e. only when invoked with emacsclient -t and not when invoked with emacs). Adding an extra (unless window-system (set-face-background 'default "unspecified-bg" (selected-frame))) doesn't work and only confuses graphical frames.
Any ideas on why this might happen?
(defun on-after-init ()
(unless (display-graphic-p (selected-frame))
(set-face-background 'default "unspecified-bg" (selected-frame))))
(add-hook 'window-setup-hook 'on-after-init)
Combined with the code in your edit, it worked nicely for me for both emacsterms and newly started emacsen. As for why window-setup-hook:
http://www.gnu.org/software/emacs/manual/html_node/elisp/Startup-Summary.html
(neither of the earlier hooks seemed to work except for this one.)
I tried the method that was suggested in this answer but I had no luck getting it to work. this snippet works for me though
(defun on-frame-open (&optional frame)
"If the FRAME created in terminal don't load background color."
(unless (display-graphic-p frame)
(set-face-background 'default "unspecified-bg" frame)))
(add-hook 'after-make-frame-functions 'on-frame-open)
Although it has a setback, if the terminal has a different background settings than the theme I use (dark vs. light), default theme faces are being used which may not seem good on the light or dark background. but in my case which both terminal and theme are dark it works fine.
There are already two answers to this question, one using window-setup-hook, which is called on startup, and another using after-make-frame-functions, which is called when a new frame is made, including after invoking emacsclient. To cover all possible cases, I found that I needed to do it this way:
(defun set-background-for-terminal (&optional frame)
(or frame (setq frame (selected-frame)))
"unsets the background color in terminal mode"
(unless (display-graphic-p frame)
(set-face-background 'default "unspecified-bg" frame)))
(add-hook 'after-make-frame-functions 'set-background-for-terminal)
(add-hook 'window-setup-hook 'set-background-for-terminal)
Note that I am only using selected-frame if necessary; it seems that in client mode, the hook is called before the frame is selected, so it is important to use the frame argument in that case.

Breaking out of .emacs script

I use two emacs (Aquamcs and text based emacs) on my Mac.
I normally use text based emacs for just editing something, so I don't want to load anything with it.
What I came up with is to have the checking code in .emacs to exit/break if it's text based emacs (darwin system but not aquamacs).
(when (and (equal system-type 'darwin) (not (boundp 'aquamacs-version)))
(exit) ??? (break) ????
)
It seems to work, but I don't know how to break out of .emacs. How to do that?
ADDED
I just wanted to speed up in loading text based emacs on my mac, and I thought about breaking out as a solution. Based on the helpful answers, I came up with the following code that runs .emacs only when it's not a text based emacs.
(setq inhibit-splash-screen t)
(unless (null window-system)
I don't know of any way to do exactly what you want. Some workarounds:
You can stop the evaluation of your .emacs by evaluating (error "message") but that's a bit unpleasant.
You can re-order your .emacs so that there's a (unless (CONDITION) ...) around the whole of the file.
You can run emacs -Q FILE when you're at the command line.
Why do you want to do this? Are you concerned at the time it takes to load your .emacs? If so, you might consider using the Emacs client/server instead.
I am not sure how to exit as well but..... I would rather advice another kind of logic for your init file than a flat file with all different configurations.
Take for example your ~/.emacs (or better ~/.emacs.d/init.el) as your controller and files like ~/.emacs.d/aquamacs.el or ~/.emacs.d/textmode.el as your individual configuration files.
That would make your init having something like this :
(defun my-short-hostname()
(string-match "[0-9A-Za-z]+" system-name)
(substring system-name (match-beginning 0) (match-end 0))
)
;Load different config file in text mode or gui mode.
(if (null window-system)
(load-file "~/.emacs.d/texmode-emacs.el")
(load-file "~/.emacs.d/gui.el"))
;Load configuration for this host only, ie ~/.emacs.d/myhostname.el if exist
(if (file-exists-p
(downcase (concat "~/.emacs.d/" (my-short-hostname) ".el")))
(load-file (downcase "~/.emacs.d/" (my-short-hostname) ".el"))))
I suggest having specific, different files to load conditionally from your .emacs, one for one setup, another for another setup.
Alternatively, just wrap the code for each setup in a progn and do the conditional in place, in .emacs itself.

How to maximize Emacs on Windows at startup?

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)