From 21ddffc7f685e1b57cb781c47b5aa8e4947faca0 Mon Sep 17 00:00:00 2001
From: Oleh Krehel <ohwoeowho@gmail.com>
Date: Thu, 10 Oct 2013 10:09:11 +0200
Subject: [PATCH] Add indentation for tabular environment.

* latex.el: add `LaTeX-indent-tabular' to `LaTeX-indent-environment-list'
* tests/latex/latex-test.el: added an ERT test for `LaTeX-indent-tabular'
* tests/latex/tabular-in.tex: input to latex-test.el
* tests/latex/tabular-out.tex: input to latex-test.el
---
 latex.el                    | 47 +++++++++++++++++++++++++++++++++++++++++++--
 tests/latex/latex-test.el   | 17 ++++++++++++++++
 tests/latex/tabular-in.tex  | 32 ++++++++++++++++++++++++++++++
 tests/latex/tabular-out.tex | 32 ++++++++++++++++++++++++++++++
 4 files changed, 126 insertions(+), 2 deletions(-)
 create mode 100644 tests/latex/latex-test.el
 create mode 100644 tests/latex/tabular-in.tex
 create mode 100644 tests/latex/tabular-out.tex

diff --git a/latex.el b/latex.el
index f59168f..392dc74 100644
--- a/latex.el
+++ b/latex.el
@@ -2633,8 +2633,8 @@ consideration just as is in the non-commented source code."
     ("tabbing")
     ("table")
     ("table*")
-    ("tabular")
-    ("tabular*"))
+    ("tabular" LaTeX-indent-tabular)
+    ("tabular*" 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."
@@ -5996,6 +5996,49 @@ 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 ()
+  (save-excursion
+    (LaTeX-find-matching-begin)
+    (cons (point) (current-column))))
+
+(defun LaTeX-hanging-ampersand-position ()
+  "Return indent position for a hanging (i.e. ^\\s-*&) ampersand."
+  (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 (cl-count ?& (buffer-substring (point) cur-pos))))
+              (goto-char beg-pos)
+              (re-search-forward "&" cur-pos t (+ 1 cur-idx))
+              (- (current-column) 1))
+          (+ 2 beg-col))))))
+
+(defconst LaTeX-tabularlike-end "\\\\end{\\(?:tabular\\|align\\)\\*?}")
+
+(defun LaTeX-indent-tabular ()
+  "Return indent position for the current line
+in a tabular-like environment"
+  (destructuring-bind (beg-pos . beg-col)
+      (LaTeX-env-beginning-pos-col)
+    (cond ((looking-at LaTeX-tabularlike-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..89a8baf
--- /dev/null
+++ b/tests/latex/latex-test.el
@@ -0,0 +1,17 @@
+(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 (equal
+           (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..fc6f02e
--- /dev/null
+++ b/tests/latex/tabular-in.tex
@@ -0,0 +1,32 @@
+\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}
+\end{document}
diff --git a/tests/latex/tabular-out.tex b/tests/latex/tabular-out.tex
new file mode 100644
index 0000000..11ef9c9
--- /dev/null
+++ b/tests/latex/tabular-out.tex
@@ -0,0 +1,32 @@
+\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}
+\end{document}
-- 
1.8.4

