texinfmt.el 157 KB
Newer Older
1
;;; texinfmt.el --- format Texinfo files into Info files
Richard M. Stallman's avatar
Richard M. Stallman committed
2

3
;; Copyright (C) 1985, 1986, 1988, 1990, 1991, 1992, 1993,
4
;;   1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003,
Glenn Morris's avatar
Glenn Morris committed
5
;;   2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
Brian Preble's avatar
Brian Preble committed
6

Andreas Schwab's avatar
Andreas Schwab committed
7
;; Maintainer: Robert J. Chassell <bug-texinfo@gnu.org>
8
;; Keywords: maint, tex, docs
Eric S. Raymond's avatar
Eric S. Raymond committed
9

Richard M. Stallman's avatar
Richard M. Stallman committed
10
;; This file is part of GNU Emacs.
Brian Preble's avatar
Brian Preble committed
11

12
;; GNU Emacs is free software: you can redistribute it and/or modify
Brian Preble's avatar
Brian Preble committed
13
;; it under the terms of the GNU General Public License as published by
14 15
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Brian Preble's avatar
Brian Preble committed
16 17 18 19 20 21 22

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
23
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Brian Preble's avatar
Brian Preble committed
24

25 26
;;; Commentary:

27
;;; Code:
Eric S. Raymond's avatar
Eric S. Raymond committed
28

29
;;; Emacs lisp functions to convert Texinfo files to Info files.
Brian Preble's avatar
Brian Preble committed
30

31
(defvar texinfmt-version "2.42 of  7 Jul 2006")
32 33 34 35 36

