071d00f865a780a4c9eff5770a2347441c949b06
[elisp/gnus.git-] / texi / 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 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
11 ;; Maintainer: TAKAHASHI Kaoru <kaoru@kaisei.org>
12 ;; Created: 7 Jul 2000
13 ;; Keywords: maint, tex, docs, emulation, compatibility
14
15 ;; This program is free software; you can redistribute it and/or
16 ;; modify it under the terms of the GNU General Public License as
17 ;; published by the Free Software Foundation; either version 2, or (at
18 ;; your option) any later version.
19
20 ;; This program is distributed in the hope that it will be useful, but
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23 ;; General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
29
30 ;;; Commentary:
31
32 ;; Original code: Yoshiki Hayashi <yoshiki@xemacs.org>
33 ;;      makeinfo.el (gnujdoc project)
34
35 ;; Support texinfmt.el 2.32 or later.
36
37 ;; Modified by Yamaoka not to use APEL functions.
38
39 ;; Unimplemented command:
40 ;;  @float, @caption, @shortcaption, @listoffloats
41 ;;  @deftypecv[x]
42 ;;  @headitem
43 ;;  @comma{}
44 ;;  @quotation (optional arguments)
45 ;;  @acronym (optional argument)
46 ;;  @dofirstparagraphindent
47 ;;  @indent
48 ;;  @verbatiminclude
49 ;;  @\
50 ;;  @definfoenclose
51 ;;  @deftypeivar
52 ;;  @deftypeop
53
54 ;;; Code:
55
56 (require 'backquote)
57 (require 'texinfmt)
58
59 ;;; Broken
60 (defvar ptexinfmt-disable-broken-notice-flag t
61   "If non-nil disable notice, when call `ptexinfmt-broken-facility'.
62 This is last argument in `ptexinfmt-broken-facility'.")
63
64 (put 'ptexinfmt-broken-facility 'lisp-indent-function 'defun)
65 (defmacro ptexinfmt-broken-facility (facility docstring assertion
66                                               &optional dummy)
67   "Declare a symbol FACILITY is broken if ASSERTION is nil.
68 DOCSTRING will be printed if ASSERTION is nil and
69 `ptexinfmt-disable-broken-notice-flag' is nil."
70   (` (let ((facility '(, facility))
71            (docstring (, docstring))
72            (assertion (eval '(, assertion))))
73        (put facility 'broken (not assertion))
74        (if assertion
75            nil
76          (put facility 'broken-docstring docstring)
77          (if ptexinfmt-disable-broken-notice-flag
78              nil
79            (message "BROKEN FACILITY DETECTED: %s" docstring))))))
80
81 (put 'ptexinfmt-defun-if-broken 'lisp-indent-function 'defun)
82 (defmacro ptexinfmt-defun-if-broken (&rest args)
83   "Redefine a function just like `defun' if it is considered broken."
84   (let ((name (list 'quote (car args))))
85     (setq args (cdr args))
86     (` (prog1
87            (, name)
88          (if (get (, name) 'broken)
89              (defalias (, name)
90                (function (lambda (,@ args)))))))))
91
92 (put 'ptexinfmt-defun-if-void 'lisp-indent-function 'defun)
93 (defmacro ptexinfmt-defun-if-void (&rest args)
94   "Define a function just like `defun' unless it is already defined."
95   (let ((name (list 'quote (car args))))
96     (setq args (cdr args))
97     (` (prog1
98            (, name)
99          (if (fboundp (, name))
100              nil
101            (defalias (, name)
102              (function (lambda (,@ args)))))))))
103
104 (put 'ptexinfmt-defvar-if-void 'lisp-indent-function 'defun)
105 (defmacro ptexinfmt-defvar-if-void (&rest args)
106   "Define a variable just like `defvar' unless it is already defined."
107   (let ((name (car args)))
108     (setq args (cdr args))
109     (` (prog1
110            (defvar (, name))
111          (if (boundp '(, name))
112              nil
113            (defvar (, name) (,@ args)))))))
114
115 ;; sort -fd
116 (ptexinfmt-broken-facility texinfo-format-printindex
117   "Can't sort on Mule for Windows."
118   t)
119
120 ;; @var
121 (ptexinfmt-broken-facility texinfo-format-var
122   "Don't perse @var argument."
123   (condition-case nil
124       (with-temp-buffer
125         (let (texinfo-enclosure-list texinfo-alias-list)
126           (texinfo-mode)
127           (insert "@var{@asis{foo}}\n")
128           (texinfo-format-expand-region (point-min) (point-max))
129           t))
130     (error nil)))
131
132 ;; @xref
133 (ptexinfmt-broken-facility texinfo-format-xref
134   "Can't format @xref, 1st argument is empty."
135   (condition-case nil
136       (with-temp-buffer
137         (let (texinfo-enclosure-list texinfo-alias-list)
138           (texinfo-mode)
139           (insert "@xref{, xref, , file}\n")
140           (texinfo-format-expand-region (point-min) (point-max))
141           t))
142     (error nil)))
143
144 ;; @uref
145 (ptexinfmt-broken-facility texinfo-format-uref
146   "Parse twice @uref argument."
147   (condition-case nil
148       (with-temp-buffer
149         (let (texinfo-enclosure-list texinfo-alias-list)
150           (texinfo-mode)
151           (insert "@uref{mailto:foo@@noncommand.example.com}\n")
152           (texinfo-format-expand-region (point-min) (point-max))
153           t))
154     (error nil)))
155
156 ;; @multitable
157 (ptexinfmt-broken-facility texinfo-multitable-widths
158   "`texinfo-multitable-widths' unsupport wide-char."
159   (if (fboundp 'texinfo-multitable-widths)
160       (with-temp-buffer
161         (let ((str "\e$BI}9-J8;z\e(B"))
162           (texinfo-mode)
163           (insert (format " {%s}\n" str))
164           (goto-char (point-min))
165           (if (= (car (texinfo-multitable-widths)) (length str))
166               nil
167             t)))
168     ;; function definition is void
169     t))
170
171 (ptexinfmt-broken-facility texinfo-multitable-item
172   "`texinfo-multitable-item' unsupport wide-char."
173   (not (get 'texinfo-multitable-widths 'broken)))
174
175
176 ;;; Hardcopy and HTML (discard)
177 ;; html
178 (put 'documentlanguage 'texinfo-format 'texinfo-discard-line-with-args)
179 (put 'documentencoding 'texinfo-format 'texinfo-discard-line-with-args)
180 (put 'documentdescription 'texinfo-format 'texinfo-discard-line-with-args)
181
182 ;; size
183 (put 'smallbook 'texinfo-format 'texinfo-discard-line)
184 (put 'letterpaper 'texinfo-format 'texinfo-discard-line)
185 (put 'afourpaper 'texinfo-format 'texinfo-discard-line)
186 (put 'afourlatex 'texinfo-format 'texinfo-discard-line)
187 (put 'afourwide 'texinfo-format 'texinfo-discard-line)
188 (put 'afivepaper 'texinfo-format 'texinfo-discard-line)
189 (put 'pagesizes 'texinfo-format 'texinfo-discard-line-with-args)
190
191 ;; style
192 (put 'setchapternewpage 'texinfo-format 'texinfo-discard-line-with-args)
193 (put 'kbdinputstyle 'texinfo-format 'texinfo-discard-line-with-args)
194
195 ;; flags
196 (put 'setcontentsaftertitlepage 'texinfo-format 'texinfo-discard-line)
197 (put 'setshortcontentsaftertitlepage 'texinfo-format 'texinfo-discard-line)
198 (put 'novalidate 'texinfo-format 'texinfo-discard-line-with-args)
199
200 ;; head & foot
201 (put 'headings 'texinfo-format 'texinfo-discard-line-with-args)
202 (put 'evenfooting 'texinfo-format 'texinfo-discard-line-with-args)
203 (put 'evenheading 'texinfo-format 'texinfo-discard-line-with-args)
204 (put 'oddfooting 'texinfo-format 'texinfo-discard-line-with-args)
205 (put 'oddheading 'texinfo-format 'texinfo-discard-line-with-args)
206 (put 'everyfooting 'texinfo-format 'texinfo-discard-line-with-args)
207 (put 'everyheading 'texinfo-format 'texinfo-discard-line-with-args)
208
209 ;; misc
210 (put 'page 'texinfo-format 'texinfo-discard-line)
211 (put 'hyphenation 'texinfo-format 'texinfo-discard-command-and-arg)
212
213 ;; @s{} (makeinfo 4.8 or later)
214 (put 's 'texinfo-format 'texinfo-format-noop)
215
216 ;; @tie{} (makeinfo 4.3 or later)
217 (put 'tie 'texinfo-format 'texinfo-format-tie)
218 (ptexinfmt-defun-if-void texinfo-format-tie ()
219   (texinfo-parse-arg-discard)
220   (insert " "))
221
222 \f
223 ;;; Directory File
224 ;; @direcategory
225 (put 'dircategory 'texinfo-format 'texinfo-format-dircategory)
226 (ptexinfmt-defun-if-void texinfo-format-dircategory ()
227   (let ((str (texinfo-parse-arg-discard)))
228     (delete-region (point)
229                    (progn
230                      (skip-chars-forward " ")
231                      (point)))
232     (insert "INFO-DIR-SECTION " str "\n")))
233
234 ;; @direntry
235 (put 'direntry 'texinfo-format 'texinfo-format-direntry)
236 (ptexinfmt-defun-if-void texinfo-format-direntry ()
237   (texinfo-push-stack 'direntry nil)
238   (texinfo-discard-line)
239   (insert "START-INFO-DIR-ENTRY\n"))
240
241 (put 'direntry 'texinfo-end 'texinfo-end-direntry)
242 (ptexinfmt-defun-if-void texinfo-end-direntry ()
243   (texinfo-discard-command)
244   (insert "END-INFO-DIR-ENTRY\n\n")
245   (texinfo-pop-stack 'direntry))
246
247
248 ;;; Block Enclosing
249 ;; @detailmenu ... @end detailmenu
250 (put 'detailmenu 'texinfo-format 'texinfo-discard-line)
251 (put 'detailmenu 'texinfo-end 'texinfo-discard-command)
252
253 ;; @smalldisplay ... @end smalldisplay
254 (put 'smalldisplay 'texinfo-format 'texinfo-format-example)
255 (put 'smalldisplay 'texinfo-end 'texinfo-end-example)
256
257 ;; @smallformat ... @end smallformat
258 (put 'smallformat 'texinfo-format 'texinfo-format-flushleft)
259 (put 'smallformat 'texinfo-end 'texinfo-end-flushleft)
260
261 ;; @cartouche  ... @end cartouche
262 (put 'cartouche 'texinfo-format 'texinfo-discard-line)
263 (put 'cartouche 'texinfo-end 'texinfo-discard-command)
264
265
266 ;;; Conditional
267 ;; @ifnottex ... @end ifnottex (makeinfo 3.11 or later)
268 (put 'ifnottex 'texinfo-format 'texinfo-discard-line)
269 (put 'ifnottex 'texinfo-end 'texinfo-discard-command)
270
271 ;; @ifnothtml ... @end ifnothtml (makeinfo 3.11 or later)
272 (put 'ifnothtml 'texinfo-format 'texinfo-discard-line)
273 (put 'ifnothtml 'texinfo-end 'texinfo-discard-command)
274
275 ;; @ifnotplaintext ... @end ifnotplaintext (makeinfo 4.2 or later)
276 (put 'ifnotplaintext 'texinfo-format 'texinfo-discard-line)
277 (put 'ifnotplaintext 'texinfo-end 'texinfo-discard-command)
278
279 ;; @ifnotdocbook ... @end ifnotdocbook (makeinfo 4.7 or later)
280 (put 'ifnotdocbook 'texinfo-format 'texinfo-discard-line)
281 (put 'ifnotdocbook 'texinfo-end 'texinfo-discard-command)
282
283 ;; @ifnotinfo ... @end ifnotinfo (makeinfo 3.11 or later)
284 (put 'ifnotinfo 'texinfo-format 'texinfo-format-ifnotinfo)
285 (ptexinfmt-defun-if-void texinfo-format-ifnotinfo ()
286   (delete-region texinfo-command-start
287                  (progn (re-search-forward "@end ifnotinfo[ \t]*\n")
288                         (point))))
289
290 ;; @html ... @end html (makeinfo 3.11 or later)
291 (put 'html 'texinfo-format 'texinfo-format-html)
292 (ptexinfmt-defun-if-void texinfo-format-html ()
293   (delete-region texinfo-command-start
294                  (progn (re-search-forward "@end html[ \t]*\n")
295                         (point))))
296
297 ;; @docbook ... @end docbook (makeinfo 4.7 or later)
298 (put 'docbook 'texinfo-format 'texinfo-format-docbook)
299 (ptexinfmt-defun-if-void texinfo-format-docbook ()
300   (delete-region texinfo-command-start
301                  (progn (re-search-forward "@end docbook[ \t]*\n")
302                         (point))))
303
304 ;; @ifhtml ... @end ifhtml (makeinfo 3.8 or later)
305 (put 'ifhtml 'texinfo-format 'texinfo-format-ifhtml)
306 (defun texinfo-format-ifhtml ()
307   (delete-region texinfo-command-start
308                  (progn (re-search-forward "@end ifhtml[ \t]*\n")
309                         (point))))
310
311 ;; @ifplaintext ... @end ifplaintext (makeinfo 4.2 or later)
312 (put 'ifplaintext 'texinfo-format 'texinfo-format-ifplaintext)
313 (ptexinfmt-defun-if-void texinfo-format-ifplaintext ()
314   (delete-region texinfo-command-start
315                  (progn (re-search-forward "@end ifplaintext[ \t]*\n")
316                         (point))))
317
318 ;; @ifdocbook ... @end ifdocbook (makeinfo 4.7 or later)
319 (put 'ifdocbook 'texinfo-format 'texinfo-format-ifdocbook)
320 (ptexinfmt-defun-if-void texinfo-format-ifdocbook ()
321   (delete-region texinfo-command-start
322                  (progn (re-search-forward "@end ifdocbook[ \t]*\n")
323                         (point))))
324
325 \f
326 ;;; Marking
327 ;; @indicateurl, @url, @env, @command, 
328 (put 'env 'texinfo-format 'texinfo-format-code)
329 (put 'command 'texinfo-format 'texinfo-format-code)
330
331 (put 'indicateurl 'texinfo-format 'texinfo-format-code)
332 (put 'url 'texinfo-format 'texinfo-format-uref) ; Texinfo 4.7
333
334 ;; @acronym
335 (put 'acronym 'texinfo-format 'texinfo-format-var)
336
337 (ptexinfmt-defun-if-broken texinfo-format-var ()
338   (let ((arg (texinfo-parse-expanded-arg)))
339     (texinfo-discard-command)
340     (insert (upcase arg))))
341
342 ;; @key
343 (put 'key 'texinfo-format 'texinfo-format-key)
344 (ptexinfmt-defun-if-void texinfo-format-key ()
345   (insert (texinfo-parse-arg-discard))
346   (goto-char texinfo-command-start))
347
348 ;; @email{EMAIL-ADDRESS[, DISPLAYED-TEXT]}
349 (put 'email 'texinfo-format 'texinfo-format-email)
350 (ptexinfmt-defun-if-void texinfo-format-email ()
351   "Format EMAIL-ADDRESS and optional DISPLAYED-TXT.
352 Insert < ... > around EMAIL-ADDRESS."
353   (let ((args (texinfo-format-parse-args)))
354   (texinfo-discard-command)
355     ;; if displayed-text
356     (if (nth 1 args)
357         (insert (nth 1 args) " <" (nth 0 args) ">")
358       (insert "<" (nth 0 args) ">"))))
359
360 ;; @option
361 (put 'option 'texinfo-format 'texinfo-format-option)
362 (ptexinfmt-defun-if-void texinfo-format-option ()
363   "Insert ` ... ' around arg unless inside a table; in that case, no quotes."
364   ;; `looking-at-backward' not available in v. 18.57, 20.2
365   ;; searched-for character is a control-H
366   (if (not (search-backward "\010"
367                             (save-excursion (beginning-of-line) (point))
368                             t))
369       (insert "`" (texinfo-parse-arg-discard) "'")
370     (insert (texinfo-parse-arg-discard)))
371   (goto-char texinfo-command-start))
372
373 ;; @verb{<char>TEXT<char>}  (makeinfo 4.1 or later)
374 (put 'verb 'texinfo-format 'texinfo-format-verb)
375 (ptexinfmt-defun-if-void texinfo-format-verb ()
376   "Format text between non-quoted unique delimiter characters verbatim.
377 Enclose the verbatim text, including the delimiters, in braces.  Print
378 text exactly as written (but not the delimiters) in a fixed-width.
379
380 For example, @verb\{|@|\} results in @ and
381 @verb\{+@'e?`!`+} results in @'e?`!`."
382
383   (let ((delimiter (buffer-substring-no-properties
384                     (1+ texinfo-command-end) (+ 2 texinfo-command-end))))
385     (unless (looking-at "{")
386       (error "Not found: @verb start brace"))
387     (delete-region texinfo-command-start (+ 2 texinfo-command-end))
388     (search-forward  delimiter))
389   (delete-backward-char 1)
390   (unless (looking-at "}")
391     (error "Not found: @verb end brace"))
392   (delete-char 1))
393
394 \f
395 ;;; @LaTeX, @registeredsymbol{}
396 (put 'LaTeX 'texinfo-format 'texinfo-format-LaTeX)
397 (ptexinfmt-defun-if-void texinfo-format-LaTeX ()
398   (texinfo-parse-arg-discard)
399   (insert "LaTeX"))
400
401 (put 'registeredsymbol 'texinfo-format 'texinfo-format-registeredsymbol)
402 (ptexinfmt-defun-if-void texinfo-format-registeredsymbol ()
403   (texinfo-parse-arg-discard)
404   (insert "(R)"))
405
406 ;;; Accents and Special characters
407 ;; @pounds{}    ==>     #       Pounds Sterling
408 (put 'pounds 'texinfo-format 'texinfo-format-pounds)
409 (ptexinfmt-defun-if-void texinfo-format-pounds ()
410   (texinfo-parse-arg-discard)
411   (insert "#"))
412
413 ;; @ordf{}      ==>     a       Spanish feminine
414 (put 'ordf 'texinfo-format 'texinfo-format-ordf)
415 (ptexinfmt-defun-if-void texinfo-format-ordf ()
416   (texinfo-parse-arg-discard)
417   (insert "o"))
418
419 ;; @ordm{}      ==>     o       Spanish masculine
420 (put 'ordm 'texinfo-format 'texinfo-format-ordm)
421 (ptexinfmt-defun-if-void texinfo-format-ordm ()
422   (texinfo-parse-arg-discard)
423   (insert "o"))
424
425 ;; @OE{}        ==>     OE      French-OE-ligature
426 (put 'OE 'texinfo-format 'texinfo-format-French-OE-ligature)
427 (ptexinfmt-defun-if-void texinfo-format-French-OE-ligature ()
428   (insert "OE" (texinfo-parse-arg-discard))
429   (goto-char texinfo-command-start))
430
431 ;; @oe{}        ==>     oe
432 (put 'oe 'texinfo-format 'texinfo-format-French-oe-ligature)
433 (ptexinfmt-defun-if-void texinfo-format-French-oe-ligature () ; lower case
434   (insert "oe" (texinfo-parse-arg-discard))
435   (goto-char texinfo-command-start))
436
437 ;; @AA{}        ==>     AA      Scandinavian-A-with-circle
438 (put 'AA 'texinfo-format 'texinfo-format-Scandinavian-A-with-circle)
439 (ptexinfmt-defun-if-void texinfo-format-Scandinavian-A-with-circle ()
440   (insert "AA" (texinfo-parse-arg-discard))
441   (goto-char texinfo-command-start))
442
443 ;; @aa{}        ==>     aa
444 (put 'aa 'texinfo-format 'texinfo-format-Scandinavian-a-with-circle)
445 (ptexinfmt-defun-if-void texinfo-format-Scandinavian-a-with-circle () ; lower case
446   (insert "aa" (texinfo-parse-arg-discard))
447   (goto-char texinfo-command-start))
448
449 ;; @AE{}        ==>     AE      Latin-Scandinavian-AE
450 (put 'AE 'texinfo-format 'texinfo-format-Latin-Scandinavian-AE)
451 (ptexinfmt-defun-if-void texinfo-format-Latin-Scandinavian-AE ()
452   (insert "AE" (texinfo-parse-arg-discard))
453   (goto-char texinfo-command-start))
454
455 ;; @ae{}        ==>     ae
456 (put 'ae 'texinfo-format 'texinfo-format-Latin-Scandinavian-ae)
457 (ptexinfmt-defun-if-void texinfo-format-Latin-Scandinavian-ae () ; lower case
458   (insert "ae" (texinfo-parse-arg-discard))
459   (goto-char texinfo-command-start))
460
461 ;; @ss{}        ==>     ss      German-sharp-S
462 (put 'ss 'texinfo-format 'texinfo-format-German-sharp-S)
463 (ptexinfmt-defun-if-void texinfo-format-German-sharp-S ()
464   (insert "ss" (texinfo-parse-arg-discard))
465   (goto-char texinfo-command-start))
466
467 ;; @questiondown{}      ==>     ?       upside-down-question-mark
468 (put 'questiondown 'texinfo-format 'texinfo-format-upside-down-question-mark)
469 (ptexinfmt-defun-if-void texinfo-format-upside-down-question-mark ()
470   (insert "?" (texinfo-parse-arg-discard))
471   (goto-char texinfo-command-start))
472
473 ;; @exclamdown{}        ==>     !       upside-down-exclamation-mark
474 (put 'exclamdown 'texinfo-format 'texinfo-format-upside-down-exclamation-mark)
475 (ptexinfmt-defun-if-void texinfo-format-upside-down-exclamation-mark ()
476   (insert "!" (texinfo-parse-arg-discard))
477   (goto-char texinfo-command-start))
478
479 ;; @L{}         ==>     L/      Polish suppressed-L (Lslash)
480 (put 'L 'texinfo-format 'texinfo-format-Polish-suppressed-L)
481 (ptexinfmt-defun-if-void texinfo-format-Polish-suppressed-L ()
482   (insert (texinfo-parse-arg-discard) "/L")
483   (goto-char texinfo-command-start))
484
485 ;; @l{}         ==>     l/      Polish suppressed-L (Lslash) (lower case)
486 (put 'l 'texinfo-format 'texinfo-format-Polish-suppressed-l-lower-case)
487 (ptexinfmt-defun-if-void texinfo-format-Polish-suppressed-l-lower-case ()
488   (insert (texinfo-parse-arg-discard) "/l")
489   (goto-char texinfo-command-start))
490
491 ;; @O{}         ==>     O/      Scandinavian O-with-slash
492 (put 'O 'texinfo-format 'texinfo-format-Scandinavian-O-with-slash)
493 (ptexinfmt-defun-if-void texinfo-format-Scandinavian-O-with-slash ()
494   (insert (texinfo-parse-arg-discard) "O/")
495   (goto-char texinfo-command-start))
496
497 ;; @o{}         ==>     o/      Scandinavian O-with-slash (lower case)
498 (put 'o 'texinfo-format 'texinfo-format-Scandinavian-o-with-slash-lower-case)
499 (ptexinfmt-defun-if-void texinfo-format-Scandinavian-o-with-slash-lower-case ()
500   (insert (texinfo-parse-arg-discard) "o/")
501   (goto-char texinfo-command-start))
502
503 ;; @,{c}        ==>     c,      cedilla accent
504 (put ', 'texinfo-format 'texinfo-format-cedilla-accent)
505 (ptexinfmt-defun-if-void texinfo-format-cedilla-accent ()
506   (insert (texinfo-parse-arg-discard) ",")
507   (goto-char texinfo-command-start))
508
509
510 ;; @dotaccent{o}        ==>     .o      overdot-accent
511 (put 'dotaccent 'texinfo-format 'texinfo-format-overdot-accent)
512 (ptexinfmt-defun-if-void texinfo-format-overdot-accent ()
513   (insert "." (texinfo-parse-arg-discard))
514   (goto-char texinfo-command-start))
515
516 ;; @ubaraccent{o}       ==>     _o      underbar-accent
517 (put 'ubaraccent 'texinfo-format 'texinfo-format-underbar-accent)
518 (ptexinfmt-defun-if-void texinfo-format-underbar-accent ()
519   (insert "_" (texinfo-parse-arg-discard))
520   (goto-char texinfo-command-start))
521
522 ;; @udotaccent{o}       ==>     o-.     underdot-accent
523 (put 'udotaccent 'texinfo-format 'texinfo-format-underdot-accent)
524 (ptexinfmt-defun-if-void texinfo-format-underdot-accent ()
525   (insert (texinfo-parse-arg-discard) "-.")
526   (goto-char texinfo-command-start))
527
528 ;; @H{o}        ==>     ""o     long Hungarian umlaut
529 (put 'H 'texinfo-format 'texinfo-format-long-Hungarian-umlaut)
530 (ptexinfmt-defun-if-void texinfo-format-long-Hungarian-umlaut ()
531   (insert "\"\"" (texinfo-parse-arg-discard))
532   (goto-char texinfo-command-start))
533
534 ;; @ringaccent{o}       ==>     *o      ring accent
535 (put 'ringaccent 'texinfo-format 'texinfo-format-ring-accent)
536 (ptexinfmt-defun-if-void texinfo-format-ring-accent ()
537   (insert "*" (texinfo-parse-arg-discard))
538   (goto-char texinfo-command-start))
539
540 ;; @tieaccent{oo}       ==>     [oo     tie after accent
541 (put 'tieaccent 'texinfo-format 'texinfo-format-tie-after-accent)
542 (ptexinfmt-defun-if-void texinfo-format-tie-after-accent ()
543   (insert "[" (texinfo-parse-arg-discard))
544   (goto-char texinfo-command-start))
545
546 ;; @u{o}        ==>     (o      breve accent
547 (put 'u 'texinfo-format 'texinfo-format-breve-accent)
548 (ptexinfmt-defun-if-void texinfo-format-breve-accent ()
549   (insert "(" (texinfo-parse-arg-discard))
550   (goto-char texinfo-command-start))
551
552 ;; @v{o}        ==>     <o      hacek accent
553 (put 'v 'texinfo-format 'texinfo-format-hacek-accent)
554 (ptexinfmt-defun-if-void texinfo-format-hacek-accent ()
555   (insert "<" (texinfo-parse-arg-discard))
556   (goto-char texinfo-command-start))
557
558 ;; @dotless{i}  ==>     i       dotless i and dotless j
559 (put 'dotless 'texinfo-format 'texinfo-format-dotless)
560 (ptexinfmt-defun-if-void texinfo-format-dotless ()
561   (insert (texinfo-parse-arg-discard))
562   (goto-char texinfo-command-start))
563
564 ;; @.
565 (put '\. 'texinfo-format 'texinfo-format-\.)
566 (ptexinfmt-defun-if-void texinfo-format-\. ()
567   (texinfo-discard-command)
568   (insert "."))
569
570 ;; @:
571 (put '\: 'texinfo-format 'texinfo-format-\:)
572 (ptexinfmt-defun-if-void texinfo-format-\: ()
573   (texinfo-discard-command))
574
575 ;; @-
576 (put '\- 'texinfo-format 'texinfo-format-soft-hyphen)
577 (ptexinfmt-defun-if-void texinfo-format-soft-hyphen ()
578   (texinfo-discard-command))
579
580 ;; @/
581 (put '\/ 'texinfo-format 'texinfo-format-\/)
582 (ptexinfmt-defun-if-void texinfo-format-\/ ()
583   (texinfo-discard-command))
584
585 \f
586 ;;; Cross References
587 ;; @ref, @xref
588 (put 'ref 'texinfo-format 'texinfo-format-xref)
589
590 (ptexinfmt-defun-if-broken texinfo-format-xref ()
591   (let ((args (texinfo-format-parse-args)))
592     (texinfo-discard-command)
593     (insert "*Note ")
594     (let ((fname (or (nth 1 args) (nth 2 args))))
595       (if (null (or fname (nth 3 args)))
596           (insert (nth 0 args) "::")
597         (insert (or fname (nth 0 args)) ": ")
598         (if (nth 3 args)
599             (insert "(" (nth 3 args) ")"))
600         (unless (null (nth 0 args))
601           (insert (nth 0 args)))))))
602
603 ;; @uref{URL [,TEXT] [,REPLACEMENT]}
604 (put 'uref 'texinfo-format 'texinfo-format-uref)
605 (ptexinfmt-defun-if-broken texinfo-format-uref ()
606   "Format URL and optional URL-TITLE.
607 Insert ` ... ' around URL if no URL-TITLE argument;
608 otherwise, insert URL-TITLE followed by URL in parentheses."
609   (let ((args (texinfo-format-parse-args)))
610     (texinfo-discard-command)
611     ;; if url-title
612     (if (nth 1 args)
613         (insert  (nth 1 args) " (" (nth 0 args) ")")
614       (insert "`" (nth 0 args) "'"))))
615
616 ;; @inforef
617 (put 'inforef 'texinfo-format 'texinfo-format-inforef)
618 (ptexinfmt-defun-if-void texinfo-format-inforef ()
619   (let ((args (texinfo-format-parse-args)))
620     (texinfo-discard-command)
621     (if (nth 1 args)
622         (insert "*Note " (nth 1 args) ": (" (nth 2 args) ")" (car args))
623       (insert "*Note " "(" (nth 2 args) ")" (car args) "::"))))
624
625
626 ;; @anchor
627 ;; don't emulation
628 ;; If support @anchor for Mule 2.3, We must fix informat.el and info.el:
629 ;;  - Info-tagify suport @anthor-*-refill.
630 ;;  - info.el support Ref in Tag table.
631 (unless (get 'anchor 'texinfo-format)
632   (put 'anchor 'texinfo-format 'texinfo-discard-command-and-arg))
633
634
635 \f
636 ;;; New command definition
637 ;; @alias NEW=EXISTING
638 (put 'alias 'texinfo-format 'texinfo-alias)
639 (ptexinfmt-defun-if-void texinfo-alias ()
640   (let ((start (1- (point)))
641         args)
642     (skip-chars-forward " ")
643     (save-excursion (end-of-line) (setq texinfo-command-end (point)))
644     (if (not (looking-at "\\([^=]+\\)=\\(.*\\)"))
645         (error "Invalid alias command")
646       (setq texinfo-alias-list
647             (cons
648              (cons
649               (buffer-substring (match-beginning 1) (match-end 1))
650               (buffer-substring (match-beginning 2) (match-end 2)))
651              texinfo-alias-list))
652       (texinfo-discard-command))))
653
654 \f
655 ;;; Indent
656 ;; @exampleindent INDENT  (makeinfo 4.0 or later)
657
658 ;; @paragraphindent INDENT  (makeinfo 4.0 or later)
659 ;; INDENT: asis, 0, n
660
661 ;; @firstparagraphindent WORD   (makeinfo 4.6 or later)
662 ;; WORD: none, insert
663
664
665 \f
666 ;;; Special
667 ;; @image{FILENAME [, WIDTH] [, HEIGHT]}
668 (put 'image 'texinfo-format 'texinfo-format-image)
669 (ptexinfmt-defun-if-void texinfo-format-image ()
670   ;; I don't know makeinfo parse FILENAME.
671   (let ((args (texinfo-format-parse-args))
672         filename)
673     (when (null (nth 0 args))
674       (error "Invalid image command"))
675     (texinfo-discard-command)
676     ;; makeinfo uses FILENAME.txt
677     (setq filename (format "%s.txt" (nth 0 args)))
678     (message "Reading included file: %s" filename)
679     ;; verbatim for Info output
680     (goto-char (+ (point) (cadr (insert-file-contents filename))))
681     (message "Reading included file: %s...done" filename)))
682
683 ;; @hyphenation command discards an argument within braces
684 (put 'hyphenation 'texinfo-format 'texinfo-discard-command-and-arg)
685 (ptexinfmt-defun-if-void texinfo-discard-command-and-arg ()
686   "Discard both @-command and its argument in braces."
687   (goto-char texinfo-command-end)
688   (forward-list 1)
689   (setq texinfo-command-end (point))
690   (delete-region texinfo-command-start texinfo-command-end))
691
692 \f
693 ;;; @multitable ... @end multitable
694 (ptexinfmt-defvar-if-void texinfo-extra-inter-column-width 0
695   "*Number of extra spaces between entries (columns) in @multitable.")
696
697 (ptexinfmt-defvar-if-void texinfo-multitable-buffer-name
698   "*multitable-temporary-buffer*")
699 (ptexinfmt-defvar-if-void texinfo-multitable-rectangle-name
700   "texinfo-multitable-temp-")
701
702 ;; These commands are defined in texinfo.tex for printed output.
703 (put 'multitableparskip 'texinfo-format 'texinfo-discard-line-with-args)
704 (put 'multitableparindent 'texinfo-format 'texinfo-discard-line-with-args)
705 (put 'multitablecolmargin 'texinfo-format 'texinfo-discard-line-with-args)
706 (put 'multitablelinespace 'texinfo-format 'texinfo-discard-line-with-args)
707
708 (put 'multitable 'texinfo-format 'texinfo-multitable)
709
710 (ptexinfmt-defun-if-void texinfo-multitable ()
711   "Produce multi-column tables."
712
713 ;; This function pushes information onto the `texinfo-stack'.
714 ;; A stack element consists of:
715 ;;   - type-of-command, i.e., multitable
716 ;;   - the information about column widths, and
717 ;;   - the position of texinfo-command-start.
718 ;; e.g., ('multitable (1 2 3 4) 123)
719 ;; The command line is then deleted.
720   (texinfo-push-stack
721    'multitable
722    ;; push width information on stack
723    (texinfo-multitable-widths))
724   (texinfo-discard-line-with-args))
725
726 (put 'multitable 'texinfo-end 'texinfo-end-multitable)
727 (ptexinfmt-defun-if-void texinfo-end-multitable ()
728   "Discard the @end multitable line and pop the stack of multitable."
729   (texinfo-discard-command)
730   (texinfo-pop-stack 'multitable))
731
732 (ptexinfmt-defun-if-broken texinfo-multitable-widths ()
733   "Return list of widths of each column in a multi-column table."
734   (let (texinfo-multitable-width-list)
735     ;; Fractions format:
736     ;;  @multitable @columnfractions .25 .3 .45
737     ;;
738     ;; Template format:
739     ;;  @multitable {Column 1 template} {Column 2} {Column 3 example}
740     ;; Place point before first argument
741     (skip-chars-forward " \t")
742     (cond
743      ;; Check for common misspelling
744      ((looking-at "@columnfraction ")
745       (error "In @multitable, @columnfractions misspelled"))
746      ;; Case 1: @columnfractions .25 .3 .45
747      ((looking-at "@columnfractions")
748       (forward-word 1)
749       (while (not (eolp))
750         (setq texinfo-multitable-width-list
751               (cons
752                (truncate
753                 (1-
754                  (* fill-column (read (get-buffer (current-buffer))))))
755                texinfo-multitable-width-list))))
756      ;;
757      ;; Case 2: {Column 1 template} {Column 2} {Column 3 example}
758      ((looking-at "{")
759       (let ((start-of-templates (point)))
760         (while (not (eolp))
761           (skip-chars-forward " \t")
762           (let* ((start-of-template (1+ (point)))
763                  (end-of-template
764                   ;; forward-sexp works with braces in Texinfo mode
765                   (progn (forward-sexp 1) (1- (point)))))
766             (setq texinfo-multitable-width-list
767                   (cons (- (progn
768                              (goto-char end-of-template)
769                              (current-column))
770                            (progn
771                              (goto-char start-of-template)
772                              (current-column)))
773                         texinfo-multitable-width-list))
774             ;; Remove carriage return from within a template, if any.
775             ;; This helps those those who want to use more than
776             ;; one line's worth of words in @multitable line.
777             (narrow-to-region start-of-template end-of-template)
778             (goto-char (point-min))
779             (while (search-forward "\n" nil t)
780               (delete-char -1))
781             (goto-char (point-max))
782             (widen)
783             (forward-char 1)))))
784      ;;
785      ;; Case 3: Trouble
786      (t
787       (error "\
788 You probably need to specify column widths for @multitable correctly")))
789     ;; Check whether columns fit on page.
790     (let ((desired-columns
791            (+
792             ;; between column spaces
793             (length texinfo-multitable-width-list)
794             ;; additional between column spaces, if any
795             texinfo-extra-inter-column-width
796             ;; sum of spaces for each entry
797             (apply '+ texinfo-multitable-width-list))))
798       (if (> desired-columns fill-column)
799           (error (format "\
800 Multi-column table width, %d chars, is greater than page width, %d chars."
801                          desired-columns fill-column))))
802     texinfo-multitable-width-list))
803
804 ;; @item  A1  @tab  A2  @tab  A3
805 (ptexinfmt-defun-if-void texinfo-multitable-extract-row ()
806   "Return multitable row, as a string.
807 End of row is beginning of next @item or beginning of @end.
808 Cells within rows are separated by @tab."
809   (skip-chars-forward " \t")
810   (let* ((start (point))
811          (end (progn
812                 (re-search-forward "@item\\|@end")
813                 (match-beginning 0)))
814          (row (progn (goto-char end)
815                      (skip-chars-backward " ")
816                      ;; remove whitespace at end of argument
817                      (delete-region (point) end)
818                      (buffer-substring start (point)))))
819     (delete-region texinfo-command-start end)
820     row))
821
822 (put 'multitable 'texinfo-item 'texinfo-multitable-item)
823 (ptexinfmt-defun-if-void texinfo-multitable-item ()
824   "Format a row within a multicolumn table.
825 Cells in row are separated by @tab.
826 Widths of cells are specified by the arguments in the @multitable line.
827 All cells are made to be the same height.
828 This command is executed when texinfmt sees @item inside @multitable."
829   (let ((original-buffer (current-buffer))
830         (table-widths (reverse (car (cdr (car texinfo-stack)))))
831         (existing-fill-column fill-column)
832         start
833         end
834         (table-column       0)
835         (table-entry-height 0)
836         ;; unformatted row looks like:  A1  @tab  A2  @tab  A3
837         ;; extract-row command deletes the source line in the table.
838         (unformated-row (texinfo-multitable-extract-row)))
839     ;; Use a temporary buffer
840     (set-buffer (get-buffer-create texinfo-multitable-buffer-name))
841     (delete-region (point-min) (point-max))
842     (insert unformated-row)
843     (goto-char (point-min))
844 ;; 1. Check for correct number of @tab in line.
845     (let ((tab-number 1)) ;; one @tab between two columns
846       (while (search-forward "@tab" nil t)
847         (setq tab-number (1+ tab-number)))
848       (if (/= tab-number (length table-widths))
849           (error "Wrong number of @tab's in a @multitable row")))
850     (goto-char (point-min))
851 ;; 2. Format each cell, and copy to a rectangle
852     ;; buffer looks like this:    A1  @tab  A2  @tab  A3
853     ;; Cell #1: format up to @tab
854     ;; Cell #2: format up to @tab
855     ;; Cell #3: format up to eob
856     (while (not (eobp))
857       (setq start (point))
858       (setq end (save-excursion
859                   (if (search-forward "@tab" nil 'move)
860                       ;; Delete the @tab command, including the @-sign
861                       (delete-region
862                        (point)
863                        (progn (forward-word -1) (1- (point)))))
864                   (point)))
865       ;; Set fill-column *wider* than needed to produce inter-column space
866       (setq fill-column (+ 1
867                            texinfo-extra-inter-column-width
868                            (nth table-column table-widths)))
869       (narrow-to-region start end)
870       ;; Remove whitespace before and after entry.
871       (skip-chars-forward " ")
872       (delete-region (point) (save-excursion (beginning-of-line) (point)))
873       (goto-char (point-max))
874       (skip-chars-backward " ")
875       (delete-region (point) (save-excursion (end-of-line) (point)))
876       ;; Temorarily set texinfo-stack to nil so texinfo-format-scan
877       ;; does not see an unterminated @multitable.
878       (let (texinfo-stack) ;; nil
879         (texinfo-format-scan))
880       (let (fill-prefix) ;; no fill prefix
881         (fill-region (point-min) (point-max)))
882       (setq table-entry-height
883             (max table-entry-height (count-lines (point-min) (point-max))))
884 ;; 3. Move point to end of bottom line, and pad that line to fill column.
885       (goto-char (point-min))
886       (forward-line (1- table-entry-height))
887       (let* ((beg (point)) ;; beginning of line
888              ;; add one more space for inter-column spacing
889              (needed-whitespace
890               (1+
891                (- fill-column
892                   (progn
893                     (end-of-line)
894                     (current-column)))))) ;; end of existing line
895         (insert (make-string
896                  (if (> needed-whitespace 0) needed-whitespace 1)
897                  ? )))
898       ;; now, put formatted cell into a rectangle
899       (set (intern (concat texinfo-multitable-rectangle-name
900                            (int-to-string table-column)))
901            (extract-rectangle (point-min) (point)))
902       (delete-region (point-min) (point))
903       (goto-char (point-max))
904       (setq table-column (1+ table-column))
905       (widen))
906 ;; 4. Add extra lines to rectangles so all are of same height
907     (let ((total-number-of-columns table-column)
908           (column-number 0)
909           here)
910       (while (> table-column 0)
911         (let ((this-rectangle (int-to-string table-column)))
912           (while (< (length this-rectangle) table-entry-height)
913             (setq this-rectangle (append this-rectangle '("")))))
914         (setq table-column (1- table-column)))
915 ;; 5. Insert formatted rectangles in original buffer
916       (switch-to-buffer original-buffer)
917       (open-line table-entry-height)
918       (while (< column-number total-number-of-columns)
919         (setq here (point))
920         (insert-rectangle
921          (eval (intern
922                 (concat texinfo-multitable-rectangle-name
923                         (int-to-string column-number)))))
924         (goto-char here)
925         (end-of-line)
926         (setq column-number (1+ column-number))))
927     (kill-buffer texinfo-multitable-buffer-name)
928     (setq fill-column existing-fill-column)))
929
930 \f
931 (ptexinfmt-defun-if-broken texinfo-format-printindex ()
932   (let ((indexelts (symbol-value
933                     (cdr (assoc (texinfo-parse-arg-discard)
934                                 texinfo-indexvar-alist))))
935         opoint)
936     (insert "\n* Menu:\n\n")
937     (setq opoint (point))
938     (texinfo-print-index nil indexelts)
939
940     (if (memq system-type '(vax-vms windows-nt ms-dos))
941         (texinfo-sort-region opoint (point))
942       (shell-command-on-region opoint (point) "sort -fd" 1))))
943
944 (provide 'ptexinfmt)
945
946 ;;; ptexinfmt.el ends here