auctex-devel
[Top][All Lists]
Advanced

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

Re: filling and indentation, again


From: Ikumi Keita
Subject: Re: filling and indentation, again
Date: Fri, 25 Feb 2022 14:57:27 +0900

>>>>> Uwe Brauer <oub@mat.ucm.es> writes:
>>>> "IK" == Ikumi Keita <ikumi@ikumi.que.jp> writes:
>> How about the attached patch?

> Great, filling is much better now! Thanks!
> You could apply it I think.

Thank you for confirmation.

I ended up with the following patch in case that the envs with alignment
are deeply nested. However, this might be overkilling. What does others
think about it?

Regards,
Ikumi Keita
#StandWithUkraine #StopRussianAggression

diff --git a/latex.el b/latex.el
index bfd5b8c6..a210fabc 100644
--- a/latex.el
+++ b/latex.el
@@ -7751,14 +7751,25 @@ function would return non-nil and `(match-string 1)' 
would return
     (LaTeX-find-matching-begin)
     (cons (point) (current-column))))
 
-(defun LaTeX-hanging-ampersand-position ()
-  "Return indent column for a hanging ampersand (that is, ^\\s-*&)."
+(defun LaTeX-hanging-ampersand-position (&optional pos col)
+  "Return indent column for a hanging ampersand (that is, ^\\s-*&).
+When you know the position and column of the current environment,
+supply them as optional arguments POS and COL for efficiency."
   (cl-destructuring-bind
       (beg-pos . beg-col)
-      (LaTeX-env-beginning-pos-col)
+      (if pos
+          (cons pos col)
+        (LaTeX-env-beginning-pos-col))
     (let* ((cur-pos (point)))
       (save-excursion
-        (if (re-search-backward "\\\\\\\\" beg-pos t)
+        (if (catch 'found
+              ;; Search "\\", not in an inner env of current env.
+              (while (re-search-backward "\\\\\\\\" beg-pos t)
+                (if (= beg-pos
+                       (save-excursion
+                         (LaTeX-find-matching-begin)
+                         (point)))
+                    (throw 'found t))))
             (let ((cur-idx (how-many "[^\\]&" (point) cur-pos)))
               (goto-char beg-pos)
               (re-search-forward "[^\\]&" cur-pos t (+ 1 cur-idx))
@@ -7790,13 +7801,23 @@ function would return non-nil and `(match-string 1)' 
would return
              (+ 2 beg-col))
 
             ((looking-at "&")
-             (LaTeX-hanging-ampersand-position))
+             (LaTeX-hanging-ampersand-position beg-pos beg-col))
 
             (t
              (+ 2
-                (let ((any-col (save-excursion
-                                 (when (re-search-backward "\\\\\\\\\\|[^\\]&" 
beg-pos t)
-                                   (current-column)))))
+                (let ((any-col
+                       (save-excursion
+                         (when
+                             (catch 'found
+                               ;; Search "\\" or "&", not in an inner
+                               ;; env of the current env.
+                               (while (re-search-backward "\\\\\\\\\\|[^\\]&" 
beg-pos t)
+                                 (if (= beg-pos
+                                        (save-excursion
+                                          (LaTeX-find-matching-begin)
+                                          (point)))
+                                     (throw 'found t))))
+                           (current-column)))))
                   (if (and any-col (= ?& (char-before (match-end 0))))
                       (1+ any-col)
                     beg-col))))))))

reply via email to

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