[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [AUCTeX-devel] Extending `LaTeX-array-count-columns'
From: |
Arash Esbati |
Subject: |
Re: [AUCTeX-devel] Extending `LaTeX-array-count-columns' |
Date: |
Sat, 17 Dec 2016 09:25:54 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.1 |
Hi Ikumi,
Ikumi Keita <address@hidden> writes:
>> 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
> columns.
>
> \begin{tabular}[t]{*{3}{|cr}l}
> 1 & 2 & 3 & 4 & 5 & 6 & 7 \\
> &&&
> \end{tabular}
>
> (2) When *-operator is nested.
> \begin{tabular}[t]{*{3}{c*{2}{r}}l}
> 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 0 \\
> &&&
> \end{tabular}
Thanks for looking at this. Indeed, I totally ignored these cases! I
admit that I've never seen case (2), but it works and your code supports
it.
> 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."
> (save-excursion
> (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.
> (progn
> ;; pick up repetition number and count
> ;; how many columns are repeated.
> (re-search-forward
> "\\*[ \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.
> (skip-chars-forward
> 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]+}")
> (progn
> (re-search-forward
> "\\*[ \t\r\n%]*{[ \t\r\n%]*\\([0-9]+\\)[ \t\r\n%]*}" end t)
> to
> (if (eq (following-char) ?*)
> (progn
> (re-search-forward
> "\\*[ \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
> emacsen.)
> [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.
Thanks! I'm fine with your suggestions. I have already pushed some
patches to git for other styles. Once we have this function sorted out,
I would add some additional tests (see my other message to Mosè) and
update latex.el. Then `M-RET' should work for all tabular env's.
Best, Arash
- Re: [AUCTeX-devel] Extending `LaTeX-array-count-columns', (continued)
- Re: [AUCTeX-devel] Extending `LaTeX-array-count-columns', Mosè Giordano, 2016/12/15
- Re: [AUCTeX-devel] Extending `LaTeX-array-count-columns', Arash Esbati, 2016/12/16
- Re: [AUCTeX-devel] Extending `LaTeX-array-count-columns', Mosè Giordano, 2016/12/20
- Re: [AUCTeX-devel] Extending `LaTeX-array-count-columns', Arash Esbati, 2016/12/20
- Re: [AUCTeX-devel] Extending `LaTeX-array-count-columns', Mosè Giordano, 2016/12/20
- Re: [AUCTeX-devel] Extending `LaTeX-array-count-columns', Arash Esbati, 2016/12/23
Re: [AUCTeX-devel] Extending `LaTeX-array-count-columns', Ikumi Keita, 2016/12/14
Re: [AUCTeX-devel] Extending `LaTeX-array-count-columns', Ikumi Keita, 2016/12/16
- Re: [AUCTeX-devel] Extending `LaTeX-array-count-columns',
Arash Esbati <=