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

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().

Related

Does `evil-mode` have vim like `changes` function?

As per vim wiki:
Vim remembers the locations where changes occurred. Each position
(column number, line number) is recorded in a change list, and each
buffer has a separate change list that records the last 100 positions
where an undo-able change occurred.
One can then use g; to move to the last change in the change list. This list survives through different sessions. This means, that even if one did not make any change to the file after opening it in a new session, g; will move the cursor/point to the line where the latest change occurred in previous session.
From what I can tell, evil-mode does not have the change list per buffer which survives session. Or does it?
You probably want to have a look at Undo Tree, which is used by evil-mode if undo-tree is installed. I do not think that it has the g; functionality that you describe though. It can, however, maintain undo history between sessions.
Take a look at goto-last-change on melpa. Evil has default integration through g;.

Is there a region change hook in emacs lisp?

I'm trying to get the content of current selected region in buffer. I'm aware of idle timer, but a hook should be more efficient/cleaner...
Not sure what you mean by "region change". If you mean "the text in the region is modified", then you'll need to use after-change-functions. If you mean that the selected text is modified by changing its bounds, then you'll probably want post-command-hook or maybe an idle timer (which is not less efficient than a hook, the main difference is that you get less guarantees about when it gets run; e.g. it won't be run between two commands if there's no idle time between the two, as is the case when running a keyboard macro).
A way to go seems to advice handle-shift-selection. AFAICT this function is called with every region-change by keyboard. Resp. advice mouse-drag-region.

Switch to original buffer after chasing tags in Emacs

I use M-. to jump to definitions of class/functions. Sometimes there are multiple classes with the same tag, so I need to use C-u M-. to jump to multiple files, hence multiple buffers. Now my question is, how do I go back to the original buffer quickly? I know C-x b, but you need to type in the buffer name, or it just give you by default the last buffer you visited, is there anyway to go further? For example, go to the previous buffer of the last buffer?
I believe that M-. calls find-tag by default. You should be able to go back up the stack of locations with M-* (pop-tag-mark).
From C-h f find-tag:
A marker representing the point when this command is invoked is pushed
onto a ring and may be popped back to with M-*. Contrast this with the
ring of marks gone to by the command.
Icicles multi-command icicle-find-tag, bound to M-. in Icicle mode, combines all of what vanilla Emacs commands M-. (find-tag), M-, (tags-loop-continue), tags-apropos, and list-tags do. And it does more.
You can complete against any tags, cycle (in different orders) among a subset of tags matching an additional pattern, and so on, visiting multiple tags in a single command invocation. You choose the tags you want to visit, in any order --- you need not visit each one in sequence.
You first enter (using RET) a regexp that all tags you are interested in must match (it could be vacuous, to get all tags).
After that, you can type a pattern that a subset of the tags and or their source files must match.
That is, by default you can complete against multi-completion candidates that are composed of the tag itself and its source file name.
You can choose candidates to visit using C-mouse-2 in *Completions* or by cycling among their names using down and up and then using C-RET to visit.
You can return to your original location using M-* (icicle-pop-tag-mark). You can also return to it by just using C-g to finish your M-. invocation.
More information here.
I use winner-mode for this (and other similar situations).
Add (winner-mode 1) to your init file, and then when you wish to return to the window configuration that you were in before jumping to the tags, you just type:
C-c<left> to call winner-undo (repeating as many times as necessary)
If you had visited multiple tags in another buffer, this will get you back to your original buffer (or the previous buffer, at any rate) in a single step, rather than stepping back through the individual tags one by one.
If the tags have taken you through multiple buffers, then you'll need to type C-c<left> once for each buffer (or C-c<left>C-xzzz... if you'd gone on quite a long detour :)

Emacs buffer undo limit

I get this warning in my matlab-shell buffer when I print alot to stdout:
Warning (undo): Buffer `*MATLAB*' undo info was 12268000 bytes long.
The undo info was discarded because it exceeded `undo-outer-limit'.
This is normal if you executed a command that made a huge change
to the buffer. In that case, to prevent similar problems in the
future, set `undo-outer-limit' to a value that is large enough to
cover the maximum size of normal changes you expect a single
command to make, but not so large that it might exceed the
maximum memory allotted to Emacs.
My emacs looks like this:
I really don't need any undo in the matlab-shell which is the right buffer. Is there a way to disable this warning? Note that the left buffer is a MATLAB script which means that the major mode is MATLAB, and certainly undo should not be disabled there.
As that warning message says (or used to say?):
You can disable the popping up of this buffer by adding the entry
(undo discard-info) to the user option warning-suppress-types,
which is defined in the warnings library.
That is:
(add-to-list 'warning-suppress-types '(undo discard-info))
(That will of course just disable the warning, not the undo data collection itself.)
Your question is a little ambiguous, but assuming you're saying that you have no need to undo things in this buffer, then you can disable the undo system on a per-buffer basis:
buffer-disable-undo is an interactive compiled Lisp function in `simple.el'.
(buffer-disable-undo &optional BUFFER)
Make BUFFER stop keeping undo information.
No argument or nil as argument means do this for the current buffer.
So you can call M-x buffer-disable-undo RET interactively, or if you're sure about it, you could add this to a hook function for the mode in question.
Edit:
So based on the extra information in the question comments, I would suggest this:
(add-hook 'matlab-shell-mode-hook 'buffer-disable-undo)

Highlight buffer modifications

It often occurs that a file buffer is modified (duh!). Before exiting, emacs asks whether to save the changes. Now it would be interesting to know what actually changed. Is there a way to find out?
As of Emacs 22.1 (at least), 'save-buffers-kill-emacs (the default binding for C-x C-c) prompts you for each unsaved buffer that has a file. Type a d when prompted to save and see the diff.
From the help documentation:
Save some modified file-visiting buffers. Asks user about each one.
You can answer `y' to save, `n' not to save, `C-r' to look at the
buffer in question with `view-buffer' before deciding or `d' to
view the differences using `diff-buffer-with-file'.
If you look at the prompt, it should say something like:
Save file /path/to/file.txt? (y, n, !, ., q, C-r, d, or C-h)
Typing C-h gives you a little more verbose description (but d is what you are asking for):
Type SPC or `y' to save the current buffer;
DEL or `n' to skip the current buffer;
RET or `q' to give up on the save (skip all remaining buffers);
C-g to quit (cancel the whole command);
! to save all remaining buffers;
C-r to view this buffer;
d to view changes in this buffer;
or . (period) to save the current buffer and exit.
I use diff-buffer-with-file, and select the file that the buffer came from (which is the default anyway for the command... just hit enter).
You can also use highlight-changes-mode, though this won't track changes until you turn it on, so not so useful if you want to see what changed when you're closing a file that has not been in this mode :-)
You can have highlight-changes-mode enabled. It will display all changes in red. However it won't show you whitespace changes and will mark removals only with an red _. See also http://www.emacswiki.org/emacs/TrackChanges.
I found this post about tracking changes by djcb most helpful regarding tracking changes in Emacs. The trick is to add the following to your .emacs:
;; higlight changes in documents
(global-highlight-changes-mode t)
(setq highlight-changes-visibility-initial-state nil); initially hide
and then toggle highlight-changes-visible-mode when you want to see what has changed.
In this case I type undo to see the last change (usually some stray character which got typed in the wrong window, since I save early and often).
It would be nice if there were some other indication of the current changes, e.g. in the border like quick diff in Eclipse text editors.
I use goto-chg for things like that. It's not perfect, but it always is enough to jog my memory about what change I made and promptly forgot about.