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