7cc5887fd7bd7933fb35fe87247c58bd7a8cf878
[gnus] / texi / infohack.el
1 ;;; infohack.el --- a hack to format info file.
2 ;; Copyright (C)  2001, 2003, 2004, 2008, 2009, 2010
3 ;;   Free Software Foundation, Inc.
4
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
6 ;; Keywords: info
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'texinfmt)
30
31 (if (fboundp 'texinfo-copying)
32     nil
33   ;; Support @copying and @insertcopying for Emacs 21.3 and lesser and
34   ;; XEmacs.
35   (defvar texinfo-copying-text ""
36     "Text of the copyright notice and copying permissions.")
37
38   (defun texinfo-copying ()
39     "Copy the copyright notice and copying permissions from the Texinfo file,
40 as indicated by the @copying ... @end copying command;
41 insert the text with the @insertcopying command."
42     (let ((beg (progn (beginning-of-line) (point)))
43           (end  (progn (re-search-forward "^@end copying[ \t]*\n") (point))))
44       (setq texinfo-copying-text
45             (buffer-substring-no-properties
46              (save-excursion (goto-char beg) (forward-line 1) (point))
47              (save-excursion (goto-char end) (forward-line -1) (point))))
48       (delete-region beg end)))
49
50   (defun texinfo-insertcopying ()
51     "Insert the copyright notice and copying permissions from the Texinfo file,
52 which are indicated by the @copying ... @end copying command."
53     (insert (concat "\n" texinfo-copying-text)))
54
55   (defadvice texinfo-format-scan (before expand-@copying-section activate)
56     "Extract @copying and replace @insertcopying with it."
57     (goto-char (point-min))
58     (when (search-forward "@copying" nil t)
59       (texinfo-copying))
60     (while (search-forward "@insertcopying" nil t)
61       (delete-region (match-beginning 0) (match-end 0))
62       (texinfo-insertcopying))))
63
64 (defun infohack-remove-unsupported ()
65   (goto-char (point-min))
66   (while (re-search-forward "@\\(end \\)?ifnottex" nil t) 
67     (replace-match ""))
68   (goto-char (point-min))
69   (while (search-forward "\n@iflatex\n" nil t)
70     (delete-region (1+ (match-beginning 0))
71                    (search-forward "\n@end iflatex\n"))))
72
73 (defun infohack-include-files ()
74   "Insert @include files."
75   (goto-char (point-min))
76   (set-syntax-table texinfo-format-syntax-table)
77   (let (start texinfo-command-end filename)
78     (while (re-search-forward "^@include" nil t)
79       (setq start (match-beginning 0)
80             texinfo-command-end (point)
81             filename (texinfo-parse-line-arg))
82       (delete-region start (point-at-bol 2))
83       (message "Reading included file: %s" filename)
84       (save-excursion
85         (save-restriction
86           (narrow-to-region
87            (point)
88            (+ (point) (car (cdr (insert-file-contents filename)))))
89           (goto-char (point-min))
90           ;; Remove `@setfilename' line from included file, if any,
91           ;; so @setfilename command not duplicated.
92           (if (re-search-forward "^@setfilename" (point-at-eol 100) t)
93               (delete-region (point-at-bol 1)
94                              (point-at-bol 2))))))))
95
96 (defun infohack (file)
97   (let ((dest-directory default-directory)
98         (max-lisp-eval-depth (max max-lisp-eval-depth 600))
99         coding-system)
100     ;; Emacs 21.3 doesn't support @documentencoding
101     (unless (get 'documentencoding 'texinfo-format)
102       (put 'documentencoding 'texinfo-format 
103            'texinfo-discard-line-with-args))
104     (find-file file)
105     (setq buffer-read-only nil)
106     (setq coding-system buffer-file-coding-system)
107     (infohack-remove-unsupported)
108     (infohack-include-files)
109     (texinfo-every-node-update) 
110     (texinfo-format-buffer t) ;; Don't save any file.
111     (setq default-directory dest-directory)
112     (setq buffer-file-name 
113           (expand-file-name (file-name-nondirectory buffer-file-name)
114                             default-directory))
115     (setq buffer-file-coding-system coding-system)
116     (if (> (buffer-size) (if (boundp 'Info-split-threshold)
117                              (+ 50000 Info-split-threshold)
118                            100000))
119         (Info-split))
120     (save-buffer)))
121
122 (eval-and-compile
123   (when (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
124                       (symbol-name system-type))
125     (defun subst-char-in-region (START END FROMCHAR TOCHAR &optional NOUNDO)
126       "From START to END, replace FROMCHAR with TOCHAR each time it occurs.
127 If optional arg NOUNDO is non-nil, don't record this change for undo
128 and don't mark the buffer as really changed.
129 Both characters must have the same length of multi-byte form."
130       (let ((original-buffer-undo-list buffer-undo-list)
131             (modified (buffer-modified-p)))
132         (if NOUNDO
133             (setq buffer-undo-list t))
134         (goto-char START)
135         (let ((from (char-to-string FROMCHAR))
136               (to (char-to-string TOCHAR)))
137           (while (search-forward from END t)
138             (replace-match to t t)))
139         (if NOUNDO
140             (progn (setq buffer-undo-list original-buffer-undo-list)
141                    (set-buffer-modidifed-p modified)))))))
142
143 (defun batch-makeinfo ()
144   "Emacs makeinfo in batch mode."
145   (infohack (car command-line-args-left))
146   (setq command-line-args-left nil))
147
148 ;;; infohack.el ends here