In GNU Emacs I have a particular buffer *my-special-buffer* which I create as the output of running a sub-process and storing the output. I mark this buffer as read only after filling the contents. Occasionally when I try to exit Emacs I notice that I am prompted to save this buffer:
Save file /foo/bar/.../*my-special-buffer*? (y, n, !, ...
Is there a buffer local variable I can set as part of the initialization of this buffer to prevent the save prompt from interrupting my attempt to shut down Emacs? Just to be clear, I don't want to save this buffer; the buffer's purpose is only to show read-only data from the sub-process.
It looks like this is what I should set after filling in the buffer.
(set-buffer-modified-p nil)
More details here. After that, I make the buffer read only.
If you want to save all buffers when exiting without any questions at all, do C-u C-x C-c:
C-x C-c runs the command save-buffers-kill-terminal...
...With prefix ARG, silently save all file-visiting buffers, then kill.
If you want Emacs to think that the buffer should not be saved at all, all you need to do is to mark it as unmodified: M-~.
Related
In Emacs org-mode, I use the command save-some-buffers as a default save command to save all the buffers I've been working with. How do I instruct Emacs to not prompt me to save my Calendar and Org Agenda buffers?
If these are file buffers, then you can tell Emacs to save them without prompting by setting buffer-save-without-query (of course, you only want to set it buffer-locally in those buffers). And if these aren't file buffers, then Emacs prompts you only if buffer-offer-save is non-nil in that buffer, so you can avoid the prompt by setting that variable back to its nil default (in which case it won't save those buffers for you).
What's the easiest way to close a buffer in emacs? This would be synonymous with closing an actual file, right? It would probably prompt you to save, if necessary.
I found this in the Emacs help:
s-^ kill-some-buffers
But I don't know how to invoke that or what it means.
kill-buffer, which is usually bound to C-x k:
C-x k runs the command kill-buffer, which is an interactive built-in
function in `C source code'.
It is bound to C-x k.
(kill-buffer &optional BUFFER-OR-NAME)
Kill the buffer specified by BUFFER-OR-NAME. The argument may be a
buffer or the name of an existing buffer. Argument nil or omitted
means kill the current buffer. Return t if the buffer is actually
killed, nil otherwise.
The functions in kill-buffer-query-functions are called with the
buffer to be killed as the current buffer. If any of them returns
nil, the buffer is not killed. The hook kill-buffer-hook is run
before the buffer is actually killed. The buffer being killed will be
current while the hook is running. Functions called by any of these
hooks are supposed to not change the current buffer.
Any processes that have this buffer as the process-buffer are killed
with SIGHUP. This function calls replace-buffer-in-windows for
cleaning up all windows currently displaying the buffer to be killed.
Does anyone know what are the Emacs shortcut keys to write-protect the file in the buffer, and unprotect it?
C-x C-q calls toggle-read-only (documented here). This state prevents that modifications are made to the buffer and the buffer cannot be written to the file it is associated with (if any), but it will not change the writability on the OS level (e.g. other processes will still be able to write to the underlying file.
In Emacs, how do I copy a region of text (to paste it in another buffer) without killing it (for example: the file I want to copy from is opened in read-only mode, so killing it isn't an option).
Just mark it (C-space at one end of the range, and move to the other end) and use M-w (kill-ring-save):
(kill-ring-save BEG END)
Save the region as if killed, but don't kill it.
Two additional ways:
You can also select it with the mouse (mouse-button-1), which will copy the region to the kill ring.
When the buffer is read-only, you can use the kill-* routines (C-w and C-k) to copy the region/line to the kill ring. Emacs will beep at you, but it's a documented feature:
If the buffer is read-only, Emacs will
beep and refrain from deleting the
text, but put the text in the kill
ring anyway. This means that you can
use the killing commands to copy text
from a read-only buffer.
I use the command
M-x append-to-file
the problem with this is that if the file you want to copy it to is open, you will need to refresh the screen somehow so that the new stuff appears there. Also, the stuff you copied will go to the end of the file you choose as the target.
You might also find the commands
M-x write-region
and
C-x i (insert-file)
useful.
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.