When I need a function to operate on a region I do, for example:
(defun myfunction (B E)
(interactive "r")
(let ((b (make-marker))
(e (make-marker)))
(set-marker b B)
(set-marker e E)
;; DO SOMETHING THAT CHANGES THE BUFFER, THEN
(comment-region b e)))
Is there a simpler way to have B and E as markers?
(let ((m (make-marker))) (unwind-protect (progn (set-marker m pos) ...) (set-marker m nil)))
to avoid the dangling marker living on until the nextgarbage-collect
. When you don't care about the dangling marker you can do(let ((m (set-marker (make-marker) pos))) ...)
. One sees that quite often in libraries. In some cases(save-restriction (narrow-to-region b e) ...)
is an alternative to markers. – Tobias Feb 11 '18 at 12:01with-markers
) and reusing it. – wasamasa Feb 11 '18 at 17:59buffer-has-markers-at
can be used to confirm that the marker still exists after thelet
-bound variable goes out of scope. – phils Feb 11 '18 at 20:19(set-marker MARKER nil)
while you still have a handle on it (e.g. before thelet
-binding goes out of scope). That said, I did just confirm that the markers do get garbage-collected in thatlet
-bound scenario (at some later point after they are out of scope); so although it's still best-practice to explicitly set them tonil
when you are finished with them, they don't stick around forever. – phils Feb 11 '18 at 21:30