auctex-diffs
[Top][All Lists]
Advanced

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

[AUCTeX-diffs] [elpa] externals/auctex 0ce906cfc7 32/60: Improve indenta


From: Tassilo Horn
Subject: [AUCTeX-diffs] [elpa] externals/auctex 0ce906cfc7 32/60: Improve indentation of conditionals
Date: Fri, 8 Apr 2022 11:52:51 -0400 (EDT)

branch: externals/auctex
commit 0ce906cfc7a33a0be50180a6344ddfdfe0d89cb7
Author: Arash Esbati <arash@gnu.org>
Commit: Arash Esbati <arash@gnu.org>

    Improve indentation of conditionals
    
    * latex.el (LaTeX-indent-begin-list)
    (LaTeX-indent-begin-exceptions-list, LaTeX-indent-mid-list)
    (LaTeX-indent-end-list): New variables containing list of macros
    which alter the indentation.
    (LaTeX-indent-begin-regexp-local)
    (LaTeX-indent-begin-regexp-exceptions-local)
    (LaTeX-indent-mid-regexp-local, LaTeX-indent-end-regexp-local):
    New variables containing the regexp's calculated from the
    variables above.
    (LaTeX-indent-commands-regexp-make): New function calculating the
    regexp's.
    (LaTeX-indent-calculate):
    (LaTeX-indent-calculate-last): Indent at regexps from
    `LaTeX-indent-end-regexp-local' and
    `LaTeX-indent-mid-regexp-local'.
    (LaTeX-indent-level-count): Increase/decrease indentation
    depending on `LaTeX-indent-begin-regexp-local' and
    `LaTeX-indent-end-regexp-local'.
    
    * tests/latex/latex-test.el (LaTeX-conditionals-indent): New test.
---
 latex.el                                | 141 +++++++++++++++++++++++++++++++-
 tests/latex/conditionals-indent-in.tex  |  96 ++++++++++++++++++++++
 tests/latex/conditionals-indent-out.tex |  96 ++++++++++++++++++++++
 tests/latex/latex-test.el               |  26 +++++-
 4 files changed, 355 insertions(+), 4 deletions(-)

