auctex-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[AUCTeX-diffs] [elpa] externals/auctex f2bac9d 11/43: Improve environmen


From: Tassilo Horn
Subject: [AUCTeX-diffs] [elpa] externals/auctex f2bac9d 11/43: Improve environment insertion (bug#35284)
Date: Sat, 11 Apr 2020 15:05:19 -0400 (EDT)

branch: externals/auctex
commit f2bac9dcde12d939a5c1267fdba02c2a869bd406
Author: Ikumi Keita <address@hidden>
Commit: Ikumi Keita <address@hidden>

    Improve environment insertion (bug#35284)
    
    * latex.el (LaTeX-insert-environment): Place the point and the mark at
    appropriate place.
    * tests/latex/latex-test.el (LaTeX-insert-environment-with-active-region):
    New test.
---
 latex.el                  |  88 +++++++++++++++----
 tests/latex/latex-test.el | 218 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 286 insertions(+), 20 deletions(-)

diff --git a/latex.el b/latex.el
index aaa4a1f..f57fcfb 100644
--- a/latex.el
+++ b/latex.el
@@ -668,41 +668,91 @@ environment just inserted, the buffer position just before
         (concat "^\\([ \t]*" TeX-comment-start-regexp "+\\)+[ \t]*"))
        (setq prefix (match-string 0))))
     ;; What to do with the line containing point.
