Subject: 27.0.50; Binding deactivate-mark to nil unexpectedly deactivates the mark

From: Markus Triska <[email-masked]>
Date: Sun, 07 Apr 2019 02:11:02 +0200

Please start Emacs with "emacs -Q" and insert the following form in the
*scratch* buffer:

    (progn
      (let (deactivate-mark)
        (switch-to-buffer (get-buffer-create "t"))
        (erase-buffer)
        (insert "hello"))
      (let (deactivate-mark)
        (set-mark-command nil)
        (move-beginning-of-line nil)))

then place point at the end of the form and evaluate it with C-x C-e.

You will see a new buffer ("t"), where "hello" is inserted and nothing
is highlighted because the mark is inactive.

However, I expect the mark to be active and the region highlighted,
because the form binds deactivate-mark to nil. This is the way that is
recommended in the Elisp manual, see for Example 31.7 "The 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.  For example:

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

When I remove the second let-binding in the snippet, then the mark is in
fact active after the form is evaluated. For example, when I evaluate:

    (let (deactivate-mark)
      (switch-to-buffer (get-buffer-create "t"))
      (erase-buffer)
      (insert "hello")
      (set-mark-command nil)
      (move-beginning-of-line nil))

then "hello" is highlighted.

Hence, if I bind deactivate-mark to nil, then the mark is in some cases
unexpectedly deactivated.

As another test case, here is the first form bound to F4:

    (global-set-key [f4]
                    (lambda ()
                      (interactive)
                      (progn
                        (let (deactivate-mark)
                          (switch-to-buffer (get-buffer-create "t"))
                          (erase-buffer)
                          (insert "hello"))
                        (let (deactivate-mark)
                          (set-mark-command nil)
                          (move-beginning-of-line nil)))))

with this binding in place, when I press F4, I also expect "hello" to be
highlighted, since this form binds deactivate-mark to nil. However,
"hello" is unexpectedly not highlighted when I then press F4.

In contrast, when I evaluate the first form with C-M-x, then the region
is highlighted. Could you please make C-x C-e and F4 (with this binding
in place) give the same result as using C-M-x on the form?

Thank you and all the best!
Markus

In GNU Emacs 27.0.50 (build 1, x86_64-apple-darwin18.0.0, X toolkit, Xaw scroll bars)
 of 2018-11-15 built on mt-computer
Repository revision: b4eb908f858284a7962851fd99c94598f76afa6f
Windowing system distributor 'The X.Org Foundation', version 11.0.11804000
System Description:  Mac OS X 10.14.2