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