+ ;; 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.
+ (list outfile
+ texinfo-vindex texinfo-findex texinfo-cindex
+ texinfo-pindex texinfo-tindex texinfo-kindex)))
+
+\f
+;;; 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)
+ (delete-region (1+ (match-beginning 2)) (+ 2 (match-beginning 2)))))
+
+\f
+;;; Handle paragraph filling
+
+;; Keep as concatinated lists for ease of maintenance
+
+(defvar texinfo-no-refill-regexp
+ (concat
+ "^@"
+ "\\("
+ ;; add "itemize\\|" (from experiment of 2001 Nov 28)
+ ;; because of a problem with @end itemize@refill
+ ;; 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
+ ;; commented out on 2005 Feb 28 by Bob
+ ;; "itemize\\|"
+ "direntry\\|"
+ "lisp\\|"
+ "smalllisp\\|"
+ "example\\|"
+ "smallexample\\|"
+ "display\\|"
+ "smalldisplay\\|"
+ "format\\|"
+ "smallformat\\|"
+ "flushleft\\|"
+ "flushright\\|"
+ "menu\\|"
+ "multitable\\|"
+ "titlepage\\|"
+ "iftex\\|"
+ "ifhtml\\|"
+ "tex\\|"
+ "html"
+ "\\)")
+ "Regexp specifying environments in which paragraphs are not filled.")
+
+(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{"
+ ))
+
+(defvar texinfo-part-of-para-regexp
+ (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
+ "\\)"
+ )
+ "Regexp specifying @-commands found within paragraphs.")
+
+(defun texinfo-append-refill ()
+ "Append @refill at end of each paragraph that should be filled.
+Do not append @refill to paragraphs within @example and similar environments.
+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.
+
+ (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;
+ ;; skip over lines beginning with @-commands,
+ ;; 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))
+ (not (looking-at
+ (concat
+ "\\("
+ texinfo-no-refill-regexp
+ "\\|"
+ 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)
+ (let ((environment (match-string-no-properties 1)))
+ (progn (re-search-forward (concat "^@end " environment) nil t)
+ (forward-line 1)))
+ ;; Else
+ ;; 3. Do not refill a paragraph containing @w or @*, or ending
+ ;; with @<newline> followed by a newline.
+ (if (or (>= (point) (point-max))
+ (re-search-forward
+ "@w{\\|@\\*\\|@\n\n"
+ (save-excursion (forward-paragraph) (forward-line 1) (point))
+ t))
+ ;; Go to end of paragraph and do nothing.
+ (forward-paragraph)
+ ;; 4. Else go to end of paragraph and insert @refill
+ (forward-paragraph)
+ (forward-line -1)
+ (let ((line-beg (point)))
+ (end-of-line)
+ (delete-region
+ (point)
+ (save-excursion (skip-chars-backward " \t") (point)))
+ (forward-char 1)
+ (unless (re-search-backward "@c[ \t\n]\\|@comment[ \t\n]" line-beg t)
+ (forward-char -1))
+ (unless (re-search-backward "@refill\\|^[ \t]*@" line-beg t)
+ (insert "@refill")))
+ (forward-line 1))))))
+
+\f
+;;; Handle `@raisesections' and `@lowersections' commands
+
+;; These commands change the hierarchical level of chapter structuring
+;; commands.
+;;
+;; @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.
+;;
+;; 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 ()
+ "Raise or lower the hierarchical level of chapters, sections, etc.
+
+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."
+
+ ;; `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))
+ (while
+ (re-search-forward
+ (concat
+ "\\(\\(^@\\(raise\\|lower\\)sections\\)\\|\\("
+ texinfo-section-types-regexp
+ "\\)\\)")
+ nil t)
+ (beginning-of-line)
+ (save-excursion (setq type (read (current-buffer))))
+ (cond
+
+ ;; 1. Increment level
+ ((eq type '@raisesections)
+ (setq level (1+ level))
+ (delete-region
+ (point) (save-excursion (forward-line 1) (point))))
+
+ ;; 2. Decrement level
+ ((eq type '@lowersections)
+ (setq level (1- level))
+ (delete-region
+ (point) (save-excursion (forward-line 1) (point))))
+
+ ;; Now handle structuring commands
+ ((cond
+
+ ;; 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))))
+
+ ;; 4. Do nothing except move point when level is zero
+ ((= level 0) (forward-line 1))
+
+ ;; 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))))))))))
+
+(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.")
+
+\f
+;;; Perform those texinfo-to-info conversions that apply to the whole input
+;;; uniformly.
+
+(defun texinfo-format-scan ()
+ (texinfo-format-convert (point-min) (point-max))
+ ;; 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.
+ (goto-char (point-min))
+ (while (search-forward "@" nil t)
+ ;;
+ ;; These are the single-character accent commands: @^ @` @' @" @= @~
+ ;; In Info, they are simply quoted and the @ deleted.
+ ;; Other single-character commands:
+ ;; @* forces a line break,
+ ;; @- is a discretionary hyphenation point; does nothing in Info.
+ ;; @<space>, @<tab>, @<newline> each produce a single space,
+ ;; unless followed by a newline.
+ ;;
+ ;; Old version 2.34 expression: (looking-at "[@{}^'` *\"?!]")
+ (if (looking-at "[@{}^'`\"=~ \t\n*?!-]")
+ ;; @*, causes a line break.
+ (cond
+ ;; @*, 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))
+ (insert ?\n)))
+ ;; @-, 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))
+ (insert ? )))
+ ((= (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
+ ((= (following-char) ?\n)
+ ;; 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)
+ ;; Be compatible with makeinfo: if @' and its ilk are
+ ;; 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))))
+ ;; @ 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))
+ ;; 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))
+ ;; Handle let aliasing
+ (setq texinfo-command-name
+ (let (trial
+ (cmdname
+ (buffer-substring-no-properties
+ (1+ texinfo-command-start) texinfo-command-end)))
+ (while (setq trial (assoc cmdname texinfo-alias-list))
+ (setq cmdname (cdr trial)))
+ (intern cmdname)))
+ ;; Call the handler for this command.
+ (let ((enclosure-type
+ (assoc
+ (symbol-name texinfo-command-name)
+ texinfo-enclosure-list)))
+ (if enclosure-type
+ (progn
+ (insert
+ (car (car (cdr enclosure-type)))
+ (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)))))))
+
+ (cond (texinfo-stack
+ (goto-char (nth 2 (car texinfo-stack)))
+ (error "Unterminated @%s" (car (car texinfo-stack)))))
+
+ ;; Remove excess whitespace
+ (let ((whitespace-silent t))
+ (whitespace-cleanup)))
+
+(defvar texinfo-copying-text ""
+ "Text of the copyright notice and copying permissions.")
+
+(defun texinfo-copying ()
+ "Copy the copyright notice and copying permissions from the Texinfo file,
+as indicated by the @copying ... @end copying command;
+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 ()
+ "Insert the copyright notice and copying permissions from the Texinfo file,
+which are indicated by the @copying ... @end copying command."
+ (insert (concat "\n" texinfo-copying-text)))