Subject: Re: bug#51041: [External] : bug#51041: 28.0.60; toggle-truncate-lines should not print message

From: Tyler Grinn <[email-masked]>
Date: Tue, 05 Oct 2021 16:01:14 -0400

Drew Adams <[email-masked]> writes:

>> (toggle-truncate-lines t)
>> 
>> Prints the message "Truncate long lines enabled". When called from
>> elisp, this message should be ignored. If called interactively, the
>> message should be displayed.
>
> Yes.
>
> But the right fix is to add "&optional msg",
> use (interactive "P\np"), and test for non-nil
> MSG as the condition for showing the message.
>
> Lisp code for a command can use that toggle
> function, and when _that_ command is invoked
> interactively it too might make sense to show
> the message (that can depend on the command
> and when the toggling occurs as part of it).
>
> The same kind of fix is no doubt appropriate
> for some other existing commands that instead
> just test `called-interactively-p'.  Surely
> we shouldn't perpetuate such design by adding
> more such.

Sounds good to me.

diff --git a/lisp/simple.el b/lisp/simple.el
index 3695415163..47a00fca3c 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -8294,14 +8294,15 @@ set-selective-display
 
 (defvaralias 'indicate-unused-lines 'indicate-empty-lines)
 
-(defun toggle-truncate-lines (&optional arg)
+(defun toggle-truncate-lines (&optional arg msg)
   "Toggle truncating of long lines for the current buffer.
-When truncating is off, long lines are folded.
-With prefix argument ARG, truncate long lines if ARG is positive,
-otherwise fold them.  Note that in side-by-side windows, this
-command has no effect if `truncate-partial-width-windows' is
-non-nil."
-  (interactive "P")
+When truncating is off, long lines are folded.  With prefix
+argument ARG, truncate long lines if ARG is positive, otherwise
+fold them.  When called interactively or if MSG is non-nil, print
+a message describing the new state of truncate-lines.  Note that
+in side-by-side windows, this command has no effect if
+`truncate-partial-width-windows' is non-nil."
+  (interactive "P\np")
   (setq truncate-lines
 	(if (null arg)
 	    (not truncate-lines)
@@ -8313,13 +8314,14 @@ toggle-truncate-lines
 		      (if (eq buffer (window-buffer window))
 			  (set-window-hscroll window 0)))
 		    nil t)))
-  (message "Truncate long lines %s%s"
-	   (if truncate-lines "enabled" "disabled")
-           (if (and truncate-lines visual-line-mode)
-               (progn
-                 (visual-line-mode -1)
-                 (format-message " and `visual-line-mode' disabled"))
-             "")))
+  (let ((disable-vlm (and truncate-lines visual-line-mode)))
+    (if disable-vlm (visual-line-mode -1))
+    (if msg
+        (message "Truncate long lines %s%s"
+	         (if truncate-lines "enabled" "disabled")
+                 (if disable-vlm
+                     (format-message " and `visual-line-mode' disabled")
+                   "")))))
 
 (defun toggle-word-wrap (&optional arg)
   "Toggle whether to use word-wrapping for continuation lines.