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