Hi, may I call you निरंजन?

>>>>> निरंजन <[email protected]> writes:
> I am having a code like the following in my mind:

> -----------------------
> \documentclass{article}
> \usepackage[%
>   Scale=1.5,%
>   Renderer=Harfbuzz%
> ]{fontspec}
> -----------------------

> Why not to support indentation of the code inside the square
> brackets? Currently it doesn't get indented. This support
> can be very useful for packages which have complicated
> optional parameters, like say, package `hyperref'. Or even
> better would be an example from package `forest'. See the
> following code which has nested square brackets:

> -----------------------
> \begin{forest}
> [a
>   [b
>     [c]
>   ]
> ]
> \end{forest}
> -----------------------

> Please let me know if this is possible. Feel free to point
> out the challenges or problem cases with this approach.

Thanks for your suggestion, I think it is useful. It seems that the
attached patch achieves what you want. Unless someone finds difficulties
with it, I can polish it and commit into the git repo.

Maybe I should modify `TeX-brace-count-line' instead of introducing a
new function `LaTeX-brace-count-line'. Currently I'm not sure which is
better.

Regards,
Ikumi Keita

diff --git a/latex.el b/latex.el
index f8dd1551..96ef0cf8 100644
--- a/latex.el
+++ b/latex.el
@@ -3891,7 +3891,7 @@ outer indentation in case of a commented line.  The symbols
                                  "\\)"))
              ;; Items.
              (+ (LaTeX-indent-calculate-last force-type) LaTeX-item-indent))
-            ((looking-at "}")
+            ((looking-at "}\\|\\]")
              ;; End brace in the start of the line.
              (- (LaTeX-indent-calculate-last force-type)
                 TeX-brace-indent-level))
@@ -3925,6 +3925,25 @@ outer indentation in case of a commented line.  The symbols
             (forward-char 1))))
         count))))
 
+(defun LaTeX-brace-count-line ()
+  "Count number of open/closed braces."
+  (save-excursion
+    (let ((count 0) (limit (line-end-position)) char)
+      (while (progn
+               (skip-chars-forward "^{}[]\\\\" limit)
+               (when (and (< (point) limit) (not (TeX-in-comment)))
+                 (setq char (char-after))
+                 (forward-char)
+                 (cond ((memq char '(?\{ ?\[))
+                        (setq count (+ count TeX-brace-indent-level)))
+                       ((memq char '(?\} ?\]))
+                        (setq count (- count TeX-brace-indent-level)))
+                       ((eq char ?\\)
+                        (when (< (point) limit)
+                          (forward-char)
+                          t))))))
+      count)))
+
 (defun LaTeX-indent-calculate-last (&optional force-type)
   "Return the correct indentation of a normal line of text.
 The point is supposed to be at the beginning of the current line.
@@ -3983,7 +4002,7 @@ outer indentation in case of a commented line.  The symbols
            (+ (LaTeX-current-indentation force-type)
               ;; Some people have opening braces at the end of the
               ;; line, e.g. in case of `\begin{letter}{%'.
-              (TeX-brace-count-line)))
+              (LaTeX-brace-count-line)))
           ((and (eq major-mode 'doctex-mode)
                 (looking-at (concat (regexp-quote TeX-esc)
                                     "end[ \t]*{macrocode\\*?}"))
@@ -4018,7 +4037,7 @@ outer indentation in case of a commented line.  The symbols
                               (eq force-type 'outer)
                               (TeX-in-commented-line)))
                     (+ (LaTeX-indent-level-count)
-                       (TeX-brace-count-line))
+                       (LaTeX-brace-count-line))
                   0)
                 (cond ((looking-at (concat (regexp-quote TeX-esc)
                                            "\\("
@@ -4033,7 +4052,7 @@ outer indentation in case of a commented line.  The symbols
                                            LaTeX-item-regexp
                                            "\\)"))
                        (- LaTeX-item-indent))
-                      ((looking-at "}")
+                      ((looking-at "}\\|\\]")
                        TeX-brace-indent-level)
                       (t 0)))))))
 

Reply via email to