2

I have some sample code like this:

(defun some-function ()
  (interactive)
  (push-mark)
  (goto-char (point-min))
  ; do some stuff
  (pop-mark))

Now I'm expecting pop-mark to get me back to the original location. But that doesn't seem to be happening. Any idea on how to do this properly ?

1
  • I would recommend storing your location points in let-bound variables and then going back there again: (let* ((point-one (point)) point-two) (goto-char (point-min)) [do stuff . . .] (goto-char point-one)) You can create a place holder for the variable -- e.g., point-two and then when you get there, set it with (setq point-two (point))
    – lawlist
    May 5, 2015 at 2:24

1 Answer 1

6

From pop-mark's documentation: ...This does not move point in the buffer

I think you want:

;; do some stuff
(goto-char (mark))
(pop-mark)

But if all you care about is returning to a previous location, and not actually using the mark ring, then you could either

1) save (point) in a variable and return to it

2) use save-excursion which does this for you

1
  • 8
    save-excursion really is the best way to do this. You don't have to worry about saving the initial position of point, just let save-excursion do the work for you.
    – zck
    May 5, 2015 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.