Emacs Lisp has two entities called exactly the same – the deactivate-mark
function and the deactivate-mark
variable. (This is possible at all because Elisp is is a Lisp-2.) As the name suggests, they both serve the same purpose (or rather a similar one), but there seems to be a subtle difference.
The function deactivate-mark
is simple – it deactivates the mark, done. The variable works in a different way, though. If a command sets it to a non-nil value, then the Emacs command loop will deactivate the mark upon exiting from that command.
You may then ask whether you should say (deactivate-mark)
or (setq
deactivate-mark t)
in your command if you want it to deactivate the mark. The answer is (probably) that it doesn’t matter, but the main use case for the variable seems to be something else entirely. According to the docs:
All the primitives that change the buffer set deactivate-mark. […] To write Lisp code that modifies the buffer without causing deactivation of the mark at the end of the command, bind deactivate-mark to nil around the code that does the modification.
This means that if you have a command that e.g. inserts a space, the (insert " ")
hidden somewhere in its code will set deactivate-mark
to t and hence the mark will get deactivated after it finishes. The reason this variable exists seems to be that you may actually want the mark not to get deactivated. The manual goes on to give a simple example:
(let (deactivate-mark) (insert " "))
This means that insert
will set deactivate-mark
to t
, but it will only change the local binding created by let
– so after the let
exits, the variable’s value will still be the previous one (which may happen to be nil, since this is what the command loop sets it to before running any command – this is not mentioned in the Elisp reference, but the docstring of the deactivate-mark
variable says so).
Incidentally, this works similarly:
(let ((deactivate-mark t)) (insert " "))
which is not intuitive, but of course makes perfect sense.
Of course, you can just say (setq deactivate-mark nil)
at the end instead of playing around with let
in this case, but the let
trick may still be useful if you want to make the activation or deactivation of the mark more fine-grained (e.g., instead of just leaving it activated at the end of the command, you set it to some value depending on some condition at the beginning and only then insert that space, or something like that). There are probably tons of ways you can use it, but the bottom line is that its main use seems to be to prevent the deactivation of the mark (quite contrary to its name). As one of the participants in the discussion I mentioned put it:
In a nutshell: deactivating (i.e. setting to nil) deactivate-mark
deactivates the mark-deactivating code. Phew
And that’s it for today!