Running python interpreter or language X in Emacs - emacs

Every time I start the interpreter for a programming language (lets consider python in this case) emacs opens the interpreter in the window which is not current. Note that I have two windows open, so the interpreter is always opening in the opposite window that I am currently in. This is annoying because then I always have to switch windows after I open an interpreter...
How can I get the python interpreter to open in the current window inside emacs?
Can I solve this problem generally for other programs/shells/buffers that do not open in the current window?
I note that this also happens frequently with other emacs commands (such as C-h v and the description of the variable opens in the window that is not current)
All help is greatly appreciated!

Interpreter-buffers connect with a process. If not already there, it must be a different one than the buffer called from - otherwise the current buffers contents is lost.
You might be interested in org-mode, org-babel, which provides a way to insert results in current buffer when executing source-code.
With python-mode.el, set py-switch-buffers-on-execute-p to non-nil. After M-x py-shell, cursor is in new shell.
If py-split-windows-on-execute-p is nil, M-x py-shell should switch to Python shell without splitting the window. See more options if re-using an existing py-shell etc.
With python.el, M-x run-python switches into the Python-shell.
See also customizable variable pop-up-windows.

Related

Emacs opens ibuffer or compilation in same window rather than other window

I have been using emacs for many years. The past year or so, there has been an annoying problem, which is that sometimes Ibuffer or the compilation buffer runs in the same window that I am already in. I generally have 2 buffers open, one above the other. I always want Ibuffer to open in the one I am not currently in. But it seems to pick randomly. Is this a bug or feature? Can I stop it? I have searched for an answer several times, but I have not found one. I am running GNU Emacs 24.5.1 in the terminal. Thanks.
The following answer relates to the usage of ibuffer:
The original poster may wish to use the keyboard shortcut bound to the letter o, which is ibuffer-visit-buffer-other-window. The doc-string states that this function is designed to: Visit the buffer on this line in another window. The function ibuffer-visit-buffer-other-window uses pop-to-buffer and switch-to-buffer-other-window, which should avoid a window that is dedicated: http://www.gnu.org/software/emacs/manual/html_node/elisp/Dedicated-Windows.html
The return key is bound to ibuffer-visit-buffer (which uses switch-to-buffer), and the doc-string states that this function is designed to: Visit the buffer on this line. If optional argument SINGLE is non-nil, then also ensure there is only one window. The optional argument can be used interactively by typing C-u RET.

How to have find-file's prompt match Emacs shell's $PWD?

With Emacs, if the current buffer is one that's "visiting" a normal file (for example), whose full pathname is /path/to/somefile, and one runs find-file (C-x C-f), the prompt that appears in the mini-buffer is something like
Find file: /path/to/▮
...with the cursor placed as indicated above by ▮. IOW, the suggested path shown by default is always to the directory containing the file that the current buffer is visiting.
If, however, the current buffer is an Emacs shell process, and one runs find-file, then, AFAICT, the path shown in the prompt remains fixed at the value of $PWD when the shell process was started, irrespective of the current value of $PWD:
Find file: /pwd/at/startup/▮
This behavior is not so useful, because the $PWD at startup often becomes irrelevant later on. It would be really nice if the directory shown in find-file's prompt were instead the shell process's current $PWD.
Is there a simple way to modify find-file to behave this way whenever the current buffer is a shell process?
You want "shell directory tracking". E.g. check dirtrack-mode or shell-dirtrack-mode.
shell-dirtrack-mode tries to parse "cd" commands, (event_jr: which in my experience does not work consistently). dirtrack-mode uses the prompt regexp, which works very well.
There are a number of ways to manage this. As Stefan notes, there are a couple of built in packages that manage it.
My preferred way is to alter your prompt (when in Emacs) to have the $PWD embedded in it, and then Emacs strips it out and uses it. This has the benefit of always being up to date. I've found that dirtrack-mode sometimes gets out of sync.
Check out my solution here, which is a modification of a similar implementation on the Emacs Wiki.

Emacs: How can I automatically start a python interpreter in python-mode?

