10

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))) ... (goto-char pos)) 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" (...


9

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.


7

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


5

You're on the right track, but the standard elisp idiom for "go somewhere else in the buffer, do some stuff, and then go back to where you came from" is to use save-excursion. You can rewrite (and simplify!) your command as follows: (defun my-refresh-org-fle () (interactive) (save-excursion (goto-char (point-min)) (org-ctrl-c-ctrl-c))) ...


4

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


3

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 &...


3

You're not doing anything wrong, but org-publish probably is. However, you can fix the problem by using save-excursion: (define-key org-mode-map (kbd "C-c C-p") (lambda () (interactive) (save-excursion (org-publish-project "publish-website")))) save-excursion is a macro that saves the point and the current buffer, ...


3

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 the line. (From: Andreas Politz) From Elisp Manual [ (info "(elisp)Excursions") ]: Warning: Ordinary insertion of ...


3

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") (insert "blah")) That way, just-a-test is loaded or switched to if it already visited in the background. It remains open. As ...


2

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: save-current-buffer Function: Record which buffer is current; execute BODY; make that buffer current. save-excursion Function: Save point, mark, ...


2

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


1

You're probably looking for save-window-excursion. Note the docstring warnings; but as org-id-goto uses pop-to-buffer-same-window which will use the current window unless called from a minibuffer or the current window is dedicated, you would be ok in most situations. save-excursion just ensures that elisp code acting on a buffer will be able to continue ...


1

I think your first idea is the best: ... (goto-char (let ((pt (point))) (save-excursion (if BODY (point) pt))))) You could probably write a macro for it. Evaluating BODY twice is not a good idea: it may have side-effects that make it non-idempotent, and/or it may be expensive ...


1

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) (save-excursion (forward-char) (setq ...


1

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.*$\\)") sort-item-whole sort-item-partial) ...


1

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


Only top voted, non community-wiki answers of a minimum length are eligible