emacstext-cursor

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


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.


Solution

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