[Top][All Lists]

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

Re: [AUCTeX-devel] Extending `LaTeX-array-count-columns'

From: Ikumi Keita
Subject: Re: [AUCTeX-devel] Extending `LaTeX-array-count-columns'
Date: Sat, 17 Dec 2016 00:56:03 +0900

Hi Arash and all,

> 2 thing occurred to me when hitting `M-RET' in tabular environments:
> Current code in `LaTeX-array-count-columns' cannot handle multi column
> specs like `*{num}{spec}' and optional args to column specs S and s from
> siunitx.el.  I have a patch to handle both issues.  Can people please
> test this code against their tabular's and see if it does the right
> thing?

I found two cases that the code does not work as expected.
(1) When the second argument of *-operator contains more than 1

  1 & 2 & 3 & 4 & 5 & 6 & 7 \\

(2) When *-operator is nested.
  1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 0 \\

Apparently recursive call of LaTeX-array-count-columns is needed do the
task.  How about this one?

(defun LaTeX-array-count-columns (start end)
  "Count number of ampersands to be inserted.
The columns are specified by the letters found in the string
`LaTeX-array-column-letters' and the number of those letters within the
text between START and END is basically considered to be the number of
columns.  The arguments surrounded between braces such as p{30pt} do not
interfere the count of columns.

Return one less number than the columns, or nil on failing to count the
right number."
    (let (p (cols 0))
      (goto-char start)
      (while (< (setq p (point)) end)

        ;; The below block accounts for one unit of move for
        ;; one column.
        (setq cols (+ cols
                      ;; treat *-operator specially.
                      (if (eq (following-char) ?*)
                          ;; *-operator is there.
                            ;; pick up repetition number and count
                            ;; how many columns are repeated.
                             "\\*[ \t\r\n%]*{[ \t\r\n%]*\\([0-9]+\\)[ 
\t\r\n%]*}" end)
                            (let ((n (string-to-number
                                        (match-string-no-properties 1)))
                                  ;; get start and end of repeated spec.
                                  (s (progn (down-list 1) (point)))
                                  (e (progn (up-list 1) (1- (point)))))
                              (* n (1+ (LaTeX-array-count-columns s e)))))
                        ;; not *-operator.
                         LaTeX-array-column-letters end))))
        (skip-chars-forward (concat
                             "^" LaTeX-array-column-letters "*"
                             TeX-grop LaTeX-optop) end)
        (when (or (eq (following-char) ?\{)
                  (eq (following-char) ?\[))
          (forward-list 1))

        ;; Not sure whether this is really necessary or not, but
        ;; prepare for possible infinite loop anyway.
        (when (eq p (point))
          (setq cols nil)
          (goto-char end)))
      ;; The number of ampersands is one less than column.
      (if cols (1- cols)))))

The above version also changes the construct
      (if (looking-at-p "\\*[ \t\r\n%]*{[ \t\r\n%0-9]+}")
             "\\*[ \t\r\n%]*{[ \t\r\n%]*\\([0-9]+\\)[ \t\r\n%]*}" end t)
      (if (eq (following-char) ?*)
             "\\*[ \t\r\n%]*{[ \t\r\n%]*\\([0-9]+\\)[ \t\r\n%]*}" end)
based on the following two reasons.
[a] Simple check of `the next char is * or not?' seems sufficient for
the purpose.  (In addition, looking-at-p is not available in older
[b] The noerror argument of re-search-forward is dropped.  If that
re-search-forward fails, the string-to-number on the next line does not
make sense.  Even if the error is raised here, it is captured by
ignore-erros in LaTeX-insert-ampersands so the users are not bothered by
the error.

Ikumi Keita

reply via email to

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