emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[elpa] 182/299: Add indentation for tabular environment.


From: Stefan Monnier
Subject: [elpa] 182/299: Add indentation for tabular environment.
Date: Sun, 02 Nov 2014 03:11:18 +0000

monnier pushed a commit to branch externals/auctex
in repository elpa.

commit f8c1d44c62f958e19d924deeb51c54f2413e1ef6
Author: Oleh Krehel <address@hidden>
Date:   Sat Oct 12 12:16:16 2013 +0200

    Add indentation for tabular environment.
    
    * latex.el: `LaTeX-indent-tabular' now indents tabular-like environments.
      (LaTeX--tabular-like-end): new variable.
      (LaTeX-indent-environment-list): added `LaTeX-indent-tabular' as
      indenter for "tabular" and "align", added a setter that recomputes
      `LaTeX--tabular-like-end'
      (LaTeX-env-beginning-pos-col): new function.
      (LaTeX-hanging-ampersand-position): new function.
      (LaTeX-indent-tabular): new function.
    
    * tests/latex/latex-test.el : added an ERT test for `LaTeX-indent-tabular'
      (LaTeX-indent-tabular-test/in): input filename variable
      (LaTeX-indent-tabular-test/out): output filename variable
    
    * tests/latex/tabular-in.tex: input to latex-test.el
    
    * tests/latex/tabular-out.tex: input to latex-test.el
    
    * tex.el (TeX-how-many): added for compatibility with XEmacs.
    
    Signed-off-by: Tassilo Horn <address@hidden>
---
 ChangeLog                   |   23 +++++++++++++++
 latex.el                    |   67 +++++++++++++++++++++++++++++++++++++++++--
 tests/latex/latex-test.el   |   13 ++++++++
 tests/latex/tabular-in.tex  |   41 ++++++++++++++++++++++++++
 tests/latex/tabular-out.tex |   41 ++++++++++++++++++++++++++
 tex.el                      |    9 ++++++
 6 files changed, 191 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e5c8100..b8ff631 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,26 @@
+2013-10-14  Oleh Krehel  <address@hidden>
+
+       * latex.el: `LaTeX-indent-tabular' now indents tabular-like
+       environments.
+       (LaTeX--tabular-like-end): new variable.
+       (LaTeX-indent-environment-list): added `LaTeX-indent-tabular' as
+       indenter for "tabular" and "align", added a setter that recomputes
+       `LaTeX--tabular-like-end'
+       (LaTeX-env-beginning-pos-col): new function.
+       (LaTeX-hanging-ampersand-position): new function.
+       (LaTeX-indent-tabular): new function.
+
+       * tests/latex/latex-test.el : added an ERT test for
+       `LaTeX-indent-tabular'
+       (LaTeX-indent-tabular-test/in): input filename variable
+       (LaTeX-indent-tabular-test/out): output filename variable
+
+       * tests/latex/tabular-in.tex: input to latex-test.el
+
+       * tests/latex/tabular-out.tex: input to latex-test.el
+
+       * tex.el (TeX-how-many): added for compatibility with XEmacs.
+
 2013-10-11  Mos� Giordano  <address@hidden>
 
        * doc/auctex.texi (Quotes): Fix typo.
diff --git a/latex.el b/latex.el
index f59168f..8488966 100644
--- a/latex.el
+++ b/latex.el
@@ -2633,14 +2633,34 @@ consideration just as is in the non-commented source 
code."
     ("tabbing")
     ("table")
     ("table*")
-    ("tabular")
-    ("tabular*"))
+    ("tabular" LaTeX-indent-tabular)
+    ("tabular*" LaTeX-indent-tabular)
+    ("align" LaTeX-indent-tabular)
+    ("align*" LaTeX-indent-tabular))
     "Alist of environments with special indentation.
 The second element in each entry is the function to calculate the
 indentation level in columns."
     :group 'LaTeX-indentation
     :type '(repeat (list (string :tag "Environment")
-                        (option function))))
+                        (option function)))
+    :set (lambda (symbol value)
+           (setq LaTeX--tabular-like-end
+                 (format "\\\\end{%s}"
+                         (regexp-opt
+                          (let (out)
+                            (mapc (lambda (x)
+                                    (when (eq (cadr x) 'LaTeX-indent-tabular)
+                                      (push (car x) out)))
+                                  value)
+                            out))))
+           (set-default symbol value)))
+
+(defvar LaTeX--tabular-like-end nil
+  "A regexp matching tabular-like environment ends.
+Those will be aligned with `LaTeX-indent-tabular'.
+
+Do not set this variable. This variable is auto-set
+by customizing `LaTeX-indent-environment-list'.")
 
 (defcustom LaTeX-indent-environment-check t
   "*If non-nil, check for any special environments."
@@ -5996,6 +6016,47 @@ i.e. you do _not_ have to cater for this yourself by 
adding \\\\' or $."
       (replace-match "\\\\input{" nil nil)))))
   (TeX-normal-mode nil))
 
