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