New save-excursion Semantics

Marcin Borkowski (mbork) has a useful heads up for Elisp programmers. Starting with Emacs 25.1, save-excursion will no longer save the mark state. If you want to save the mark, you should use save-mark-and-excursion instead.

While reading mbork’s post, I remembered that when I first started learning Elisp, there was an issue with saving the mark and you had to take additional action to make things work correctly. I checked the source code and the problem was that save-excursion didn’t save deactivate-mark. From what I can tell1, the new code fixes that. I’m not sure why they chose to implement the macro save-mark-and-excursion instead of just fixing things up in save-excursion (the semantics changed regardless). The NEWS file and Elisp manual don’t say but I’m sure one of you will let me know.

As mbork points out, programmers mostly didn’t depend on save-excursion saving the mark anyway so this won’t be a huge issue for most code. Just be aware that if you depend on the mark being the same after your protected code runs, you should use save-mark-and-excursion rather than save-excursion.

Footnotes:

1

I really need to figure out the C macros that Emacs uses so that I can read the C code better.

This entry was posted in Programming and tagged . Bookmark the permalink.