Add suffix to Texinfo file when formatting.
[elisp/gnus.git-] / lisp / dgnushack.el
1 ;;; dgnushack.el --- a hack to set the load path for byte-compiling
2 ;; Copyright (C) 1994,95,96,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
6 ;; Version: 4.19
7 ;; Keywords: news, path
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 ;; Set coding priority of Shift-JIS to the bottom.
31 (defvar *predefined-category*)
32 (defvar coding-category-list)
33 (if (featurep 'xemacs)
34     (fset 'set-coding-priority 'ignore)
35   (fset 'coding-priority-list 'ignore)
36   (fset 'set-coding-priority-list 'ignore))
37 (cond ((and (featurep 'xemacs) (featurep 'mule))
38        (if (memq 'shift-jis (coding-priority-list))
39            (set-coding-priority-list
40             (nconc (delq 'shift-jis (coding-priority-list)) '(shift-jis)))))
41       ((boundp 'MULE)
42        (put '*coding-category-sjis* 'priority (length *predefined-category*)))
43       ((featurep 'mule)
44        (if (memq 'coding-category-sjis coding-category-list)
45            (set-coding-priority
46             (nconc (delq 'coding-category-sjis coding-category-list)
47                    '(coding-category-sjis))))))
48
49 (fset 'facep 'ignore)
50
51 (require 'cl)
52 (require 'bytecomp)
53
54 ;; Attempt to pickup the additional load-path(s).
55 (load (expand-file-name "./dgnuspath.el") nil nil t)
56 (condition-case err
57     (load "~/.lpath.el" t nil t)
58   (error (message "Error in \"~/.lpath.el\" file: %s" err)))
59
60 (push "." load-path)
61 (load "./lpath.el" nil t)
62
63 (condition-case nil
64     (char-after)
65   (wrong-number-of-arguments
66    ;; Optimize byte code for `char-after',
67    (put 'char-after 'byte-optimizer 'byte-optimize-char-after)
68    (defun byte-optimize-char-after (form)
69      (if (null (cdr form))
70          '(char-after (point))
71        form))))
72
73 (condition-case nil
74     (char-before)
75   (wrong-number-of-arguments
76    ;; Optimize byte code for `char-before',
77    (put 'char-before 'byte-optimizer 'byte-optimize-char-before)
78    (defun byte-optimize-char-before (form)
79      (if (null (cdr form))
80          '(char-before (point))
81        form))))
82
83 (unless (fboundp 'byte-compile-file-form-custom-declare-variable)
84   ;; Bind defcustom'ed variables.
85   (put 'custom-declare-variable 'byte-hunk-handler
86        'byte-compile-file-form-custom-declare-variable)
87   (defun byte-compile-file-form-custom-declare-variable (form)
88     (if (memq 'free-vars byte-compile-warnings)
89         (setq byte-compile-bound-variables
90               (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
91     form))
92
93 ;; Bind functions defined by `defun-maybe'.
94 (put 'defun-maybe 'byte-hunk-handler 'byte-compile-file-form-defun-maybe)
95 (defun byte-compile-file-form-defun-maybe (form)
96   (if (and (not (fboundp (nth 1 form)))
97            (memq 'unresolved byte-compile-warnings))
98       (setq byte-compile-function-environment
99             (cons (cons (nth 1 form)
100                         (cons 'lambda (cdr (cdr form))))
101                   byte-compile-function-environment)))
102   form)
103
104 (condition-case nil
105     :symbol-for-testing-whether-colon-keyword-is-available-or-not
106   (void-variable
107    ;; Bind keywords.
108    (mapcar (lambda (keyword) (set keyword keyword))
109            '(:button-keymap
110              :data :file :mime-handle :path :predicate :user))))
111
112 ;; Unknown variables and functions.
113 (unless (boundp 'buffer-file-coding-system)
114   (defvar buffer-file-coding-system (symbol-value 'file-coding-system)))
115 (autoload 'font-lock-set-defaults "font-lock")
116 (unless (fboundp 'coding-system-get)
117   (defalias 'coding-system-get 'ignore))
118 (when (boundp 'MULE)
119   (defalias 'find-coding-system 'ignore))
120 (unless (fboundp 'get-charset-property)
121   (defalias 'get-charset-property 'ignore))
122 (unless (featurep 'xemacs)
123   (defalias 'Custom-make-dependencies 'ignore)
124   (defalias 'toolbar-gnus 'ignore)
125   (defalias 'update-autoloads-from-directory 'ignore))
126
127 (unless (fboundp 'with-temp-buffer)
128   ;; Pickup some macros.
129   (require 'emu))
130
131 (defalias 'device-sound-enabled-p 'ignore)
132 (defalias 'play-sound-file 'ignore)
133 (defalias 'nndb-request-article 'ignore)
134 (defalias 'efs-re-read-dir 'ignore)
135 (defalias 'ange-ftp-re-read-dir 'ignore)
136 (defalias 'define-mail-user-agent 'ignore)
137
138 (eval-and-compile
139   (unless (string-match "XEmacs" emacs-version)
140     (fset 'get-popup-menu-response 'ignore)
141     (fset 'event-object 'ignore)
142     (fset 'x-defined-colors 'ignore)
143     (fset 'read-color 'ignore)))
144
145 (defun dgnushack-compile (&optional warn)
146   ;;(setq byte-compile-dynamic t)
147   (unless warn
148     (setq byte-compile-warnings
149           '(free-vars unresolved callargs redefine)))
150   (unless (locate-library "cus-edit")
151     (error "You do not seem to have Custom installed.
152 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
153 You also then need to add the following to the lisp/dgnushack.el file:
154
155      (push \"~/lisp/custom\" load-path)
156
157 Modify to suit your needs."))
158   (let ((files (delete "dgnuspath.el"
159                        (directory-files "." nil "^[^=].*\\.el$")))
160         (xemacs (string-match "XEmacs" emacs-version))
161         ;;(byte-compile-generate-call-tree t)
162         file elc)
163     (condition-case ()
164         (require 'w3-forms)
165       (error (setq files (delete "nnweb.el" (delete "nnlistserv.el" files)))))
166     (condition-case ()
167         (require 'bbdb)
168       (error (setq files (delete "gnus-bbdb.el" files))))
169     (while (setq file (pop files))
170       (when (or (and (not xemacs)
171                      (not (member file '("gnus-xmas.el" "gnus-picon.el"
172                                          "messagexmas.el" "nnheaderxm.el"
173                                          "smiley.el" "x-overlay.el"))))
174                 (and xemacs
175                      (not (member file '("md5.el")))))
176         (when (or (not (file-exists-p (setq elc (concat file "c"))))
177                   (file-newer-than-file-p file elc))
178           (ignore-errors
179            (byte-compile-file file)))))))
180
181 (defun dgnushack-recompile ()
182   (require 'gnus)
183   (byte-recompile-directory "." 0))
184
185 \f
186 ;; Avoid byte-compile warnings.
187 (defvar gnus-product-name)
188 (defvar early-package-load-path)
189 (defvar early-packages)
190 (defvar last-package-load-path)
191 (defvar last-packages)
192 (defvar late-package-load-path)
193 (defvar late-packages)
194
195 (defconst dgnushack-info-file-regexp
196   (concat "^\\(gnus\\|message\\|emacs-mime\\|gnus-ja\\|message-ja\\)"
197           "\\.info\\(-[0-9]+\\)?$"))
198
199 (defconst dgnushack-texi-file-regexp
200   "^\\(gnus\\|message\\|emacs-mime\\|gnus-ja\\|message-ja\\)\\.texi$")
201
202 (defun dgnushack-make-package ()
203   (require 'gnus)
204   (let* ((product-name (downcase gnus-product-name))
205          (lisp-dir (concat "lisp/" product-name "/"))
206          make-backup-files)
207
208     (message "Updating autoloads for directory %s..." default-directory)
209     (let ((generated-autoload-file "auto-autoloads.el")
210           noninteractive
211           (omsg (symbol-function 'message)))
212       (defun message (fmt &rest args)
213         (cond ((and (string-equal "Generating autoloads for %s..." fmt)
214                     (file-exists-p (file-name-nondirectory (car args))))
215                (funcall omsg fmt (file-name-nondirectory (car args))))
216               ((string-equal "No autoloads found in %s" fmt))
217               ((string-equal "Generating autoloads for %s...done" fmt))
218               (t (apply omsg fmt args))))
219       (unwind-protect
220           (update-autoloads-from-directory default-directory)
221         (fset 'message omsg)))
222     (byte-compile-file "auto-autoloads.el")
223
224     (with-temp-buffer
225       (let ((standard-output (current-buffer)))
226         (Custom-make-dependencies "."))
227       (message (buffer-string)))
228     (require 'cus-load)
229     (byte-compile-file "custom-load.el")
230
231     (message "Generating MANIFEST.%s for the package..." product-name)
232     (with-temp-buffer
233       (insert "pkginfo/MANIFEST." product-name "\n"
234               lisp-dir
235               (mapconcat
236                'identity
237                (sort (delete "dgnuspath.el"
238                              (delete "patchs.elc"
239                                      (directory-files "." nil "\\.elc?$")))
240                      'string-lessp)
241                (concat "\n" lisp-dir))
242               "\ninfo/"
243               (mapconcat
244                'identity
245                (sort (directory-files "../texi/"
246                                       nil dgnushack-info-file-regexp)
247                      'string-lessp)
248                "\ninfo/")
249               "\n")
250       (write-file (concat "../MANIFEST." product-name)))))
251
252 (defun dgnushack-install-package ()
253   (let ((package-dir (car command-line-args-left))
254         dirs info-dir pkginfo-dir product-name lisp-dir manifest files)
255     (unless package-dir
256       (when (boundp 'early-packages)
257         (setq dirs (delq nil (append (when early-package-load-path
258                                        early-packages)
259                                      (when late-package-load-path
260                                        late-packages)
261                                      (when last-package-load-path
262                                        last-packages))))
263         (while (and dirs (not package-dir))
264           (when (file-exists-p (car dirs))
265             (setq package-dir (car dirs)
266                   dirs (cdr dirs))))))
267     (unless package-dir
268       (error "%s" "
269 You must specify the name of the package path as follows:
270
271 % make install-package PACKAGEDIR=/usr/local/lib/xemacs/xemacs-packages
272 "
273              ))
274     (setq info-dir (expand-file-name "info/" package-dir)
275           pkginfo-dir (expand-file-name "pkginfo/" package-dir))
276     (require 'gnus)
277     (setq product-name (downcase gnus-product-name)
278           lisp-dir (expand-file-name (concat "lisp/" product-name "/")
279                                      package-dir)
280           manifest (concat "MANIFEST." product-name))
281
282     (unless (file-directory-p lisp-dir)
283       (make-directory lisp-dir t))
284     (unless (file-directory-p info-dir)
285       (make-directory info-dir))
286     (unless (file-directory-p pkginfo-dir)
287       (make-directory pkginfo-dir))
288
289     (setq files
290           (sort (delete "dgnuspath.el"
291                         (delete "dgnuspath.elc"
292                                 (directory-files "." nil "\\.elc?$")))
293                 'string-lessp))
294     (mapcar
295      (lambda (file)
296        (unless (member file files)
297          (setq file (expand-file-name file lisp-dir))
298          (message "Removing %s..." file)
299          (condition-case nil
300              (delete-file file)
301            (error nil))))
302      (directory-files lisp-dir nil nil nil t))
303     (mapcar
304      (lambda (file)
305        (message "Copying %s to %s..." file lisp-dir)
306        (copy-file file (expand-file-name file lisp-dir) t t))
307      files)
308
309     (mapcar
310      (lambda (file)
311        (message "Copying ../texi/%s to %s..." file info-dir)
312        (copy-file (expand-file-name file "../texi/")
313                   (expand-file-name file info-dir)
314                   t t))
315      (sort (directory-files "../texi/" nil dgnushack-info-file-regexp)
316            'string-lessp))
317
318     (message "Copying ../%s to %s..." manifest pkginfo-dir)
319     (copy-file (expand-file-name manifest "../")
320                (expand-file-name manifest pkginfo-dir) t t)
321
322     (message "Done")))
323
324 (defun dgnushack-texi-add-suffix-and-format ()
325   (dgnushack-texi-format t))
326
327 (defun dgnushack-texi-format (&optional addsuffix)
328   (if (not noninteractive)
329       (error "batch-texinfo-format may only be used -batch."))
330   (require 'texinfmt)
331   (let ((auto-save-default nil)
332         (find-file-run-dired nil)
333         coding-system-for-write)
334     (let ((error 0)
335           file
336           (files ()))
337       (while command-line-args-left
338         (setq file (expand-file-name (car command-line-args-left)))
339         (cond ((not (file-exists-p file))
340                (message ">> %s does not exist!" file)
341                (setq error 1
342                      command-line-args-left (cdr command-line-args-left)))
343               ((file-directory-p file)
344                (setq command-line-args-left
345                      (nconc (directory-files file)
346                             (cdr command-line-args-left))))
347               (t
348                (setq files (cons file files)
349                      command-line-args-left (cdr command-line-args-left)))))
350       (while files
351         (setq file (car files)
352               files (cdr files))
353         (condition-case err
354             (progn
355               (if buffer-file-name (kill-buffer (current-buffer)))
356               (find-file file)
357               (setq coding-system-for-write buffer-file-coding-system)
358               (when (and addsuffix
359                          (re-search-forward
360                           "^@setfilename[\t ]+\\([^\t\n ]+\\)" nil t)
361                          (not (string-match "\\.info$" (match-string 1))))
362                 (insert ".info"))
363               (buffer-disable-undo (current-buffer))
364               ;; process @include before updating node
365               ;; This might produce some problem if we use @lowersection or such.
366               (let ((input-directory default-directory)
367                     (texinfo-command-end))
368                 (while (re-search-forward "^@include" nil t)
369                   (setq texinfo-command-end (point))
370                   (let ((filename (concat input-directory
371                                           (texinfo-parse-line-arg))))
372                     (re-search-backward "^@include")
373                     (delete-region (point) (save-excursion (forward-line 1) (point)))
374                     (message "Reading included file: %s" filename)
375                     (save-excursion
376                       (save-restriction
377                         (narrow-to-region
378                          (point)
379                          (+ (point) (car (cdr (insert-file-contents filename)))))
380                         (goto-char (point-min))
381                         ;; Remove `@setfilename' line from included file, if any,
382                         ;; so @setfilename command not duplicated.
383                         (if (re-search-forward 
384                              "^@setfilename" (save-excursion (forward-line 100) (point)) t)
385                             (progn
386                               (beginning-of-line)
387                               (delete-region
388                                (point) (save-excursion (forward-line 1) (point))))))))))
389               (texinfo-mode)
390               (texinfo-every-node-update)
391               (set-buffer-modified-p nil)
392               (message "texinfo formatting %s..." file)
393               (texinfo-format-buffer nil)
394               (if (buffer-modified-p)
395                   (progn (message "Saving modified %s" (buffer-file-name))
396                          (save-buffer))))
397           (error
398            (message ">> Error: %s" (prin1-to-string err))
399            (message ">>  point at")
400            (let ((s (buffer-substring (point)
401                                       (min (+ (point) 100)
402                                            (point-max))))
403                  (tem 0))
404              (while (setq tem (string-match "\n+" s tem))
405                (setq s (concat (substring s 0 (match-beginning 0))
406                                "\n>>  "
407                                (substring s (match-end 0)))
408                      tem (1+ tem)))
409              (message ">>  %s" s))
410            (setq error 1))))
411       (kill-emacs error))))
412
413 ;;; dgnushack.el ends here