diff --git a/latex.el b/latex.el
index 142d075c66..d5bb9b04b3 100644
--- a/latex.el
+++ b/latex.el
@@ -3751,9 +3751,11 @@ value."
 
 (defvar docTeX-indent-inner-fixed
   `((,(concat (regexp-quote TeX-esc)
-             "\\(begin\\|end\\)[ \t]*{macrocode\\*?}") 4 t)
+              "\\(begin\\|end\\)[ \t]*{macrocode\\*?}")
+     4 t)
     (,(concat (regexp-quote TeX-esc)
-             "\\(begin\\|end\\)[ \t]*{\\(macro\\|environment\\)\\*?}") 0 nil))
+              "\\(begin\\|end\\)[ \t]*{\\(macro\\|environment\\)\\*?}")
+     0 nil))
   "List of items which should have a fixed inner indentation.
 The items consist of three parts.  The first is a regular
 expression which should match the respective string.  The second
@@ -3762,6 +3764,110 @@ toggles if comment padding is relevant or not.  If t 
padding is
 part of the amount given, if nil the amount of spaces will be
 inserted after potential padding.")
 
+(defvar-local LaTeX-indent-begin-list nil
+  "List of macros increasing indentation.
+Each item in this list is a string with the name of the macro
+without a backslash.  The final regexp will be calculated by the
+function `LaTeX-indent-commands-regexp-make'.  A regexp for the
+\\if contructs is added by the function as well.  AUCTeX styles
+should add their macros to this variable and then run
+`LaTeX-indent-commands-regexp-make'.")
+
+(defvar-local LaTeX-indent-begin-exceptions-list nil
+  "List of macros which shouldn't increase the indentation.
+Each item in this list is a string without a backslash and will
+mostly start with 'if'.  These macros should not increase
+indentation although they start with 'if', for example the
+'ifthenelse' macro provided by the ifthen package.  AUCTeX styles
+should add their macros to this variable and then run
+`LaTeX-indent-commands-regexp-make'.")
+
+(defvar-local LaTeX-indent-mid-list nil
+  "List of macros which backindent the line where they appear.
+Each item in this list is a string with the name of the macro
+without a backslash.  The final regexp will be calculated by the
+function `LaTeX-indent-commands-regexp-make' which takes care of
+\\else and \\or.  AUCTeX styles should add their macros to this
+variable and then run `LaTeX-indent-commands-regexp-make'.")
+
+(defvar-local LaTeX-indent-end-list nil
+  "List of macros decreasing indentation.
+Each item in this list is a string with the name of the macro
+without a backslash.  The final regexp will be calculated by the
+function `LaTeX-indent-commands-regexp-make' which takes care of
+\\fi.  AUCTeX styles should add their macros to this variable and
+then run `LaTeX-indent-commands-regexp-make'.")
+
+(defvar-local LaTeX-indent-begin-regexp-local nil
+  "Regexp calculated from `LaTeX-indent-begin-list'.
+The value is calculated and set by the function
+`LaTeX-indent-commands-regexp-make' which already takes care of
+\\if constructs.")
+
+(defvar-local LaTeX-indent-begin-regexp-exceptions-local nil
+  "Regexp calculated from `LaTeX-indent-begin-exceptions-list'.
+The value is calculated and set by the function
+`LaTeX-indent-commands-regexp-make' which already takes care of
+\\ifthenelse.")
+
+(defvar-local LaTeX-indent-mid-regexp-local nil
+  "Regexp calculated from `LaTeX-indent-mid-list'.
+The value is calculated and set by the function
+`LaTeX-indent-commands-regexp-make' which already takes care of
+\\else and \\or.")
+
+(defvar-local LaTeX-indent-end-regexp-local nil
+  "Regexp calculated from `LaTeX-indent-end-list'.
+The value is calculated and set by the function
+`LaTeX-indent-commands-regexp-make' which already takes care of
+\\fi.")
+
+(defun LaTeX-indent-commands-regexp-make ()
+  "Calculate final regexp for adjusting indentation.
+This function takes the elements provided in
+`LaTeX-indent-begin-list', `LaTeX-indent-begin-exceptions-list',
+`LaTeX-indent-mid-list' and `LaTeX-indent-end-list' and generates
+the regexp's which are stored in
+`LaTeX-indent-begin-regexp-local',
+`LaTeX-indent-begin-regexp-exceptions-local',
+`LaTeX-indent-mid-regexp-local' and
+`LaTeX-indent-end-regexp-local' accordingly.  Some standard
+macros are added to the regexp's.  This function is called in
+`LaTeX-common-initialization' to set the regexp's."
+  (let* (cmds
+         symbs
+         (func (lambda (in regexp out)
+                 (setq cmds nil
+                       symbs nil)
+                 (dolist (elt in)
+                   (if (string-match "[^a-zA-Z@]" elt)
+                       (push elt symbs)
+                     (push elt cmds)))
+                 (set out (concat regexp
+                                  (when cmds
+                                    (concat "\\|"
+                                            (regexp-opt cmds)
+                                            "\\b"))
+                                  (when symbs
+                                    (concat "\\|"
+                                            (regexp-opt symbs))))))))
+    (funcall func
+             LaTeX-indent-begin-list
+             "if[a-zA-Z@]*\\b"
+             'LaTeX-indent-begin-regexp-local)
+    (funcall func
+             LaTeX-indent-mid-list
+             "else\\b\\|or\\b"
+             'LaTeX-indent-mid-regexp-local)
+    (funcall func
+             LaTeX-indent-end-list
+             "fi\\b"
+             'LaTeX-indent-end-regexp-local)
+    (funcall func
+             LaTeX-indent-begin-exceptions-list
+             "ifthenelse\\b"
+             'LaTeX-indent-begin-regexp-exceptions-local)))
+
 (defun LaTeX-indent-line ()
   "Indent the line containing point, as LaTeX source.
 Add `LaTeX-indent-level' indentation in each \\begin{ - \\end{ block.
@@ -3899,6 +4005,20 @@ outer indentation in case of a commented line.  The 
symbols
                                  "\\)"))
              ;; Items.
              (+ (LaTeX-indent-calculate-last force-type) LaTeX-item-indent))
+            ;; Other (La)TeX programming constructs which end
+            ;; something, \fi for example where we backindent:
+            ((looking-at (concat (regexp-quote TeX-esc)
+                                 "\\("
+                                 LaTeX-indent-end-regexp-local
+                                 "\\)"))
+             (- (LaTeX-indent-calculate-last force-type) LaTeX-indent-level))
+            ;; (La)TeX programming contructs which backindent only the
+            ;; current line, for example \or or \else where we backindent:
+            ((looking-at (concat (regexp-quote TeX-esc)
+                                 "\\("
+                                 LaTeX-indent-mid-regexp-local
+                                 "\\)"))
+             (- (LaTeX-indent-calculate-last force-type) LaTeX-indent-level))
             ((memq (char-after) (append
                                  TeX-indent-close-delimiters '(?\})))
              ;; End brace in the start of the line.
@@ -3930,6 +4050,11 @@ outer indentation in case of a commented line.  The 
symbols
             (setq count (+ count LaTeX-indent-level)))
            ((looking-at LaTeX-end-regexp)
             (setq count (- count LaTeX-indent-level)))
+           ((and (not (looking-at LaTeX-indent-begin-regexp-exceptions-local))
+                 (looking-at LaTeX-indent-begin-regexp-local))
+            (setq count (+ count LaTeX-indent-level)))
+           ((looking-at LaTeX-indent-end-regexp-local)
+            (setq count (- count LaTeX-indent-level)))
            ((looking-at (regexp-quote TeX-esc))
             (forward-char 1))))
         count))))
