How to create buffer similar to *compilation* in Emacs? - emacs

I have an asynchronous process in Emacs, which creates a TAGS file.
This process creates a process buffer called *ctags*. If the process result is "finished\n", I kill the buffer.
If the process result is anything else I want to display the process buffer similar to the *compilation* status output when running M-x compile.
I.e. I want to vertically split the screen and show the *ctags* buffer at the bottom. Pressing q would preferably kill the bottom buffer and just show my original buffer.
I tried using this in my process sentinel callback:
(split-window-vertically)
(set-window-buffer (selected-window) (get-buffer "*ctags*"))
but aside from the fact that it puts the *ctags* buffer on top, the buffer does not have the same characteristics as the *compilation* output, e.g. pressing q inserts q.
How do I create a buffer like *compilation*?
EDIT:
Inspired by Trey Jackson's answer below, this does exactly what I want:
(pop-to-buffer (get-buffer "*ctags*"))
(compilation-mode)
It selects the *ctags* buffer, puts it into compilation mode and q will quit the window.
EDIT2:
Using (compilation-mode) (major mode instead of minor mode) since Emacs somehow doesn't like reapplying the minor mode to an exisiting buffer.
The Error message I get is:
Toggling compilation-minor-mode off; better pass explicit argument.

To get the behavior of the *compilation* buffer, add this to your script:
(compilation-mode)

It's better to derive your own mode from compilation-mode, and define error regex, etc.

Related

how to empty or clear the emacs minibuffer?

