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