Save-excursion restores point AND mark (as of emacs 24.4), so it is no help for you here.
You could save the point manually with this idiom:
(let ((pos (point)))
However, in your case it's better to just set the mark at the end of the line:
(defun mark-from-point-to-end-of-line ()
"Marks everything from point to end of line"
Definitely. You will especially gain if you just want to test char-before or search backward for a literal string. And if you must use looking-back then try to use a LIMIT argument, if possible.
See Emacs bug #17284 for an example.
Your call to save-excursion has no body.
It is saving point, mark, and the current buffer, then doing nothing, and then 'restoring' those things to their saved values.
However that's the wrong macro. You want save-selected-window.
It sounds like you are under the misconception that there is a single point and mark which 'move' around between buffers. This ...
This post provides a work-around, but not an answer to "why does
save-excursion not work here?" The problem is interesting. I find org internals to be baroque, and a skim through the source code for
org-sort-entries seems to conform with that view.
Unless you're deeply interested in the internals, you can use the following
work-around to save and restore ...
Why do you think so? with-current-buffer does not do a save-excursion. Its purpose is just to temporarily change the current buffer (i.e., set it to some buffer and at the end restore what was the current buffer beforehand as the current buffer once again).
This is all that macro with-current-buffer does:
(defmacro with-current-buffer (buffer-or-name &...
Thanks to @nanny!
I found the answer in the mailing list which he mentioned in the comment!
save-excursion stores a marker, not the 'byte' number. When you kill
the line where point is, this marker gets replaced to the beginning of
(From: Andreas Politz)
From Elisp Manual [ (info "(elisp)Excursions") ]:
Warning: Ordinary insertion ...
Facing a similar problem, I realize this behavior is due to find-file in particular. I didn't try using save-window-excursion instead of save-excursion. What I used instead was
(with-current-buffer (find-file-noselect "just-a-test")
That way, just-a-test is loaded or switched to if it already visited in the background. It remains open. As ...
The current buffer need not be visible in a window.
You're looking for save-window-excursion (or quite possibly a combination of the two).
n.b. C-uC-ha ^save- will point out all of the following:
Function: Record which buffer is current; execute BODY; make that
Function: Save point, mark, ...
I think the following code will help. It is modified from an example in the emacs-lisp intro at https://www.gnu.org/software/emacs/manual/html_node/eintr/append_002dto_002dbuffer-overview.html.
(defun append-string-to-buffer (buffer string)
;; Modified from append-to-buffer, defined in the emacs-lisp intro
"Append to specified buffer the specified text.
further to my comment on your own answer something like this makes more sense to me as the check doesnt alter anything.
(defun next-pair-exists-in (func)
"Check if inner or outer pair exists.
Function employs `up-list` or `down-list` as argument to work."
(setq-local reslt nil)
One idea would be to record the headline and search for it to get back there again at the end (but this example will find the last occurrence if there are duplicate headings in the same file):
(let ((sort-init-pos (point))
(heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*\\(\n.*DEADLINE.*$\\)")
I knew, I'll need this function someday...
(defun mark-line (&optional arg allow-extend)
"Set mark ARG lines away from point.
The place mark goes is the same place \\[forward-line] would move
to with the same argument. Interactively, if this command is
repeated or (in Transient Mark mode) if the mark is active, it
marks the next ARG lines after the ...