echo current content of emacs's buffer upon closing - emacs

Is there anyway for me to configure emacs so that when I close it (C-c), it'd return the current content of the buffer to the shell?
Addition:
I know that buffer-string gives me a string containing the entire content of the currently focused buffer.
The question is, how do I return this string back to the shell?

Related

Emacs switch-to-buffer (C-x b) will change window if the buffer is opened in some other window

I would like to change the behavior of switch-to-buffer to always open the desired buffer in the current window instead of switching to another window if already opened.
My rationale for doing this :
I have 2 screens, one Emacs on each. One is for reading, the other one for writing. Some buffers will be in the reading side for a while, but now I want to edit them. I'd like to have them in front of me instead of automatically switch to this other window on the side.
Thank you.
That is what switch-to-buffer does - exactly what you say you want. Do you see something different when you start Emacs without an init file (emacs -Q)?
The only exception is when the window cannot be used for that buffer. As the doc string says:
If the selected window cannot display the specified
buffer (e.g. if it is a minibuffer window or strongly dedicated
to another buffer), call `pop-to-buffer' to select the buffer in
another window.

Turn off emacs automatic filename fill feature

Emacs has a somewhat annoying filename fill feature that when I put my cursor at some place that looks like a filename, e.g. /looks/like/filename and do C-x C-f, the find file path will starts at /looks/like/filename instead of current folder. I see this feature can sometimes be handy, but gets really annoying when I wants to open a file but my cursor happens to stay at a /** (which is usually the first line of my c++ file). Is there any way to turn this feature off? Thanks.

Display TTY in Emacs Shell mode dirtrack

Is there any way to display the current TTY when using Emacs shell mode? Right now I get around by having tty displayed as part of the prompt but this requires scrolling back
You can display it on the mode line.
Look at the documentation , in elisp manual, section 23.4 -- Mode Line Format. In subsection 23.4.2 there is written how you do it: you write a form that returns the value you are interested about.
`(:eval FORM)'
A list whose first element is the symbol `:eval' says to evaluate
FORM, and use the result as a string to display. Make sure this
evaluation cannot load any files, as doing so could cause infinite
recursion.

What does "buffer's restrictions" mean in save-restriction?

When I search for description of "save-restriction" in Emacs, it has a sentence about "The buffer's restrictions" - I have included the complete description below. What does this term mean? how does save-restriction work and when it should be used considering this?
(save-restriction &rest BODY)
Execute BODY, saving and restoring current buffer's restrictions.
The buffer's restrictions make parts of the beginning and end invisible.
(They are set up with `narrow-to-region' and eliminated with `widen'.)
This special form, `save-restriction', saves the current buffer's restrictions
when it is entered, and restores them when it is exited.
So any `narrow-to-region' within BODY lasts only until the end of the form.
The old restrictions settings are restored
even in case of abnormal exit (throw or error).
The value returned is the value of the last form in BODY.
Unless the purpose of your code is to modify the restriction, current buffer, point or window configuration, then you should use the appropriate save method to remember the state and automatically restore it for you.
save-current-buffer saves the current buffer, so that you can switch to another buffer without having to remember to switch back.
save-excursion saves the current buffer and its current point and mark too, so you can move point without having to remember to restore it.
save-restriction saves the restriction so that you can narrow or widen it without having to remember to restore it.
save-window-excursion saves the complete configuration of all the windows on the frame, except for the value of point in the current buffer.
(Side note: when I last used save-window-excursion there was no window-configuration-p method.)
save-restriction is used by narrow-* functions to save the current buffer , before to hide it, in order to be able to restore it.
'save-restriction' memorizes all 'buffer' data strucuture , in particular point-min, point-max, point-max-marker, etc . For example, before a narrow-function modifies the visibility of a buffer, it memorizes the old configuration, in order to be able to restore it using widen().

On Emacs, getting the output of a command sent to a buffer via Emacs-Lisp

I would like to write a small script in ELisp that would:
send a command to a given buffer
get its output
parse it
send it to another buffer
I am struggling with point 2: I cant get the output of a command. For example, if I have a shell buffer on, I can use
(process-send-string "shell" "help\n")
to send "help" to my shell buffer. It will then show the list of the commands available. But how can I get this list to use it somewhere else?
Thanks,
S4m
(buffer-string) returns the contents of the current buffer, so (with-current-buffer <buf> (buffer-string)) will return the contents of <buf>.
I don't know the exact emacs commands for this off the top of my head, but one option would be to do the following:
Set the mark in the shell buffer right below the command line
Execute the command.
Move the point to the end of the file and kill the text between there and the mark.
Move to the destination buffer and yank the text into there.
Have you considered using the shell-command or shell-command-to-string functions?
The don't "send a command to a buffer" like you asked, but they do both allow running a command through a process that will be started just for that purpose and either dumping the output into a target buffer or collecting it into a string.