Subject: bug#23444: 25.0.92; activate-mark-hook does not run after a command has changed the region

From: Lars Ingebrigtsen <[email-masked]>
Date: Sun, 07 Feb 2021 14:30:51 +0100

Nicolas Richard <[email-masked]> writes:

>> It is also run at the end of a command, if the mark is active and
>> it is possible that the region may have changed.
>
> However, when changing the region, nothing happens. As an example I
> tried evaluating:
>
> (defun foo ()
>   (message "ran: %s" this-command))
> (add-hook 'activate-mark-hook 'foo)
>
> then using C-M-SPC (mark-sexp) correctly showed "ran: mark-sexp", but
> moving the cursor afterwards doesn't produce any more message.

Indeed, and the manual even has this bit, added in 2012:

@defvar activate-mark-hook
@defvarx deactivate-mark-hook
These normal hooks are run, respectively, when the mark becomes active
and when it becomes inactive.  The hook @code{activate-mark-hook} is
also run at the end of the command loop if the mark is active and it
is possible that the region may have changed.
@ignore
This piece of command_loop_1, run unless deactivating the mark:
  if (current_buffer != prev_buffer || MODIFF != prev_modiff)
    {
      Lisp_Object hook = intern ("activate-mark-hook");
      Frun_hooks (1, &hook);
    }
@end ignore

And that code snippet, which is (surprisingly enough), still in Emacs
today, pretty much contradicts what the manual says in the visible text?

That is, the hook is only run if the command changes the buffer, which
then reactivates the mark.  And I can confirm that that's how it's
actually working: If you mark a region in a buffer, switch to a
different buffer, and then back again, the hook is run (after the region
has been activated again).

And that makes sense to me, I think?  Does anybody know whether this is
how it's supposed to work?  If so, the fix would be to alter the
documentation.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no