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