auctex-diffs
[Top][All Lists]
Advanced

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

[AUCTeX-diffs] [elpa] externals/auctex 4bcda2d50c 35/60: Improve indenta


From: Tassilo Horn
Subject: [AUCTeX-diffs] [elpa] externals/auctex 4bcda2d50c 35/60: Improve indentation in env from algpseudocode package
Date: Fri, 8 Apr 2022 11:52:51 -0400 (EDT)

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

    Improve indentation in env from algpseudocode package
    
    * style/algpseudocode.el (LaTeX-algpseudocode-package-options):
    Adjust package options.
    ("algpseudocode"): Rearrange provided macros.  Add macros to
    appropriate indentation variables.  Inhibit filling by adding the
    environment "algorithmic" to `LaTeX-indent-environment-list'
    without a function.  Cater for fontification.
    
    * tests/latex/latex-test.el (LaTeX-conditionals-indent): Parse the
    test file and run the style hook for loaded package algpseudocode.
    
    * tests/latex/conditionals-indent-in.tex:
    * tests/latex/conditionals-indent-out.tex: Expand files with code
    from algpseudocode package.
---
 style/algpseudocode.el                  | 103 ++++++++++++++++++++++++++------
 tests/latex/conditionals-indent-in.tex  |  55 +++++++++++++++++
 tests/latex/conditionals-indent-out.tex |  55 +++++++++++++++++
 tests/latex/latex-test.el               |   4 +-
 4 files changed, 198 insertions(+), 19 deletions(-)

diff --git a/style/algpseudocode.el b/style/algpseudocode.el
index b0c455fef1..619052b614 100644
--- a/style/algpseudocode.el
+++ b/style/algpseudocode.el
@@ -1,6 +1,6 @@
 ;;; algpseudocode.el --- AUCTeX style for the (LaTeX) algpseudocode package  
-*- lexical-binding: t; -*-
 
-;; Copyright (C) 2020 Free Software Foundation, Inc.
+;; Copyright (C) 2020--2022 Free Software Foundation, Inc.
 
 ;; Author: Uwe Brauer <oub@mat.ucm.es>
 ;; Created: 2020-01-26
@@ -24,6 +24,7 @@
 ;; 02110-1301, USA.
 
 ;;; Commentary:
+
 ;; This file adds support for the algpseudocode package.
 
 ;;; Code:
@@ -31,46 +32,112 @@
 (require 'tex)
 (require 'latex)
 
+;; Silence the compiler:
+(declare-function font-latex-add-keywords
+                  "font-latex"
+                  (keywords class))
+
 (defvar LaTeX-algpseudocode-package-options
-  '("compatible" "nocompatible")
+  '("compatible" "nocompatible" "end" "noend")
   "Package options for the algpseudocode package.")
 
-
 (TeX-add-style-hook
  "algpseudocode"
  (lambda ()
    (TeX-add-symbols
-    '("algref" 2)
+    ;; 2.3 Simple lines
+    '("State"  (TeX-arg-literal " "))
+    '("Statex" 0)
+
+    ;; 2.4 Placing comments in sources
+    '("Comment" 1)
+
+    ;; 2.5 Labels and references
+    '("algref" (TeX-arg-ref "Algorithm") (TeX-arg-ref "Line"))
+
+    ;; 2.6 Breaking up long algorithms
     '("algstore" 1)
-    '("algrestore" 1)
     '("algstore*" 1)
+    '("algrestore" 1)
     '("algrestore*" 1)
-    '("Procedure" 2)
-    '("Comment" 1)
-    '("State" 0)
-    '("While" 0)
-    '("EndWhile" 0)
-    '("EndProcedure" 0)
-    '("Repeat" 0)
-    '("Until" 0)
+
+    ;; 3.1.1 The for block
     '("For" 1)
     '("ForAll" 1)
     '("EndFor" 0)
+
+    ;; 3.1.2 The while block
+    '("While" 0)
+    '("EndWhile" 0)
+
+    ;; 3.1.3 The repeat block
+    '("Repeat" 0)
+    '("Until" 1)
+
+    ;; 3.1.4 The if block
     '("If" 1)
     '("ElsIf" 1)
     '("Else" 0)
     '("EndIf" 0)
+
+    ;; 3.1.5 The procedure block
+    '("Procedure" 2)
+    '("EndProcedure" 0)
+
+    ;; 3.1.6 The function block
     '("Function" 2)
     '("EndFunction" 0)
+
+    ;; 3.1.7 The loop block
     '("Loop" 0)
     '("EndLoop" 0)
-    '("Require" 0)
-    '("Ensure" 0)
-    '("State" 0)
-    '("Statex" 0)
-    '("Call" 0))
+
+    ;; 3.1.8 Other commands in this layout
+    '("Require" (TeX-arg-literal " "))
+    '("Ensure"  (TeX-arg-literal " "))
+    '("Call" 2))
+
    (LaTeX-add-environments
     '("algorithmic" [ "Number" ]))
+
+   ;; Indentation: Add the keywords above to the respective variables
+   ;; and run `LaTeX-indent-commands-regexp-make'.
+   (let ((beg '("For" "ForAll"
+                "While"
+                "Repeat"
+                "If"
+                "Procedure"
+                "Function"
+                "Loop"))
+         (mid '("ElsIf" "Else"))
+         (end '("EndFor"
+                "EndWhile"
+                "Until"
+                "EndIf"
+                "EndProcedure"
+                "EndFunction"
+                "EndLoop")))
+     (dolist (elt beg)
+       (add-to-list 'LaTeX-indent-begin-list elt t))
+     (dolist (elt mid)
+       (add-to-list 'LaTeX-indent-mid-list elt t))
+     (dolist (elt end)
+       (add-to-list 'LaTeX-indent-end-list elt t))
+     (LaTeX-indent-commands-regexp-make))
+
+   ;; Add the 'algorithmic' environment to a local version of
+   ;; `LaTeX-indent-environment-list'.  This effectively kills filling
+   ;; but indenting works as expected.  Hence, 'M-q' gives a better
+   ;; experience.
+   (add-to-list (make-local-variable 'LaTeX-indent-environment-list)
+                '("algorithmic")
+                t)
+
+   ;; Fontification
+   (when (and (featurep 'font-latex)
+              (eq TeX-install-font-lock 'font-latex-setup))
+     (font-latex-add-keywords '(("algref" "{{"))
+                              'reference))
    TeX-dialect))
 
 ;;; algpseudocode.el ends here
diff --git a/tests/latex/conditionals-indent-in.tex 
b/tests/latex/conditionals-indent-in.tex
index 606177516f..b832d5e2dd 100644
--- a/tests/latex/conditionals-indent-in.tex
+++ b/tests/latex/conditionals-indent-in.tex
@@ -1,5 +1,6 @@
 \documentclass{article}
 
+\usepackage{algpseudocode}
 \usepackage[%
   key = val , %
                key = val , %
@@ -88,6 +89,60 @@
 \wlog{\string#1=\string\insert\the\allocationnumber}%
 }
 
+\begin{algorithmic}[1]
+ \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
+  \State $r\gets a\bmod b$
+  \While{$r\not=0$}\Comment{We have the answer if r is 0 and what
+   about a large comment}
+   \State $a\gets b$
+\State $b\gets r$
+ \State $r\gets a\bmod b$
+ \EndWhile\label{euclidendwhile}
+ \State \textbf{return} $b$\Comment{The gcd is b}
+ \State
+\Statex
+ \EndProcedure
+\end{algorithmic}
+
+\begin{algorithmic}[1]
+\State $sum\gets 0$
+\For{$i\gets 1, n$}
+\State $sum\gets sum+i$
+\EndFor
+\end{algorithmic}
+
+\begin{algorithmic}[1]
+\State $sum\gets 0$
+\State $i\gets 1$
+\While{$i\le n$}
+\State $sum\gets sum+i$
+ \State $i\gets i+1$
+           \EndWhile
+\end{algorithmic}
+
+\begin{algorithmic}[1]
+ \If{$quality\ge 9$}
+ \State $a\gets perfect$
+ \ElsIf{$quality\ge 7$}
+ \State $a\gets good$
+ \ElsIf{$quality\ge 5$}
+ \State $a\gets medium$
+ \ElsIf{$quality\ge 3$}
+             \State $a\gets bad$
+  \Else
+                 \State $a\gets unusable$
+  \EndIf
+\end{algorithmic}
+
+\begin{algorithmic}[1]
+            \Require $x\ge5$
+\Ensure $x\le-5$
+             \Statex
+\While{$x>-5$}
+                  \State $x\gets x-1$
+                                \EndWhile
+\end{algorithmic}
+
 \end{document}
 
 %%% Local Variables:
diff --git a/tests/latex/conditionals-indent-out.tex 
b/tests/latex/conditionals-indent-out.tex
index 2440466428..864f718090 100644
--- a/tests/latex/conditionals-indent-out.tex
+++ b/tests/latex/conditionals-indent-out.tex
@@ -1,5 +1,6 @@
 \documentclass{article}
 
+\usepackage{algpseudocode}
 \usepackage[%
   key = val , %
   key = val , %
@@ -88,6 +89,60 @@
   \wlog{\string#1=\string\insert\the\allocationnumber}%
 }
 
+\begin{algorithmic}[1]
+  \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
+    \State $r\gets a\bmod b$
+    \While{$r\not=0$}\Comment{We have the answer if r is 0 and what
+        about a large comment}
+      \State $a\gets b$
+      \State $b\gets r$
+      \State $r\gets a\bmod b$
+    \EndWhile\label{euclidendwhile}
+    \State \textbf{return} $b$\Comment{The gcd is b}
+    \State
+    \Statex
+  \EndProcedure
+\end{algorithmic}
+
+\begin{algorithmic}[1]
+  \State $sum\gets 0$
+  \For{$i\gets 1, n$}
+    \State $sum\gets sum+i$
+  \EndFor
+\end{algorithmic}
+
+\begin{algorithmic}[1]
+  \State $sum\gets 0$
+  \State $i\gets 1$
+  \While{$i\le n$}
+    \State $sum\gets sum+i$
+    \State $i\gets i+1$
+  \EndWhile
+\end{algorithmic}
+
+\begin{algorithmic}[1]
+  \If{$quality\ge 9$}
+    \State $a\gets perfect$
+  \ElsIf{$quality\ge 7$}
+    \State $a\gets good$
+  \ElsIf{$quality\ge 5$}
+    \State $a\gets medium$
+  \ElsIf{$quality\ge 3$}
+    \State $a\gets bad$
+  \Else
+    \State $a\gets unusable$
+  \EndIf
+\end{algorithmic}
+
+\begin{algorithmic}[1]
+  \Require $x\ge5$
+  \Ensure $x\le-5$
+  \Statex
+  \While{$x>-5$}
+    \State $x\gets x-1$
+  \EndWhile
+\end{algorithmic}
+
 \end{document}
 
 %%% Local Variables:
diff --git a/tests/latex/latex-test.el b/tests/latex/latex-test.el
index 2ab7d3a96f..d10b98a6e1 100644
--- a/tests/latex/latex-test.el
+++ b/tests/latex/latex-test.el
@@ -663,7 +663,9 @@ check the indentation for optional argument of 
\\usepackage."
              (insert-file-contents LaTeX-conditionals-indent/in)
              (LaTeX-mode)
              (let ((TeX-indent-open-delimiters "[")
-                   (TeX-indent-close-delimiters "]"))
+                   (TeX-indent-close-delimiters "]")
+                   (TeX-parse-self t))
+               (TeX-update-style t)
                (indent-region (point-min) (point-max))
                (buffer-string)))
            (with-temp-buffer




reply via email to

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