Remove obsolete commands.
[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, 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
547 ;; @exampleindent
548
549
550 \f
551 ;;; @multitable ... @end multitable
552 (defvar-maybe texinfo-extra-inter-column-width 0
553   "*Number of extra spaces between entries (columns) in @multitable.")
554
555 (defvar-maybe texinfo-multitable-buffer-name "*multitable-temporary-buffer*")
556 (defvar-maybe texinfo-multitable-rectangle-name "texinfo-multitable-temp-")
557
558 ;; These commands are defined in texinfo.tex for printed output.
559 (put 'multitableparskip 'texinfo-format 'texinfo-discard-line-with-args)
560 (put 'multitableparindent 'texinfo-format 'texinfo-discard-line-with-args)
561 (put 'multitablecolmargin 'texinfo-format 'texinfo-discard-line-with-args)
562 (put 'multitablelinespace 'texinfo-format 'texinfo-discard-line-with-args)
563
564 (put 'multitable 'texinfo-format 'texinfo-multitable)
565
566 (defun-maybe texinfo-multitable ()
567   "Produce multi-column tables."
568
569 ;; This function pushes information onto the `texinfo-stack'.
570 ;; A stack element consists of:
571 ;;   - type-of-command, i.e., multitable
572 ;;   - the information about column widths, and
573 ;;   - the position of texinfo-command-start.
574 ;; e.g., ('multitable (1 2 3 4) 123)
575 ;; The command line is then deleted.
576   (texinfo-push-stack
577    'multitable
578    ;; push width information on stack
579    (texinfo-multitable-widths))
580   (texinfo-discard-line-with-args))
581
582 (put 'multitable 'texinfo-end 'texinfo-end-multitable)
583 (defun-maybe texinfo-end-multitable ()
584   "Discard the @end multitable line and pop the stack of multitable."
585   (texinfo-discard-command)
586   (texinfo-pop-stack 'multitable))
587
588 (when-broken texinfo-multitable-widths
589   (fmakunbound 'texinfo-multitable-widths))
590
591 (defun-maybe texinfo-multitable-widths ()
592   "Return list of widths of each column in a multi-column table."
593   (let (texinfo-multitable-width-list)
594     ;; Fractions format:
595     ;;  @multitable @columnfractions .25 .3 .45
596     ;;
597     ;; Template format:
598     ;;  @multitable {Column 1 template} {Column 2} {Column 3 example}
599     ;; Place point before first argument
600     (skip-chars-forward " \t")
601     (cond
602      ;; Check for common misspelling
603      ((looking-at "@columnfraction ")
604       (error "In @multitable, @columnfractions misspelled"))
605      ;; Case 1: @columnfractions .25 .3 .45
606      ((looking-at "@columnfractions")
607       (forward-word 1)
608       (while (not (eolp))
609         (setq texinfo-multitable-width-list
610               (cons
611                (truncate
612                 (1-
613                  (* fill-column (read (get-buffer (current-buffer))))))
614                texinfo-multitable-width-list))))
615      ;;
616      ;; Case 2: {Column 1 template} {Column 2} {Column 3 example}
617      ((looking-at "{")
618       (let ((start-of-templates (point)))
619         (while (not (eolp))
620           (skip-chars-forward " \t")
621           (let* ((start-of-template (1+ (point)))
622                  (end-of-template
623                  ;; forward-sexp works with braces in Texinfo mode
624                   (progn (forward-sexp 1) (1- (point)))))
625             (setq texinfo-multitable-width-list
626                   (cons (- (progn (goto-char end-of-template) (current-column))
627                            (progn (goto-char start-of-template) (current-column)))
628                         texinfo-multitable-width-list))
629             ;; Remove carriage return from within a template, if any.
630             ;; This helps those those who want to use more than
631             ;; one line's worth of words in @multitable line.
632             (narrow-to-region start-of-template end-of-template)
633             (goto-char (point-min))
634             (while (search-forward "
635 " nil t)
636               (delete-char -1))
637             (goto-char (point-max))
638             (widen)
639             (forward-char 1)))))
640      ;;
641      ;; Case 3: Trouble
642      (t
643       (error
644        "You probably need to specify column widths for @multitable correctly")))
645     ;; Check whether columns fit on page.
646     (let ((desired-columns
647            (+
648             ;; between column spaces
649             (length texinfo-multitable-width-list)
650             ;; additional between column spaces, if any
651             texinfo-extra-inter-column-width
652             ;; sum of spaces for each entry
653             (apply '+ texinfo-multitable-width-list))))
654       (if (> desired-columns fill-column)
655           (error
656            (format
657             "Multi-column table width, %d chars, is greater than page width, %d chars."
658             desired-columns fill-column))))
659     texinfo-multitable-width-list))
660
661 ;; @item  A1  @tab  A2  @tab  A3
662 (defun-maybe texinfo-multitable-extract-row ()
663   "Return multitable row, as a string.
664 End of row is beginning of next @item or beginning of @end.
665 Cells within rows are separated by @tab."
666   (skip-chars-forward " \t")
667   (let* ((start (point))
668          (end (progn
669                 (re-search-forward "@item\\|@end")
670                 (match-beginning 0)))
671          (row (progn (goto-char end)
672                      (skip-chars-backward " ")
673                      ;; remove whitespace at end of argument
674                      (delete-region (point) end)
675                      (buffer-substring start (point)))))
676     (delete-region texinfo-command-start end)
677     row))
678
679 (when-broken texinfo-multitable-item
680   (fmakunbound 'texinfo-multitable-item))
681
682 (put 'multitable 'texinfo-item 'texinfo-multitable-item)
683 (defun-maybe texinfo-multitable-item ()
684   "Format a row within a multicolumn table.
685 Cells in row are separated by @tab.
686 Widths of cells are specified by the arguments in the @multitable line.
687 All cells are made to be the same height.
688 This command is executed when texinfmt sees @item inside @multitable."
689   (let ((original-buffer (current-buffer))
690         (table-widths (reverse (car (cdr (car texinfo-stack)))))
691         (existing-fill-column fill-column)
692         start
693         end
694         (table-column       0)
695         (table-entry-height 0)
696         ;; unformatted row looks like:  A1  @tab  A2  @tab  A3
697         ;; extract-row command deletes the source line in the table.
698         (unformated-row (texinfo-multitable-extract-row)))
699     ;; Use a temporary buffer
700     (set-buffer (get-buffer-create texinfo-multitable-buffer-name))
701     (delete-region (point-min) (point-max))
702     (insert unformated-row)
703     (goto-char (point-min))
704 ;; 1. Check for correct number of @tab in line.
705     (let ((tab-number 1))                       ; one @tab between two columns
706       (while (search-forward "@tab" nil t)
707         (setq tab-number (1+ tab-number)))
708       (if (/= tab-number (length table-widths))
709           (error "Wrong number of @tab's in a @multitable row")))
710     (goto-char (point-min))
711 ;; 2. Format each cell, and copy to a rectangle
712     ;; buffer looks like this:    A1  @tab  A2  @tab  A3
713     ;; Cell #1: format up to @tab
714     ;; Cell #2: format up to @tab
715     ;; Cell #3: format up to eob
716     (while (not (eobp))
717       (setq start (point))
718       (setq end (save-excursion
719                   (if (search-forward "@tab" nil 'move)
720                       ;; Delete the @tab command, including the @-sign
721                       (delete-region
722                        (point)
723                        (progn (forward-word -1) (1- (point)))))
724                   (point)))
725       ;; Set fill-column *wider* than needed to produce inter-column space
726       (setq fill-column (+ 1
727                            texinfo-extra-inter-column-width
728                            (nth table-column table-widths)))
729       (narrow-to-region start end)
730       ;; Remove whitespace before and after entry.
731       (skip-chars-forward " ")
732       (delete-region (point) (save-excursion (beginning-of-line) (point)))
733       (goto-char (point-max))
734       (skip-chars-backward " ")
735       (delete-region (point) (save-excursion (end-of-line) (point)))
736       ;; Temorarily set texinfo-stack to nil so texinfo-format-scan
737       ;; does not see an unterminated @multitable.
738       (let (texinfo-stack)                      ; nil
739         (texinfo-format-scan))
740       (let (fill-prefix)                        ; no fill prefix
741         (fill-region (point-min) (point-max)))
742       (setq table-entry-height
743             (max table-entry-height (count-lines (point-min) (point-max))))
744 ;; 3. Move point to end of bottom line, and pad that line to fill column.
745       (goto-char (point-min))
746       (forward-line (1- table-entry-height))
747       (let* ((beg (point))                      ; beginning of line
748              ;; add one more space for inter-column spacing
749              (needed-whitespace
750               (1+
751                (- fill-column
752                   (progn (end-of-line) (current-column)))))) ; end of existing line
753         (insert (make-string
754                  (if (> needed-whitespace 0) needed-whitespace 1)
755                  ? )))
756       ;; now, put formatted cell into a rectangle
757       (set (intern (concat texinfo-multitable-rectangle-name
758                            (int-to-string table-column)))
759            (extract-rectangle (point-min) (point)))
760       (delete-region (point-min) (point))
761       (goto-char (point-max))
762       (setq table-column (1+ table-column))
763       (widen))
764 ;; 4. Add extra lines to rectangles so all are of same height
765     (let ((total-number-of-columns table-column)
766           (column-number 0)
767           here)
768       (while (> table-column 0)
769         (let ((this-rectangle (int-to-string table-column)))
770           (while (< (length this-rectangle) table-entry-height)
771             (setq this-rectangle (append this-rectangle '("")))))
772         (setq table-column (1- table-column)))
773 ;; 5. Insert formatted rectangles in original buffer
774       (switch-to-buffer original-buffer)
775       (open-line table-entry-height)
776       (while (< column-number total-number-of-columns)
777         (setq here (point))
778         (insert-rectangle
779          (eval (intern
780                 (concat texinfo-multitable-rectangle-name
781                         (int-to-string column-number)))))
782         (goto-char here)
783         (end-of-line)
784         (setq column-number (1+ column-number))))
785     (kill-buffer texinfo-multitable-buffer-name)
786     (setq fill-column existing-fill-column)))
787
788 \f
789 (when-broken texinfo-format-printindex
790   (fmakunbound 'texinfo-format-printindex))
791
792 (defun-maybe texinfo-format-printindex ()
793   (let ((indexelts (symbol-value
794                     (cdr (assoc (texinfo-parse-arg-discard)
795                                 texinfo-indexvar-alist))))
796         opoint)
797     (insert "\n* Menu:\n\n")
798     (setq opoint (point))
799     (texinfo-print-index nil indexelts)
800
801     (if (memq system-type '(vax-vms windows-nt ms-dos))
802         (texinfo-sort-region opoint (point))
803       (shell-command-on-region opoint (point) "sort -fd" 1))))
804
805 (provide 'ptexinfmt)
806
807 ;;; ptexinfmt.el ends here