0

I'm regularly compiling some C programs and one thing is annoying : after I've compiled, I'm opening a shell with M-x shell ./foo, but the shell open in place of the source code's window. I would like the shell buffer to open in place of the compilation window, which I don't really need anymore. Otherwise, it's an overkill.

I've read about this topic open buffer in a specified frame but my emacs lisp competence are not that great and I can't adapt it to my case.

Thanks in advance! I guess I'm not the only one it can help.

EDIT

I've added a screenshot to make things clearer. Let's call the above window "A" and the below "B". When I call the shell, i.e M-x shell (I rather use M-x term but that the same here), A has the focus, which is probably the reason why the shell window open in place of A. I want it to open in place of A (originally the compilation window).

screenshot

loukios
  • 815
  • 1
  • 8
  • 19
  • Are you familiar with the difference between Emacs terminology of a frame and a window, and do you **really** mean frame? **What's the difference between a buffer, a file, a window, and a frame?**: http://emacs.stackexchange.com/questions/13583/whats-the-difference-between-a-buffer-a-file-a-window-and-a-frame – lawlist Nov 02 '16 at 15:48
  • Yes you're right, I may edit my question accordingly. I hope my question makes sense though. – loukios Nov 02 '16 at 15:54
  • I would recommend specifying how many windows are open and in what layout -- e.g., side by side, and label them such as **A = left window = source code buffer** and **B = right window = compilation buffer**; the window that has focus before calling `M-x shell` is **A** *or* **B**; after calling `M-x shell`, **B should become `*shell*`** or **A should become `*shell*`**. Focus should be in the `*shell*` buffer (window **A** or **B**) when done. – lawlist Nov 02 '16 at 15:58
  • I like to modify the source code and/or create new functions, whereas most people prefer things like `advice`, the `display-buffer-alist`, `special-display-regexps`, etc. If it were me, I would just copy the `shell` function and rename it to something like `my-shell` and I would change `(pop-to-buffer buffer)` to whatever floats my boat -- e.g., `(switch-to-buffer buffer)` assuming that I am in the window that I want to contain the `*shell*` buffer when I'm done typing `M-x my-shell`. I suspect there are already several threads that suggest the *other* three (3) above-mentioned alternatives. – lawlist Nov 02 '16 at 16:11
  • The same concept with `term`, but it is reversed -- since the *function* `term` uses `(switch-to-buffer "*terminal*")` and perhaps you want `(pop-to-buffer "*terminal*")`. To read about `pop-to-buffer`, type `M-x describe-function RET pop-to-buffer RET`: "*Select buffer BUFFER in some window, preferably a different one. BUFFER may be a buffer, a string (a buffer name), or nil. If it is a string not naming an existent buffer, create a buffer with that name. If BUFFER is nil, choose some other buffer. Return the buffer.*" – lawlist Nov 02 '16 at 16:17
  • I'll try to do it but I guess I've to work on my emacs lisp first.. – loukios Nov 02 '16 at 16:24
  • No special skills are needed. Just type `M-x find-function RET term RET`. Copy the entire 15-line long `term` function to your `.emacs` or `init.el` file, change the function name from `term` to `my-term`, change `(switch-to-buffer "*terminal*")` to `(pop-to-buffer "*terminal*")`, and save your `.emacs` or `init.el` file. Either evaluate the function, or evaluate the entire buffer, or simply restart Emacs. Then try it out with `M-x my-term` – lawlist Nov 02 '16 at 16:31
  • I know this doesn't really answer your actual question, but I think you're fighting the wrong battle in trying to make Emacs open the shell window somewhere else. If you're just opening the shell to run the programme that you just compiled, you'd probably be better off running the programme as part of the compilation. For example, use M-x compile and instead of just putting "make" or "gcc foo.c -o foo" use `gcc foo.c -o foo && ./foo` and then, if the compilation succeeds the command will be run and you'll get the output in the compilation buffer. – stevoooo Mar 30 '17 at 11:36

0 Answers0