gnus-notifications: add actions support
[gnus] / texi / infohack.el
1 ;;; infohack.el --- a hack to format info file.
2 ;; Copyright (C) 2001-2012 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.  If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 (require 'texinfmt)
27
28 (if (fboundp 'texinfo-copying)
29     nil
30   ;; Support @copying and @insertcopying for Emacs 21.3 and lesser and
31   ;; XEmacs.
32   (defvar texinfo-copying-text ""
33     "Text of the copyright notice and copying permissions.")
34
35   (defun texinfo-copying ()
36     "Copy the copyright notice and copying permissions from the Texinfo file,
37 as indicated by the @copying ... @end copying command;
38 insert the text with the @insertcopying command."
39     (let ((beg (progn (beginning-of-line) (point)))
40           (end  (progn (re-search-forward "^@end copying[ \t]*\n") (point))))
41       (setq texinfo-copying-text
42             (buffer-substring-no-properties
43              (save-excursion (goto-char beg) (forward-line 1) (point))
44              (save-excursion (goto-char end) (forward-line -1) (point))))
45       (delete-region beg end)))
46
47   (defun texinfo-insertcopying ()
48     "Insert the copyright notice and copying permissions from the Texinfo file,
49 which are indicated by the @copying ... @end copying command."
50     (insert (concat "\n" texinfo-copying-text)))
51
52   (defadvice texinfo-format-scan (before expand-@copying-section activate)
53     "Extract @copying and replace @insertcopying with it."
54     (goto-char (point-min))
55     (when (search-forward "@copying" nil t)
56       (texinfo-copying))
57     (while (search-forward "@insertcopying" nil t)
58       (delete-region (match-beginning 0) (match-end 0))
59       (texinfo-insertcopying))))
60
61 (defun infohack-remove-unsupported ()
62   (goto-char (point-min))
63   (while (re-search-forward "@\\(end \\)?ifnottex" nil t) 
64     (replace-match ""))
65   (goto-char (point-min))
66   (while (search-forward "\n@iflatex\n" nil t)
67     (delete-region (1+ (match-beginning 0))
68                    (search-forward "\n@end iflatex\n"))))
69
70 (defun infohack-include-files ()
71   "Insert @include files."
72   (goto-char (point-min))
73   (set-syntax-table texinfo-format-syntax-table)
74   (let (start texinfo-command-end filename)
75     (while (re-search-forward "^@include" nil t)
76       (setq start (match-beginning 0)
77             texinfo-command-end (point)
78             filename (texinfo-parse-line-arg))
79       (delete-region start (point-at-bol 2))
80       (message "Reading included file: %s" filename)
81       (save-excursion
82         (save-restriction
83           (narrow-to-region
84            (point)
85            (+ (point) (car (cdr (insert-file-contents filename)))))
86           (goto-char (point-min))
87           ;; Remove `@setfilename' line from included file, if any,
88           ;; so @setfilename command not duplicated.
89           (if (re-search-forward "^@setfilename" (point-at-eol 100) t)
90               (delete-region (point-at-bol 1)
91                              (point-at-bol 2))))))))
92
93 (defun infohack (file)
94   (let ((dest-directory default-directory)
95         (max-lisp-eval-depth (max max-lisp-eval-depth 600))
96         coding-system)
97     ;; Emacs 21.3 doesn't support @documentencoding
98     (unless (get 'documentencoding 'texinfo-format)
99       (put 'documentencoding 'texinfo-format 
100            'texinfo-discard-line-with-args))
101     (find-file file)
102     (setq buffer-read-only nil)
103     (setq coding-system buffer-file-coding-system)
104     (infohack-remove-unsupported)
105     (infohack-include-files)
106     (texinfo-every-node-update) 
107     (texinfo-format-buffer t) ;; Don't save any file.
108     (setq default-directory dest-directory)
109     (setq buffer-file-name 
110           (expand-file-name (file-name-nondirectory buffer-file-name)
111                             default-directory))
112     (setq buffer-file-coding-system coding-system)
113     (if (> (buffer-size) (if (boundp 'Info-split-threshold)
114                              (+ 50000 Info-split-threshold)
115                            100000))
116         (Info-split))
117     (save-buffer)))
118
119 (eval-and-compile
120   (when (string-match "windows-nt\\|os/2\\|emx\\|cygwin"
121                       (symbol-name system-type))
122     (defun subst-char-in-region (START END FROMCHAR TOCHAR &optional NOUNDO)
123       "From START to END, replace FROMCHAR with TOCHAR each time it occurs.
124 If optional arg NOUNDO is non-nil, don't record this change for undo
125 and don't mark the buffer as really changed.
126 Both characters must have the same length of multi-byte form."
127       (let ((original-buffer-undo-list buffer-undo-list)
128             (modified (buffer-modified-p)))
129         (if NOUNDO
130             (setq buffer-undo-list t))
131         (goto-char START)
132         (let ((from (char-to-string FROMCHAR))
133               (to (char-to-string TOCHAR)))
134           (while (search-forward from END t)
135             (replace-match to t t)))
136         (if NOUNDO
137             (progn (setq buffer-undo-list original-buffer-undo-list)
138                    (set-buffer-modidifed-p modified)))))))
139
140 (defun batch-makeinfo ()
141   "Emacs makeinfo in batch mode."
142   (infohack (car command-line-args-left))
143   (setq command-line-args-left nil))
144
145 ;;; infohack.el ends here