Subject: bug#74056: `LaTeX-insert-item' inserts comment characters if called from inside comment

From: Ikumi Keita <[email-masked]>
Date: Sat, 02 Nov 2024 18:11:08 +0900

Hi Arash,

>>>>> Arash Esbati <[email-masked]> writes:
> Thanks, I have 2 minor comments, see below.

> I don't think we need to talk about the history in the manual.  I
> suggest to change the last paragraph to something like this:

>   The default value of this option is @code{nil} in normal @LaTeX{} mode
>   and is set to @code{t} in doc@TeX{} mode.

OK. I've further simplified.

> Reg. your other patch: Does it need an ERT-test?

I think it's better to have one. I added.

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine
#Gaza #StopMassiveKilling #CeasefireNOW

>From c596290236f1a981c4d3ff557987546518e4da19 Mon Sep 17 00:00:00 2001
From: Ikumi Keita <[email-masked]>
Date: Fri, 1 Nov 2024 15:27:09 +0900
Subject: [PATCH 1/2] Change default of `LaTeX-insert-into-comments'
 (bug#74056)

* latex.el (LaTeX-insert-into-comments): Change default value to nil.
Add :safe and :package-version entries.
Refill the doc string.
* NEWS.org (Changed): Announce the change.
* doc/auctex.texi (Commenting): Add explanation of
`LaTeX-insert-into-comments' (at last).
* doc/todo.texi (Mid-term Goals): Adjust accordingly.
---
 NEWS.org        |  5 +++++
 doc/auctex.texi | 15 +++++++++++++++
 doc/todo.texi   |  2 +-
 latex.el        | 10 ++++++----
 4 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/NEWS.org b/NEWS.org
index b5a4bb91..28371831 100644
--- a/NEWS.org
+++ b/NEWS.org
@@ -30,6 +30,11 @@
 - Add preliminary support for parsing =\graphicspath= in
   =style/graphicx.el=.
 
+** Changed
+
+- Change the default value of ~LaTeX-insert-into-comments~ from ~t~ to
+  ~nil~.
+
 * [14.0.7] - 2024-10-05
 
 ** Added
diff --git a/doc/auctex.texi b/doc/auctex.texi
index 718d4c2a..114b9e8d 100644
--- a/doc/auctex.texi
+++ b/doc/auctex.texi
@@ -1616,6 +1616,21 @@ paragraph is considered to consist of all preceding and succeeding
 lines starting with a @samp{%}, until the first non-comment line.
 @end deffn
 
+In doc@TeX{} document, all documentations are commented out.  @AUCTeX{}
+inserts @samp{%} (with an accompanying space) at the beginning of line
+when you issue some commands including sectioning (@kbd{C-c C-s}) and
+inserting environments (@kbd{C-c C-e}), on a commented line.  This
+behavior is controlled by @code{LaTeX-insert-into-comments}.
+
+@defopt LaTeX-insert-into-comments
+When this option is non-@code{nil}, some editing commands are aware of
+comment prefix at the beginning of line and insert it in the line created
+anew.
+
+The default value of this option is @code{nil} and is set to @code{t}
+in doc@TeX{} mode.
+@end defopt
+
 @node Indenting
 @section Indenting
 @cindex Formatting
diff --git a/doc/todo.texi b/doc/todo.texi
index d144ba72..825e21aa 100644
--- a/doc/todo.texi
+++ b/doc/todo.texi
@@ -78,7 +78,7 @@ texinfo mode.)
 Following entries should be included in the document:
 @itemize @minus
 @item
-Variables @code{LaTeX-insert-into-comments}, @code{TeX-translate-location-hook}
+Variable @code{TeX-translate-location-hook}
 
 @item
 How to use @code{TeX-auto-add-type}, as well as functions and variables
diff --git a/latex.el b/latex.el
index 02fd1e33..5e1c628c 100644
--- a/latex.el
+++ b/latex.el
@@ -66,12 +66,14 @@ A comma-seperated list of strings."
 
 (make-variable-buffer-local 'LaTeX-default-options)
 
-(defcustom LaTeX-insert-into-comments t
+(defcustom LaTeX-insert-into-comments nil
   "Whether insertion commands stay in comments.
-This allows using the insertion commands even when
-the lines are outcommented, like in dtx files."
+This allows using the insertion commands even when the lines are
+outcommented, like in dtx files."
   :group 'LaTeX-environment
-  :type 'boolean)
+  :type 'boolean
+  :safe #'booleanp
+  :package-version '(auctex . "14.0.8"))
 
 (defcustom docTeX-indent-across-comments nil
   "If non-nil, indentation in docTeX is done across comments."
-- 
2.46.2

>From a853f3fed7ef3648ecd9a31e0f5102cb5401a95d Mon Sep 17 00:00:00 2001
From: Ikumi Keita <[email-masked]>
Date: Fri, 1 Nov 2024 15:41:58 +0900
Subject: [PATCH 2/2] Improve insertion of \item (bug#74056)

* latex.el (LaTeX-insert-item): On whitespace-only line, use that line
to put \item and don't create a new line after it.
* tests/latex/latex-test.el (LaTeX-insert-item-in-whitespace-only-line):
New test.
---
 latex.el                  | 13 ++++++++++++-
 tests/latex/latex-test.el | 18 ++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/latex.el b/latex.el
index 5e1c628c..91aa16b5 100644
--- a/latex.el
+++ b/latex.el
@@ -1535,7 +1535,18 @@ You may use `LaTeX-item-list' to change the routines used to insert the item."
     (when (and (TeX-active-mark)
                (> (point) (mark)))
       (exchange-point-and-mark))
-    (unless (bolp) (LaTeX-newline))
+    (if (save-excursion
+          ;; If the current line has only whitespace characters, put
+          ;; the new \item on this line, not creating a new line
+          ;; below.
+          (goto-char (line-beginning-position))
+          (if LaTeX-insert-into-comments
+              (re-search-forward
+               (concat "\\=" TeX-comment-start-regexp "+")
+               (line-end-position) t))
+          (looking-at "[ \t]*$"))
+        (delete-region (match-beginning 0) (match-end 0))
+      (LaTeX-newline))
     (if (assoc environment LaTeX-item-list)
         (funcall (cdr (assoc environment LaTeX-item-list)))
       (TeX-insert-macro "item"))
diff --git a/tests/latex/latex-test.el b/tests/latex/latex-test.el
index e22d2a08..d7d202fc 100644
--- a/tests/latex/latex-test.el
+++ b/tests/latex/latex-test.el
@@ -730,4 +730,22 @@ check the indentation for optional argument of \\usepackage."
              (insert-file-contents docTeX/out)
              (buffer-string)))))
 
+;; bug#74056
+(ert-deftest LaTeX-insert-item-in-whitespace-only-line ()
+  "Test if `LaTeX-insert-item' doesn't insert spurious line."
+  (with-temp-buffer
+    (LaTeX-mode)
+    (insert "\
+\\begin{itemize}
+\\item abc
+\s\s\t")
+    (backward-char 1)
+    (LaTeX-insert-item)
+    (should (string=
+             (buffer-string)
+             "\
+\\begin{itemize}
+\\item abc
+\\item "))))
+
 ;;; latex-test.el ends here
-- 
2.46.2