Subject: bug#10056: 24.0.91; Mark deactivation

From: "Drew Adams" <[email-masked]>
Date: Sat, 8 Dec 2012 16:00:36 -0800

> >> As I pointed out previously in this thread, the mark should be
> >> deactivated (in general - there can be some exceptions) after any
> >> command that operates on the active region.  Not doing so 
> >> is annoying,
> 
> The problem is how to implement a general rule "the mark should be
> deactivated after any command that operates on the active region".

To me, the question is why we would do that, and even what the latter part
means.

> One way to define "operates on the active region" is when a command
> uses `region-beginning' and `region-end'.  You can try this definition
> by evaluating:
> (defun deactivate-mark--advice () (setq deactivate-mark t))
> (advice-add 'region-beginning :after #'deactivate-mark--advice)
> (advice-add 'region-end       :after #'deactivate-mark--advice)
> Does this do what you want with the following functions?
> ...
> Does this have a side effect undesirable for other functions?

Please think again before doing this.  "Operates on the active region" can mean
just about anything, and I fear that such a blanket manipulation will have
adverse affects here and there.

And I expect in particular that any use of either `region-beginning' or
`region-end' casts the net far too wide.  Those functions are often used without
making any changes to the text in the region (or even in the buffer). 

For one thing, any such blanket change would still need to respect (not
override) a commands's explicit setting or let-binding of `deactivate-mark'.

This has been the of policy ever since there was a notion of mark "activeness"
(i.e., since `transient-mark-mode'):

 -- Variable: deactivate-mark
     If an editor command sets this variable non-`nil', then the editor
     command loop deactivates the mark after the command returns (if
     Transient Mark mode is enabled).  All the primitives that change
     the buffer set `deactivate-mark', to deactivate the mark when the
     command is finished.

     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.  For example:

          (let (deactivate-mark) (insert " "))

I, and I'm sure others too, have a certain amount of code that binds or sets
`deactivate-mark'.  Just grep the Emacs Lisp sources for examples.

There are commands that use the region and are used as functions within other
commands.  There are commands that can be used repetitively and might expect to
leave the region active between successive calls.  There are commands whose
purpose is to set the active region.  Emacs is complex, and one size does not
fit all for this kind of thing.

If  for some reason a particular Emacs command does not deactivate the mark when
it is finished, but you think it should, then just deactivate it explicitly in
that command.

Just because you find some commands that should but do not yet do so is not a
sufficient reason to try forcing this everywhere.

Someone took the time to ensure that "All the primitives that change the buffer"
DTRT in this regard, and that the command loop respects `deactivate-mark'.  You
have only to continue that policy carefully: if deactivating (or activating)
needs doing here or there, then do it here or there, case by case.

Otherwise, I think you're asking for trouble.  I could be wrong, of course.  But
it seems a bit heavy-handed to set out on such a crusade just because this or
that command might need fixing.  Let's try first to fix (only) the commands that
Dani or others report such have a problem.