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