Maximize GNU Emacs on startup on Windows 8.1 - emacs

I'm running a 64-bit GNU Emacs version 25.0.50.1 on a 64-bit Windows 8.1 OS.
Adding the following to my .emacs does nothing:
(w32-send-sys-command 61488)
However, something like:
(global-set-key [(f4)] (function (lambda () "Maximize frame" (interactive) (w32-send-sys-command 61488))))
Works flawlessly; i.e. maximizes Emacs whenever F4 is pressed. However, I do not want to have to press F4 in order to do this -- I want it done automatically at startup. Why doesn't the first simple line work (it doesn't work even if it is the only line in my .emacs)?

Some time around the version 24 release, Emacs added a native function toggle-frame-maximized. This function doesn't take any arguments, but since the default state of Emacs it to have a non-maximized frame, on my Linux system simply adding
(toggle-frame-maximized)
to my init makes Emacs start up maximized.
As far as I know, this should also work on Windows.

This worked for me :
(defun maximize-frame ()
"Maximize the current frame"
(interactive)
(w32-send-sys-command 61488))
(defun post-load-stuff ()
(interactive)
(maximize-frame)
)
(add-hook 'window-setup-hook 'post-load-stuff t)

Related

Emacs version 24.4: New obnoxious loss of indentation on hitting RETURN

Starting with Emacs 24.4, when I type a line beginning with white space (a typical way to
denote a new paragraph) and at the end of it I hit RETURN, the white space disappears.
This problem appears also with 'emacs -Q'.
My .emacs file uses a rather plain text-mode paragraphing scheme, namely,
(setq default-major-mode 'text-mode)
(add-hook 'text-mode-hook 'paragraph-indent-minor-mode)
which has been working without problems for a dozen years. The bug appeared when I installed the current (24.4) version.
Basically, I type:
This is a line beginning with four spaces
and as soon as I type RETURN my line immediately becomes
This is a line beginning with four spaces
That is, the indentation vanishes. I'd much appreciate some advice.
Should I post a bug?
In Emacs 24.4, electric-indent-mode is enabled by default. It seems like that's what's causing this problem in combination with paragraph-indent-minor-mode. You can avoid that by turning off Electric Indent mode everywhere (M-x electric-indent-mode) or just in the local buffer (M-x electric-indent-local-mode).
The following will try to keep electric-indent-mode from stepping on the toes of paragraph-indent-minor-mode. It doesn't attempt to be robust in all situations, but I suspect it's entirely sufficient in your situation.
(defvar-local my-local-electric-indent-status :unknown)
(defun my-local-electric-indent-disable ()
"Make `electric-indent-mode' ineffective in the current buffer."
(setq my-local-electric-indent-status electric-indent-mode)
(electric-indent-local-mode -1))
(defun my-local-electric-indent-restore ()
"Restore original status of `electric-indent-mode' in the current buffer."
(unless (eq my-local-electric-indent-status :unknown)
(electric-indent-local-mode my-local-electric-indent-status)))
(add-hook 'paragraph-indent-minor-mode-on-hook #'my-local-electric-indent-disable)
(add-hook 'paragraph-indent-minor-mode-off-hook #'my-local-electric-indent-restore)
If you're not running at least Emacs 24.3, replace the defvar-local with:
(defvar my-local-electric-indent-status :unknown)
(make-variable-buffer-local 'my-local-electric-indent-status)
;;(global-set-key "\em" 'newline) ;;for emacs 23
global-set-key "\em" 'electric-newline-and-maybe-indent) ;;for emacs 24

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.

function to call same shell command in dired

i'd like to be able to call the same shell command on the marked files in dired without the need for emacs to prompt the command input as the command will always be the same. in particular, the command is "open" (for mac os x).
i tried to hack the function dired-do-shell-command in dired-aux.el but i don't understand the interactive line.
at the end of the day, i'd like to be able to bind this function to C-o for dired-mode so that i don't have to use mac os x's Finder to navigate files and open them. this will allow me to move to emacs entirely.
thanks.
(defun dired-open ()
(interactive)
(dired-do-async-shell-command
"open" current-prefix-arg
(dired-get-marked-files t current-prefix-arg)))
(define-key dired-mode-map (kbd "C-o") 'dired-open)
Edit:
We may use save-window-excursion to protect the existing window configuration from being messed up by the output buffer:
(defun dired-open ()
(interactive)
(save-window-excursion
(dired-do-async-shell-command
"open" current-prefix-arg
(dired-get-marked-files t current-prefix-arg))))

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)