@@ -4042,6 +4167,16 @@ outer indentation in case of a commented line.  The 
symbols
                                            LaTeX-item-regexp
                                            "\\)"))
                        (- LaTeX-item-indent))
+                      ((looking-at (concat (regexp-quote TeX-esc)
+                                           "\\("
+                                           LaTeX-indent-end-regexp-local
+                                           "\\)"))
+                       LaTeX-indent-level)
+                      ((looking-at (concat (regexp-quote TeX-esc)
+                                           "\\("
+                                           LaTeX-indent-mid-regexp-local
+                                           "\\)"))
+                       LaTeX-indent-level)
                       ((memq (char-after) (append
                                            TeX-indent-close-delimiters
                                            '(?\})))
@@ -7126,6 +7261,8 @@ function would return non-nil and `(match-string 1)' 
would return
   (setq-local beginning-of-defun-function #'LaTeX-find-matching-begin
               end-of-defun-function       #'LaTeX-find-matching-end)
 
+  (LaTeX-indent-commands-regexp-make)
+
   (set (make-local-variable 'LaTeX-item-list) '(("description" . 
LaTeX-item-argument)
                                                 ("thebibliography" . 
LaTeX-item-bib)
                                                 ("array" . LaTeX-item-array)
diff --git a/tests/latex/conditionals-indent-in.tex 
b/tests/latex/conditionals-indent-in.tex
new file mode 100644
index 0000000000..606177516f
--- /dev/null
+++ b/tests/latex/conditionals-indent-in.tex
@@ -0,0 +1,96 @@
+\documentclass{article}
+
+\usepackage[%
+  key = val , %
+               key = val , %
+]{package}
+
+\begin{document}
+
+\def\IfFileExists#1#2#3{%
+\openin\@inputcheck#1 %
+\ifeof\@inputcheck
+#3\relax
+\else
+\read\@inputcheck to \reserved@a
+\ifx\reserved@a\today
+\typeout{#1 found}#2\relax
+\else
+\typeout{BAD: old file \reserved@a (should be \today)}%
+#3\relax
+\fi
+\fi
+\closein\@inputcheck}
+
+\def\filename@simple#1.#2\\{%
+\ifx\\#2\\%
+\let\filename@ext\relax
+\edef\filename@base{#1}%
+\else
+\filename@dots{#1}#2\\%
+\fi}
+
+\ifx\directlua\@undefined
+\def\newwrite {\e@alloc\write
+\chardef{\count17}
+\m@ne\sixt@@n}
+\else
+\def\newwrite {\e@alloc\write {%
+\ifnum\allocationnumber=18
+\advance\count17\@ne
+\allocationnumber\count17 %
+        \fi
+\global\chardef
+}%
+{\count17}%
+\m@ne
+{128}}
+\fi
+
+\gdef\e@ch@ck#1#2#3#4{%
+\ifnum#1<#2\else
+ \ifnum#1=#2\relax
+ \global#1\@cclvi
+      \ifx\count#4\global\advance#1 10 \fi
+            \fi
+  \ifnum#1<#3\relax
+ \else
+ \errmessage{No room for a new \string#4}%
+ \fi
+   \fi
+}%
+
+\def\newinsert#1{%
+\@tempswafalse
+\global\advance\insc@unt\m@ne
+\ifnum\count10<\insc@unt
+\ifnum\count11<\insc@unt
+\ifnum\count12<\insc@unt
+\ifnum\count14<\insc@unt
+\@tempswatrue
+\fi\fi\fi\fi
+\if@tempswa
+\allocationnumber\insc@unt
+\else
+\global\advance\insc@unt\@ne
+\extrafloats\@ne
+\@next\@currbox\@freelist
+ {%
+\ifnum\@currbox<\e@insert@top
+\allocationnumber\@currbox
+\else
+\ch@ck0\m@ne\insert
+  \fi
+}%
+ {\ch@ck0\m@ne\insert}%
+\fi
+\global\chardef#1\allocationnumber
+\wlog{\string#1=\string\insert\the\allocationnumber}%
+}
+
+\end{document}
+
+%%% Local Variables:
+%%% mode: latex
+%%% TeX-master: t
+%%% End:
diff --git a/tests/latex/conditionals-indent-out.tex 
b/tests/latex/conditionals-indent-out.tex
new file mode 100644
index 0000000000..2440466428
--- /dev/null
+++ b/tests/latex/conditionals-indent-out.tex
@@ -0,0 +1,96 @@
+\documentclass{article}
+
+\usepackage[%
+  key = val , %
+  key = val , %
+]{package}
+
+\begin{document}
+
+\def\IfFileExists#1#2#3{%
+  \openin\@inputcheck#1 %
+  \ifeof\@inputcheck
+    #3\relax
+  \else
+    \read\@inputcheck to \reserved@a
+    \ifx\reserved@a\today
+      \typeout{#1 found}#2\relax
+    \else
+      \typeout{BAD: old file \reserved@a (should be \today)}%
+      #3\relax
+    \fi
+  \fi
+  \closein\@inputcheck}
+
+\def\filename@simple#1.#2\\{%
+  \ifx\\#2\\%
+    \let\filename@ext\relax
+    \edef\filename@base{#1}%
+  \else
+    \filename@dots{#1}#2\\%
+  \fi}
+
+\ifx\directlua\@undefined
+  \def\newwrite {\e@alloc\write
+    \chardef{\count17}
+    \m@ne\sixt@@n}
+\else
+  \def\newwrite {\e@alloc\write {%
+      \ifnum\allocationnumber=18
+        \advance\count17\@ne
+        \allocationnumber\count17 %
+      \fi
+      \global\chardef
+    }%
+    {\count17}%
+    \m@ne
+    {128}}
+\fi
+
+\gdef\e@ch@ck#1#2#3#4{%
+  \ifnum#1<#2\else
+    \ifnum#1=#2\relax
+      \global#1\@cclvi
+      \ifx\count#4\global\advance#1 10 \fi
+    \fi
+    \ifnum#1<#3\relax
+    \else
+      \errmessage{No room for a new \string#4}%
+    \fi
+  \fi
+}%
+
+\def\newinsert#1{%
+  \@tempswafalse
+  \global\advance\insc@unt\m@ne
+  \ifnum\count10<\insc@unt
+    \ifnum\count11<\insc@unt
+      \ifnum\count12<\insc@unt
+        \ifnum\count14<\insc@unt
+          \@tempswatrue
+        \fi\fi\fi\fi
+  \if@tempswa
+    \allocationnumber\insc@unt
+  \else
+    \global\advance\insc@unt\@ne
+    \extrafloats\@ne
+    \@next\@currbox\@freelist
+    {%
+      \ifnum\@currbox<\e@insert@top
+        \allocationnumber\@currbox
+      \else
+        \ch@ck0\m@ne\insert
+      \fi
+    }%
+    {\ch@ck0\m@ne\insert}%
+  \fi
+  \global\chardef#1\allocationnumber
+  \wlog{\string#1=\string\insert\the\allocationnumber}%
+}
+
+\end{document}
+
+%%% Local Variables:
+%%% mode: latex
+%%% TeX-master: t
+%%% End:
diff --git a/tests/latex/latex-test.el b/tests/latex/latex-test.el
index 15f31a8e1a..2ab7d3a96f 100644
--- a/tests/latex/latex-test.el
+++ b/tests/latex/latex-test.el
@@ -1,6 +1,6 @@
 ;;; latex-test.el --- tests for LaTeX mode  -*- lexical-binding: t; -*-
 
-;; Copyright (C) 2014-2021  Free Software Foundation, Inc.
+;; Copyright (C) 2014-2022  Free Software Foundation, Inc.
 
 ;; This file is part of AUCTeX.
 
@@ -50,7 +50,11 @@
  'tabular-count-ampersands/in
  "tabular-count-ampersands-in.tex"
  'tabular-count-ampersands/out
- "tabular-count-ampersands-out.tex")
+ "tabular-count-ampersands-out.tex"
+ 'LaTeX-conditionals-indent/in
+ "conditionals-indent-in.tex"
+ 'LaTeX-conditionals-indent/out
+ "conditionals-indent-out.tex")
 
 ;; Test for detecting \& in a table cell added; see
 ;; https://debbugs.gnu.org/cgi/bugreport.cgi?bug=26010
@@ -648,4 +652,22 @@ ghi"))
       (or orig-mode
           (electric-pair-mode -1)))))
 
+(ert-deftest LaTeX-conditionals-indent ()
+  "Test if conditionals are indented correctly.
+The code inside the test is randomely taken from source2e.  This
+test also sets the variables `TeX-indent-open-delimiters' and
+`TeX-indent-close-delimiters' to opening and closing brackets to
+check the indentation for optional argument of \\usepackage."
+  (should (string=
+           (with-temp-buffer
+             (insert-file-contents LaTeX-conditionals-indent/in)
+             (LaTeX-mode)
+             (let ((TeX-indent-open-delimiters "[")
+                   (TeX-indent-close-delimiters "]"))
+               (indent-region (point-min) (point-max))
+               (buffer-string)))
+           (with-temp-buffer
+             (insert-file-contents LaTeX-conditionals-indent/out)
+             (buffer-string)))))
+
 ;;; latex-test.el ends here




reply via email to

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