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