diff options
author | Daniel Colascione <dancol@dancol.org> | 2015-05-04 11:46:12 -0700 |
---|---|---|
committer | Daniel Colascione <dancol@dancol.org> | 2015-05-04 11:46:12 -0700 |
commit | 255a011f0ecf004b31c59945b10154b10fac3af1 (patch) | |
tree | ef4c1809537fd50c98cd137dbb70a8d48c015616 /lisp/simple.el | |
parent | fe4e258b17feb529ac364daee67a5f0441f851f4 (diff) | |
download | emacs-255a011f0ecf004b31c59945b10154b10fac3af1.tar.gz |
Add `save-mark-and-excursion', which has the old `save-excursion' behavior
* doc/lispref/positions.texi (Excursions): Document
`save-mark-and-excursion'.
* lisp/font-lock.el (font-lock-fontify-block): Use
`save-mark-and-excursion' instead of `save-excursion', restoring
Emacs 24 behavior.
* lisp/simple.el (save-mark-and-excursion--save)
(save-mark-and-excursion--restore): New functions.
(save-mark-and-excursion): New user macro.
* src/editfns.c (Fsave_excursion): Mention
`save-mark-and-excursion' in `save-excursion' documentation.
Diffstat (limited to 'lisp/simple.el')
-rw-r--r-- | lisp/simple.el | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/lisp/simple.el b/lisp/simple.el index 31efe3896d4..9f42f00b149 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -4870,6 +4870,44 @@ store it in a Lisp variable. Example: (setq mark-active nil) (set-marker (mark-marker) nil))) +(defun save-mark-and-excursion--save () + (cons + (let ((mark (mark-marker))) + (and mark (marker-position mark) (copy-marker mark))) + mark-active)) + +(defun save-mark-and-excursion--restore (saved-mark-info) + (let ((saved-mark (car saved-mark-info)) + (omark (marker-position (mark-marker))) + (nmark nil) + (saved-mark-active (cdr saved-mark-info))) + ;; Mark marker + (if (null saved-mark) + (set-marker (mark-marker nil)) + (setf nmark (marker-position saved-mark)) + (set-marker (mark-marker) nmark) + (set-marker saved-mark nil)) + ;; Mark active + (let ((cur-mark-active mark-active)) + (setf mark-active saved-mark-active) + ;; If mark is active now, and either was not active or was at a + ;; different place, run the activate hook. + (if saved-mark-active + (unless (eq omark nmark) + (run-hooks 'activate-mark-hook)) + ;; If mark has ceased to be active, run deactivate hook. + (when cur-mark-active + (run-hooks 'deactivate-mark-hook)))))) + +(defmacro save-mark-and-excursion (&rest body) + "Like `save-excursion', but also save and restore the mark state. +This macro does what `save-excursion' did before Emacs 25.1." + (let ((saved-marker-sym (make-symbol "saved-marker"))) + `(let ((,saved-marker-sym (save-mark-and-excursion--save))) + (unwind-protect + (save-excursion ,@body) + (save-mark-and-excursion--restore ,saved-marker-sym))))) + (defcustom use-empty-active-region nil "Whether \"region-aware\" commands should act on empty regions. If nil, region-aware commands treat empty regions as inactive. |