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

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

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 terminal mode: how to copy and paste efficiently

I'm having a hard time making this emacs -nw work effectively under the terminal mode (emacs -nw).
Some setup information:
The working server is connected via SSH, and emacs is running on the server. Usually I'm connecting using SSH and "emacs -nw" to work on my files.
The emacs config is picked up from: https://hugoheden.wordpress.com/2009/03/08/copypaste-with-emacs-in-terminal/
;; make mouse selection to be emacs region marking
(require 'mouse)
(xterm-mouse-mode t)
(defun track-mouse (e))
(setq mouse-sel-mode t)
;; enable clipboard in emacs
(setq x-select-enable-clipboard t)
;; enable copy/paste between emacs and other apps (terminal version of emacs)
(unless window-system
(when (getenv "DISPLAY")
;; Callback for when user cuts
(defun xsel-cut-function (text &optional push)
;; Insert text to temp-buffer, and "send" content to xsel stdin
(with-temp-buffer
(insert text)
;; I prefer using the "clipboard" selection (the one the
;; typically is used by c-c/c-v) before the primary selection
;; (that uses mouse-select/middle-button-click)
(call-process-region (point-min) (point-max) "xsel" nil 0 nil "--clipboard" "--input")))
;; Call back for when user pastes
(defun xsel-paste-function()
;; Find out what is current selection by xsel. If it is different
;; from the top of the kill-ring (car kill-ring), then return
;; it. Else, nil is returned, so whatever is in the top of the
;; kill-ring will be used.
(let ((xsel-output (shell-command-to-string "xsel --clipboard --output")))
(unless (string= (car kill-ring) xsel-output)
xsel-output )))
;; Attach callbacks to hooks
(setq interprogram-cut-function 'xsel-cut-function)
(setq interprogram-paste-function 'xsel-paste-function)
;; Idea from
;; http://shreevatsa.wordpress.com/2006/10/22/emacs-copypaste-and-x/
;; http://www.mail-archive.com/help-gnu-emacs#gnu.org/msg03577.html
))
The reason to have:
(require 'mouse)
(xterm-mouse-mode t)
(defun track-mouse (e))
(setq mouse-sel-mode t)
is to enable mouse selection over text such that the text region is highlighted just as "C-x SPC" marking the region. Then I can use "M-x w" to copy and "C-x y" to paste text within emacs and between emacs and other apps.
All look perfect except that any operations related to X are REALLY SLOW! My connection to the remote server is smooth -- the latency is usually under 100ms. But to kill one line of text using "C-x k", it takes ~5 seconds! To paste it, it takes another 5 seconds!
When copy/paste is frequent sometimes, this becomes really annoying. I think this is related to the X sever messaging, but not sure if there is good way to fix this.
Any ideas?
Thanks!
This is not an ideal solution per se, but i figured out a way that I feel better than the previous one.
The idea is to get rid of X which causes heavy latency issues, i.e. keep only the following:
;; enable clipboard in emacs
(setq x-select-enable-clipboard t)
The results are:
copy/paste within Emacs is straightforward and fast.
copy from other apps to Emacs: Ctrl+Shift+v
copy from Emacs to other apps: mouse selection is now on X Selection, so right-click and copy shall copy the text into the Selection. Note that 'M-w" now won't copy anything into Selection or system clipboard.
This is again a compromise rather than a solution, but considering the fact that i copy/paste more often than inter-app operations, this is acceptable at the moment.
Still looking forward to a good solution!
You can accomplish this by using a terminal escape code!
There is a unique category of terminal escape codes called "Operating System Controls" (OSC) and one of these sequences (\033]52) is meant for interacting with the system clipboard. The great thing is that your terminal doesn't care where the code came from so it will work in remote sessions as well.
Most terminal emulators support it (iTerm2, OS X Terminal, and I think all Linux terminals besides GNOME). You can test if your terminal supports this sequence by simply running:
$ printf "\033]52;c;$(printf "Hello, world" | base64)\a"
Then paste from your system clipboard. If it pastes "Hello, world" then your terminal supports it!
I have this function in my init.el so when I call yank-to-clipboard Emacs will yank the value from my kill ring into the system clipboard:
(defun yank-to-clipboard ()
"Use ANSI OSC 52 escape sequence to attempt clipboard copy"
(interactive)
(send-string-to-terminal
(format "\033]52;c;%s\a"
(base64-encode-string
(encode-coding-string
(substring-no-properties
(nth 0 kill-ring)) 'utf-8) t))))
As I type this, I stumbled upon an almost-identical script supported by Chromium community: https://chromium.googlesource.com/apps/libapps/+/master/hterm/etc/osc52.el
For those running Emacs inside Tmux:
Tmux consumes the sequence, so you'll need to pipe the sequence to the Tmux active tty for this to work. I have a solution in my blog post here: https://justinchips.medium.com/have-vim-emacs-tmux-use-system-clipboard-4c9d901eef40
To extend on #justinokamoto's answer for use in tmux, it works great and is truly amazing. I haven't debugged it with e.g. tramp or other fancy emacs settings but to get it to work
Follow https://sunaku.github.io/tmux-yank-osc52.html great instructions, modifying your tmux.conf and ~/bin/yank
Make sure terminal access to your clipboard is enabled on your terminal
Then to pull into emacs you can use a function like:
(Caveat emptor, I am very new to elisp. This writes to a temporary file in /tmp/yank)
(defun custom-terminal-yank (&rest args)
(message "-> CLIP")
;; FOR EVIL MODE: UNCOMMENT SO FIRST YANKS TO KILL RING
;; need to yank first, with all those args
;; ;; https://emacs.stackexchange.com/questions/19215/how-to-write-a-transparent-pass-through-function-wrapper
;; (interactive (advice-eval-interactive-spec
;; (cadr (interactive-form #'evil-yank))))
;; (apply #'evil-yank args)
;; https://stackoverflow.com/questions/27764059/emacs-terminal-mode-how-to-copy-and-paste-efficiently
;; https://sunaku.github.io/tmux-yank-osc52.html
(f-write-text (nth 0 kill-ring) 'utf-8 "/tmp/yank")
(send-string-to-terminal (shell-command-to-string "~/bin/yank /tmp/yank"))
)
If anyone else uses evil mode as well (just to make things complicated) you can uncomment those lines and use something like
(define-key evil-visual-state-map "Y" 'jonah-terminal-yank)
So that normal "y" is for normal yanking in visual mode, but "Y" is for cross-clipboard yanking

How to split windows horizontally in emacs when executing current buffer?

I am coding in python using Emacs. I have modified ".emacs" to suit my needs. However, I do not know how to change the default vertical split behavior of windows when executing current buffer.
I know that:
(setq split-height-threshold nil)
(setq split-width-threshold 0)
works in general. But it does not work when I execute buffer for the first time using C-c C-c
Despite having the above code in my ~/.emacs, the following happens on C-c C-c :
I hope my question is clear, let me know if you have any idea about this.
I use the following. It fixes your problem (I hope) as well as preventing Emacs from splitting windows in the first place.
;;; ------------------------------------------------------------------
;;; display-buffer
;; The default behaviour of `display-buffer' is to always create a new
;; window. As I normally use a large display sporting a number of
;; side-by-side windows, this is a bit obnoxious.
;;
;; The code below will make Emacs reuse existing windows, with the
;; exception that if have a single window open in a large display, it
;; will be split horisontally.
(setq pop-up-windows nil)
(defun my-display-buffer-function (buf not-this-window)
(if (and (not pop-up-frames)
(one-window-p)
(or not-this-window
(not (eq (window-buffer (selected-window)) buf)))
(> (frame-width) 162))
(split-window-horizontally))
;; Note: Some modules sets `pop-up-windows' to t before calling
;; `display-buffer' -- Why, oh, why!
(let ((display-buffer-function nil)
(pop-up-windows nil))
(display-buffer buf not-this-window)))
(setq display-buffer-function 'my-display-buffer-function)

How to open multiple terminals?

In Emacs, I often find myself in a situation where I need to jump back and forth between various source files to various terminals. However, I feel like I do not have a good way to do this efficiently and it's clumsy that you can only open one shell in Emacs (shell, eshell, or term).
Moreover, I need an efficient way of juggle between multiple terminals and source files.
How can I achieve this?
You can have as many terminals and shells open at once as you want. Just use M-x rename-buffer to change the name of an existing *term* or *shell* buffer, and the next time you do M-x term or M-x shell, a brand new buffer will be created. In the case of M-x shell, a prefix argument will cause you to be prompted for the name of the new shell buffer, as offby1 noted.
A few years ago I had a job where I had to regularly log in to various production servers named "host01.foo.com", "host02.foo.com", etc. I wrote a little function like this one to make it easier to manage them all:
(defun ssh-to-host (num)
(interactive "P")
(let* ((buffer-name (format "*host%02d*" num))
(buffer (get-buffer buffer-name)))
(if buffer
(switch-to-buffer buffer)
(term "/bin/bash")
(term-send-string
(get-buffer-process (rename-buffer buffer-name))
(format "ssh host%02d.foo.com\r" num)))))
Then I bound this command to (say) s-h (super H), enabling me to just type M-5 s-h. If I didn't already have a buffer named *host05*, it would start a new terminal emulator buffer, rename it to *host05*, and ssh me into host05.foo.com. If buffer *host05* already existed, it would simply switch me to it. Quite handy!
You can certainly have multiple interactive shells open. Try typing C-u M-x shell RET RET.
Try using MultiTerm to open multiple shells.
You can use Emacs Lisp Screen, which emulates GNU Screen and provides easy key bindings to jump to and between a number of different shells.
I use many methods for incorporating my terminal life into Emacs:
elscreen.el is a life saver, if you have a complicated window layout like gdb or have simply become overwhelmed with clutter you just open a new screen. In your case you could dedicate one screen to terminals.
multi-term.el makes managing terminals a bit easier.
shell-pop.el, a great tool for quick terminal access. shell-pop lets you assign a key to opening and closing a specific shell buffer window, if you've used drop-down terminals like tilda you know how incredibly handy this can be:
Here's and example of my shell-pop configuration, I use the key C-t to pop up an eshell:
(require 'shell-pop)
(shell-pop-set-internal-mode "eshell") ; Or "ansi-term" if you prefer
(shell-pop-set-window-height 60) ; Give shell buffer 60% of window
;; If you use "ansi-term" and want to use C-t
;; (defvar ansi-term-after-hook nil)
;; (add-hook 'ansi-term-after-hook
;; '(lambda ()
;; (define-key term-raw-map (kbd "C-t") 'shell-pop)))
;; (defadvice ansi-term (after ansi-term-after-advice (org))
;; (run-hooks 'ansi-term-after-hook))
;; (ad-activate 'ansi-term)
(global-set-key (kbd "C-t") 'shell-pop)
I usually do an an M-x server-start and then use emacsclient --no-wait to open files. I've aliased that to e with some embellishments so that it's a little more convenient.
I do all my work in a single terminal and just "throw" the files I want to edit into Emacs using e. Inside Emacs, I juggle around using iswitchb and it works just fine. YMMV.
I regularly used 10 or so shells in my old workplace. The secret is you have to rename additional shell buffers. I did this automatically though in my .emacs, creating and naming the shells logically (I had projnameRun and projnameBuild for every project). Worked really well together with anything, making it very easy to refind the right shell (you use the end of the project name combined with either r or b for run/build).
Instead of having several terminal windows in emacs, I spawn a different xterm whenever I need a new terminal. This of course is bearable because I use a very lightweight terminal emulator (urxvt) which starts in under 0.2s.
Then I use my window manager to switch between them and emacs frames. A configurable window manager will have plenty of options to tune to switch between windows (extremely) efficiently. Inside emacs, I use windmove and ido-mode, and have bound to C-tab a function that switches to the last buffer (because I use C-x b in that fashion a lot).
So um, not sure how useful it is to you since it's quite different from your use pattern, but this is what works for me.
I had exactly the same problem some years ago, and found nothing that satisfied me; so I wrote my own "toggle shell" function. It toggles between the current frame or window configuration and a system shell buffer. It can also put the shell into a dedicated frame, and inject a pushd to the current buffer directory.
This is an excerpt from my .emacs:
(defvar --toggle-shell-last-window-conf nil "The last window configuration.")
(defvar --toggle-shell-last-buf nil "The last buffer object in case there's no last window configuration.")
(defvar --toggle-shell-last-frame nil "The frame that was selected when opening a shell buffer.")
(defun --toggle-shell-have-conf ()
(window-configuration-p --toggle-shell-last-window-conf))
(defun --toggle-shell-store-last-conf ()
(setq --toggle-shell-last-buf (current-buffer)
--toggle-shell-last-frame (selected-frame)
--toggle-shell-last-window-conf (current-window-configuration)))
(defun --toggle-shell-restore-last-conf ()
(if (--toggle-shell-have-conf)
(progn (raise-frame --toggle-shell-last-frame)
(set-window-configuration --toggle-shell-last-window-conf))
(let ((bufnam (if (bufferp --toggle-shell-last-buf)
(buffer-name --toggle-shell-last-buf) --toggle-shell-last-buf)))
(if bufnam
(if (get-buffer bufnam) (switch-to-buffer bufnam t)
(message "%s: buffer not available" bufnam))))))
(defun --toggle-shell (&optional display inject-cd)
"Toggles between current buffers and a system shell buffer. With prefix-arg
close the shell.
When DISPLAY is 'vertical splits the shell as vertical window; when 'frame uses
a dedicated frame (default: single window). When INJECT-CD executes a `pushd'
to the working directory of the buffer from which you toggled the shell."
(interactive)
(let* ((shell-buf (get-buffer "*shell*"))
(shell-window ; non-nil when currently displayed
(if shell-buf (get-buffer-window shell-buf t)))
(shell-frame
(if shell-window (window-frame shell-window)))
(in-shell (eq (current-buffer) shell-buf))
(vertical (string= display 'vertical))
(popup-frame (or (string= display 'frame)
(and inject-cd (not (bufferp shell-buf)))
(and (framep shell-frame)
(not (eq shell-frame (selected-frame)))))))
;; With prefix-arg close shell, restore windows. Otherwise (no prefix-arg)
;; toggle shell window; restore windows when called twice in a row, or the
;; current buffer is the shell buffer (`in-shell').
(if current-prefix-arg
(if (bufferp shell-buf)
(progn (message "Exiting shell '%s'" (buffer-name shell-buf))
(kill-buffer shell-buf)
(if in-shell (--toggle-shell-restore-last-conf)))
(error "No shell buffer to kill."))
;; If already in shell-buffer toggle back to stored frame-configuration.
(if (and in-shell (not inject-cd))
(progn
(--toggle-shell-restore-last-conf)
;; Recurse to reopen the shell-buffer in a dedicated frame, or
;; close the dedicated frame and reopen the buffer in a window.
(if (and popup-frame (eq shell-frame (selected-frame)))
(--toggle-shell 'frame inject-cd)
(when (and popup-frame shell-frame)
(delete-frame shell-frame)
(--toggle-shell nil inject-cd))))
;; Not in shell buffer. Warp to it or create new one.
(unless in-shell
(--toggle-shell-store-last-conf))
(if popup-frame
(progn (switch-to-buffer-other-frame (or shell-buf "*shell*"))
(raise-frame
(or shell-frame (window-frame (get-buffer-window "*shell*" t)))))
(if (> (count-windows) 1)
(delete-other-windows)))
;; Finally `cd' into the working directory the current buffer.
(let ((new-shell (not (bufferp shell-buf)))
(new-dir ; `default-directory' of `--toggle-shell-last-buf'
(if --toggle-shell-last-buf
(buffer-local-value 'default-directory --toggle-shell-last-buf))))
;; Open shell, move point to end-of-buffer. The new shell-buffer's
;; `default-directory' will be that of the buffer the shell was
;; launched from.
(when vertical
(if (> (count-windows) 1)
(delete-other-windows))
(split-window-vertically) (other-window 1))
(funcall 'shell)
(when new-shell
(message "New shell %s (%s)" (buffer-name (current-buffer)) new-dir)
(if inject-cd (sit-for 2))) ; wait for prompt
(goto-char (point-max))
;; If on a command-prompt insert and launch a "cd" command (assume no
;; job is running).
(when (and inject-cd new-dir)
(save-excursion
(backward-line-nomark) (end-of-line)
(unless (setq inject-cd (re-search-forward comint-prompt-regexp (point-max) t))
(error "Cannot `pushd', shell is busy")))
(when (and inject-cd)
(let* ((cmd (format
"pushd '%s' %s" (comint-quote-filename new-dir)
(if (buffer-file-name --toggle-shell-last-buf)
(format "# '%s'" (file-name-directory (buffer-file-name --toggle-shell-last-buf)))
""))))
;; `shell-process-cd' set new `default-directory' and set
;; `shell-last-dir' to old. (If the pushd command is
;; successful, a dirs is performed as well; >nul discards this
;; output.)
(shell-process-cd new-dir)
(insert cmd)
(comint-send-input)
(message "%s: cd '%s'" (buffer-name --toggle-shell-last-buf) new-dir))
)
)
)
)
)
)
)
--toggle-shell is the function that does the trick. I bind it to F12:
;; F12 toggle between shell buffer and current window configuration
;; SHIFT-F12 like before, but let shell buffer appear in a dedicated frame
;; ALT-F12 inject a pushd to change to directory of current buffer
;; CTRL-F12 `shell-command'
(global-set-key [(f12)] '--toggle-shell)
(global-set-key [(shift f12)] '(lambda()(interactive)(--toggle-shell 'frame)))
(global-set-key [(meta f12)] '(lambda()(interactive)(--toggle-shell nil t)))
(global-set-key [(meta f10)] '(lambda()(interactive)(--toggle-shell nil t)))
(global-set-key [(control f12)] 'shell-command) ; alias M-!
This is a significant bunch of code to be posted here. But it shall work well.
Semi related - you can quickly run a shell command on selected file with
M+shift+!
It saves a lot of time for smaller commands chmod etc
And maybe my quick pop-up shell also might help you. A quick pop-up shell for emacs
Ecb + eshell will be what you want exactly!
I use vi, but hope this helps. I can open as many terminals as I want by (eg. in Ubuntu 16.04):
ctrl + alt + t
I usually open 2 terminals, and move (position) one terminal to the right by:
ctrl + super + right-arrow
and move the other terminal to the left by:
ctrl + super + left-arrow
so that I have a divided screen by 2 terminals.

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)