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