(defun texinfmt-version (&optional here)
  "Show the version of texinfmt.el in the minibuffer.
If optional argument HERE is non-nil, insert info at point."
  (interactive "P")
37
  (let ((version-string
38
         (format "Version of \`texinfmt.el\': %s" texinfmt-version)))
39
    (if here
40
        (insert version-string)
41
      (if (called-interactively-p 'interactive)
42 43 44
          (message "%s" version-string)
        version-string))))

45 46 47 48 49

;;; Variable definitions

(require 'texinfo)          ; So `texinfo-footnote-style' is defined.
(require 'texnfo-upd)       ; So `texinfo-section-types-regexp' is defined.
Eric S. Raymond's avatar
Eric S. Raymond committed
50

Brian Preble's avatar
Brian Preble committed
51 52 53 54 55 56 57 58
(defvar texinfo-vindex)
(defvar texinfo-findex)
(defvar texinfo-cindex)
(defvar texinfo-pindex)
(defvar texinfo-tindex)
(defvar texinfo-kindex)
(defvar texinfo-last-node)
(defvar texinfo-node-names)
59
(defvar texinfo-enclosure-list)
60
(defvar texinfo-alias-list)
61
(defvar texinfo-fold-nodename-case nil)
62

63 64 65 66 67 68 69 70 71 72
(defvar texinfo-command-start)
(defvar texinfo-command-end)
(defvar texinfo-command-name)
(defvar texinfo-defun-type)
(defvar texinfo-last-node-pos)
(defvar texinfo-stack)
(defvar texinfo-short-index-cmds-alist)
(defvar texinfo-short-index-format-cmds-alist)
(defvar texinfo-format-filename)
(defvar texinfo-footnote-number)
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

(defvar texinfo-raisesections-alist
  '((@chapter . @chapter)             ; Cannot go higher
    (@unnumbered . @unnumbered)
    (@centerchap . @unnumbered)

    (@majorheading . @majorheading)
    (@chapheading . @chapheading)
    (@appendix . @appendix)

    (@section . @chapter)
    (@unnumberedsec . @unnumbered)
    (@heading . @chapheading)
    (@appendixsec . @appendix)

    (@subsection . @section)
    (@unnumberedsubsec . @unnumberedsec)
    (@subheading . @heading)
    (@appendixsubsec . @appendixsec)

    (@subsubsection . @subsection)
    (@unnumberedsubsubsec . @unnumberedsubsec)
    (@subsubheading . @subheading)
    (@appendixsubsubsec . @appendixsubsec))
  "*An alist of next higher levels for chapters, sections, etc...
For example, section to chapter, subsection to section.
Used by `texinfo-raise-lower-sections'.
The keys specify types of section; the values correspond to the next
higher types.")

(defvar texinfo-lowersections-alist
  '((@chapter . @section)
    (@unnumbered . @unnumberedsec)
    (@centerchap . @unnumberedsec)
    (@majorheading . @heading)
    (@chapheading . @heading)
    (@appendix . @appendixsec)

    (@section . @subsection)
    (@unnumberedsec . @unnumberedsubsec)
    (@heading . @subheading)
    (@appendixsec . @appendixsubsec)

    (@subsection . @subsubsection)
    (@unnumberedsubsec . @unnumberedsubsubsec)
    (@subheading . @subsubheading)
    (@appendixsubsec . @appendixsubsubsec)

    (@subsubsection . @subsubsection) ; Cannot go lower.
    (@unnumberedsubsubsec . @unnumberedsubsubsec)
    (@subsubheading . @subsubheading)
    (@appendixsubsubsec . @appendixsubsubsec))
  "*An alist of next lower levels for chapters, sections, etc...
For example, chapter to section, section to subsection.
Used by `texinfo-raise-lower-sections'.
The keys specify types of section; the values correspond to the next
lower types.")
130 131

;;; Syntax table
Brian Preble's avatar
Brian Preble committed
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146
(defvar texinfo-format-syntax-table
  (let ((st (make-syntax-table)))
    (modify-syntax-entry ?\" " " st)
    (modify-syntax-entry ?\\ " " st)
    (modify-syntax-entry ?@ "\\" st)
    (modify-syntax-entry ?\^q "\\" st)
    (modify-syntax-entry ?\[ "." st)
    (modify-syntax-entry ?\] "." st)
    (modify-syntax-entry ?\( "." st)
    (modify-syntax-entry ?\) "." st)
    (modify-syntax-entry ?{ "(}" st)
    (modify-syntax-entry ?} "){" st)
    (modify-syntax-entry ?\' "." st)
    st))
Brian Preble's avatar
Brian Preble committed
147

148 149 150

;;; Top level buffer and region formatting functions

151
;;;###autoload
152
(defun texinfo-format-buffer (&optional nosplit)
Brian Preble's avatar
Brian Preble committed
153 154
  "Process the current buffer as texinfo code, into an Info file.
The Info file output is generated in a buffer visiting the Info file
155
name specified in the @setfilename command.
Brian Preble's avatar
Brian Preble committed
156 157

Non-nil argument (prefix, if interactive) means don't make tag table
158 159
and don't split the file if large.  You can use `Info-tagify' and
`Info-split' to do these manually."
Brian Preble's avatar
Brian Preble committed
160
  (interactive "P")
161 162
  (let ((lastmessage "Formatting Info file...")
	(coding-system-for-write buffer-file-coding-system))
Brian Preble's avatar
Brian Preble committed
163
    (message lastmessage)
164
    (widen)
Brian Preble's avatar
Brian Preble committed
165
    (texinfo-format-buffer-1)
166 167
    (Info-tagify)
    (if nosplit
Brian Preble's avatar
Brian Preble committed
168
        nil
169
      (if (> (buffer-size) (+ 50000 Info-split-threshold))
Brian Preble's avatar
Brian Preble committed
170 171 172 173
          (progn
            (message (setq lastmessage "Splitting Info file..."))
            (Info-split))))
    (message (concat lastmessage
174 175
                     (if (called-interactively-p 'interactive)
			 "done.  Now save it." "done.")))))
Brian Preble's avatar
Brian Preble committed
176

177 178 179
(defvar texinfo-region-buffer-name "*Info Region*"
  "*Name of the temporary buffer used by \\[texinfo-format-region].")

180 181 182 183 184
(defvar texinfo-pre-format-hook nil
  "Hook called before the conversion of the Texinfo file to Info format.
The functions on this hook are called with argument BUFFER, the buffer
containing the Texinfo file.")

185 186 187 188
;; These come from tex-mode.el.
(defvar tex-start-of-header)
(defvar tex-end-of-header)

189
;;;###autoload
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
(defun texinfo-format-region (region-beginning region-end)
  "Convert the current region of the Texinfo file to Info format.
This lets you see what that part of the file will look like in Info.
The command is bound to \\[texinfo-format-region].  The text that is
converted to Info is stored in a temporary buffer."
  (interactive "r")
  (message "Converting region to Info format...")
  (let (texinfo-command-start
        texinfo-command-end
        texinfo-command-name
        texinfo-vindex
        texinfo-findex
        texinfo-cindex
        texinfo-pindex
        texinfo-tindex
        texinfo-kindex
        texinfo-stack
        (texinfo-format-filename "")
        texinfo-example-start
        texinfo-last-node-pos
        texinfo-last-node
        texinfo-node-names
        (texinfo-footnote-number 0)
        last-input-buffer
        (fill-column-for-info fill-column)
        (input-buffer (current-buffer))
        (input-directory default-directory)
        (header-text "")
        (header-beginning 1)
        (header-end 1))
220 221

;;; Copy lines between beginning and end of header lines,
222 223 224 225 226 227 228 229
;;;    if any, or else copy the `@setfilename' line, if any.
    (save-excursion
        (save-restriction
          (widen)
          (goto-char (point-min))
          (let ((search-end (save-excursion (forward-line 100) (point))))
            (if (or
                 ;; Either copy header text.
230 231
                 (and
                  (prog1
232
                      (search-forward tex-start-of-header search-end t)
233 234 235
                    (forward-line 1)
                    ;; Mark beginning of header.
                    (setq header-beginning (point)))
236
                  (prog1
237
                      (search-forward tex-end-of-header nil t)
238 239 240 241 242 243 244 245 246 247 248
                    (beginning-of-line)
                    ;; Mark end of header
                    (setq header-end (point))))
                 ;; Or copy @filename line.
                 (prog2
                  (goto-char (point-min))
                  (search-forward "@setfilename" search-end t)
                  (beginning-of-line)
                  (setq header-beginning (point))
                  (forward-line 1)
                  (setq header-end (point))))
249 250

                ;; Copy header
251
                (setq header-text
252
                      (buffer-substring-no-properties
253 254 255 256 257
                       (min header-beginning region-beginning)
                       header-end))))))

;;; Find a buffer to use.
    (switch-to-buffer (get-buffer-create texinfo-region-buffer-name))
Robert J. Chassell's avatar
Robert J. Chassell committed
258 259
    (setq buffer-read-only t)
    (let ((inhibit-read-only t))
260 261 262 263 264 265 266 267
    (erase-buffer)
    ;; Insert the header into the buffer.
    (insert header-text)
    ;; Insert the region into the buffer.
    (insert-buffer-substring
     input-buffer
     (max region-beginning header-end)
     region-end)
268
    (run-hook-with-args 'texinfo-pre-format-hook input-buffer)
269 270 271
    ;; Make sure region ends in a newline.
    (or (= (preceding-char) ?\n)
        (insert "\n"))
272

273 274 275 276 277 278 279 280 281
    (goto-char (point-min))
    (texinfo-mode)
    (message "Converting region to Info format...")
    (setq fill-column fill-column-for-info)
    ;; Install a syntax table useful for scanning command operands.
    (set-syntax-table texinfo-format-syntax-table)

    ;; Insert @include files so `texinfo-raise-lower-sections' can
    ;; work on them without losing track of multiple
282
    ;; @raise/@lowersections commands.
283 284 285 286
    (while (re-search-forward "^@include" nil t)
      (setq texinfo-command-end (point))
      (let ((filename (concat input-directory
                              (texinfo-parse-line-arg))))
287
        (re-search-backward "^@include")
288 289 290 291 292 293 294 295 296 297
        (delete-region (point) (save-excursion (forward-line 1) (point)))
        (message "Reading included file: %s" filename)
        (save-excursion
          (save-restriction
            (narrow-to-region
             (point)
             (+ (point) (car (cdr (insert-file-contents filename)))))
            (goto-char (point-min))
            ;; Remove `@setfilename' line from included file, if any,
            ;; so @setfilename command not duplicated.
298 299 300
            (if (re-search-forward "^@setfilename" (line-end-position 100) t)
		(delete-region (line-beginning-position 1)
			       (line-beginning-position 2)))))))
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

    ;; Raise or lower level of each section, if necessary.
    (goto-char (point-min))
    (texinfo-raise-lower-sections)
    ;; Append @refill to appropriate paragraphs for filling.
    (goto-char (point-min))
    (texinfo-append-refill)
    ;; If the region includes the effective end of the data,
    ;; discard everything after that.
    (goto-char (point-max))
    (if (re-search-backward "^@bye" nil t)
        (delete-region (point) (point-max)))
    ;; Make sure buffer ends in a newline.
    (or (= (preceding-char) ?\n)
        (insert "\n"))
    ;; Don't use a previous value of texinfo-enclosure-list.
    (setq texinfo-enclosure-list nil)
318
    (setq texinfo-alias-list nil)
319 320 321

    (goto-char (point-min))
    (if (looking-at "\\\\input[ \t]+texinfo")
322
        (delete-region (point) (line-beginning-position 2)))
323 324 325

    ;; Insert Info region title text.
    (goto-char (point-min))
326
    (if (search-forward
327 328 329 330 331 332 333 334
         "@setfilename" (save-excursion (forward-line 100) (point)) t)
        (progn
          (setq texinfo-command-end (point))
          (beginning-of-line)
          (setq texinfo-command-start (point))
          (let ((arg (texinfo-parse-arg-discard)))
            (insert " "
              texinfo-region-buffer-name
335
              " buffer for:  `")
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
            (insert (file-name-nondirectory (expand-file-name arg)))
            (insert "',        -*-Text-*-\n")))
      ;; Else no `@setfilename' line
      (insert " "
              texinfo-region-buffer-name
              " buffer                       -*-Text-*-\n"))
    (insert "produced by `texinfo-format-region'\n"
            "from a region in: "
            (if (buffer-file-name input-buffer)
                  (concat "`"
                          (file-name-sans-versions
                           (file-name-nondirectory
                            (buffer-file-name input-buffer)))
                          "'")
                (concat "buffer `" (buffer-name input-buffer) "'"))
              "\nusing `texinfmt.el' version "
              texinfmt-version
              ".\n\n")

    ;; Now convert for real.
    (goto-char (point-min))
    (texinfo-format-scan)
    (goto-char (point-min))
359 360
    (Info-tagify input-buffer)
    (goto-char (point-min))
Robert J. Chassell's avatar
Robert J. Chassell committed
361
    (message "Done."))))
362

363
;;;###autoload
364
(defun texi2info (&optional nosplit)
365 366 367 368 369 370 371 372 373
  "Convert the current buffer (written in Texinfo code) into an Info file.
The Info file output is generated in a buffer visiting the Info file
names specified in the @setfilename command.

This function automatically updates all node pointers and menus, and
creates a master menu.  This work is done on a temporary buffer that
is automatically removed when the Info file is created.  The original
Texinfo source buffer is not changed.

374
Non-nil argument (prefix, if interactive) means don't split the file
375
if large.  You can use `Info-split' to do this manually."
376 377 378 379 380 381 382 383 384
  (interactive "P")
  (let ((temp-buffer (concat  "*--" (buffer-name) "--temporary-buffer*" )))
    (message "First updating nodes and menus, then creating Info file.")
    ;;  (sit-for 2)
    (copy-to-buffer temp-buffer (point-min) (point-max))
    (switch-to-buffer temp-buffer)
    (texinfo-master-menu t)
    (message "Now creating Info file.")
    (sit-for 2)
385
    (texinfo-format-buffer nosplit)
386 387 388
    (save-buffer)
    (kill-buffer temp-buffer)))

Brian Preble's avatar
Brian Preble committed
389

390 391
;;; Primary internal formatting function for the whole buffer.

Brian Preble's avatar
Brian Preble committed
392 393 394 395 396 397 398
(defun texinfo-format-buffer-1 ()
  (let (texinfo-format-filename
        texinfo-example-start
        texinfo-command-start
        texinfo-command-end
        texinfo-command-name
        texinfo-last-node
399
        texinfo-last-node-pos
Brian Preble's avatar
Brian Preble committed
400 401 402 403 404 405 406 407
        texinfo-vindex
        texinfo-findex
        texinfo-cindex
        texinfo-pindex
        texinfo-tindex
        texinfo-kindex
        texinfo-stack
        texinfo-node-names
408
        (texinfo-footnote-number 0)
Brian Preble's avatar
Brian Preble committed
409 410
        last-input-buffer
        outfile
411
        (fill-column-for-info fill-column)
Brian Preble's avatar
Brian Preble committed
412 413
        (input-buffer (current-buffer))
        (input-directory default-directory))
414
    (setq texinfo-enclosure-list nil)
415
    (setq texinfo-alias-list nil)
Brian Preble's avatar
Brian Preble committed
416 417
    (save-excursion
      (goto-char (point-min))
418
      (or (search-forward "@setfilename" nil t)
419
          (error "Texinfo file needs an `@setfilename FILENAME' line"))
Brian Preble's avatar
Brian Preble committed
420 421
      (setq texinfo-command-end (point))
      (setq outfile (texinfo-parse-line-arg)))
422

Brian Preble's avatar
Brian Preble committed
423 424
    (find-file outfile)
    (texinfo-mode)
425
    (erase-buffer)
426
    (buffer-disable-undo)
427 428 429 430 431

    (message "Formatting Info file: %s" outfile)
    (setq texinfo-format-filename
          (file-name-nondirectory (expand-file-name outfile)))

432
    (setq fill-column fill-column-for-info)
Brian Preble's avatar
Brian Preble committed
433
    (set-syntax-table texinfo-format-syntax-table)
434

Brian Preble's avatar
Brian Preble committed
435
    (insert-buffer-substring input-buffer)
436
    (run-hook-with-args 'texinfo-pre-format-hook input-buffer)
437
    (message "Converting %s to Info format..." (buffer-name input-buffer))
438

439 440
    ;; Insert @include files so `texinfo-raise-lower-sections' can
    ;; work on them without losing track of multiple
441
    ;; @raise/@lowersections commands.
442 443 444 445 446
    (goto-char (point-min))
    (while (re-search-forward "^@include" nil t)
      (setq texinfo-command-end (point))
      (let ((filename (concat input-directory
                              (texinfo-parse-line-arg))))
447
        (re-search-backward "^@include")
448
        (delete-region (point) (line-beginning-position 2))
449 450 451 452 453 454 455 456 457
        (message "Reading included file: %s" filename)
        (save-excursion
          (save-restriction
            (narrow-to-region
             (point)
             (+ (point) (car (cdr (insert-file-contents filename)))))
            (goto-char (point-min))
            ;; Remove `@setfilename' line from included file, if any,
            ;; so @setfilename command not duplicated.
458 459 460
            (if (re-search-forward "^@setfilename" (line-end-position 100) t)
		(delete-region (line-beginning-position 1)
			       (line-beginning-position 2)))))))