After every Emacs startup in python-mode I need to open the interpreter in a different buffer and
change the size of the new buffer that it fits emacs-code-browser's history. I want to automate
this activity.
How can I automate that an interactive python shell always appears (after Emacs startup) in an extra buffer like in the screenshot?
UPDATE: See below: workgroups.el and emacs-code-browser seem to collide. See screenshot
Before Editing
After Editing
I use Emacs23, emacs-code-browser and python-mode.el.
UPDATE: I tried to use workgroups.el. I defined a new python workgroup and added the following line to my .emacs: (wg-load "~/.emacs.d/workgroups/python_workgroup.wg"). The windows are, however, messed up. You can see it on the screenshot below:
It think that emacs-code-browser and the settings of my workgroup collide. Is there a way
to avoid this behavior?
also you may try desktop-save-mode:
desktop-save-mode is an interactive autoloaded Lisp
function.
(desktop-save-mode &optional ARG)
Toggle desktop saving (Desktop Save mode).
With a prefix argument ARG, enable Desktop Save mode if ARG is
positive, and disable it otherwise. If called from Lisp, enable
the mode if ARG is omitted or nil.
If Desktop Save mode is enabled, the state of Emacs is saved from
one session to another. See variable desktop-save' and function
desktop-read' for details.
How about using workgroups.el ?
It is used for saving your window configuration which is exactly what you need.
This way you can create a workgroup called for example Python, adjust buffers (including one containing interactive python shell) and everything and save the group, and next time you just open workgroup Python and all buffers open as they were before. If you want it to open automatically when emacs is started, just set your saved Python workgroup as a default workgroup.

Emacs, GDB, and multiple X windows

I use gdb-many-windows in emacs for my debugging.
Recently, I discovered how to move a buffer to another X window using C-x 5 2.
I find it convenient: since I use multiple displays, I can have one window with source and variables, and gdb's command line in another window.
But my problem is when I perform a step-by-step in gdb: when it opens another source file, it does that in the same window where the command line is located, while I expect it to be updated in the other window.
Does anybody know how to tweak gdb or emacs to do that? Thanks,
The value display-buffer-reuse-frames does address exactly this "issue". Session-wide setting in configuration file:
(setq-default display-buffer-reuse-frames t)

Preserve window layout in Emacs

I have setup my windows in a certain way. How do I save this setting to be invoked later?
I sometimes still use C-x r w <register> to store a window configuration in a register, and C-x r j <register> (where <register> is a single character) to jump back to it.
While this is a nice way for storing a few window configurations which you want to go back to after some time, I find winner-mode to be more convenient in a few regards. (For example, you won't have to bother naming the configurations).
Just put (winner-mode 1) in your .emacs, bind winner-undo and winner-redo to convenient shortcuts (or use the IMHO awkward C-c <left> and C-c <right> predefined ones), and you'll be able to switch back to previous window configurations.
See also: M-: (info "(emacs) Window Convenience") and M-: (info "(emacs) RegConfig")
I know you allready accepted an answer, however because I understand your question in another way (concerning layout) and this question showed up for me when I was in search for something to persist my emacs window layout, I would like to mention:
workgroups.el
which purpose is to save and restore the way, emacs windows are layed out and many more.
Check out emacs desktop. I have no emacs available but I believe it's part of the standard lisp packages you get when you install emacs.
Use the desktop library to save the
state of Emacs from one session to
another. Once you save the Emacs
desktop—the buffers, their file names,
major modes, buffer positions, and so
on—then subsequent Emacs sessions
reload the saved desktop.
Give Layout Restore a try if you just want to restore the window layout later.
If you want to persist a layout in your hard-drive, desktop-save is the option.
If you need to store multiple layouts during emacs' process lifetime (e.g. you connect to a long-running emacs daemon using emacsclient), you can use C-x r f <register> to store the frame layout in some register and C-x r j <register> for restore.
Since I now and then remotely connect to the work station to do sth, disconnect for a while and later reconnect, restoring layout helps me continue stuff seamlessly (note in this case window layouts stored by C-x r w would be gone).