* wl-util.el (wl-read-search-condition-internal): Protect
[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 ;; 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
55 ;;; Code:
56
57 (require 'backquote)
58 (require 'texinfmt)
59
60 ;;; Broken
61 (defvar ptexinfmt-disable-broken-notice-flag t
62   "If non-nil disable notice, when call `ptexinfmt-broken-facility'.
63 This is last argument in `ptexinfmt-broken-facility'.")
64
65 (put 'ptexinfmt-broken-facility 'lisp-indent-function 'defun)
66 (defmacro ptexinfmt-broken-facility (facility docstring assertion
67                                               &optional dummy)
68   "Declare a symbol FACILITY is broken if ASSERTION is nil.
69 DOCSTRING will be printed if ASSERTION is nil and
70 `ptexinfmt-disable-broken-notice-flag' is nil."
71   (` (let ((facility '(, facility))
72            (docstring (, docstring))
73            (assertion (eval '(, assertion))))
74        (put facility 'broken (not assertion))
75        (if assertion
76            nil
77          (put facility 'broken-docstring docstring)
78          (if ptexinfmt-disable-broken-notice-flag
79              nil
80            (message "BROKEN FACILITY DETECTED: %s" docstring))))))
81
82 (put 'ptexinfmt-defun-if-broken 'lisp-indent-function 'defun)
83 (defmacro ptexinfmt-defun-if-broken (&rest args)
84   "Redefine a function just like `defun' if it is considered broken."
85   (let ((name (list 'quote (car args))))
86     (setq args (cdr args))
87     (` (prog1
88            (, name)
89          (if (get (, name) 'broken)
90              (defalias (, name)
91                (function (lambda (,@ args)))))))))
92
93 (put 'ptexinfmt-defun-if-void 'lisp-indent-function 'defun)
94 (defmacro ptexinfmt-defun-if-void (&rest args)
95   "Define a function just like `defun' unless it is already defined."
96   (let ((name (list 'quote (car args))))
97     (setq args (cdr args))
98     (` (prog1
99            (, name)
100          (if (fboundp (, name))
101              nil
102            (defalias (, name)
103              (function (lambda (,@ args)))))))))
104
105 (put 'ptexinfmt-defvar-if-void 'lisp-indent-function 'defun)
106 (defmacro ptexinfmt-defvar-if-void (&rest args)
107   "Define a variable just like `defvar' unless it is already defined."
108   (let ((name (car args)))
109     (setq args (cdr args))
110     (` (prog1
111            (defvar (, name))
112          (if (boundp '(, name))
113              nil
114            (defvar (, name) (,@ args)))))))
115
116 ;; sort -fd
117 (ptexinfmt-broken-facility texinfo-format-printindex
118   "Can't sort on Mule for Windows."
119   (if (and (memq system-type '(windows-nt ms-dos))
120 ;;; I don't know version threshold.
121 ;;;        (string< texinfmt-version "2.37 of 24 May 1997")
122            (boundp 'MULE) (not (featurep 'meadow))) ; Mule for Windows
123       nil
124     t))
125
126 ;; @var
127 (ptexinfmt-broken-facility texinfo-format-var
128   "Don't perse @var argument."
129   (condition-case nil
130       (with-temp-buffer
131         (let (texinfo-enclosure-list texinfo-alias-list)
132           (texinfo-mode)
133           (insert "@var{@asis{foo}}\n")
134           (texinfo-format-expand-region (point-min) (point-max))
135           t))
136     (error nil)))
137
138 ;; @xref
139 (ptexinfmt-broken-facility texinfo-format-xref
140   "Can't format @xref, 1st argument is empty."
141   (condition-case nil
142       (with-temp-buffer
143         (let (texinfo-enclosure-list texinfo-alias-list)
144           (texinfo-mode)
145           (insert "@xref{, xref, , file}\n")
146           (texinfo-format-expand-region (point-min) (point-max))
147           t))
148     (error nil)))
149
150 ;; @uref
151 (ptexinfmt-broken-facility texinfo-format-uref
152   "Parse twice @uref argument."
153   (condition-case nil
154       (with-temp-buffer
155         (let (texinfo-enclosure-list texinfo-alias-list)
156           (texinfo-mode)
157           (insert "@uref{mailto:foo@@noncommand.example.com}\n")
158           (texinfo-format-expand-region (point-min) (point-max))
159           t))
160     (error nil)))
161
162 ;; @multitable
163 (ptexinfmt-broken-facility texinfo-multitable-widths
164   "`texinfo-multitable-widths' unsupport wide-char."
165   (if (fboundp 'texinfo-multitable-widths)
166       (with-temp-buffer
167         (let ((str "\e$BI}9-J8;z\e(B"))
168           (texinfo-mode)
169           (insert (format " {%s}\n" str))
170           (goto-char (point-min))
171           (if (= (car (texinfo-multitable-widths)) (length str))
172               nil
173             t)))
174     ;; function definition is void
175     t))
176
177 (ptexinfmt-broken-facility texinfo-multitable-item
178   "`texinfo-multitable-item' unsupport wide-char."
179   (not (get 'texinfo-multitable-widths 'broken)))
180
181
182 ;;; Hardcopy and HTML (discard)
183 ;; html
184 (put 'documentlanguage 'texinfo-format 'texinfo-discard-line-with-args)
185 (put 'documentencoding 'texinfo-format 'texinfo-discard-line-with-args)
186 (put 'documentdescription 'texinfo-format 'texinfo-discard-line-with-args)
187
188 ;; size
189 (put 'smallbook 'texinfo-format 'texinfo-discard-line)
190 (put 'letterpaper 'texinfo-format 'texinfo-discard-line)
191 (put 'afourpaper 'texinfo-format 'texinfo-discard-line)
192 (put 'afourlatex 'texinfo-format 'texinfo-discard-line)
193 (put 'afourwide 'texinfo-format 'texinfo-discard-line)
194 (put 'afivepaper 'texinfo-format 'texinfo-discard-line)
195 (put 'pagesizes 'texinfo-format 'texinfo-discard-line-with-args)
196
197 ;; style
198 (put 'setchapternewpage 'texinfo-format 'texinfo-discard-line-with-args)
199 (put 'kbdinputstyle 'texinfo-format 'texinfo-discard-line-with-args)
200
201 ;; flags
202 (put 'setcontentsaftertitlepage 'texinfo-format 'texinfo-discard-line)
203 (put 'setshortcontentsaftertitlepage 'texinfo-format 'texinfo-discard-line)
204 (put 'novalidate 'texinfo-format 'texinfo-discard-line-with-args)
205
206 ;; head & foot
207 (put 'headings 'texinfo-format 'texinfo-discard-line-with-args)
208 (put 'evenfooting 'texinfo-format 'texinfo-discard-line-with-args)
209 (put 'evenheading 'texinfo-format 'texinfo-discard-line-with-args)
210 (put 'oddfooting 'texinfo-format 'texinfo-discard-line-with-args)
211 (put 'oddheading 'texinfo-format 'texinfo-discard-line-with-args)
212 (put 'everyfooting 'texinfo-format 'texinfo-discard-line-with-args)
213 (put 'everyheading 'texinfo-format 'texinfo-discard-line-with-args)
214
215 ;; misc
216 (put 'page 'texinfo-format 'texinfo-discard-line)
217 (put 'hyphenation 'texinfo-format 'texinfo-discard-command-and-arg)
218
219 ;; @slanted{} (makeinfo 4.8 or later)
220 (put 'slanted 'texinfo-format 'texinfo-format-noop)
221
222 ;; @tie{} (makeinfo 4.3 or later)
223 (put 'tie 'texinfo-format 'texinfo-format-tie)
224 (ptexinfmt-defun-if-void texinfo-format-tie ()
225   (texinfo-parse-arg-discard)
226   (insert " "))
227
228 \f
229 ;;; Directory File
230 ;; @direcategory
231 (put 'dircategory 'texinfo-format 'texinfo-format-dircategory)
232 (ptexinfmt-defun-if-void texinfo-format-dircategory ()
233   (let ((str (texinfo-parse-arg-discard)))
234     (delete-region (point)
235                    (progn
236                      (skip-chars-forward " ")
237                      (point)))
238     (insert "INFO-DIR-SECTION " str "\n")))
239
240 ;; @direntry
241 (put 'direntry 'texinfo-format 'texinfo-format-direntry)
242 (ptexinfmt-defun-if-void texinfo-format-direntry ()
243   (texinfo-push-stack 'direntry nil)
244   (texinfo-discard-line)
245   (insert "START-INFO-DIR-ENTRY\n"))
246
247 (put 'direntry 'texinfo-end 'texinfo-end-direntry)
248 (ptexinfmt-defun-if-void texinfo-end-direntry ()
249   (texinfo-discard-command)
250   (insert "END-INFO-DIR-ENTRY\n\n")
251   (texinfo-pop-stack 'direntry))
252
253
254 ;;; Block Enclosing
255 ;; @detailmenu ... @end detailmenu
256 (put 'detailmenu 'texinfo-format 'texinfo-discard-line)
257 (put 'detailmenu 'texinfo-end 'texinfo-discard-command)
258
259 ;; @smalldisplay ... @end smalldisplay
260 (put 'smalldisplay 'texinfo-format 'texinfo-format-example)
261 (put 'smalldisplay 'texinfo-end 'texinfo-end-example)
262
263 ;; @smallformat ... @end smallformat
264 (put 'smallformat 'texinfo-format 'texinfo-format-flushleft)
265 (put 'smallformat 'texinfo-end 'texinfo-end-flushleft)
266
267 ;; @cartouche  ... @end cartouche
268 (put 'cartouche 'texinfo-format 'texinfo-discard-line)
269 (put 'cartouche 'texinfo-end 'texinfo-discard-command)
270
271
272 ;;; Conditional
273 ;; @ifnottex ... @end ifnottex (makeinfo 3.11 or later)
274 (put 'ifnottex 'texinfo-format 'texinfo-discard-line)
275 (put 'ifnottex 'texinfo-end 'texinfo-discard-command)
276
277 ;; @ifnothtml ... @end ifnothtml (makeinfo 3.11 or later)
278 (put 'ifnothtml 'texinfo-format 'texinfo-discard-line)
279 (put 'ifnothtml 'texinfo-end 'texinfo-discard-command)
280
281 ;; @ifnotplaintext ... @end ifnotplaintext (makeinfo 4.2 or later)
282 (put 'ifnotplaintext 'texinfo-format 'texinfo-discard-line)
283 (put 'ifnotplaintext 'texinfo-end 'texinfo-discard-command)
284
285 ;; @ifnotdocbook ... @end ifnotdocbook (makeinfo 4.7 or later)
286 (put 'ifnotdocbook 'texinfo-format 'texinfo-discard-line)
287 (put 'ifnotdocbook 'texinfo-end 'texinfo-discard-command)
288
289
290 ;; @ifnotinfo ... @end ifnotinfo (makeinfo 3.11 or later)
291 (put 'ifnotinfo 'texinfo-format 'texinfo-format-ifnotinfo)
292 (ptexinfmt-defun-if-void texinfo-format-ifnotinfo ()
293   (delete-region texinfo-command-start
294                  (progn (re-search-forward "@end ifnotinfo[ \t]*\n")
295                         (point))))
296
297 ;; @html ... @end html (makeinfo 3.11 or later)
298 (put 'html 'texinfo-format 'texinfo-format-html)
299 (ptexinfmt-defun-if-void texinfo-format-html ()
300   (delete-region texinfo-command-start
301                  (progn (re-search-forward "@end html[ \t]*\n")
302                         (point))))
303
304 ;; @docbook ... @end docbook (makeinfo 4.7 or later)
305 (put 'docbook 'texinfo-format 'texinfo-format-docbook)
306 (ptexinfmt-defun-if-void texinfo-format-docbook ()
307   (delete-region texinfo-command-start
308                  (progn (re-search-forward "@end docbook[ \t]*\n")
309                         (point))))
310
311 ;; @ifhtml ... @end ifhtml (makeinfo 3.8 or later)
312 (put 'ifhtml 'texinfo-format 'texinfo-format-ifhtml)
313 (defun texinfo-format-ifhtml ()
314   (delete-region texinfo-command-start
315                  (progn (re-search-forward "@end ifhtml[ \t]*\n")
316                         (point))))
317
318 ;; @ifplaintext ... @end ifplaintext (makeinfo 4.2 or later)
319 (put 'ifplaintext 'texinfo-format 'texinfo-format-ifplaintext)
320 (ptexinfmt-defun-if-void texinfo-format-ifplaintext ()
321   (delete-region texinfo-command-start
322                  (progn (re-search-forward "@end ifplaintext[ \t]*\n")
323                         (point))))
324
325 ;; @ifdocbook ... @end ifdocbook (makeinfo 4.7 or later)
326 (put 'ifdocbook 'texinfo-format 'texinfo-format-ifdocbook)
327 (ptexinfmt-defun-if-void texinfo-format-ifdocbook ()
328   (delete-region texinfo-command-start
329                  (progn (re-search-forward "@end ifdocbook[ \t]*\n")
330                         (point))))
331
332
333 \f
334 ;;; Marking
335 ;; @indicateurl, @url, @env, @command, 
336 (put 'env 'texinfo-format 'texinfo-format-code)
337 (put 'command 'texinfo-format 'texinfo-format-code)
338
339 (put 'indicateurl 'texinfo-format 'texinfo-format-code)
340 (put 'url 'texinfo-format 'texinfo-format-uref) ; Texinfo 4.7
341
342 ;; @acronym
343 (put 'acronym 'texinfo-format 'texinfo-format-var)
344
345 (ptexinfmt-defun-if-broken texinfo-format-var ()
346   (let ((arg (texinfo-parse-expanded-arg)))
347     (texinfo-discard-command)
348     (insert (upcase arg))))
349
350 ;; @key
351 (put 'key 'texinfo-format 'texinfo-format-key)
352 (ptexinfmt-defun-if-void texinfo-format-key ()
353   (insert (texinfo-parse-arg-discard))
354   (goto-char texinfo-command-start))
355
356 ;; @email{EMAIL-ADDRESS[, DISPLAYED-TEXT]}
357 (put 'email 'texinfo-format 'texinfo-format-email)
358 (ptexinfmt-defun-if-void texinfo-format-email ()
359   "Format EMAIL-ADDRESS and optional DISPLAYED-TXT.
360 Insert < ... > around EMAIL-ADDRESS."
361   (let ((args (texinfo-format-parse-args)))
362   (texinfo-discard-command)
363     ;; if displayed-text
364     (if (nth 1 args)
365         (insert (nth 1 args) " <" (nth 0 args) ">")
366       (insert "<" (nth 0 args) ">"))))
367
368 ;; @option
369 (put 'option 'texinfo-format 'texinfo-format-option)
370 (ptexinfmt-defun-if-void texinfo-format-option ()
371   "Insert ` ... ' around arg unless inside a table; in that case, no quotes."
372   ;; `looking-at-backward' not available in v. 18.57, 20.2
373   ;; searched-for character is a control-H
374   (if (not (search-backward "\010"
375                             (save-excursion (beginning-of-line) (point))
376                             t))
377       (insert "`" (texinfo-parse-arg-discard) "'")
378     (insert (texinfo-parse-arg-discard)))
379   (goto-char texinfo-command-start))
380
381 ;; @verb{<char>TEXT<char>}  (makeinfo 4.1 or later)
382 (put 'verb 'texinfo-format 'texinfo-format-verb)
383 (ptexinfmt-defun-if-void texinfo-format-verb ()
384   "Format text between non-quoted unique delimiter characters verbatim.
385 Enclose the verbatim text, including the delimiters, in braces.  Print
386 text exactly as written (but not the delimiters) in a fixed-width.
387
388 For example, @verb\{|@|\} results in @ and
389 @verb\{+@'e?`!`+} results in @'e?`!`."
390
391   (let ((delimiter (buffer-substring-no-properties
392                     (1+ texinfo-command-end) (+ 2 texinfo-command-end))))
393     (unless (looking-at "{")
394       (error "Not found: @verb start brace"))
395     (delete-region texinfo-command-start (+ 2 texinfo-command-end))
396     (search-forward  delimiter))
397   (delete-backward-char 1)
398   (unless (looking-at "}")
399     (error "Not found: @verb end brace"))
400   (delete-char 1))
401
402 \f
403 ;;; @LaTeX, @registeredsymbol{}
404 (put 'LaTeX 'texinfo-format 'texinfo-format-LaTeX)
405 (ptexinfmt-defun-if-void texinfo-format-LaTeX ()
406   (texinfo-parse-arg-discard)
407   (insert "LaTeX"))
408
409 (put 'registeredsymbol 'texinfo-format 'texinfo-format-registeredsymbol)
410 (ptexinfmt-defun-if-void texinfo-format-registeredsymbol ()
411   (texinfo-parse-arg-discard)
412   (insert "(R)"))
413
414 ;;; Accents and Special characters
415 ;; @pounds{}    ==>     #       Pounds Sterling
416 (put 'pounds 'texinfo-format 'texinfo-format-pounds)
417 (ptexinfmt-defun-if-void texinfo-format-pounds ()
418   (texinfo-parse-arg-discard)
419   (insert "#"))
420
421 ;; @ordf{}      ==>     a       Spanish feminine
422 (put 'ordf 'texinfo-format 'texinfo-format-ordf)
423 (ptexinfmt-defun-if-void texinfo-format-ordf ()
424   (texinfo-parse-arg-discard)
425   (insert "o"))
426
427 ;; @ordm{}      ==>     o       Spanish masculine
428 (put 'ordm 'texinfo-format 'texinfo-format-ordm)
429 (ptexinfmt-defun-if-void texinfo-format-ordm ()
430   (texinfo-parse-arg-discard)
431   (insert "o"))
432
433 ;; @OE{}        ==>     OE      French-OE-ligature
434 (put 'OE 'texinfo-format 'texinfo-format-French-OE-ligature)
435 (ptexinfmt-defun-if-void texinfo-format-French-OE-ligature ()
436   (insert "OE" (texinfo-parse-arg-discard))
437   (goto-char texinfo-command-start))
438
439 ;; @oe{}        ==>     oe
440 (put 'oe 'texinfo-format 'texinfo-format-French-oe-ligature)
441 (ptexinfmt-defun-if-void texinfo-format-French-oe-ligature () ; lower case
442   (insert "oe" (texinfo-parse-arg-discard))
443   (goto-char texinfo-command-start))
444
445 ;; @AA{}        ==>     AA      Scandinavian-A-with-circle
446 (put 'AA 'texinfo-format 'texinfo-format-Scandinavian-A-with-circle)
447 (ptexinfmt-defun-if-void texinfo-format-Scandinavian-A-with-circle ()
448   (insert "AA" (texinfo-parse-arg-discard))
449   (goto-char texinfo-command-start))
450
451 ;; @aa{}        ==>     aa
452 (put 'aa 'texinfo-format 'texinfo-format-Scandinavian-a-with-circle)
453 (ptexinfmt-defun-if-void texinfo-format-Scandinavian-a-with-circle () ; lower case
454   (insert "aa" (texinfo-parse-arg-discard))
455   (goto-char texinfo-command-start))
456
457 ;; @AE{}        ==>     AE      Latin-Scandinavian-AE
458 (put 'AE 'texinfo-format 'texinfo-format-Latin-Scandinavian-AE)
459 (ptexinfmt-defun-if-void texinfo-format-Latin-Scandinavian-AE ()
460   (insert "AE" (texinfo-parse-arg-discard))
461   (goto-char texinfo-command-start))
462
463 ;; @ae{}        ==>     ae
464 (put 'ae 'texinfo-format 'texinfo-format-Latin-Scandinavian-ae)
465 (ptexinfmt-defun-if-void texinfo-format-Latin-Scandinavian-ae () ; lower case
466   (insert "ae" (texinfo-parse-arg-discard))
467   (goto-char texinfo-command-start))
468
469 ;; @ss{}        ==>     ss      German-sharp-S
470 (put 'ss 'texinfo-format 'texinfo-format-German-sharp-S)
471 (ptexinfmt-defun-if-void texinfo-format-German-sharp-S ()
472   (insert "ss" (texinfo-parse-arg-discard))
473   (goto-char texinfo-command-start))
474
475 ;; @questiondown{}      ==>     ?       upside-down-question-mark
476 (put 'questiondown 'texinfo-format 'texinfo-format-upside-down-question-mark)
477 (ptexinfmt-defun-if-void texinfo-format-upside-down-question-mark ()
478   (insert "?" (texinfo-parse-arg-discard))
479   (goto-char texinfo-command-start))
480
481 ;; @exclamdown{}        ==>     !       upside-down-exclamation-mark
482 (put 'exclamdown 'texinfo-format 'texinfo-format-upside-down-exclamation-mark)
483 (ptexinfmt-defun-if-void texinfo-format-upside-down-exclamation-mark ()
484   (insert "!" (texinfo-parse-arg-discard))
485   (goto-char texinfo-command-start))
486
487 ;; @L{}         ==>     L/      Polish suppressed-L (Lslash)
488 (put 'L 'texinfo-format 'texinfo-format-Polish-suppressed-L)
489 (ptexinfmt-defun-if-void texinfo-format-Polish-suppressed-L ()
490   (insert (texinfo-parse-arg-discard) "/L")
491   (goto-char texinfo-command-start))
492
493 ;; @l{}         ==>     l/      Polish suppressed-L (Lslash) (lower case)
494 (put 'l 'texinfo-format 'texinfo-format-Polish-suppressed-l-lower-case)
495 (ptexinfmt-defun-if-void texinfo-format-Polish-suppressed-l-lower-case ()
496   (insert (texinfo-parse-arg-discard) "/l")
497   (goto-char texinfo-command-start))
498
499 ;; @O{}         ==>     O/      Scandinavian O-with-slash
500 (put 'O 'texinfo-format 'texinfo-format-Scandinavian-O-with-slash)
501 (ptexinfmt-defun-if-void texinfo-format-Scandinavian-O-with-slash ()
502   (insert (texinfo-parse-arg-discard) "O/")
503   (goto-char texinfo-command-start))
504
505 ;; @o{}         ==>     o/      Scandinavian O-with-slash (lower case)
506 (put 'o 'texinfo-format 'texinfo-format-Scandinavian-o-with-slash-lower-case)
507 (ptexinfmt-defun-if-void texinfo-format-Scandinavian-o-with-slash-lower-case ()
508   (insert (texinfo-parse-arg-discard) "o/")
509   (goto-char texinfo-command-start))
510
511 ;; @,{c}        ==>     c,      cedilla accent
512 (put ', 'texinfo-format 'texinfo-format-cedilla-accent)
513 (ptexinfmt-defun-if-void texinfo-format-cedilla-accent ()
514   (insert (texinfo-parse-arg-discard) ",")
515   (goto-char texinfo-command-start))
516
517
518 ;; @dotaccent{o}        ==>     .o      overdot-accent
519 (put 'dotaccent 'texinfo-format 'texinfo-format-overdot-accent)
520 (ptexinfmt-defun-if-void texinfo-format-overdot-accent ()
521   (insert "." (texinfo-parse-arg-discard))
522   (goto-char texinfo-command-start))
523
524 ;; @ubaraccent{o}       ==>     _o      underbar-accent
525 (put 'ubaraccent 'texinfo-format 'texinfo-format-underbar-accent)
526 (ptexinfmt-defun-if-void texinfo-format-underbar-accent ()
527   (insert "_" (texinfo-parse-arg-discard))
528   (goto-char texinfo-command-start))
529
530 ;; @udotaccent{o}       ==>     o-.     underdot-accent
531 (put 'udotaccent 'texinfo-format 'texinfo-format-underdot-accent)
532 (ptexinfmt-defun-if-void texinfo-format-underdot-accent ()
533   (insert (texinfo-parse-arg-discard) "-.")
534   (goto-char texinfo-command-start))
535
536 ;; @H{o}        ==>     ""o     long Hungarian umlaut
537 (put 'H 'texinfo-format 'texinfo-format-long-Hungarian-umlaut)
538 (ptexinfmt-defun-if-void texinfo-format-long-Hungarian-umlaut ()
539   (insert "\"\"" (texinfo-parse-arg-discard))
540   (goto-char texinfo-command-start))
541
542 ;; @ringaccent{o}       ==>     *o      ring accent
543 (put 'ringaccent 'texinfo-format 'texinfo-format-ring-accent)
544 (ptexinfmt-defun-if-void texinfo-format-ring-accent ()
545   (insert "*" (texinfo-parse-arg-discard))
546   (goto-char texinfo-command-start))
547
548 ;; @tieaccent{oo}       ==>     [oo     tie after accent
549 (put 'tieaccent 'texinfo-format 'texinfo-format-tie-after-accent)
550 (ptexinfmt-defun-if-void texinfo-format-tie-after-accent ()
551   (insert "[" (texinfo-parse-arg-discard))
552   (goto-char texinfo-command-start))
553
554 ;; @u{o}        ==>     (o      breve accent
555 (put 'u 'texinfo-format 'texinfo-format-breve-accent)
556 (ptexinfmt-defun-if-void texinfo-format-breve-accent ()
557   (insert "(" (texinfo-parse-arg-discard))
558   (goto-char texinfo-command-start))
559
560 ;; @v{o}        ==>     <o      hacek accent
561 (put 'v 'texinfo-format 'texinfo-format-hacek-accent)
562 (ptexinfmt-defun-if-void texinfo-format-hacek-accent ()
563   (insert "<" (texinfo-parse-arg-discard))
564   (goto-char texinfo-command-start))
565
566 ;; @dotless{i}  ==>     i       dotless i and dotless j
567 (put 'dotless 'texinfo-format 'texinfo-format-dotless)
568 (ptexinfmt-defun-if-void texinfo-format-dotless ()
569   (insert (texinfo-parse-arg-discard))
570   (goto-char texinfo-command-start))
571
572 ;; @.
573 (put '\. 'texinfo-format 'texinfo-format-\.)
574 (ptexinfmt-defun-if-void texinfo-format-\. ()
575   (texinfo-discard-command)
576   (insert "."))
577
578 ;; @:
579 (put '\: 'texinfo-format 'texinfo-format-\:)
580 (ptexinfmt-defun-if-void texinfo-format-\: ()
581   (texinfo-discard-command))
582
583 ;; @-
584 (put '\- 'texinfo-format 'texinfo-format-soft-hyphen)
585 (ptexinfmt-defun-if-void texinfo-format-soft-hyphen ()
586   (texinfo-discard-command))
587
588 ;; @/
589 (put '\/ 'texinfo-format 'texinfo-format-\/)
590 (ptexinfmt-defun-if-void texinfo-format-\/ ()
591   (texinfo-discard-command))
592
593 \f
594 ;;; Cross References
595 ;; @ref, @xref
596 (put 'ref 'texinfo-format 'texinfo-format-xref)
597
598 (ptexinfmt-defun-if-broken texinfo-format-xref ()
599   (let ((args (texinfo-format-parse-args)))
600     (texinfo-discard-command)
601     (insert "*Note ")
602     (let ((fname (or (nth 1 args) (nth 2 args))))
603       (if (null (or fname (nth 3 args)))
604           (insert (nth 0 args) "::")
605         (insert (or fname (nth 0 args)) ": ")
606         (if (nth 3 args)
607             (insert "(" (nth 3 args) ")"))
608         (unless (null (nth 0 args))
609           (insert (nth 0 args)))))))
610
611 ;; @uref{URL [,TEXT] [,REPLACEMENT]}
612 (put 'uref 'texinfo-format 'texinfo-format-uref)
613 (ptexinfmt-defun-if-broken texinfo-format-uref ()
614   "Format URL and optional URL-TITLE.
615 Insert ` ... ' around URL if no URL-TITLE argument;
616 otherwise, insert URL-TITLE followed by URL in parentheses."
617   (let ((args (texinfo-format-parse-args)))
618     (texinfo-discard-command)
619     ;; if url-title
620     (if (nth 1 args)
621         (insert  (nth 1 args) " (" (nth 0 args) ")")
622       (insert "`" (nth 0 args) "'"))))
623
624 ;; @inforef
625 (put 'inforef 'texinfo-format 'texinfo-format-inforef)
626 (ptexinfmt-defun-if-void texinfo-format-inforef ()
627   (let ((args (texinfo-format-parse-args)))
628     (texinfo-discard-command)
629     (if (nth 1 args)
630         (insert "*Note " (nth 1 args) ": (" (nth 2 args) ")" (car args))
631       (insert "*Note " "(" (nth 2 args) ")" (car args) "::"))))
632
633
634 ;; @anchor
635 ;; don't emulation
636 ;; If support @anchor for Mule 2.3, We must fix informat.el and info.el:
637 ;;  - Info-tagify suport @anthor-*-refill.
638 ;;  - info.el support Ref in Tag table.
639 (unless (get 'anchor 'texinfo-format)
640   (put 'anchor 'texinfo-format 'texinfo-discard-command-and-arg))
641
642
643 \f
644 ;;; New command definition
645 ;; @alias NEW=EXISTING
646 (put 'alias 'texinfo-format 'texinfo-alias)
647 (ptexinfmt-defun-if-void texinfo-alias ()
648   (let ((start (1- (point)))
649         args)
650     (skip-chars-forward " ")
651     (save-excursion (end-of-line) (setq texinfo-command-end (point)))
652     (if (not (looking-at "\\([^=]+\\)=\\(.*\\)"))
653         (error "Invalid alias command")
654       (setq texinfo-alias-list
655             (cons
656              (cons
657               (buffer-substring (match-beginning 1) (match-end 1))
658               (buffer-substring (match-beginning 2) (match-end 2)))
659              texinfo-alias-list))
660       (texinfo-discard-command))))
661
662 \f
663 ;;; Indent
664 ;; @exampleindent INDENT  (makeinfo 4.0 or later)
665
666 ;; @paragraphindent INDENT  (makeinfo 4.0 or later)
667 ;; INDENT: asis, 0, n
668
669 ;; @firstparagraphindent WORD   (makeinfo 4.6 or later)
670 ;; WORD: none, insert
671
672
673 \f
674 ;;; Special
675 ;; @image{FILENAME [, WIDTH] [, HEIGHT]}
676 (put 'image 'texinfo-format 'texinfo-format-image)
677 (ptexinfmt-defun-if-void texinfo-format-image ()
678   ;; I don't know makeinfo parse FILENAME.
679   (let ((args (texinfo-format-parse-args))
680         filename)
681     (when (null (nth 0 args))
682       (error "Invalid image command"))
683     (texinfo-discard-command)
684     ;; makeinfo uses FILENAME.txt
685     (setq filename (format "%s.txt" (nth 0 args)))
686     (message "Reading included file: %s" filename)
687     ;; verbatim for Info output
688     (goto-char (+ (point) (cadr (insert-file-contents filename))))
689     (message "Reading included file: %s...done" filename)))
690
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