461 462 463 464 465 466
    ;; Raise or lower level of each section, if necessary.
    (goto-char (point-min))
    (texinfo-raise-lower-sections)
    ;; Append @refill to appropriate paragraphs
    (goto-char (point-min))
    (texinfo-append-refill)
Brian Preble's avatar
Brian Preble committed
467 468 469 470 471 472 473 474 475 476 477 478 479 480
    (goto-char (point-min))
    (search-forward "@setfilename")
    (beginning-of-line)
    (delete-region (point-min) (point))
    ;; Remove @bye at end of file, if it is there.
    (goto-char (point-max))
    (if (search-backward "@bye" nil t)
        (delete-region (point) (point-max)))
    ;; Make sure buffer ends in a newline.
    (or (= (preceding-char) ?\n)
        (insert "\n"))
    ;; Scan the whole buffer, converting to Info format.
    (texinfo-format-scan)
    (goto-char (point-min))
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
    ;; Insert info about how this file was made.
    (insert "Info file: "
            texinfo-format-filename ",    -*-Text-*-\n"
            "produced by `texinfo-format-buffer'\n"
            ;; Date string removed so that regression testing is easier.
            ;; "on "
            ;; (insert (format-time-string "%e %b %Y")) " "
            "from file"
            (if (buffer-file-name input-buffer)
                (concat " `"
                        (file-name-sans-versions
                         (file-name-nondirectory
                          (buffer-file-name input-buffer)))
                        "'")
              (concat "buffer `" (buffer-name input-buffer) "'"))
            "\nusing `texinfmt.el' version "
            texinfmt-version
            ".\n\n")
    ;; Return data for indices.
