Synch with Gnus.
[elisp/gnus.git-] / lisp / ptexinfmt.el
1 ;;; ptexinfmt.el -- portable Texinfo formatter.
2
3 ;; Copyright (C) 1985, 1986, 1988, 1990, 1991, 1992, 1993, 
4 ;;               1994, 1995, 1996, 1997 Free Software Foundation, Inc.
5 ;; Copyright (C) 1999 Yoshiki Hayashi <yoshiki@xemacs.org>
6 ;; Copyright (C) 2000 TAKAHASHI Kaoru <kaoru@kaisei.org>
7
8 ;; Author: TAKAHASHI Kaoru <kaoru@kaisei.org>
9 ;;      Yoshiki Hayashi <yoshiki@xemacs.org>
10 ;; Maintainer: TAKAHASHI Kaoru <kaoru@kaisei.org>
11 ;; Created: 7 Jul 2000
12 ;; Keywords: maint, tex, docs, emulation, compatibility
13
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License as
16 ;; published by the Free Software Foundation; either version 2, or (at
17 ;; your option) any later version.
18
19 ;; This program is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 ;; General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Commentary:
30
31 ;; Original code: Yoshiki Hayashi <yoshiki@xemacs.org>
32 ;;      makeinfo.el (gnujdoc project)
33
34 ;; Support texinfmt.el 2.32 or later.
35
36 ;;; Code:
37 (require 'texinfmt)
38 (require 'poe)
39 (require 'broken)
40
41 (provide 'ptexinfmt)
42
43 ;;; Broken
44 ;; @var
45 (broken-facility texinfo-format-var
46   "Don't perse @COMMAND included @var"
47   (condition-case nil
48       (with-temp-buffer
49         (let (texinfo-enclosure-list texinfo-alias-list)
50           (texinfo-mode)
51           (insert "@var{@asis{foo}}\n")
52           (texinfo-format-expand-region (point-min) (point-max))
53           t))
54     (error nil))
55   'no-notice)
56
57 ;; @xref
58 (broken-facility texinfo-format-xref
59   "Can't format 1st argument empty @xref."
60   (condition-case nil
61       (with-temp-buffer
62         (let (texinfo-enclosure-list texinfo-alias-list)
63           (texinfo-mode)
64           (insert "@xref{, xref, , file}\n")
65           (texinfo-format-expand-region (point-min) (point-max))
66           t))
67     (error nil))
68   'no-notice)
69
70 ;; @uref
71 (broken-facility texinfo-format-uref
72   "Parse 2 times @uref argument."
73   (condition-case nil
74       (with-temp-buffer
75         (let (texinfo-enclosure-list texinfo-alias-list)
76           (texinfo-mode)
77           (insert "@uref{mailto:foo@@bar.com}\n")
78           (texinfo-format-expand-region (point-min) (point-max))
79           t))
80     (error nil))
81   'no-notice)
82
83 ;;; Obsolete
84 ;; Removed Texinfo 3.8
85 (put 'overfullrule 'texinfo-format 'texinfo-discard-line)
86 (put 'smallbreak 'texinfo-format 'texinfo-discard-line)
87 (put 'medbreak 'texinfo-format 'texinfo-discard-line)
88 (put 'bigbreak 'texinfo-format 'texinfo-discard-line)
89 ;; Removed Texinfo 3.9
90 (put 'setchapterstyle 'texinfo-format 'texinfo-discard-line-with-args)
91
92 ;;; Hardcopy and HTML (discard)
93 ;; I18N
94 (put 'documentlanguage 'texinfo-format 'texinfo-discard-line-with-args)
95 (put 'documentencoding 'texinfo-format 'texinfo-discard-line-with-args)
96
97 ;; size
98 (put 'smallbook 'texinfo-format 'texinfo-discard-line)
99 (put 'afourpaper 'texinfo-format 'texinfo-discard-line)
100 (put 'afourlatex 'texinfo-format 'texinfo-discard-line)
101 (put 'afourwide 'texinfo-format 'texinfo-discard-line)
102 (put 'pagesizes 'texinfo-format 'texinfo-discard-line-with-args)
103
104 ;; style
105 (put 'setchapternewpage 'texinfo-format 'texinfo-discard-line-with-args)
106 (put 'kbdinputstyle 'texinfo-format 'texinfo-discard-line-with-args)
107
108 ;; flags
109 (put 'setcontentsaftertitlepage 'texinfo-format 'texinfo-discard-line)
110 (put 'setshortcontentsaftertitlepage 'texinfo-format 'texinfo-discard-line)
111 (put 'novalidate 'texinfo-format 'texinfo-discard-line-with-args)
112
113 ;; head & foot
114 (put 'headings 'texinfo-format 'texinfo-discard-line-with-args)
115 (put 'evenfooting 'texinfo-format 'texinfo-discard-line-with-args)
116 (put 'evenheading 'texinfo-format 'texinfo-discard-line-with-args)
117 (put 'oddfooting 'texinfo-format 'texinfo-discard-line-with-args)
118 (put 'oddheading 'texinfo-format 'texinfo-discard-line-with-args)
119 (put 'everyfooting 'texinfo-format 'texinfo-discard-line-with-args)
120 (put 'everyheading 'texinfo-format 'texinfo-discard-line-with-args)
121
122 ;; misc
123 (put 'page 'texinfo-format 'texinfo-discard-line)
124 (put 'hyphenation 'texinfo-format 'texinfo-discard-command-and-arg)
125
126
127 \f
128 ;;; Directory File
129 ;; @direcategory
130 (put 'dircategory 'texinfo-format 'texinfo-format-dircategory)
131 (defun-maybe texinfo-format-dircategory ()
132   (let ((str (texinfo-parse-arg-discard)))
133     (delete-region (point)
134                    (progn
135                      (skip-chars-forward " ")
136                      (point)))
137     (insert "INFO-DIR-SECTION " str "\n")))
138
139 ;; @direntry
140 (put 'direntry 'texinfo-format 'texinfo-format-direntry)
141 (defun-maybe texinfo-format-direntry ()
142   (texinfo-push-stack 'direntry nil)
143   (texinfo-discard-line)
144   (insert "START-INFO-DIR-ENTRY\n\n"))
145
146 (put 'direntry 'texinfo-end 'texinfo-end-direntry)
147 (defun-maybe texinfo-end-direntry ()
148   (texinfo-discard-command)
149   (insert "END-INFO-DIR-ENTRY\n\n")
150   (texinfo-pop-stack 'direntry))
151
152
153 ;;; Block Enclosing and Conditional
154 ;; @detailmenu ... @end detailmenu
155 (put 'detailmenu 'texinfo-format 'texinfo-discard-line)
156 (put 'detailmenu 'texinfo-end 'texinfo-discard-command)
157
158 ;; @smalldisplay ... @end smalldisplay
159 (put 'smalldisplay 'texinfo-format 'texinfo-format-example)
160 (put 'smalldisplay 'texinfo-end 'texinfo-end-example)
161
162 ;; @smallformat ... @end smallformat
163 (put 'smallformat 'texinfo-format 'texinfo-format-flushleft)
164 (put 'smallformat 'texinfo-end 'texinfo-end-flushleft)
165
166 ;; @ifnottex ... @end ifnottex
167 (put 'ifnottex 'texinfo-format 'texinfo-discard-line)
168 (put 'ifnottex 'texinfo-end 'texinfo-discard-command)
169
170 ;; @ifnothtml ... @end ifnothtml
171 (put 'ifnothtml 'texinfo-format 'texinfo-discard-line)
172 (put 'ifnothtml 'texinfo-end 'texinfo-discard-command)
173
174 ;; @ifnotinfo ... @end ifnotinfo
175 (put 'ifnotinfo 'texinfo-format 'texinfo-format-ifnotinfo)
176 (put 'endifnotinfo 'texinfo-format 'texinfo-discard-line)
177 (defun-maybe texinfo-format-ifnotinfo ()
178   (delete-region texinfo-command-start
179                  (progn (re-search-forward "@end ifnotinfo[ \t]*\n")
180                         (point))))
181
182 ;; @html ... @end html
183 (put 'html 'texinfo-format 'texinfo-format-html)
184 (put 'endhtml 'texinfo-format 'texinfo-discard-line)
185 (defun-maybe texinfo-format-html ()
186   (delete-region texinfo-command-start
187                  (progn (re-search-forward "@end html[ \t]*\n")
188                         (point))))
189
190
191 \f
192 ;;; Marking
193 ;; @url, @env, @command
194 (put 'url 'texinfo-format 'texinfo-format-code)
195 (put 'env 'texinfo-format 'texinfo-format-code)
196 (put 'command 'texinfo-format 'texinfo-format-code)
197
198 ;; @acronym
199 (put 'acronym 'texinfo-format 'texinfo-format-var)
200
201 (when-broken texinfo-format-var
202   (fmakunbound 'texinfo-format-var))
203 (defun-maybe texinfo-format-var ()
204   (let ((arg (texinfo-parse-expanded-arg)))
205     (texinfo-discard-command)
206     (insert (upcase arg))))
207
208 ;; @key
209 (put 'key 'texinfo-format 'texinfo-format-key)
210 (defun-maybe texinfo-format-key ()
211   (insert (texinfo-parse-arg-discard))
212   (goto-char texinfo-command-start))
213
214 ;; @email{EMAIL-ADDRESS[, DISPLAYED-TEXT]}
215 (put 'email 'texinfo-format 'texinfo-format-email)
216 (defun-maybe texinfo-format-email ()
217   "Format EMAIL-ADDRESS and optional DISPLAYED-TXT.
218 Insert < ... > around EMAIL-ADDRESS."
219   (let ((args (texinfo-format-parse-args)))
220   (texinfo-discard-command)
221     ;; if displayed-text
222     (if (nth 1 args)
223         (insert (nth 1 args) " <" (nth 0 args) ">")
224       (insert "<" (nth 0 args) ">"))))
225
226 ;; @option
227 (put 'option 'texinfo-format 'texinfo-format-option)
228 (defun texinfo-format-option ()
229   "Insert ` ... ' around arg unless inside a table; in that case, no quotes."
230   ;; `looking-at-backward' not available in v. 18.57, 20.2
231   ;; searched-for character is a control-H
232   (if (not (search-backward "\010"
233                             (save-excursion (beginning-of-line) (point))
234                             t))
235       (insert "`" (texinfo-parse-arg-discard) "'")
236     (insert (texinfo-parse-arg-discard)))
237   (goto-char texinfo-command-start))
238
239
240 \f
241 ;;; Accents and Special characters
242 ;; @pounds{}    ==>     #       Pounds Sterling
243 (put 'pounds 'texinfo-format 'texinfo-format-pounds)
244 (defun-maybe texinfo-format-pounds ()
245   (texinfo-parse-arg-discard)
246   (insert "#"))
247
248 ;; @OE{}        ==>     OE      French-OE-ligature
249 (put 'OE 'texinfo-format 'texinfo-format-French-OE-ligature)
250 (defun-maybe texinfo-format-French-OE-ligature ()
251   (insert "OE" (texinfo-parse-arg-discard))
252   (goto-char texinfo-command-start))
253
254 ;; @oe{}        ==>     oe
255 (put 'oe 'texinfo-format 'texinfo-format-French-oe-ligature)
256 (defun-maybe texinfo-format-French-oe-ligature () ; lower case
257   (insert "oe" (texinfo-parse-arg-discard))
258   (goto-char texinfo-command-start))
259
260 ;; @AA{}        ==>     AA      Scandinavian-A-with-circle
261 (put 'AA 'texinfo-format 'texinfo-format-Scandinavian-A-with-circle)
262 (defun-maybe texinfo-format-Scandinavian-A-with-circle ()
263   (insert "AA" (texinfo-parse-arg-discard))
264   (goto-char texinfo-command-start))
265
266 ;; @aa{}        ==>     aa
267 (put 'aa 'texinfo-format 'texinfo-format-Scandinavian-a-with-circle)
268 (defun-maybe texinfo-format-Scandinavian-a-with-circle () ; lower case
269   (insert "aa" (texinfo-parse-arg-discard))
270   (goto-char texinfo-command-start))
271
272 ;; @AE{}        ==>     AE      Latin-Scandinavian-AE
273 (put 'AE 'texinfo-format 'texinfo-format-Latin-Scandinavian-AE)
274 (defun-maybe texinfo-format-Latin-Scandinavian-AE ()
275   (insert "AE" (texinfo-parse-arg-discard))
276   (goto-char texinfo-command-start))
277
278 ;; @ae{}        ==>     ae
279 (put 'ae 'texinfo-format 'texinfo-format-Latin-Scandinavian-ae)
280 (defun-maybe texinfo-format-Latin-Scandinavian-ae () ; lower case
281   (insert "ae" (texinfo-parse-arg-discard))
282   (goto-char texinfo-command-start))
283
284 ;; @ss{}        ==>     ss      German-sharp-S
285 (put 'ss 'texinfo-format 'texinfo-format-German-sharp-S)
286 (defun-maybe texinfo-format-German-sharp-S ()
287   (insert "ss" (texinfo-parse-arg-discard))
288   (goto-char texinfo-command-start))
289
290 ;; @questiondown{}      ==>     ?       upside-down-question-mark
291 (put 'questiondown 'texinfo-format 'texinfo-format-upside-down-question-mark)
292 (defun-maybe texinfo-format-upside-down-question-mark ()
293   (insert "?" (texinfo-parse-arg-discard))
294   (goto-char texinfo-command-start))
295
296 ;; @exclamdown{}        ==>     !       upside-down-exclamation-mark
297 (put 'exclamdown 'texinfo-format 'texinfo-format-upside-down-exclamation-mark)
298 (defun-maybe texinfo-format-upside-down-exclamation-mark ()
299   (insert "!" (texinfo-parse-arg-discard))
300   (goto-char texinfo-command-start))
301
302 ;; @L{}         ==>     L/      Polish suppressed-L (Lslash)
303 (put 'L 'texinfo-format 'texinfo-format-Polish-suppressed-L)
304 (defun-maybe texinfo-format-Polish-suppressed-L ()
305   (insert (texinfo-parse-arg-discard) "/L")
306   (goto-char texinfo-command-start))
307
308 ;; @l{}         ==>     l/      Polish suppressed-L (Lslash) (lower case)
309 (put 'l 'texinfo-format 'texinfo-format-Polish-suppressed-l-lower-case)
310 (defun-maybe texinfo-format-Polish-suppressed-l-lower-case ()
311   (insert (texinfo-parse-arg-discard) "/l")
312   (goto-char texinfo-command-start))
313
314 ;; @O{}         ==>     O/      Scandinavian O-with-slash
315 (put 'O 'texinfo-format 'texinfo-format-Scandinavian-O-with-slash)
316 (defun-maybe texinfo-format-Scandinavian-O-with-slash ()
317   (insert (texinfo-parse-arg-discard) "O/")
318   (goto-char texinfo-command-start))
319
320 ;; @o{}         ==>     o/      Scandinavian O-with-slash (lower case)
321 (put 'o 'texinfo-format 'texinfo-format-Scandinavian-o-with-slash-lower-case)
322 (defun-maybe texinfo-format-Scandinavian-o-with-slash-lower-case ()
323   (insert (texinfo-parse-arg-discard) "o/")
324   (goto-char texinfo-command-start))
325
326 ;; @,{c}        ==>     c,      cedilla accent
327 (put ', 'texinfo-format 'texinfo-format-cedilla-accent)
328 (defun-maybe texinfo-format-cedilla-accent ()
329   (insert (texinfo-parse-arg-discard) ",")
330   (goto-char texinfo-command-start))
331
332
333 ;; @dotaccent{o}        ==>     .o      overdot-accent
334 (put 'dotaccent 'texinfo-format 'texinfo-format-overdot-accent)
335 (defun-maybe texinfo-format-overdot-accent ()
336   (insert "." (texinfo-parse-arg-discard))
337   (goto-char texinfo-command-start))
338
339 ;; @ubaraccent{o}       ==>     _o      underbar-accent
340 (put 'ubaraccent 'texinfo-format 'texinfo-format-underbar-accent)
341 (defun-maybe texinfo-format-underbar-accent ()
342   (insert "_" (texinfo-parse-arg-discard))
343   (goto-char texinfo-command-start))
344
345 ;; @udotaccent{o}       ==>     o-.     underdot-accent
346 (put 'udotaccent 'texinfo-format 'texinfo-format-underdot-accent)
347 (defun-maybe texinfo-format-underdot-accent ()
348   (insert (texinfo-parse-arg-discard) "-.")
349   (goto-char texinfo-command-start))
350
351 ;; @H{o}        ==>     ""o     long Hungarian umlaut
352 (put 'H 'texinfo-format 'texinfo-format-long-Hungarian-umlaut)
353 (defun-maybe texinfo-format-long-Hungarian-umlaut ()
354   (insert "\"\"" (texinfo-parse-arg-discard))
355   (goto-char texinfo-command-start))
356
357 ;; @ringaccent{o}       ==>     *o      ring accent
358 (put 'ringaccent 'texinfo-format 'texinfo-format-ring-accent)
359 (defun-maybe texinfo-format-ring-accent ()
360   (insert "*" (texinfo-parse-arg-discard))
361   (goto-char texinfo-command-start))
362
363 ;; @tieaccent{oo}       ==>     [oo     tie after accent
364 (put 'tieaccent 'texinfo-format 'texinfo-format-tie-after-accent)
365 (defun-maybe texinfo-format-tie-after-accent ()
366   (insert "[" (texinfo-parse-arg-discard))
367   (goto-char texinfo-command-start))
368
369 ;; @u{o}        ==>     (o      breve accent
370 (put 'u 'texinfo-format 'texinfo-format-breve-accent)
371 (defun-maybe texinfo-format-breve-accent ()
372   (insert "(" (texinfo-parse-arg-discard))
373   (goto-char texinfo-command-start))
374
375 ;; @v{o}        ==>     <o      hacek accent
376 (put 'v 'texinfo-format 'texinfo-format-hacek-accent)
377 (defun-maybe texinfo-format-hacek-accent ()
378   (insert "<" (texinfo-parse-arg-discard))
379   (goto-char texinfo-command-start))
380
381 ;; @dotless{i}  ==>     i       dotless i and dotless j
382 (put 'dotless 'texinfo-format 'texinfo-format-dotless)
383 (defun-maybe texinfo-format-dotless ()
384   (insert (texinfo-parse-arg-discard))
385   (goto-char texinfo-command-start))
386
387
388 \f
389 ;;; Cross References
390 ;; @ref, @xref
391 (put 'ref 'texinfo-format 'texinfo-format-xref)
392
393 (when-broken texinfo-format-xref
394   (fmakunbound 'texinfo-format-xref))
395 (defun-maybe texinfo-format-xref ()
396   (let ((args (texinfo-format-parse-args)))
397     (texinfo-discard-command)
398     (insert "*Note ")
399     (let ((fname (or (nth 1 args) (nth 2 args))))
400       (if (null (or fname (nth 3 args)))
401           (insert (nth 0 args) "::")
402         (insert (or fname (nth 0 args)) ": ")
403         (if (nth 3 args)
404             (insert "(" (nth 3 args) ")"))
405         (unless (null (nth 0 args))
406           (insert (nth 0 args)))))))
407
408 ;; @uref
409 (put 'uref 'texinfo-format 'texinfo-format-uref)
410 (when-broken texinfo-format-uref
411   (fmakunbound 'texinfo-format-uref))
412 (defun-maybe texinfo-format-uref ()
413   "Format URL and optional URL-TITLE.
414 Insert ` ... ' around URL if no URL-TITLE argument;
415 otherwise, insert URL-TITLE followed by URL in parentheses."
416   (let ((args (texinfo-format-parse-args)))
417     (texinfo-discard-command)
418     ;; if url-title 
419     (if (nth 1 args)
420         (insert  (nth 1 args) " (" (nth 0 args) ")")
421       (insert "`" (nth 0 args) "'"))))
422
423
424 \f
425 ;;; New command definition
426 ;; @alias NEW=EXISTING
427 (put 'alias 'texinfo-format 'texinfo-alias)
428 (defun-maybe texinfo-alias ()
429   (let ((start (1- (point)))
430         args)
431     (skip-chars-forward " ")
432     (save-excursion (end-of-line) (setq texinfo-command-end (point)))
433     (if (not (looking-at "\\([^=]+\\)=\\(.*\\)"))
434         (error "Invalid alias command")
435       (setq texinfo-alias-list
436             (cons
437              (cons
438               (buffer-substring (match-beginning 1) (match-end 1))
439               (buffer-substring (match-beginning 2) (match-end 2)))
440              texinfo-alias-list))
441       (texinfo-discard-command))))
442
443 ;; @definfoenclose NEWCMD, BEFORE, AFTER
444
445
446 \f
447 ;;; Special
448 ;; @image{FILENAME, [WIDTH], [HEIGHT]}
449 (put 'image 'texinfo-format 'texinfo-format-image)
450 (defun-maybe texinfo-format-image ()
451   (let ((args (texinfo-format-parse-args)) ; parse FILENAME?
452         filename)
453     (when (null (nth 0 args))
454       (error "Invalid image command"))
455     (texinfo-discard-command)
456     ;; makeinfo uses FILENAME.txt
457     (setq filename (format "%s.txt" (nth 0 args)))
458     (message "Reading included file: %s" filename)
459     ;; verbatim for Info output
460     (goto-char (+ (point) (cadr (insert-file-contents filename))))
461     (message "Reading included file: %s...done" filename)))
462
463
464 ;; @exampleindent
465
466
467 \f
468 ;;; @multitable ... @end multitable
469 (defvar-maybe texinfo-extra-inter-column-width 0
470   "*Number of extra spaces between entries (columns) in @multitable.")
471
472 (defvar-maybe texinfo-multitable-buffer-name "*multitable-temporary-buffer*")
473 (defvar-maybe texinfo-multitable-rectangle-name "texinfo-multitable-temp-")
474
475 ;; These commands are defined in texinfo.tex for printed output.
476 (put 'multitableparskip 'texinfo-format 'texinfo-discard-line-with-args)
477 (put 'multitableparindent 'texinfo-format 'texinfo-discard-line-with-args)
478 (put 'multitablecolmargin 'texinfo-format 'texinfo-discard-line-with-args)
479 (put 'multitablelinespace 'texinfo-format 'texinfo-discard-line-with-args)
480
481 (put 'multitable 'texinfo-format 'texinfo-multitable)
482
483 (defun-maybe texinfo-multitable ()
484   "Produce multi-column tables."
485
486 ;; This function pushes information onto the `texinfo-stack'.
487 ;; A stack element consists of:
488 ;;   - type-of-command, i.e., multitable
489 ;;   - the information about column widths, and
490 ;;   - the position of texinfo-command-start.
491 ;; e.g., ('multitable (1 2 3 4) 123)
492 ;; The command line is then deleted.
493   (texinfo-push-stack
494    'multitable
495    ;; push width information on stack
496    (texinfo-multitable-widths))
497   (texinfo-discard-line-with-args))
498
499 (put 'multitable 'texinfo-end 'texinfo-end-multitable)
500 (defun-maybe texinfo-end-multitable ()
501   "Discard the @end multitable line and pop the stack of multitable."
502   (texinfo-discard-command)
503   (texinfo-pop-stack 'multitable))
504
505 (defun-maybe texinfo-multitable-widths ()
506   "Return list of widths of each column in a multi-column table."
507   (let (texinfo-multitable-width-list)
508     ;; Fractions format:
509     ;;  @multitable @columnfractions .25 .3 .45
510     ;;
511     ;; Template format:
512     ;;  @multitable {Column 1 template} {Column 2} {Column 3 example}
513     ;; Place point before first argument
514     (skip-chars-forward " \t")
515     (cond 
516      ;; Check for common misspelling
517      ((looking-at "@columnfraction ")
518       (error "In @multitable, @columnfractions misspelled"))
519      ;; Case 1: @columnfractions .25 .3 .45
520      ((looking-at "@columnfractions")
521       (forward-word 1)
522       (while (not (eolp))
523         (setq texinfo-multitable-width-list
524               (cons
525                (truncate
526                 (1-
527                  (* fill-column (read (get-buffer (current-buffer))))))
528                texinfo-multitable-width-list))))
529      ;;
530      ;; Case 2: {Column 1 template} {Column 2} {Column 3 example}
531      ((looking-at "{")
532       (let ((start-of-templates (point)))
533         (while (not (eolp))
534           (skip-chars-forward " \t")
535           (let* ((start-of-template (1+ (point)))
536                  (end-of-template
537                  ;; forward-sexp works with braces in Texinfo mode
538                   (progn (forward-sexp 1) (1- (point)))))
539             (setq texinfo-multitable-width-list
540                   (cons (- end-of-template start-of-template)
541                         texinfo-multitable-width-list))
542             ;; Remove carriage return from within a template, if any.
543             ;; This helps those those who want to use more than
544             ;; one line's worth of words in @multitable line.
545             (narrow-to-region start-of-template end-of-template)
546             (goto-char (point-min))
547             (while (search-forward "
548 " nil t)
549               (delete-char -1))
550             (goto-char (point-max))
551             (widen)
552             (forward-char 1)))))
553      ;;
554      ;; Case 3: Trouble
555      (t
556       (error
557        "You probably need to specify column widths for @multitable correctly.")))
558     ;; Check whether columns fit on page.
559     (let ((desired-columns
560            (+
561             ;; between column spaces
562             (length texinfo-multitable-width-list)
563             ;; additional between column spaces, if any
564             texinfo-extra-inter-column-width
565             ;; sum of spaces for each entry
566             (apply '+ texinfo-multitable-width-list))))
567       (if (> desired-columns fill-column)
568           (error
569            (format
570             "Multi-column table width, %d chars, is greater than page width, %d chars."
571             desired-columns fill-column))))
572     texinfo-multitable-width-list))
573
574 ;; @item  A1  @tab  A2  @tab  A3
575 (defun-maybe texinfo-multitable-extract-row ()
576   "Return multitable row, as a string.
577 End of row is beginning of next @item or beginning of @end.
578 Cells within rows are separated by @tab."
579   (skip-chars-forward " \t")
580   (let* ((start (point))
581          (end (progn
582                 (re-search-forward "@item\\|@end")
583                 (match-beginning 0)))
584          (row (progn (goto-char end)
585                      (skip-chars-backward " ")
586                      ;; remove whitespace at end of argument
587                      (delete-region (point) end)
588                      (buffer-substring start (point)))))
589     (delete-region texinfo-command-start end)
590     row))
591
592 (put 'multitable 'texinfo-item 'texinfo-multitable-item)
593 (defun-maybe texinfo-multitable-item ()
594   "Format a row within a multicolumn table.
595 Cells in row are separated by @tab.
596 Widths of cells are specified by the arguments in the @multitable line.
597 All cells are made to be the same height.
598 This command is executed when texinfmt sees @item inside @multitable."
599   (let ((original-buffer (current-buffer))
600         (table-widths (reverse (car (cdr (car texinfo-stack)))))
601         (existing-fill-column fill-column)
602         start
603         end
604         (table-column       0)
605         (table-entry-height 0)
606         ;; unformatted row looks like:  A1  @tab  A2  @tab  A3
607         ;; extract-row command deletes the source line in the table.
608         (unformated-row (texinfo-multitable-extract-row)))
609     ;; Use a temporary buffer
610     (set-buffer (get-buffer-create texinfo-multitable-buffer-name))
611     (delete-region (point-min) (point-max))
612     (insert unformated-row)
613     (goto-char (point-min))
614 ;; 1. Check for correct number of @tab in line.
615     (let ((tab-number 1))                       ; one @tab between two columns
616       (while (search-forward "@tab" nil t)
617         (setq tab-number (1+ tab-number)))
618       (if (/= tab-number (length table-widths))
619           (error "Wrong number of @tab's in a @multitable row.")))
620     (goto-char (point-min))
621 ;; 2. Format each cell, and copy to a rectangle
622     ;; buffer looks like this:    A1  @tab  A2  @tab  A3
623     ;; Cell #1: format up to @tab
624     ;; Cell #2: format up to @tab
625     ;; Cell #3: format up to eob
626     (while (not (eobp))
627       (setq start (point))
628       (setq end (save-excursion
629                   (if (search-forward "@tab" nil 'move)
630                       ;; Delete the @tab command, including the @-sign
631                       (delete-region
632                        (point)
633                        (progn (forward-word -1) (1- (point)))))
634                   (point)))
635       ;; Set fill-column *wider* than needed to produce inter-column space
636       (setq fill-column (+ 1
637                            texinfo-extra-inter-column-width
638                            (nth table-column table-widths)))
639       (narrow-to-region start end)
640       ;; Remove whitespace before and after entry.
641       (skip-chars-forward " ")
642       (delete-region (point) (save-excursion (beginning-of-line) (point)))
643       (goto-char (point-max))
644       (skip-chars-backward " ")
645       (delete-region (point) (save-excursion (end-of-line) (point)))
646       ;; Temorarily set texinfo-stack to nil so texinfo-format-scan
647       ;; does not see an unterminated @multitable.
648       (let (texinfo-stack)                      ; nil
649         (texinfo-format-scan))
650       (let (fill-prefix)                        ; no fill prefix
651         (fill-region (point-min) (point-max)))
652       (setq table-entry-height
653             (max table-entry-height (count-lines (point-min) (point-max))))
654 ;; 3. Move point to end of bottom line, and pad that line to fill column.
655       (goto-char (point-min))
656       (forward-line (1- table-entry-height))
657       (let* ((beg (point))                      ; beginning of line
658              ;; add one more space for inter-column spacing
659              (needed-whitespace
660               (1+
661                (- fill-column
662                   (-
663                    (progn (end-of-line) (point)) ; end of existing line
664                    beg)))))
665         (insert (make-string
666                  (if (> needed-whitespace 0) needed-whitespace 1)
667                  ? )))
668       ;; now, put formatted cell into a rectangle
669       (set (intern (concat texinfo-multitable-rectangle-name
670                            (int-to-string table-column)))
671            (extract-rectangle (point-min) (point)))
672       (delete-region (point-min) (point))
673       (goto-char (point-max))
674       (setq table-column (1+ table-column))
675       (widen))
676 ;; 4. Add extra lines to rectangles so all are of same height
677     (let ((total-number-of-columns table-column)
678           (column-number 0)
679           here)
680       (while (> table-column 0)
681         (let ((this-rectangle (int-to-string table-column)))
682           (while (< (length this-rectangle) table-entry-height)
683             (setq this-rectangle (append this-rectangle '("")))))
684         (setq table-column (1- table-column)))
685 ;; 5. Insert formatted rectangles in original buffer
686       (switch-to-buffer original-buffer)
687       (open-line table-entry-height)
688       (while (< column-number total-number-of-columns)
689         (setq here (point))
690         (insert-rectangle
691          (eval (intern
692                 (concat texinfo-multitable-rectangle-name
693                         (int-to-string column-number)))))
694         (goto-char here)
695         (end-of-line)
696         (setq column-number (1+ column-number))))
697     (kill-buffer texinfo-multitable-buffer-name)
698     (setq fill-column existing-fill-column)))
699
700
701 ;;; ptexinfmt.el ends here