Subject: Re: bug#64071: 28.2; smerge-diff creates *vc-diff* without setting it up

From: Spencer Baugh <[email-masked]>
Date: Wed, 28 Jun 2023 08:48:38 -0400

Eli Zaretskii <[email-masked]> writes:

>> From: Spencer Baugh <[email-masked]>
>> Cc: Stefan Monnier <[email-masked]>,  [email-masked]
>> Date: Tue, 27 Jun 2023 16:17:45 -0400
>> 
>> Eli Zaretskii <[email-masked]> writes:
>> 
>> > That command enters diff-mode, and diff-mode doesn't force read-only
>> > status on the current buffer.  Why should Smerge force that?
>> 
>> Buffer named "*vc-diff*" are universally read-only, except if they are
>> created through this path.
>
> But users can legitimately make a *vc-diff* buffer modifiable, no?
> And your patch silently makes it read-only again, behind the user's
> back.  That's hardly nice, is it?
>
>> Stefan likes my simple patch, so what's wrong with it?
>
> See above: we shouldn't change the read-only attribute that was set by
> the user.

Good point.  How about this?

>From 4de911b6ed34ee1a728e1c57a65e9314f3785dc4 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <[email-masked]>
Date: Wed, 28 Jun 2023 08:48:01 -0400
Subject: [PATCH] Make newly-created smerge-diff-buffers read-only

Buffers name *vc-diff* are usually created by vc, which makes them
read-only.  If we create such a buffer, let's make it read-only too.
If the buffer already exists, though, don't change that since the user
might have deliberately made it writable.

* lisp/vc/smerge-mode.el (smerge-diff): Make newly-created
smerge-diff-buffers read-only. (bug#64071)
---
 lisp/vc/smerge-mode.el | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lisp/vc/smerge-mode.el b/lisp/vc/smerge-mode.el
index c39a9cc2f22..524a042fb55 100644
--- a/lisp/vc/smerge-mode.el
+++ b/lisp/vc/smerge-mode.el
@@ -1243,7 +1243,11 @@ smerge-diff
     (write-region beg1 end1 file1 nil 'nomessage)
     (write-region beg2 end2 file2 nil 'nomessage)
     (unwind-protect
-	(with-current-buffer (get-buffer-create smerge-diff-buffer-name)
+	(save-current-buffer
+          (if-let (buffer (get-buffer smerge-diff-buffer-name))
+              (set-buffer buffer)
+            (set-buffer (get-buffer-create smerge-diff-buffer-name))
+            (setq buffer-read-only t))
 	  (setq default-directory dir)
 	  (let ((inhibit-read-only t))
 	    (erase-buffer)
-- 
2.39.3