741d9b391a4f8053e4873bbd734bfe9d04f8c48e
[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
299 ;; @ifnotinfo ... @end ifnotinfo (makeinfo 3.11 or later)
300 (put 'ifnotinfo 'texinfo-format 'texinfo-format-ifnotinfo)
301 (ptexinfmt-defun-if-void texinfo-format-ifnotinfo ()
302   (delete-region texinfo-command-start
303                  (progn (re-search-forward "@end ifnotinfo[ \t]*\n")
304                         (point))))
305
306 ;; @html ... @end html (makeinfo 3.11 or later)
307 (put 'html 'texinfo-format 'texinfo-format-html)
308 (ptexinfmt-defun-if-void texinfo-format-html ()
309   (delete-region texinfo-command-start
310                  (progn (re-search-forward "@end html[ \t]*\n")
311                         (point))))
312
313 ;; @docbook ... @end docbook (makeinfo 4.7 or later)
314 (put 'docbook 'texinfo-format 'texinfo-format-docbook)
315 (ptexinfmt-defun-if-void texinfo-format-docbook ()
316   (delete-region texinfo-command-start
317                  (progn (re-search-forward "@end docbook[ \t]*\n")
318                         (point))))
319
320 ;; @ifhtml ... @end ifhtml (makeinfo 3.8 or later)
321 (put 'ifhtml 'texinfo-format 'texinfo-format-ifhtml)
322 (defun texinfo-format-ifhtml ()
323   (delete-region texinfo-command-start
324                  (progn (re-search-forward "@end ifhtml[ \t]*\n")
325                         (point))))
326
327 ;; @ifplaintext ... @end ifplaintext (makeinfo 4.2 or later)
328 (put 'ifplaintext 'texinfo-format 'texinfo-format-ifplaintext)
329 (ptexinfmt-defun-if-void texinfo-format-ifplaintext ()
330   (delete-region texinfo-command-start
331                  (progn (re-search-forward "@end ifplaintext[ \t]*\n")
332                         (point))))
333
334 ;; @ifdocbook ... @end ifdocbook (makeinfo 4.7 or later)
335 (put 'ifdocbook 'texinfo-format 'texinfo-format-ifdocbook)
336 (ptexinfmt-defun-if-void texinfo-format-ifdocbook ()
337   (delete-region texinfo-command-start
338                  (progn (re-search-forward "@end ifdocbook[ \t]*\n")
339                         (point))))
340
341
342 \f
343 ;;; Marking
344 ;; @indicateurl, @url, @env, @command, 
345 (put 'env 'texinfo-format 'texinfo-format-code)
346 (put 'command 'texinfo-format 'texinfo-format-code)
347
348 (put 'indicateurl 'texinfo-format 'texinfo-format-code)
349 (put 'url 'texinfo-format 'texinfo-format-uref) ; Texinfo 4.7
350
351 ;; @acronym
352 (put 'acronym 'texinfo-format 'texinfo-format-var)
353
354 (ptexinfmt-defun-if-broken texinfo-format-var ()
355   (let ((arg (texinfo-parse-expanded-arg)))
356     (texinfo-discard-command)
357     (insert (upcase arg))))
358
359 ;; @key
360 (put 'key 'texinfo-format 'texinfo-format-key)
361 (ptexinfmt-defun-if-void texinfo-format-key ()
362   (insert (texinfo-parse-arg-discard))
363   (goto-char texinfo-command-start))
364
365 ;; @email{EMAIL-ADDRESS[, DISPLAYED-TEXT]}
366 (put 'email 'texinfo-format 'texinfo-format-email)
367 (ptexinfmt-defun-if-void texinfo-format-email ()
368   "Format EMAIL-ADDRESS and optional DISPLAYED-TXT.
369 Insert < ... > around EMAIL-ADDRESS."
370   (let ((args (texinfo-format-parse-args)))
371   (texinfo-discard-command)
372     ;; if displayed-text
373     (if (nth 1 args)
374         (insert (nth 1 args) " <" (nth 0 args) ">")
375       (insert "<" (nth 0 args) ">"))))
376
377 ;; @option
378 (put 'option 'texinfo-format 'texinfo-format-option)
379 (ptexinfmt-defun-if-void texinfo-format-option ()
380   "Insert ` ... ' around arg unless inside a table; in that case, no quotes."
381   ;; `looking-at-backward' not available in v. 18.57, 20.2
382   ;; searched-for character is a control-H
383   (if (not (search-backward "\010"
384                             (save-excursion (beginning-of-line) (point))
385                             t))
386       (insert "`" (texinfo-parse-arg-discard) "'")
387     (insert (texinfo-parse-arg-discard)))
388   (goto-char texinfo-command-start))
389
390 ;; @verb{<char>TEXT<char>}  (makeinfo 4.1 or later)
391 (put 'verb 'texinfo-format 'texinfo-format-verb)
392 (ptexinfmt-defun-if-void texinfo-format-verb ()
393   "Format text between non-quoted unique delimiter characters verbatim.
394 Enclose the verbatim text, including the delimiters, in braces.  Print
395 text exactly as written (but not the delimiters) in a fixed-width.
396
397 For example, @verb\{|@|\} results in @ and
398 @verb\{+@'e?`!`+} results in @'e?`!`."
399
400   (let ((delimiter (buffer-substring-no-properties
401                     (1+ texinfo-command-end) (+ 2 texinfo-command-end))))
402     (unless (looking-at "{")
403       (error "Not found: @verb start brace"))
404     (delete-region texinfo-command-start (+ 2 texinfo-command-end))
405     (search-forward  delimiter))
406   (delete-backward-char 1)
407   (unless (looking-at "}")
408     (error "Not found: @verb end brace"))
409   (delete-char 1))
410
411 \f
412 ;;; @LaTeX, @registeredsymbol{}
413 (put 'LaTeX 'texinfo-format 'texinfo-format-LaTeX)
414 (ptexinfmt-defun-if-void texinfo-format-LaTeX ()
415   (texinfo-parse-arg-discard)
416   (insert "LaTeX"))
417
418 (put 'registeredsymbol 'texinfo-format 'texinfo-format-registeredsymbol)
419 (ptexinfmt-defun-if-void texinfo-format-registeredsymbol ()
420   (texinfo-parse-arg-discard)
421   (insert "(R)"))
422
423 ;;; Accents and Special characters
424 ;; @euro{}      ==>     Euro
425 (put 'euro 'texinfo-format 'texinfo-format-euro)
426 (ptexinfmt-defun-if-void texinfo-format-euro ()
427   (texinfo-parse-arg-discard)
428   (insert "Euro "))
429
430 ;; @pounds{}    ==>     #       Pounds Sterling
431 (put 'pounds 'texinfo-format 'texinfo-format-pounds)
432 (ptexinfmt-defun-if-void texinfo-format-pounds ()
433   (texinfo-parse-arg-discard)
434   (insert "#"))
435
436 ;; @ordf{}      ==>     a       Spanish feminine
437 (put 'ordf 'texinfo-format 'texinfo-format-ordf)
438 (ptexinfmt-defun-if-void texinfo-format-ordf ()
439   (texinfo-parse-arg-discard)
440   (insert "a"))
441
442 ;; @ordm{}      ==>     o       Spanish masculine
443 (put 'ordm 'texinfo-format 'texinfo-format-ordm)
444 (ptexinfmt-defun-if-void texinfo-format-ordm ()
445   (texinfo-parse-arg-discard)
446   (insert "o"))
447
448 ;; @OE{}        ==>     OE      French-OE-ligature
449 (put 'OE 'texinfo-format 'texinfo-format-French-OE-ligature)
450 (ptexinfmt-defun-if-void texinfo-format-French-OE-ligature ()
451   (insert "OE" (texinfo-parse-arg-discard))
452   (goto-char texinfo-command-start))
453
454 ;; @oe{}        ==>     oe
455 (put 'oe 'texinfo-format 'texinfo-format-French-oe-ligature)
456 (ptexinfmt-defun-if-void texinfo-format-French-oe-ligature () ; lower case
457   (insert "oe" (texinfo-parse-arg-discard))
458   (goto-char texinfo-command-start))
459
460 ;; @AA{}        ==>     AA      Scandinavian-A-with-circle
461 (put 'AA 'texinfo-format 'texinfo-format-Scandinavian-A-with-circle)
462 (ptexinfmt-defun-if-void texinfo-format-Scandinavian-A-with-circle ()
463   (insert "AA" (texinfo-parse-arg-discard))
464   (goto-char texinfo-command-start))
465
466 ;; @aa{}        ==>     aa
467 (put 'aa 'texinfo-format 'texinfo-format-Scandinavian-a-with-circle)
468 (ptexinfmt-defun-if-void texinfo-format-Scandinavian-a-with-circle () ; lower case
469   (insert "aa" (texinfo-parse-arg-discard))
470   (goto-char texinfo-command-start))
471
472 ;; @AE{}        ==>     AE      Latin-Scandinavian-AE
473 (put 'AE 'texinfo-format 'texinfo-format-Latin-Scandinavian-AE)
474 (ptexinfmt-defun-if-void texinfo-format-Latin-Scandinavian-AE ()
475   (insert "AE" (texinfo-parse-arg-discard))
476   (goto-char texinfo-command-start))
477
478 ;; @ae{}        ==>     ae
479 (put 'ae 'texinfo-format 'texinfo-format-Latin-Scandinavian-ae)
480 (ptexinfmt-defun-if-void texinfo-format-Latin-Scandinavian-ae () ; lower case
481   (insert "ae" (texinfo-parse-arg-discard))
482   (goto-char texinfo-command-start))
483
484 ;; @ss{}        ==>     ss      German-sharp-S
485 (put 'ss 'texinfo-format 'texinfo-format-German-sharp-S)
486 (ptexinfmt-defun-if-void texinfo-format-German-sharp-S ()
487   (insert "ss" (texinfo-parse-arg-discard))
488   (goto-char texinfo-command-start))
489
490 ;; @questiondown{}      ==>     ?       upside-down-question-mark
491 (put 'questiondown 'texinfo-format 'texinfo-format-upside-down-question-mark)
492 (ptexinfmt-defun-if-void texinfo-format-upside-down-question-mark ()
493   (insert "?" (texinfo-parse-arg-discard))
494   (goto-char texinfo-command-start))
495
496 ;; @exclamdown{}        ==>     !       upside-down-exclamation-mark
497 (put 'exclamdown 'texinfo-format 'texinfo-format-upside-down-exclamation-mark)
498 (ptexinfmt-defun-if-void texinfo-format-upside-down-exclamation-mark ()
499   (insert "!" (texinfo-parse-arg-discard))
500   (goto-char texinfo-command-start))
501
502 ;; @L{}         ==>     L/      Polish suppressed-L (Lslash)
503 (put 'L 'texinfo-format 'texinfo-format-Polish-suppressed-L)
504 (ptexinfmt-defun-if-void texinfo-format-Polish-suppressed-L ()
505   (insert (texinfo-parse-arg-discard) "/L")
506   (goto-char texinfo-command-start))
507
508 ;; @l{}         ==>     l/      Polish suppressed-L (Lslash) (lower case)
509 (put 'l 'texinfo-format 'texinfo-format-Polish-suppressed-l-lower-case)
510 (ptexinfmt-defun-if-void texinfo-format-Polish-suppressed-l-lower-case ()
511   (insert (texinfo-parse-arg-discard) "/l")
512   (goto-char texinfo-command-start))
513
514 ;; @O{}         ==>     O/      Scandinavian O-with-slash
515 (put 'O 'texinfo-format 'texinfo-format-Scandinavian-O-with-slash)
516 (ptexinfmt-defun-if-void texinfo-format-Scandinavian-O-with-slash ()
517   (insert (texinfo-parse-arg-discard) "O/")
518   (goto-char texinfo-command-start))
519
520 ;; @o{}         ==>     o/      Scandinavian O-with-slash (lower case)
521 (put 'o 'texinfo-format 'texinfo-format-Scandinavian-o-with-slash-lower-case)
522 (ptexinfmt-defun-if-void texinfo-format-Scandinavian-o-with-slash-lower-case ()
523   (insert (texinfo-parse-arg-discard) "o/")
524   (goto-char texinfo-command-start))
525
526 ;; @,{c}        ==>     c,      cedilla accent
527 (put '\, 'texinfo-format 'texinfo-format-cedilla-accent)
528 (ptexinfmt-defun-if-void texinfo-format-cedilla-accent ()
529   (insert (texinfo-parse-arg-discard) ",")
530   (goto-char texinfo-command-start))
531
532
533 ;; @dotaccent{o}        ==>     .o      overdot-accent
534 (put 'dotaccent 'texinfo-format 'texinfo-format-overdot-accent)
535 (ptexinfmt-defun-if-void texinfo-format-overdot-accent ()
536   (insert "." (texinfo-parse-arg-discard))
537   (goto-char texinfo-command-start))
538
539 ;; @ubaraccent{o}       ==>     _o      underbar-accent
540 (put 'ubaraccent 'texinfo-format 'texinfo-format-underbar-accent)
541 (ptexinfmt-defun-if-void texinfo-format-underbar-accent ()
542   (insert "_" (texinfo-parse-arg-discard))
543   (goto-char texinfo-command-start))
544
545 ;; @udotaccent{o}       ==>     o-.     underdot-accent
546 (put 'udotaccent 'texinfo-format 'texinfo-format-underdot-accent)
547 (ptexinfmt-defun-if-void texinfo-format-underdot-accent ()
548   (insert (texinfo-parse-arg-discard) "-.")
549   (goto-char texinfo-command-start))
550
551 ;; @H{o}        ==>     ""o     long Hungarian umlaut
552 (put 'H 'texinfo-format 'texinfo-format-long-Hungarian-umlaut)
553 (ptexinfmt-defun-if-void texinfo-format-long-Hungarian-umlaut ()
554   (insert "\"\"" (texinfo-parse-arg-discard))
555   (goto-char texinfo-command-start))
556
557 ;; @ringaccent{o}       ==>     *o      ring accent
558 (put 'ringaccent 'texinfo-format 'texinfo-format-ring-accent)
559 (ptexinfmt-defun-if-void texinfo-format-ring-accent ()
560   (insert "*" (texinfo-parse-arg-discard))
561   (goto-char texinfo-command-start))
562
563 ;; @tieaccent{oo}       ==>     [oo     tie after accent
564 (put 'tieaccent 'texinfo-format 'texinfo-format-tie-after-accent)
565 (ptexinfmt-defun-if-void texinfo-format-tie-after-accent ()
566   (insert "[" (texinfo-parse-arg-discard))
567   (goto-char texinfo-command-start))
568
569 ;; @u{o}        ==>     (o      breve accent
570 (put 'u 'texinfo-format 'texinfo-format-breve-accent)
571 (ptexinfmt-defun-if-void texinfo-format-breve-accent ()
572   (insert "(" (texinfo-parse-arg-discard))
573   (goto-char texinfo-command-start))
574
575 ;; @v{o}        ==>     <o      hacek accent
576 (put 'v 'texinfo-format 'texinfo-format-hacek-accent)
577 (ptexinfmt-defun-if-void texinfo-format-hacek-accent ()
578   (insert "<" (texinfo-parse-arg-discard))
579   (goto-char texinfo-command-start))
580
581 ;; @dotless{i}  ==>     i       dotless i and dotless j
582 (put 'dotless 'texinfo-format 'texinfo-format-dotless)
583 (ptexinfmt-defun-if-void texinfo-format-dotless ()
584   (insert (texinfo-parse-arg-discard))
585   (goto-char texinfo-command-start))
586
587 ;; @.
588 (put '\. 'texinfo-format 'texinfo-format-\.)
589 (ptexinfmt-defun-if-void texinfo-format-\. ()
590   (texinfo-discard-command)
591   (insert "."))
592
593 ;; @:
594 (put '\: 'texinfo-format 'texinfo-format-\:)
595 (ptexinfmt-defun-if-void texinfo-format-\: ()
596   (texinfo-discard-command))
597
598 ;; @-
599 (put '\- 'texinfo-format 'texinfo-format-soft-hyphen)
600 (ptexinfmt-defun-if-void texinfo-format-soft-hyphen ()
601   (texinfo-discard-command))
602
603 ;; @/
604 (put '\/ 'texinfo-format 'texinfo-format-\/)
605 (ptexinfmt-defun-if-void texinfo-format-\/ ()
606   (texinfo-discard-command))
607
608 ;; @textdegree
609 (put 'textdegree 'texinfo-format 'texinfo-format-textdegree)
610 (ptexinfmt-defun-if-void texinfo-format-textdegree ()
611   (insert "o" (texinfo-parse-arg-discard))
612   (goto-char texinfo-command-start))
613
614 \f
615 ;;; Cross References
616 ;; @ref, @xref
617 (put 'ref 'texinfo-format 'texinfo-format-xref)
618
619 (ptexinfmt-defun-if-broken texinfo-format-xref ()
620   (let ((args (texinfo-format-parse-args)))
621     (texinfo-discard-command)
622     (insert "*Note ")
623     (let ((fname (or (nth 1 args) (nth 2 args))))
624       (if (null (or fname (nth 3 args)))
625           (insert (nth 0 args) "::")
626         (insert (or fname (nth 0 args)) ": ")
627         (if (nth 3 args)
628             (insert "(" (nth 3 args) ")"))
629         (unless (null (nth 0 args))
630           (insert (nth 0 args)))))))
631
632 ;; @uref{URL [,TEXT] [,REPLACEMENT]}
633 (put 'uref 'texinfo-format 'texinfo-format-uref)
634 (ptexinfmt-defun-if-broken texinfo-format-uref ()
635   "Format URL and optional URL-TITLE.
636 Insert ` ... ' around URL if no URL-TITLE argument;
637 otherwise, insert URL-TITLE followed by URL in parentheses."
638   (let ((args (texinfo-format-parse-args)))
639     (texinfo-discard-command)
640     ;; if url-title
641     (if (nth 1 args)
642         (insert  (nth 1 args) " (" (nth 0 args) ")")
643       (insert "`" (nth 0 args) "'"))))
644
645 ;; @inforef
646 (put 'inforef 'texinfo-format 'texinfo-format-inforef)
647 (ptexinfmt-defun-if-void texinfo-format-inforef ()
648   (let ((args (texinfo-format-parse-args)))
649     (texinfo-discard-command)
650     (if (nth 1 args)
651         (insert "*Note " (nth 1 args) ": (" (nth 2 args) ")" (car args))
652       (insert "*Note " "(" (nth 2 args) ")" (car args) "::"))))
653
654
655 ;; @anchor
656 ;; don't emulation
657 ;; If support @anchor for Mule 2.3, We must fix informat.el and info.el:
658 ;;  - Info-tagify suport @anthor-*-refill.
659 ;;  - info.el support Ref in Tag table.
660 (unless (get 'anchor 'texinfo-format)
661   (put 'anchor 'texinfo-format 'texinfo-discard-command-and-arg))
662
663
664 \f
665 ;;; New command definition
666 ;; @alias NEW=EXISTING
667 (put 'alias 'texinfo-format 'texinfo-alias)
668 (ptexinfmt-defun-if-void texinfo-alias ()
669   (let ((start (1- (point)))
670         args)
671     (skip-chars-forward " ")
672     (save-excursion (end-of-line) (setq texinfo-command-end (point)))
673     (if (not (looking-at "\\([^=]+\\)=\\(.*\\)"))
674         (error "Invalid alias command")
675       (setq texinfo-alias-list
676             (cons
677              (cons
678               (buffer-substring (match-beginning 1) (match-end 1))
679               (buffer-substring (match-beginning 2) (match-end 2)))
680              texinfo-alias-list))
681       (texinfo-discard-command))))
682
683 \f
684 ;;; Indent
685 ;; @exampleindent INDENT  (makeinfo 4.0 or later)
686
687 ;; @paragraphindent INDENT  (makeinfo 4.0 or later)
688 ;; INDENT: asis, 0, n
689
690 ;; @firstparagraphindent WORD   (makeinfo 4.6 or later)
691 ;; WORD: none, insert
692
693
694 \f
695 ;;; Special
696 ;; @image{FILENAME [, WIDTH] [, HEIGHT]}
697 (put 'image 'texinfo-format 'texinfo-format-image)
698 (ptexinfmt-defun-if-void texinfo-format-image ()
699   ;; I don't know makeinfo parse FILENAME.
700   (let ((args (texinfo-format-parse-args))
701         filename)
702     (when (null (nth 0 args))
703       (error "Invalid image command"))
704     (texinfo-discard-command)
705     ;; makeinfo uses FILENAME.txt
706     (setq filename (format "%s.txt" (nth 0 args)))
707     (message "Reading included file: %s" filename)
708     ;; verbatim for Info output
709     (goto-char (+ (point) (cadr (insert-file-contents filename))))
710     (message "Reading included file: %s...done" filename)))
711
712
713 \f
714 ;;; @multitable ... @end multitable
715 (ptexinfmt-defvar-if-void texinfo-extra-inter-column-width 0
716   "*Number of extra spaces between entries (columns) in @multitable.")
717
718 (ptexinfmt-defvar-if-void texinfo-multitable-buffer-name
719   "*multitable-temporary-buffer*")
720 (ptexinfmt-defvar-if-void texinfo-multitable-rectangle-name
721   "texinfo-multitable-temp-")
722
723 ;; These commands are defined in texinfo.tex for printed output.
724 (put 'multitableparskip 'texinfo-format 'texinfo-discard-line-with-args)
725 (put 'multitableparindent 'texinfo-format 'texinfo-discard-line-with-args)
726 (put 'multitablecolmargin 'texinfo-format 'texinfo-discard-line-with-args)
727 (put 'multitablelinespace 'texinfo-format 'texinfo-discard-line-with-args)
728
729 (put 'multitable 'texinfo-format 'texinfo-multitable)
730
731 (ptexinfmt-defun-if-void texinfo-multitable ()
732   "Produce multi-column tables."
733
734 ;; This function pushes information onto the `texinfo-stack'.
735 ;; A stack element consists of:
736 ;;   - type-of-command, i.e., multitable
737 ;;   - the information about column widths, and
738 ;;   - the position of texinfo-command-start.
739 ;; e.g., ('multitable (1 2 3 4) 123)
740 ;; The command line is then deleted.
741   (texinfo-push-stack
742    'multitable
743    ;; push width information on stack
744    (texinfo-multitable-widths))
745   (texinfo-discard-line-with-args))
746
747 (put 'multitable 'texinfo-end 'texinfo-end-multitable)
748 (ptexinfmt-defun-if-void texinfo-end-multitable ()
749   "Discard the @end multitable line and pop the stack of multitable."
750   (texinfo-discard-command)
751   (texinfo-pop-stack 'multitable))
752
753 (ptexinfmt-defun-if-broken texinfo-multitable-widths ()
754   "Return list of widths of each column in a multi-column table."
755   (let (texinfo-multitable-width-list)
756     ;; Fractions format:
757     ;;  @multitable @columnfractions .25 .3 .45
758     ;;
759     ;; Template format:
760     ;;  @multitable {Column 1 template} {Column 2} {Column 3 example}
761     ;; Place point before first argument
762     (skip-chars-forward " \t")
763     (cond
764      ;; Check for common misspelling
765      ((looking-at "@columnfraction ")
766       (error "In @multitable, @columnfractions misspelled"))
767      ;; Case 1: @columnfractions .25 .3 .45
768      ((looking-at "@columnfractions")
769       (forward-word 1)
770       (while (not (eolp))
771         (setq texinfo-multitable-width-list
772               (cons
773                (truncate
774                 (1-
775                  (* fill-column (read (get-buffer (current-buffer))))))
776                texinfo-multitable-width-list))))
777      ;;
778      ;; Case 2: {Column 1 template} {Column 2} {Column 3 example}
779      ((looking-at "{")
780       (let ((start-of-templates (point)))
781         (while (not (eolp))
782           (skip-chars-forward " \t")
783           (let* ((start-of-template (1+ (point)))
784                  (end-of-template
785                   ;; forward-sexp works with braces in Texinfo mode
786                   (progn (forward-sexp 1) (1- (point)))))
787             (setq texinfo-multitable-width-list
788                   (cons (- (progn
789                              (goto-char end-of-template)
790                              (current-column))
791                            (progn
792                              (goto-char start-of-template)
793                              (current-column)))
794                         texinfo-multitable-width-list))
795             ;; Remove carriage return from within a template, if any.
796             ;; This helps those those who want to use more than
797             ;; one line's worth of words in @multitable line.
798             (narrow-to-region start-of-template end-of-template)
799             (goto-char (point-min))
800             (while (search-forward "\n" nil t)
801               (delete-char -1))
802             (goto-char (point-max))
803             (widen)
804             (forward-char 1)))))
805      ;;
806      ;; Case 3: Trouble
807      (t
808       (error "\
809 You probably need to specify column widths for @multitable correctly")))
810     ;; Check whether columns fit on page.
811     (let ((desired-columns
812            (+
813             ;; between column spaces
814             (length texinfo-multitable-width-list)
815             ;; additional between column spaces, if any
816             texinfo-extra-inter-column-width
817             ;; sum of spaces for each entry
818             (apply '+ texinfo-multitable-width-list))))
819       (if (> desired-columns fill-column)
820           (error (format "\
821 Multi-column table width, %d chars, is greater than page width, %d chars."
822                          desired-columns fill-column))))
823     texinfo-multitable-width-list))
824
825 ;; @item  A1  @tab  A2  @tab  A3
826 (ptexinfmt-defun-if-void texinfo-multitable-extract-row ()
827   "Return multitable row, as a string.
828 End of row is beginning of next @item or beginning of @end.
829 Cells within rows are separated by @tab."
830   (skip-chars-forward " \t")
831   (let* ((start (point))
832          (end (progn
833                 (re-search-forward "@item\\|@end")
834                 (match-beginning 0)))
835          (row (progn (goto-char end)
836                      (skip-chars-backward " ")
837                      ;; remove whitespace at end of argument
838                      (delete-region (point) end)
839                      (buffer-substring start (point)))))
840     (delete-region texinfo-command-start end)
841     row))
842
843 (put 'multitable 'texinfo-item 'texinfo-multitable-item)
844 (ptexinfmt-defun-if-void texinfo-multitable-item ()
845   "Format a row within a multicolumn table.
846 Cells in row are separated by @tab.
847 Widths of cells are specified by the arguments in the @multitable line.
848 All cells are made to be the same height.
849 This command is executed when texinfmt sees @item inside @multitable."
850   (let ((original-buffer (current-buffer))
851         (table-widths (reverse (car (cdr (car texinfo-stack)))))
852         (existing-fill-column fill-column)
853         start
854         end
855         (table-column       0)
856         (table-entry-height 0)
857         ;; unformatted row looks like:  A1  @tab  A2  @tab  A3
858         ;; extract-row command deletes the source line in the table.
859         (unformated-row (texinfo-multitable-extract-row)))
860     ;; Use a temporary buffer
861     (set-buffer (get-buffer-create texinfo-multitable-buffer-name))
862     (delete-region (point-min) (point-max))
863     (insert unformated-row)
864     (goto-char (point-min))
865 ;; 1. Check for correct number of @tab in line.
866     (let ((tab-number 1)) ;; one @tab between two columns
867       (while (search-forward "@tab" nil t)
868         (setq tab-number (1+ tab-number)))
869       (if (/= tab-number (length table-widths))
870           (error "Wrong number of @tab's in a @multitable row")))
871     (goto-char (point-min))
872 ;; 2. Format each cell, and copy to a rectangle
873     ;; buffer looks like this:    A1  @tab  A2  @tab  A3
874     ;; Cell #1: format up to @tab
875     ;; Cell #2: format up to @tab
876     ;; Cell #3: format up to eob
877     (while (not (eobp))
878       (setq start (point))
879       (setq end (save-excursion
880                   (if (search-forward "@tab" nil 'move)
881                       ;; Delete the @tab command, including the @-sign
882                       (delete-region
883                        (point)
884                        (progn (forward-word -1) (1- (point)))))
885                   (point)))
886       ;; Set fill-column *wider* than needed to produce inter-column space
887       (setq fill-column (+ 1
888                            texinfo-extra-inter-column-width
889                            (nth table-column table-widths)))
890       (narrow-to-region start end)
891       ;; Remove whitespace before and after entry.
892       (skip-chars-forward " ")
893       (delete-region (point) (save-excursion (beginning-of-line) (point)))
894       (goto-char (point-max))
895       (skip-chars-backward " ")
896       (delete-region (point) (save-excursion (end-of-line) (point)))
897       ;; Temorarily set texinfo-stack to nil so texinfo-format-scan
898       ;; does not see an unterminated @multitable.
899       (let (texinfo-stack) ;; nil
900         (texinfo-format-scan))
901       (let (fill-prefix) ;; no fill prefix
902         (fill-region (point-min) (point-max)))
903       (setq table-entry-height
904             (max table-entry-height (count-lines (point-min) (point-max))))
905 ;; 3. Move point to end of bottom line, and pad that line to fill column.
906       (goto-char (point-min))
907       (forward-line (1- table-entry-height))
908       (let* ((beg (point)) ;; beginning of line
909              ;; add one more space for inter-column spacing
910              (needed-whitespace
911               (1+
912                (- fill-column
913                   (progn
914                     (end-of-line)
915                     (current-column)))))) ;; end of existing line
916         (insert (make-string
917                  (if (> needed-whitespace 0) needed-whitespace 1)
918                  ? )))
919       ;; now, put formatted cell into a rectangle
920       (set (intern (concat texinfo-multitable-rectangle-name
921                            (int-to-string table-column)))
922            (extract-rectangle (point-min) (point)))
923       (delete-region (point-min) (point))
924       (goto-char (point-max))
925       (setq table-column (1+ table-column))
926       (widen))
927 ;; 4. Add extra lines to rectangles so all are of same height
928     (let ((total-number-of-columns table-column)
929           (column-number 0)
930           here)
931       (while (> table-column 0)
932         (let ((this-rectangle (int-to-string table-column)))
933           (while (< (length this-rectangle) table-entry-height)
934             (setq this-rectangle (append this-rectangle '("")))))
935         (setq table-column (1- table-column)))
936 ;; 5. Insert formatted rectangles in original buffer
937       (switch-to-buffer original-buffer)
938       (open-line table-entry-height)
939       (while (< column-number total-number-of-columns)
940         (setq here (point))
941         (insert-rectangle
942          (eval (intern
943                 (concat texinfo-multitable-rectangle-name
944                         (int-to-string column-number)))))
945         (goto-char here)
946         (end-of-line)
947         (setq column-number (1+ column-number))))
948     (kill-buffer texinfo-multitable-buffer-name)
949     (setq fill-column existing-fill-column)))
950
951 \f
952 (ptexinfmt-defun-if-broken texinfo-format-printindex ()
953   (let ((indexelts (symbol-value
954                     (cdr (assoc (texinfo-parse-arg-discard)
955                                 texinfo-indexvar-alist))))
956         opoint)
957     (insert "\n* Menu:\n\n")
958     (setq opoint (point))
959     (texinfo-print-index nil indexelts)
960
961     (if (memq system-type '(vax-vms windows-nt ms-dos))
962         (texinfo-sort-region opoint (point))
963       (shell-command-on-region opoint (point) "sort -fd" 1))))
964
965 (provide 'ptexinfmt)
966
967 ;;; ptexinfmt.el ends here