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