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