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