+(defun LaTeX-env-beginning-pos-col ()
+  "Return a cons: (POINT . COLUMN) for current environment's beginning."
+  (save-excursion
+    (LaTeX-find-matching-begin)
+    (cons (point) (current-column))))
+
+(defun LaTeX-hanging-ampersand-position ()
+  "Return indent column for a hanging ampersand (i.e. ^\\s-*&)."
+  (destructuring-bind (beg-pos . beg-col)
+      (LaTeX-env-beginning-pos-col)
+    (let* ((cur-pos (point)))
+      (save-excursion
+        (if (re-search-backward "\\\\\\\\" beg-pos t)
+            (let ((cur-idx (TeX-how-many "[^\\]&" (point) cur-pos)))
+              (goto-char beg-pos)
+              (re-search-forward "[^\\]&" cur-pos t (+ 1 cur-idx))
+              (- (current-column) 1))
+          (+ 2 beg-col))))))
+
+(defun LaTeX-indent-tabular ()
+  "Return indent column for the current tabular-like line."
+  (destructuring-bind (beg-pos . beg-col)
+      (LaTeX-env-beginning-pos-col)
+    (cond ((looking-at LaTeX--tabular-like-end)
+           beg-col)
+
+          ((looking-at "\\\\\\\\")
+           (+ 2 beg-col))
+
+          ((looking-at "&")
+           (LaTeX-hanging-ampersand-position))
+
+          (t
+           (+ 2
+              (let ((any-col (save-excursion
+                               (when (re-search-backward "\\\\\\\\\\|&" 
beg-pos t)
+                                 (current-column)))))
+                (if (and any-col (string= "&" (match-string 0)))
+                    any-col
+                  beg-col)))))))
+
 (provide 'latex)
 
 ;;; latex.el ends here
diff --git a/tests/latex/latex-test.el b/tests/latex/latex-test.el
new file mode 100644
index 0000000..a2d71e4
--- /dev/null
+++ b/tests/latex/latex-test.el
@@ -0,0 +1,13 @@
+(defvar LaTeX-indent-tabular-test/in  (expand-file-name "tabular-in.tex"))
+(defvar LaTeX-indent-tabular-test/out (expand-file-name "tabular-out.tex"))
+
+(ert-deftest LaTeX-indent-tabular ()
+  (should (string=
+           (with-temp-buffer
+             (insert-file-contents LaTeX-indent-tabular-test/in)
+             (LaTeX-mode)
+             (indent-region (point-min) (point-max))
+             (buffer-string))
+           (with-temp-buffer
+             (insert-file-contents LaTeX-indent-tabular-test/out)
+             (buffer-string)))))
diff --git a/tests/latex/tabular-in.tex b/tests/latex/tabular-in.tex
new file mode 100644
index 0000000..a8be333
--- /dev/null
+++ b/tests/latex/tabular-in.tex
@@ -0,0 +1,41 @@
+\documentclass{article}
+\begin{document}
+\begin{tabular}{llll}
+Lorem ipsum dolor & sit amet, ei mei
+paulo tation honestatis,
+intellegam & accommodare ne vim, ut
+mel solum putant
+atomorum. Posse & dolores has ut,\\
+prompta           & disputando & ne mel, ne
+viderer ceteros
+vel.            & No petentium
+\\
+
+reformidans mel.  & Quo no sale
+natum, cu
+pericula   & deterruisset
+usu. Nec        & bonorum detracto\\
+detraxit          & no.        & Ne sea doming   & deserunt.
+\end{tabular}
+
+\begin{tabular}{ll}
+1 & 2 & 3
+\\
+& 4 & 5
+\\
+6 & 7
+& 8\\
+9 &
+10
+& 11
+\end{tabular}
+\begin{align}
+1 & 2
+3 & 4
+5\\
+6 &
+7
+8 &
+9
+\end{align}
+\end{document}
diff --git a/tests/latex/tabular-out.tex b/tests/latex/tabular-out.tex
new file mode 100644
index 0000000..cd5b708
--- /dev/null
+++ b/tests/latex/tabular-out.tex
@@ -0,0 +1,41 @@
+\documentclass{article}
+\begin{document}
+\begin{tabular}{llll}
+  Lorem ipsum dolor & sit amet, ei mei
+                      paulo tation honestatis,
+                      intellegam & accommodare ne vim, ut
+                                   mel solum putant
+                                   atomorum. Posse & dolores has ut,\\
+  prompta           & disputando & ne mel, ne
+                                   viderer ceteros
+                                   vel.            & No petentium
+  \\
+
+  reformidans mel.  & Quo no sale
+                      natum, cu
+                      pericula   & deterruisset
+                                   usu. Nec        & bonorum detracto\\
+  detraxit          & no.        & Ne sea doming   & deserunt.
+\end{tabular}
+
+\begin{tabular}{ll}
+  1 & 2 & 3
+  \\
+    & 4 & 5
+  \\
+  6 & 7
+        & 8\\
+  9 &
+      10
+        & 11
+\end{tabular}
+\begin{align}
+  1 & 2
+      3 & 4
+          5\\
+  6 &
+      7
+      8 &
+          9
+\end{align}
+\end{document}
diff --git a/tex.el b/tex.el
index 0541acf..443cf25 100644
--- a/tex.el
+++ b/tex.el
@@ -5776,6 +5776,15 @@ NAME may be a package, a command, or a document."
 (put 'TeX-insert-quote 'delete-selection t)
 (put 'TeX-insert-backslash 'delete-selection t)
 
+(defun TeX-how-many (regexp &optional rstart rend)
+  "Compatibily function for `how-many'.
+Supports restriction to a region where the XEmacs version doesn't."
+  (save-excursion
+    (save-restriction
+      (narrow-to-region rstart rend)
+      (goto-char (point-min))
+      (how-many regexp))))
+
 (provide 'tex)
 
 ;; Local Variables:



reply via email to

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