Move cursor and return new position, but restore original cursor position

147 views Asked by At

I want to save the current position of the cursor at the beginning of a function so that I can restore the cursor to its original position after executing the code. So far, I have tried the following:

(setq my_cursor_position 15)
(do-some-stuff)
(goto-char my_cursor_position)

That works so far, but the position is set to a hard value of 15. I haven't found a way to directly read the position of the cursor yet. There is a function what-cursor-position, but it stores multiple informations (character under the cursor, line, column, and position), and I don't know how to extract the position from that.

2

There are 2 answers

0
choroba On BEST ANSWER

Use save-excursion to save the current point location, execute a function and restore the previous point location.

0
amalloy On

save-excursion is the right answer for what you're doing now. But if you need the cursor position for some other reason, it's just (point).