Brian Preble's avatar
Brian Preble committed
500 501 502 503
    (list outfile
          texinfo-vindex texinfo-findex texinfo-cindex
          texinfo-pindex texinfo-tindex texinfo-kindex)))

504 505 506 507 508 509 510 511 512 513 514 515 516 517

;;; Perform non-@-command file conversions: quotes and hyphens

(defun texinfo-format-convert (min max)
  ;; Convert left and right quotes to typewriter font quotes.
  (goto-char min)
  (while (search-forward "``" max t)
    (replace-match "\""))
  (goto-char min)
  (while (search-forward "''" max t)
    (replace-match "\""))
  ;; Convert three hyphens in a row to two.
  (goto-char min)
  (while (re-search-forward "\\( \\|\\w\\)\\(---\\)\\( \\|\\w\\)" max t)
518
    (delete-region (1+ (match-beginning 2)) (+ 2 (match-beginning 2)))))
Brian Preble's avatar
Brian Preble committed
519

520 521 522

;;; Handle paragraph filling

523 524
;; Keep as concatinated lists for ease of maintenance

525
(defvar texinfo-no-refill-regexp
526 527 528
  (concat
   "^@"
   "\\("
529
   ;; add "itemize\\|"   (from experiment of 2001 Nov 28)
530
   ;;     because of a problem with @end itemize@refill
531 532 533
   ;;     I don't know if this causes other problems.
   ;;     I suspect itemized lists don't get filled properly and a
   ;;     more precise fix is required.  Bob
534 535
   ;; commented out on 2005 Feb 28 by Bob
   ;; "itemize\\|"
536
   "direntry\\|"
537 538
   "lisp\\|"
   "smalllisp\\|"
539 540
   "example\\|"
   "smallexample\\|"
541
   "display\\|"
542
   "smalldisplay\\|"
543
   "format\\|"
544
   "smallformat\\|"
545 546 547 548 549 550 551 552 553 554
   "flushleft\\|"
   "flushright\\|"
   "menu\\|"
   "multitable\\|"
   "titlepage\\|"
   "iftex\\|"
   "ifhtml\\|"
   "tex\\|"
   "html"
   "\\)")
555 556
  "Regexp specifying environments in which paragraphs are not filled.")

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
(defvar texinfo-accent-commands
  (concat
   "@^\\|"
   "@`\\|"
   "@'\\|"
   "@\"\\|"
   "@,\\|"
   "@=\\|"
   "@~\\|"
   "@OE{\\|"
   "@oe{\\|"
   "@AA{\\|"
   "@aa{\\|"
   "@AE{\\|"
   "@ae{\\|"
   "@ss{\\|"
   "@questiondown{\\|"
   "@exclamdown{\\|"
   "@L{\\|"
   "@l{\\|"
   "@O{\\|"
   "@o{\\|"
   "@dotaccent{\\|"
   "@ubaraccent{\\|"
   "@d{\\|"
   "@H{\\|"
   "@ringaccent{\\|"
   "@tieaccent{\\|"
   "@u{\\|"
   "@v{\\|"
   "@dotless{"
   ))