-    (cond ((save-excursion (beginning-of-line)
+    ;; - Open a new empty line for later insertion of "\begin{foo}" and
+    ;;   put the point there.
+    ;; - If there were at first any non-whitespace texts between the
+    ;;   point and EOL, send them into their new own line with possible
+    ;;   comment prefix.
+    (cond (;; When the entire line consists of whitespaces except
+          ;; possible prefix...
+          (save-excursion (beginning-of-line)
                           (looking-at (concat prefix "[ \t]*$")))
+          ;; ...make the line empty and put the point there.
           (delete-region (match-beginning 0) (match-end 0)))
-         ((TeX-looking-at-backward (concat "^" prefix "[ \t]*")
+         (;; When there are only whitespaces except possible prefix
+          ;; between the point and BOL (including the case the point
+          ;; is at BOL)...
+          (TeX-looking-at-backward (if prefix
+                                       (concat "^\\(" prefix "\\)?[ \t]*")
+                                     "^[ \t]*")
                                    (line-beginning-position))
+          ;; ...in this case, we have non-whitespace texts between
+          ;; the point and EOL, so send the entire line into a new
+          ;; next line and put the point on the empty line just
+          ;; created.
           (beginning-of-line)
           (newline)
           (beginning-of-line 0))
-         ((bolp)
+         (;; In all other cases...
+          t
+          ;; ...insert a new empty line after deleting all
+          ;; whitespaces around the point, put the point there...
           (delete-horizontal-space)
-          (newline)
-          (beginning-of-line 0))
-         (t
-          (delete-horizontal-space)
-          (newline 2)
-          (when prefix (insert prefix))
-          (beginning-of-line 0)))
+          (if (eolp)
+              (newline)
+            ;; ...and if there were at first any non-whitespace texts
+            ;; between (the original position of) the point and EOL,
+            ;; send them into a new next line with possible comment
+            ;; prefix.
+            (newline 2)
+            (when prefix (insert prefix))
+            (beginning-of-line 0))))
     ;; What to do with the line containing mark.
+    ;; If there is active region...
     (when active-mark
+      ;; - Open a new empty line for later insertion of "\end{foo}"
+      ;;   and put the mark there.
+      ;; - If there were at first any non-whitespace texts between the
+      ;;   mark and EOL, pass them over the empty line and put them on
+      ;;   their own line with possible comment prefix.
       (save-excursion
        (goto-char (mark))
-       (cond ((save-excursion (beginning-of-line)
-                              (or (looking-at (concat prefix "[ \t]*$"))
-                                  (looking-at "[ \t]*$")))
+       (cond (;; When the entire line consists of whitespaces except
+              ;; possible prefix...
+              (save-excursion (beginning-of-line)
+                              (looking-at
+                               (if prefix
+                                   (concat "\\(" prefix "\\)?[ \t]*$")
+                                 "[ \t]*$")))
+              ;; ...make the line empty and put the mark there.
               (delete-region (match-beginning 0) (match-end 0)))
-             ((TeX-looking-at-backward (concat "^" prefix "[ \t]*")
+             (;; When there are only whitespaces except possible prefix
+              ;; between the mark and BOL (including the case the mark
+              ;; is at BOL)...
+              (TeX-looking-at-backward (if prefix
+                                           (concat "^\\(" prefix "\\)?[ \t]*")
+                                         "^[ \t]*")
                                        (line-beginning-position))
+              ;; ...in this case, we have non-whitespace texts
+              ;; between the mark and EOL, so send the entire line
+              ;; into a new next line and put the mark on the empty
+              ;; line just created.
               (beginning-of-line)
-              (newline)
-              (beginning-of-line 0))
-             (t
+              (set-mark (point))
+              (newline))
+             (;; In all other cases...
+              t
+              ;; ...make a new empty line after deleting all
+              ;; whitespaces around the mark, put the mark there...
               (delete-horizontal-space)
               (insert-before-markers "\n")
-              (newline)
-              (when prefix (insert prefix))))))
+              ;; ...and if there were at first any non-whitespace
+              ;; texts between (the original position of) the mark
+              ;; and EOL, send them into a new next line with
+              ;; possible comment prefix.
+              (unless (eolp)
+                (newline)
+                (when prefix (insert prefix)))))))
     ;; Now insert the environment.
     (when prefix (insert prefix))
     (setq env-start (point))
diff --git a/tests/latex/latex-test.el b/tests/latex/latex-test.el
index b198eae..c8ecff7 100644
--- a/tests/latex/latex-test.el
+++ b/tests/latex/latex-test.el
@@ -1,6 +1,6 @@
 ;;; latex-test.el --- tests for LaTeX mode
 
-;; Copyright (C) 2014--2018 Free Software Foundation, Inc.
+;; Copyright (C) 2014--2019 Free Software Foundation, Inc.
 
 ;; This file is part of AUCTeX.
 
@@ -320,4 +320,220 @@ backend=biber % here is a comment
       (should (not TeX-PDF-mode))
       (should (not (member "psfrag" TeX-active-styles))))))
 
+(ert-deftest LaTeX-insert-environment-with-active-region ()
+  "Check environment is inserted correctly with active region."
+  ;; The former codes of `LaTeX-insert-environment' had problems about
+  ;; the management of the point and the mark, which sometimes
+  ;; resulted in additional empty line, spurious insertion of comment
+  ;; prefix, or both.
+  (with-temp-buffer
+    (let ((transient-mark-mode t)
+         (LaTeX-insert-into-comments t))
+      (latex-mode)
+      (auto-fill-mode 1)
+
+      ;; test 1: for bug#35284
+      ;; test 1-1
+      (insert "\\begin{document}
+% This is a comment
+\\def\\foo#1{foo}
+% another comment
+\\end{document}
+")
+      (set-mark (line-beginning-position 0)) ; just before \end{document}.
+      (goto-char (point-min))
+      (beginning-of-line 2) ; just before the first "%".
+      (LaTeX-insert-environment "verbatim")
+      (should (string=
+              (buffer-string)
+              "\\begin{document}
+% \\begin{verbatim}
+% This is a comment
+\\def\\foo#1{foo}
+% another comment
+% \\end{verbatim}
+\\end{document}
+"))
+
+      ;; test 1-2
+      (erase-buffer)
+      (insert "\\begin{document}
+% This is a comment
+\\def\\foo#1{foo}
+% another comment
+\\end{document}
+")
+      (set-mark (line-end-position -1)) ; just after "another comment"
+      (goto-char (point-min))
+      (beginning-of-line 2) ; just before the first "%".
+      (LaTeX-insert-environment "verbatim")
+      (should (string=
+              (buffer-string)
+              "\\begin{document}
+% \\begin{verbatim}
+% This is a comment
+\\def\\foo#1{foo}
+% another comment
+% \\end{verbatim}
+\\end{document}
+"))
+
+      (setq LaTeX-insert-into-comments nil)
+
+      ;; test 1-3
+      (erase-buffer)
+      (insert "\\begin{document}
+% This is a comment
+\\def\\foo#1{foo}
+% another comment
+\\end{document}
+")
+      (set-mark (line-beginning-position 0)) ; just before \end{document}
+      (goto-char (point-min))
+      (beginning-of-line 2) ; just before the first "%".
+      (LaTeX-insert-environment "verbatim")
+      (should (string=
+              (buffer-string)
+              "\\begin{document}
+\\begin{verbatim}
+% This is a comment
+\\def\\foo#1{foo}
+% another comment
+\\end{verbatim}
+\\end{document}
+"))
+      ;; test 2: for
+      ;; https://lists.gnu.org/archive/html/auctex/2019-11/msg00009.html
+
+      (setq LaTeX-insert-into-comments t)
+
+      ;; test 2-1
+      (erase-buffer)
+      (insert "abc def ghi")
+      (set-mark 5) ; just before "def"
+      (goto-char 8) ; just after "def"
+      (LaTeX-insert-environment "center")
+      (should (string=
+              (buffer-string)
+              "abc
+\\begin{center}
+  def
+\\end{center}
+ghi"))
+
+      ;; test 2-2
+      (erase-buffer)
+      (insert "abc
+def
+ghi")
+      (beginning-of-line 0) ; just before "def"
+      (set-mark (line-end-position)) ; just after "def"
+      (LaTeX-insert-environment "center")
+      (should (string=
+              (buffer-string)
+              "abc
+\\begin{center}
+  def
+\\end{center}
+ghi"))
+
+      ;; test 2-3
+      (erase-buffer)
+      (insert "\\begin{quote}
+  % abc
+  % def
+  % ghi
+\\end{quote}
+")
+      (set-mark (line-beginning-position 0)) ; just before \end{quote}
+      (goto-char (point-min))
+      (beginning-of-line 2) ; just before the first "%"
+      (LaTeX-insert-environment "center")
+      (should (string=
+              (buffer-string)
+              "\\begin{quote}
+  % \\begin{center}
+  %   abc
+  %   def
+  %   ghi
+  % \\end{center}
+\\end{quote}
+"))
+
+      ;; test 2-4
+      (erase-buffer)
+      (insert "\\begin{quote}
+  %\s
+  % abc
+  % def
+  % ghi
+  %\s
+\\end{quote}
+")
+      (set-mark (line-end-position -1)) ; just after the last "% "
+      (goto-char (point-min))
+      (beginning-of-line 2) ; just before the first "  %"
+      (LaTeX-insert-environment "center")
+      (should (string=
+              (buffer-string)
+              "\\begin{quote}
+  % \\begin{center}
+  %   abc
+  %   def
+  %   ghi
+  % \\end{center}
+\\end{quote}
+"))
+
+      ;; test 2-5
+      (erase-buffer)
+      (insert "\\begin{quote}
+  %\s
+  % abc
+  % def
+  % ghi
+  %\s
+\\end{quote}
+")
+      (set-mark (1- (line-end-position -1))) ; just after the last "%"
+      (goto-char (point-min))
+      (beginning-of-line 2)
+      (forward-char 2) ; just before the first "%"
+      (LaTeX-insert-environment "center")
+      (should (string=
+              (buffer-string)
+              "\\begin{quote}
+  % \\begin{center}
+  %   abc
+  %   def
+  %   ghi
+  % \\end{center}
+\\end{quote}
+"))
+
+      (setq LaTeX-insert-into-comments nil)
+
+      ;; test 2-6
+      (erase-buffer)
+      (insert "\\begin{quote}
+  % abc
+  % def
+  % ghi
+\\end{quote}
+")
+      (set-mark (line-beginning-position 0)) ; just before \end{quote}
+      (goto-char (point-min))
+      (beginning-of-line 2) ; just before the first "  %"
+      (LaTeX-insert-environment "center")
+      (should (string=
+              (buffer-string)
+              "\\begin{quote}
+  \\begin{center}
+    % abc
+    % def
+    % ghi
+  \\end{center}
+\\end{quote}
+")))))
+
 ;;; latex-test.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]