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