Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: Problems with activating mark in an interactive command

Tags:

emacs

elisp

It seems like altering the buffer in any way, stops the defun from activating mark:

(defun mark-five-next ()
  "Marks the next five chars as expected"
  (interactive)
  (push-mark (+ 5 (point)) t t))

(defun insert-an-a-then-mark-five-next ()
  "Does not mark the next five chars"
  (interactive)
  (insert "a")
  (push-mark (+ 5 (point)) t t))

I'd prefer a way to fix it, but just an explanation is good too.

like image 240
Magnar Avatar asked Feb 21 '23 16:02

Magnar


1 Answers

It turns out that all editing commands set the var deactivate-mark which does just that after the command has finished.

To avoid this behavior, you have to wrap the buffer-altering functions in a let-statement, preventing the change of the global deactivate-mark var.

(let (deactivate-mark)
   (...))

I spent more than an hour on this problem, because I just skipped over deactivate-mark in the manual, thinking it was a description of the function. Of course, as I already knew, but have now properly learned: emacs lisp has a different namespace for functions and variables.

like image 100
Magnar Avatar answered Feb 24 '23 05:02

Magnar