Sometime the content of the minibuffer shows the output of a command (emacs 24). This is not too much of an inconvenience when the output is just one line. It's more annoying when the command is multiple lines long and the minibuffer uses many lines of display that could be used for something else.
Is there a way to clear the content of the minibuffer ?
Note: When I M-! echo usage: foo ; echo the minibuffer content changes to usage: foo.
Note: I'm not in recursive edit, the minibuffer is not active, using C-g, M-x C-g , (message nil), M-x delete-minibuffer-contents, M-: (kill-buffer " Echo Area 0") does not clear the minibuffer
Normally, C-g works just fine in those cases. It'll print "Quit" in the minibufer, which is just one line and unobtrusive enough.
If you need to clear the minibuffer programmatically, call (message nil).
If, for some reason, C-g does not work for you, make a new command and a keybinding for clearing the minibuffer
(defun my-clear-message ()
(interactive)
(message nil))
(global-set-key (kbd "C-c c") 'my-clear-message)
My guess, from your description ("the minibuffer is not active") and your replies to other answers, is that it is not the minibuffer that needs clearing - it is the echo area.
This is the same physical space, but the echo area is for output (e.g. message), whereas the minibuffer is primarily for input.
To clear the echo area, use (message nil), as suggested. Or use the minibuffer, followed by C-g - e.g., M-x C-g. That usually takes care of the job (but see below, about killing the echo-area buffer, if you really need to clear it).
If it really is the minibuffer input that you want to clear, then:
C-g (repeated, if necessary) quits the minibuffer.
You can use any text-clearing keys to clear the input without exiting. E.g., C-x DEL will clear quite a bit (it is backward-kill-sentence). I bind M-k (normally kill-sentence) to a command that deletes all of the minibuffer input (this is the case in Icicles, for instance). Command delete-minibuffer-contents wipes it all out.
(You can also kill the echo-area buffer, if it should ever get polluted with some text you want to get rid of. The buffer will be re-created automatically. With vanilla Emacs it is a bit problematic to do this interactively, but you can at least do it using M-: (kill-buffer " *Echo Area 0*") (note the SPC char prefix).)
Although I do not understand why following clears the minibuffer: M-! echo ; echo
This output is normally cleared as soon as you do anything else in Emacs. But sometimes Emacs gets confused and the message keeps reappearing in the echo area.
Deleting the buffer named *Shell Command Output* would solve the problem, but when this happens to me Emacs refuses to delete this buffer. Deleting the contents of this buffer solves the problem, but output reappears in the buffer the next time you do a shell command.
You can prevent that problem by renaming the buffer. One way to do that is to make that buffer current (e.g., with C-x b or M-x switch-to-buffer) and do M-x rename-uniquely. If you do that and delete the contents of the buffer, the problem is avoided.
I have no idea why sometimes Emacs refuses to kill this buffer, but fortunately it allows renaming it so it will no longer be reused for command output.
Try pressing C-g, this will clear the echo area.

Emacs+AucTex: can't display the output window when the reftex-toc window is dedicated

I am edting latex files with Emacs+AucTeX. When working I have two windows in the frame: the latex file window and the reftex-toc window. I set the reftex-toc window dedicated to its buffer by
(defadvice reftex-toc (after reftex-toc-window-dedicated activate)
(set-window-dedicated-p (selected-window) t))
to keep it from being replaced.
But problems arise when I call (TeX-recenter-output-buffer) by C-c C-l. The output buffer can't be displayed. I have to manually switch to the output buffer by C-x b. And if I call help commands, e.g. C-h f, the help buffer is not displayed in a new window as it should be. Instead, it replaces the latex file window. How can I fix this problem?
EDIT
I found that the problem is due to function (display-buffer BUFFER), which is internally called by (TeX-recenter-output-buffer). Under normal circumstances where there is no dedicated window in the frame, (display-buffer BUFFER) creates a new window for BUFFER. But if there are a dedicated window and some normal windows, (display-buffer BUFFER) just displays BUFFER in one of the normal windows w/o creating a new one.
As a workaround, I open another (the 3rd) window, so that if I call C-c C-l, the compilation output is displayed there and the latex file window is kept. However, I am still looking forward to a real solution. Could any one help? Thank you.

Emacs switch to buffer in different frame

I'm currently programming away in emacs. I have a function defined in my .emacs that saves all my work and executes the interpreter to run my work in the currently open shell buffer. Typically I'll be editing in one or more frames and have the shell open in a separate frame. The problem I have is when I run my save and execute function, everything is saved but the shell buffer then gets displayed in the frame I'm currently editing. So I have two frames showing the shell buffer and I can't see the source code I was just editing. Quite often when I'm programming, I'd immediately like to compare the output of the code with the code I've just written. It's a bit annoying to have to switch back to my code buffer and then go to the end of the other shell buffer to look at the output while referencing the just written code.
(defun execute-script ()
"Switch to shell buffer and re-execute the last command."
(interactive)
(save-some-buffers)
(switch-to-buffer "*shell*")
(end-of-buffer)
(comint-previous-input 0)
(comint-send-input))
As you can see my function is rather primitive at the moment, just re-executing the most recently used command in the shell.
I know that Emacs has the functionality to switch to a buffer in a different frame, as the ido buffer switcher code does this. Does anybody have any pointers as to what I need to replace my call to switch-to-buffer with to get the desired effect?
Regards Giles.
Exchange
(switch-to-buffer "*shell*")
for
(switch-to-buffer-other-window "*shell*")
This will enforce a two window layout with your previous buffer on the left side and the shell buffer on the right side.
Edit: If you're using a multiple frame layout and not a multiple window layout you can use
(switch-to-buffer-other-frame "*shell*")
You can also use pop-to-buffer which will later let you customize exactly how that behaves, e.g. via display-buffer-reuse-frame, or display-buffer-alist, or special-display-regexp.

Emacs buffer shown after closing a buffer

How can I modify the way emacs picks which buffer to show after closing a buffer?
When I have multiple columns showing the same buffer, and then open another file in one of the buffers and then close the newly opened buffer, it doesn't switch back to the previous buffer, but to another buffer.
I'll try to explain with an example:
Start with a new emacs at *scratch*
C-x 2 (split into two columns)
C-x C-f 1 (find file 1)
C-x o (switch to other frame)
C-x b 1 (find file 1)
C-x C-f 2 (find file 2)
C-x k (kill buffer)
Now it switches to scratch but I would like it to show 1 in both windows again, is it possible to make emacs behave this way?
This may not be a direct answer to your question, but it might help.
Emacs manages its buffer list, including deciding which buffer gets displayed when you kill one (via kill-buffer). I haven't looked into how it's done, but the documentation is "out there". Lots of people have created custom buffer-stack management magic to change the way emacs does things, maybe some of them are based on bayesian analysis, or whatever. You can imagine the possibilities.
I've never looked into changing the way emacs manages its buffers. Instead I just bind other-window and switch-to-buffer to easy keystrokes (C-x o, C-x b) and I get really good at using them.
you could create a simple function for what you want: it should destroys all other windows, then split the window so that the current buffer is displayed in both. Luckily, emacs has functions that do exactly those things.
(defun cheeso-show-buffer-two-windows ()
"Close all other windows; then split, and show the current
buffer in both windows."
(interactive)
(delete-other-windows)
(split-window-vertically))
Bind that to a keystroke, and badda-bing, you're there. This is a vertical split - the windows are displayed in a vertical stack. If you want it horizontally split (the windows are side-by-side), then replace ... well, you know.
This also doesn't quite help directly, but Winner mode might help you get where you want to get.
Are you using tabbar-mode? I had the same problem and for me tabbar was the cause. Tabbar adds the function tabbar-buffer-kill-buffer-hook to kill-buffer-hook. You can remove it with (remove-hook 'kill-buffer-hook 'tabbar-buffer-kill-buffer-hook).
If you don't use tabbar try M-x describe-variable kill-buffer-hook. One of the functions in this list should be responsible for messing with your buffers.

Run Emacs Command in Other Window

I'm looking for a way to send the output of an arbitrary Emacs command (in my case sql-send-region) to another window. I would prefer to maintain focus in the window I am currently in, which would effectively give me one window to edit queries and one window to view the output.
I was able to write some Emacs Lisp to solve my problem:
(defun sql-send-region-and-return (start end)
(interactive "r")
(let ((oldbuf (buffer-name)))
(sql-send-region start end)
(switch-to-buffer oldbuf)))
This sends the result of your region to the SQL buffer and returns back to your current buffer, effectively accomplishing the stated objective.
Thanks justinhj for giving me some new leads to solve my problem.
Have you tried setting this variable to t, it sounds like the behaviour you want.
sql-pop-to-buffer-after-send-region
After a call to sql-send-region' orsql-send-buffer',
the window is split and the SQLi buffer is shown. If this
variable is not nil, that buffer's window will be selected
by calling pop-to-buffer'. If this variable is nil, that
buffer is shown usingdisplay-buffer'.