590
(defvar texinfo-part-of-para-regexp
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
  (concat
   "^@"
   "\\("
   "b{\\|"
   "bullet{\\|"
   "cite{\\|"
   "code{\\|"
   "email{\\|"
   "emph{\\|"
   "equiv{\\|"
   "error{\\|"
   "expansion{\\|"
   "file{\\|"
   "i{\\|"
   "inforef{\\|"
   "kbd{\\|"
   "key{\\|"
   "lisp{\\|"
   "minus{\\|"
   "point{\\|"
   "print{\\|"
   "pxref{\\|"
   "r{\\|"
   "ref{\\|"
   "result{\\|"
   "samp{\\|"
   "sc{\\|"
   "t{\\|"
   "TeX{\\|"
   "today{\\|"
   "url{\\|"
   "var{\\|"
   "w{\\|"
   "xref{\\|"
   "@-\\|"    ; @- is a descretionary hyphen (not an accent) (a noop).
   texinfo-accent-commands
   "\\)"
   )
629 630 631 632
  "Regexp specifying @-commands found within paragraphs.")

(defun texinfo-append-refill ()
  "Append @refill at end of each paragraph that should be filled.
633
Do not append @refill to paragraphs within @example and similar environments.
634 635 636 637 638
Do not append @refill to paragraphs containing @w{TEXT} or @*."

  ;; It is necessary to append @refill before other processing because
  ;; the other processing removes information that tells Texinfo
  ;; whether the text should or should not be filled.
639

640 641 642 643 644
  (while (< (point) (point-max))
    (let ((refill-blank-lines "^[ \t\n]*$")
          (case-fold-search nil))       ; Don't confuse @TeX and @tex....
      (beginning-of-line)
      ;; 1. Skip over blank lines;
645
      ;;    skip over lines beginning with @-commands,
646 647 648 649
      ;;    but do not skip over lines
      ;;      that are no-refill environments such as @example or
      ;;      that begin with within-paragraph @-commands such as @code.
      (while (and (looking-at (concat "^@\\|^\\\\\\|" refill-blank-lines))
650
                  (not (looking-at
651
                        (concat
652
                         "\\("
653
                         texinfo-no-refill-regexp
654
                         "\\|"
655 656 657 658 659 660
                         texinfo-part-of-para-regexp
                         "\\)")))
                  (< (point) (point-max)))
        (forward-line 1))
      ;; 2. Skip over @example and similar no-refill environments.
      (if (looking-at texinfo-no-refill-regexp)
661
          (let ((environment (match-string-no-properties 1)))
662 663
            (progn (re-search-forward (concat "^@end " environment) nil t)
                   (forward-line 1)))
664 665 666
        ;; Else
        ;; 3. Do not refill a paragraph containing @w or @*, or ending
        ;;    with @<newline> followed by a newline.
667 668 669 670 671
        (if  (or (>= (point) (point-max))
		 (re-search-forward
		  "@w{\\|@\\*\\|@\n\n"
		  (save-excursion (forward-paragraph) (forward-line 1) (point))
		  t))
672
            ;; Go to end of paragraph and do nothing.
673
            (forward-paragraph)
674 675 676
          ;; 4. Else go to end of paragraph and insert @refill
          (forward-paragraph)
          (forward-line -1)
Karl Heuer's avatar
Karl Heuer committed
677 678 679 680 681
	  (let ((line-beg (point)))
	    (end-of-line)
	    (delete-region
	     (point)
	     (save-excursion (skip-chars-backward " \t") (point)))
682 683 684
	    (forward-char 1)
	    (unless (re-search-backward "@c[ \t\n]\\|@comment[ \t\n]" line-beg t)
	      (forward-char -1))
685
	    (unless (re-search-backward "@refill\\|^[ \t]*@" line-beg t)
Karl Heuer's avatar
Karl Heuer committed
686
	      (insert "@refill")))
687
          (forward-line 1))))))
Brian Preble's avatar
Brian Preble committed
688

689 690 691 692

;;; Handle `@raisesections' and `@lowersections' commands

;; These commands change the hierarchical level of chapter structuring
693 694
;; commands.
;;
695 696 697 698 699 700 701 702 703 704 705 706 707 708
;; @raisesections changes @subsection to @section,
;;                        @section    to @chapter,
;;                        etc.
;;
;; @lowersections changes @chapter    to @section
;;                        @subsection to @subsubsection,
;;                        etc.
;;
;; An @raisesections/@lowersections command changes only those
;; structuring commands that follow the @raisesections/@lowersections
;; command.
;;
;; Repeated @raisesections/@lowersections continue to raise or lower
;; the heading level.
709
;;
710 711 712 713 714 715 716 717 718 719
;; An @lowersections command cancels an @raisesections command, and
;; vice versa.
;;
;; You cannot raise or lower "beyond" chapters or subsubsections, but
;; trying to do so does not elicit an error---you just get more
;; headings that mean the same thing as you keep raising or lowering
;; (for example, after a single @raisesections, both @chapter and
;; @section produce chapter headings).

