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