(defun texinfo-raise-lower-sections ()
720
  "Raise or lower the hierarchical level of chapters, sections, etc.
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753

This function acts according to `@raisesections' and `@lowersections'
commands in the Texinfo file.

For example, an `@lowersections' command is useful if you wish to
include what is written as an outer or standalone Texinfo file in
another Texinfo file as an inner, included file.  The `@lowersections'
command changes chapters to sections, sections to subsections and so
on.

@raisesections changes @subsection to @section,
                       @section    to @chapter,
                       @heading    to @chapheading,
                       etc.

@lowersections changes @chapter    to @section,
                       @subsection to @subsubsection,
                       @heading    to @subheading,
                       etc.

An `@raisesections' or `@lowersections' command changes only those
structuring commands that follow the `@raisesections' or
`@lowersections' command.

An `@lowersections' command cancels an `@raisesections' command, and
vice versa.

Repeated use of the commands continue to raise or lower the hierarchical
level a step at a time.

An attempt to raise above `chapters' reproduces chapter commands; an
attempt to lower below subsubsections reproduces subsubsection
commands."
754

755 756 757 758 759
  ;; `texinfo-section-types-regexp' is defined in `texnfo-upd.el';
  ;; it is a regexp matching chapter, section, other headings
  ;; (but not the top node).

  (let (type (level 0))
760
    (while
761 762 763 764 765 766 767 768
        (re-search-forward
         (concat
          "\\(\\(^@\\(raise\\|lower\\)sections\\)\\|\\("
          texinfo-section-types-regexp
          "\\)\\)")
         nil t)
      (beginning-of-line)
      (save-excursion (setq type (read (current-buffer))))
769 770
      (cond

771 772 773 774 775
       ;; 1. Increment level
       ((eq type '@raisesections)
        (setq level (1+ level))
        (delete-region
         (point) (save-excursion (forward-line 1) (point))))
776

777 778 779 780 781
       ;; 2. Decrement level
       ((eq type '@lowersections)
        (setq level (1- level))
        (delete-region
         (point) (save-excursion (forward-line 1) (point))))
782

783 784
       ;; Now handle structuring commands
       ((cond
785

786 787 788 789 790 791 792 793 794 795
         ;; 3. Raise level when positive
         ((> level 0)
          (let ((count level)
                (new-level type))
            (while (> count 0)
              (setq new-level
                    (cdr (assq new-level texinfo-raisesections-alist)))
              (setq count (1- count)))
            (kill-word 1)
            (insert (symbol-name new-level))))
796

797 798
         ;; 4. Do nothing except move point when level is zero
         ((= level 0) (forward-line 1))
799

800 801 802 803 804 805 806 807 808 809
         ;; 5. Lower level when positive
         ((< level 0)
          (let ((count level)
                (new-level type))
            (while (< count 0)
              (setq new-level
                    (cdr (assq new-level texinfo-lowersections-alist)))
              (setq count (1+ count)))
            (kill-word 1)
            (insert (symbol-name new-level))))))))))
Brian Preble's avatar
Brian Preble committed
810

811 812 813
;;; Perform those texinfo-to-info conversions that apply to the whole input
;;; uniformly.

Brian Preble's avatar
Brian Preble committed
814
(defun texinfo-format-scan ()
815
  (texinfo-format-convert (point-min) (point-max))
816 817 818 819 820 821 822 823 824 825
  ;; Search for @copying, which has to be first since the
  ;; @insertcopying command then inserts the text elsewhere.
  (goto-char (point-min))
  (when (search-forward "@copying" nil t)
    (texinfo-copying))
  (while (search-forward "@insertcopying" nil t)
    (delete-region (match-beginning 0) (match-end 0))

    (texinfo-insertcopying))
  ;; Scan for other @-commands.
826 827
  (goto-char (point-min))
  (while (search-forward "@" nil t)
828 829 830 831
    ;;
    ;; These are the single-character accent commands: @^ @` @' @" @= @~
    ;; In Info, they are simply quoted and the @ deleted.
    ;; Other single-character commands:
832
    ;; @* forces a line break,
833 834 835
    ;; @- is a discretionary hyphenation point; does nothing in Info.
    ;; @<space>, @<tab>, @<newline> each produce a single space,
    ;;    unless followed by a newline.
836
    ;;
837 838 839
    ;; Old version 2.34 expression: (looking-at "[@{}^'` *\"?!]")
    (if (looking-at "[@{}^'`\"=~ \t\n*?!-]")
        ;; @*, causes a line break.
840
        (cond
841 842 843 844 845 846 847
         ;; @*, a line break
         ((= (following-char) ?*)
          ;; remove command
          (delete-region (1- (point)) (1+ (point)))
          ;; insert return if not at end of line;
          ;; else line is already broken.
          (if (not (= (following-char) ?\n))
848
              (insert ?\n)))
849 850 851 852 853 854 855 856 857 858
         ;; @-, deleted
         ((= (following-char) ?-)
          (delete-region (1- (point)) (1+ (point))))
         ;; @<space>, @<tab>, @<newline>: produce a single space,
         ;;    unless followed by a newline.
         ((= (following-char) ? )
          (delete-region (1- (point)) (1+ (point)))
          ;; insert single space if not at end of line;
          ;; else line is already broken.
          (if (not (= (following-char) ?\n))
859
              (insert ? )))
860 861 862 863 864 865 866
         ((= (following-char) ?\t)
          (delete-region (1- (point)) (1+ (point)))
          ;; insert single space if not at end of line;
          ;; else line is already broken.
          (if (not (= (following-char) ?\n))
              (insert ? )))
         ;; following char is a carriage return
867
         ((= (following-char) ?\n)
868 869 870 871 872 873 874 875 876
          ;; remove command
          (delete-region (1- (point)) (1+ (point)))
          ;; insert single space if not at end of line;
          ;; else line is already broken.
          (if (not (= (following-char) ?\n))
              (insert ? )))
         ;; Otherwise: the other characters are simply quoted.  Delete the @.
         (t
         (delete-char -1)
Eli Zaretskii's avatar
Eli Zaretskii committed
877
	 ;; Be compatible with makeinfo: if @' and its ilk are
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
	 ;; followed by a @ without a brace, barf.
	 (if (looking-at "[\"'^`~=]")
	     (progn
	       (if (= (char-after (1+ (point))) ?@)
		   (error "Use braces to give a command as an argument to @%c"
			  (following-char)))
	       (forward-char 1)
	       ;; @' etc. can optionally accept their argument in
	       ;; braces (makeinfo supports that).
	       (when (looking-at "{")
		 (let ((start (point)))
		   (forward-list 1)
		   (delete-char -1)
		   (goto-char start)
		   (delete-char 1))))
	   (forward-char 1))))
894 895 896 897 898 899
      ;; @ is followed by a command-word; find the end of the word.
      (setq texinfo-command-start (1- (point)))
      (if (= (char-syntax (following-char)) ?w)
          (forward-word 1)
        (forward-char 1))
      (setq texinfo-command-end (point))
Karl Heuer's avatar
Karl Heuer committed
900 901 902 903 904
      ;; Detect the case of two @-commands in a row;
      ;; process just the first one.
      (goto-char (1+ texinfo-command-start))
      (skip-chars-forward "^@" texinfo-command-end)
      (setq texinfo-command-end (point))
905
      ;; Handle let aliasing
906
      (setq texinfo-command-name
907
            (let (trial
908
                  (cmdname
909
                   (buffer-substring-no-properties
910 911 912
                    (1+ texinfo-command-start) texinfo-command-end)))
              (while (setq trial (assoc cmdname texinfo-alias-list))
                (setq cmdname (cdr trial)))
913 914
            (intern cmdname)))
      ;; Call the handler for this command.
915 916 917 918 919 920 921
      (let ((enclosure-type
             (assoc
              (symbol-name texinfo-command-name)
              texinfo-enclosure-list)))
        (if enclosure-type
            (progn
              (insert
922
               (car (car (cdr enclosure-type)))
923 924 925 926 927
               (texinfo-parse-arg-discard)
               (car (cdr (car (cdr enclosure-type)))))
              (goto-char texinfo-command-start))
          (let ((cmd (get texinfo-command-name 'texinfo-format)))
            (if cmd (funcall cmd) (texinfo-unsupported)))))))
928

929 930
  (cond (texinfo-stack
         (goto-char (nth 2 (car texinfo-stack)))
931 932 933
         (error "Unterminated @%s" (car (car texinfo-stack)))))

  ;; Remove excess whitespace
934 935
  (let ((whitespace-silent t))
    (whitespace-cleanup)))
936

937
(defvar texinfo-copying-text ""
938 939 940
  "Text of the copyright notice and copying permissions.")

(defun texinfo-copying ()
941 942
  "Copy the copyright notice and copying permissions from the Texinfo file,
as indicated by the @copying ... @end copying command;
943 944 945 946 947 948 949 950 951 952
insert the text with the @insertcopying command."
  (let ((beg (progn (beginning-of-line) (point)))
        (end  (progn (re-search-forward "^@end copying[ \t]*\n") (point))))
    (setq texinfo-copying-text
          (buffer-substring-no-properties
           (save-excursion (goto-char beg) (forward-line 1) (point))
           (save-excursion (goto-char end) (forward-line -1) (point))))
    (delete-region beg end)))

(defun texinfo-insertcopying ()
953
  "Insert the copyright notice and copying permissions from the Texinfo file,
954 955
which are indicated by the @copying ... @end copying command."
  (insert (concat "\n" texinfo-copying-text)))
Brian Preble's avatar
Brian Preble committed
956 957 958 959 960 961 962 963 964 965 966

(put 'begin 'texinfo-format 'texinfo-format-begin)
(defun texinfo-format-begin ()
  (texinfo-format-begin-end 'texinfo-format))

(put 'end 'texinfo-format 'texinfo-format-end)
(defun texinfo-format-end ()
  (texinfo-format-begin-end 'texinfo-end))

(defun texinfo-format-begin-end (prop)
  (setq texinfo-command-name (intern (texinfo-parse-line-arg)))
967 968 969
  (let ((cmd (get texinfo-command-name prop)))
    (if cmd (funcall cmd)
      (texinfo-unsupported))))
Brian Preble's avatar
Brian Preble committed
970

971 972
;;; Parsing functions

Brian Preble's avatar
Brian Preble committed
973
(defun texinfo-parse-line-arg ()
974
  "Return argument of @-command as string.
975
Argument is separated from command either by a space or by a brace.
976 977 978
If a space, return rest of line, with beginning and ending white
space removed.  If a brace, return string between braces.
Leave point after argument."
Brian Preble's avatar
Brian Preble committed
979 980 981
  (goto-char texinfo-command-end)
  (let ((start (point)))
    (cond ((looking-at " ")
982 983 984
           (skip-chars-forward " ")
           (setq start (point))
           (end-of-line)
Brian Preble's avatar
Brian Preble committed
985
           (skip-chars-backward " ")
986 987 988 989 990 991 992 993 994
           (delete-region (point) (progn (end-of-line) (point)))
           (setq texinfo-command-end (1+ (point))))
          ((looking-at "{")
           (setq start (1+ (point)))
           (forward-list 1)
           (setq texinfo-command-end (point))
           (forward-char -1))
          (t
           (error "Invalid texinfo command arg format")))
995
    (prog1 (buffer-substring-no-properties start (point))
996
           (if (eolp) (forward-char 1)))))
Brian Preble's avatar
Brian Preble committed
997 998 999 1000

(defun texinfo-parse-expanded-arg ()
  (goto-char texinfo-command-end)
  (let ((start (point))
1001
        marker)
Brian Preble's avatar
Brian Preble committed
1002
    (cond ((looking-at " ")
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
           (skip-chars-forward " ")
           (setq start (point))
           (end-of-line)
           (setq texinfo-command-end (1+ (point))))
          ((looking-at "{")
           (setq start (1+ (point)))
           (forward-list 1)
           (setq texinfo-command-end (point))
           (forward-char -1))
          (t
           (error "Invalid texinfo command arg format")))
Brian Preble's avatar
Brian Preble committed
1014 1015 1016 1017
    (setq marker (move-marker (make-marker) texinfo-command-end))
    (texinfo-format-expand-region start (point))
    (setq texinfo-command-end (marker-position marker))
    (move-marker marker nil)
1018
    (prog1 (buffer-substring-no-properties start (point))
1019
           (if (eolp) (forward-char 1)))))
Brian Preble's avatar
Brian Preble committed
1020 1021 1022 1023 1024

(defun texinfo-format-expand-region (start end)
  (save-restriction
    (narrow-to-region start end)
    (let (texinfo-command-start
1025 1026 1027
          texinfo-command-end
          texinfo-command-name
          texinfo-stack)
Brian Preble's avatar
Brian Preble committed
1028 1029 1030 1031
      (texinfo-format-scan))
    (goto-char (point-max))))

(defun texinfo-parse-arg-discard ()
1032
  "Delete command and argument; return argument of command."
Brian Preble's avatar
Brian Preble committed
1033
  (prog1 (texinfo-parse-line-arg)
1034
         (texinfo-discard-command)))
Brian Preble's avatar
Brian Preble committed
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044

(defun texinfo-discard-command ()
  (delete-region texinfo-command-start texinfo-command-end))

(defun texinfo-optional-braces-discard ()
  "Discard braces following command, if any."
  (goto-char texinfo-command-end)
  (let ((start (point)))
    (cond ((looking-at "[ \t]*\n"))     ; do nothing
          ((looking-at "{")             ; remove braces, if any
1045 1046 1047
           (forward-list 1)
           (setq texinfo-command-end (point)))
          (t
Brian Preble's avatar
Brian Preble committed
1048 1049 1050 1051 1052 1053
           (error
            "Invalid `texinfo-optional-braces-discard' format \(need braces?\)")))
    (delete-region texinfo-command-start texinfo-command-end)))

(defun texinfo-format-parse-line-args ()
  (let ((start (1- (point)))
1054 1055
        next beg end
        args)
Brian Preble's avatar
Brian Preble committed
1056 1057 1058 1059 1060 1061 1062 1063 1064
    (skip-chars-forward " ")
    (while (not (eolp))
      (setq beg (point))
      (re-search-forward "[\n,]")
      (setq next (point))
      (if (bolp) (setq next (1- next)))
      (forward-char -1)
      (skip-chars-backward " ")
      (setq end (point))
1065 1066
      (push (if (> end beg) (buffer-substring-no-properties beg end))
            args)
Brian Preble's avatar
Brian Preble committed
1067 1068 1069 1070 1071 1072 1073 1074
      (goto-char next)
      (skip-chars-forward " "))
    (if (eolp) (forward-char 1))
    (setq texinfo-command-end (point))
    (nreverse args)))

(defun texinfo-format-parse-args ()
  (let ((start (1- (point)))
1075 1076
        next beg end
        args)
Brian Preble's avatar
Brian Preble committed
1077 1078
    (search-forward "{")
    (save-excursion
1079
      (texinfo-format-expand-region
Brian Preble's avatar
Brian Preble committed
1080 1081
       (point)
       (save-excursion (up-list 1) (1- (point)))))
1082 1083 1084
    ;; The following does not handle cross references of the form:
    ;; `@xref{bullet, , @code{@@bullet}@{@}}.' because the
    ;; re-search-forward finds the first right brace after the second
1085
    ;; comma.
Brian Preble's avatar
Brian Preble committed
1086 1087 1088 1089 1090 1091 1092 1093 1094
    (while (/= (preceding-char) ?\})
      (skip-chars-forward " \t\n")
      (setq beg (point))
      (re-search-forward "[},]")
      (setq next (point))
      (forward-char -1)
      (skip-chars-backward " \t\n")
      (setq end (point))
      (cond ((< beg end)
1095 1096 1097
             (goto-char beg)
             (while (search-forward "\n" end t)
               (replace-match " "))))
1098 1099
      (push (if (> end beg) (buffer-substring-no-properties beg end))
            args)
Brian Preble's avatar
Brian Preble committed
1100
      (goto-char next))
1101
    ;;(if (eolp) (forward-char 1))
Brian Preble's avatar
Brian Preble committed
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
    (setq texinfo-command-end (point))
    (nreverse args)))

(defun texinfo-format-parse-defun-args ()
  (goto-char texinfo-command-end)
  (let ((start (point)))
    (end-of-line)
    (setq texinfo-command-end (1+ (point)))
    (let ((marker (move-marker (make-marker) texinfo-command-end)))
      (texinfo-format-expand-region start (point))
      (setq texinfo-command-end (marker-position marker))
      (move-marker marker nil))
    (goto-char start)
    (let ((args '())
1116
          beg end)
Brian Preble's avatar
Brian Preble committed
1117 1118
      (skip-chars-forward " ")
      (while (not (eolp))
1119 1120 1121 1122 1123 1124 1125 1126 1127
        (cond ((looking-at "{")
               (setq beg (1+ (point)))
               (forward-list 1)
               (setq end (1- (point))))
              (t
               (setq beg (point))
               (re-search-forward "[\n ]")
               (forward-char -1)
               (setq end (point))))
1128
        (push (buffer-substring-no-properties beg end) args)
1129
        (skip-chars-forward " "))
Brian Preble's avatar
Brian Preble committed
1130 1131 1132
      (forward-char 1)
      (nreverse args))))

1133 1134 1135 1136
(defun texinfo-discard-line ()
  (goto-char texinfo-command-end)
  (skip-chars-forward " \t")
  (or (eolp)