Feedback from t-gnus-6_15-quimby branch.
[elisp/gnus.git-] / lisp / dgnushack.el
1 ;;; dgnushack.el --- a hack to set the load path for byte-compiling
2 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
7 ;; Version: 4.19
8 ;; Keywords: news, path
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 ;; Set coding priority of Shift-JIS to the bottom.
32 (if (featurep 'xemacs)
33     (defalias 'set-coding-priority 'ignore)
34   (defalias 'coding-priority-list 'ignore)
35   (defalias 'set-coding-priority-list 'ignore))
36 (cond ((and (featurep 'xemacs) (featurep 'mule))
37        (if (memq 'shift-jis (coding-priority-list))
38            (set-coding-priority-list
39             (append (delq 'shift-jis (coding-priority-list)) '(shift-jis)))))
40       ((boundp 'MULE)
41        (put '*coding-category-sjis* 'priority (length *predefined-category*)))
42       ((featurep 'mule)
43        (if (memq 'coding-category-sjis coding-category-list)
44            (set-coding-priority
45             (append (delq 'coding-category-sjis
46                           (copy-sequence coding-category-list))
47                     '(coding-category-sjis))))))
48
49 (defalias 'facep 'ignore)
50
51 (require 'cl)
52 (unless (dolist (var nil t))
53   ;; Override the macro `dolist' which may have been defined in egg.el.
54   (load "cl-macs" nil t))
55
56 (defvar srcdir (or (getenv "srcdir") "."))
57
58 (defvar dgnushack-w3-directory (let ((w3dir (getenv "W3DIR")))
59                                  (unless (zerop (length w3dir))
60                                    (file-name-as-directory w3dir))))
61
62 (let ((urldir (getenv "URLDIR")))
63   (unless (zerop (length urldir))
64     (setq urldir (file-name-as-directory urldir))
65     (push (file-name-as-directory urldir) load-path))
66   (when (and dgnushack-w3-directory
67              (not (string-equal urldir dgnushack-w3-directory)))
68     (push dgnushack-w3-directory load-path)))
69
70 ;; If we are building w3 in a different directory than the source
71 ;; directory, we must read *.el from source directory and write *.elc
72 ;; into the building directory.  For that, we define this function
73 ;; before loading bytecomp.  Bytecomp doesn't overwrite this function.
74 (defun byte-compile-dest-file (filename)
75   "Convert an Emacs Lisp source file name to a compiled file name.
76  In addition, remove directory name part from FILENAME."
77   (setq filename (byte-compiler-base-file-name filename))
78   (setq filename (file-name-sans-versions filename))
79   (setq filename (file-name-nondirectory filename))
80   (if (memq system-type '(win32 w32 mswindows windows-nt))
81       (setq filename (downcase filename)))
82   (cond ((eq system-type 'vax-vms)
83          (concat (substring filename 0 (string-match ";" filename)) "c"))
84         ((string-match emacs-lisp-file-regexp filename)
85          (concat (substring filename 0 (match-beginning 0)) ".elc"))
86         (t (concat filename ".elc"))))
87
88 (require 'bytecomp)
89
90 (when (boundp 'MULE)
91   (let (current-load-list)
92     ;; Make the function to be silent at compile-time.
93     (defun locate-library (library &optional nosuffix)
94       "Show the full path name of Emacs library LIBRARY.
95 This command searches the directories in `load-path' like `M-x load-library'
96 to find the file that `M-x load-library RET LIBRARY RET' would load.
97 Optional second arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
98 to the specified name LIBRARY (a la calling `load' instead of `load-library')."
99       (interactive "sLocate library: ")
100       (catch 'answer
101         (mapcar
102          '(lambda (dir)
103             (mapcar
104              '(lambda (suf)
105                 (let ((try (expand-file-name (concat library suf) dir)))
106                   (and (file-readable-p try)
107                        (null (file-directory-p try))
108                        (progn
109                          (or noninteractive
110                              (message "Library is file %s" try))
111                          (throw 'answer try)))))
112              (if nosuffix '("") '(".elc" ".el" ""))))
113          load-path)
114         (or noninteractive
115             (message "No library %s in search path" library))
116         nil))
117     (byte-compile 'locate-library)))
118
119 (unless (fboundp 'si:byte-optimize-form-code-walker)
120   (byte-optimize-form nil);; Load `byte-opt' or `byte-optimize'.
121   (setq max-specpdl-size 3000)
122   (defalias 'si:byte-optimize-form-code-walker
123     (symbol-function 'byte-optimize-form-code-walker))
124   (defun byte-optimize-form-code-walker (form for-effect)
125     (if (and for-effect (memq (car-safe form) '(and or)))
126         ;; Fix bug in and/or forms.
127         (let ((fn (car form))
128               (backwards (reverse (cdr form))))
129           (while (and backwards
130                       (null (setcar backwards
131                                     (byte-optimize-form (car backwards) t))))
132             (setq backwards (cdr backwards)))
133           (if (and (cdr form) (null backwards))
134               (byte-compile-log
135                "  all subforms of %s called for effect; deleted" form))
136           (if backwards
137               (let ((head backwards))
138                 (while (setq backwards (cdr backwards))
139                   (setcar backwards (byte-optimize-form (car backwards)
140                                                         nil)))
141                 (cons fn (nreverse head)))))
142       (si:byte-optimize-form-code-walker form for-effect)))
143   (byte-compile 'byte-optimize-form-code-walker))
144
145 (condition-case nil
146     (char-after)
147   (wrong-number-of-arguments
148    ;; Optimize byte code for `char-after'.
149    (put 'char-after 'byte-optimizer 'byte-optimize-char-after)
150    (defun byte-optimize-char-after (form)
151      (if (null (cdr form))
152          '(char-after (point))
153        form))))
154
155 (condition-case nil
156     (char-before)
157   (wrong-number-of-arguments
158    ;; Optimize byte code for `char-before'.
159    (put 'char-before 'byte-optimizer 'byte-optimize-char-before)
160    (defun byte-optimize-char-before (form)
161      (if (null (cdr form))
162          '(char-before (point))
163        form))))
164
165 (load (expand-file-name "dgnuspath.el" srcdir) nil nil t)
166
167 (condition-case err
168     (load "~/.lpath.el" t nil t)
169   (error (message "Error in \"~/.lpath.el\" file: %s" err)))
170
171 ;; Don't load path-util until `char-after' and `char-before' have been
172 ;; optimized because it requires `poe' and then modify the functions.
173
174 ;; If the APEL modules are installed under the non-standard directory,
175 ;; for example "/var/home/john/lisp/apel-VERSION/", you should add that
176 ;; name using the configure option "--with-addpath=".
177 ;; And also the directory where the EMU modules are installed, for
178 ;; example "/usr/local/share/mule/19.34/site-lisp/", it should be
179 ;; included in the standard `load-path' or added by the configure
180 ;; option "--with-addpath=".
181 (let ((path (or (locate-library "path-util")
182                 (locate-library "apel/path-util")));; backward compat.
183       parent lpath)
184   (if path
185       (progn
186         (when (string-match "/$" (setq path (file-name-directory path)))
187           (setq path (substring path 0 (match-beginning 0))))
188         ;; path == "/var/home/john/lisp/apel-VERSION"
189         (when (string-match "/$" (setq parent (file-name-directory path)))
190           (setq parent (substring path 0 (match-beginning 0))))
191         ;; parent == "/var/home/john/lisp"
192         (if (setq lpath (or (member path load-path)
193                             (member (file-name-as-directory path) load-path)))
194             (unless (or (member parent load-path)
195                         (member (file-name-as-directory parent) load-path))
196               (push parent (cdr lpath)))
197           (push path load-path)
198           (unless (or (member parent load-path)
199                       (member (file-name-as-directory parent) load-path))
200             (push parent (cdr load-path))))
201         (require 'path-util))
202     (error "
203 APEL modules are not found in %s.
204 Try to re-configure with --with-addpath=APEL_PATH and run make again.
205 "
206            load-path)))
207
208 (unless (locate-library "mel")
209   (add-path "flim"))
210 (unless (module-installed-p 'mel)
211   ;; FLIM 1.14 may have installed in two "flim" subdirectories.
212   (push (expand-file-name "flim"
213                           (file-name-directory (get-latest-path "^apel$" t)))
214         load-path)
215   (unless (module-installed-p 'mel)
216     (error "
217 FLIM modules does not found in %s.
218 Try to re-configure with --with-addpath=FLIM_PATH and run make again.
219 "
220            load-path)))
221 (add-path "semi")
222
223 (push srcdir load-path)
224 (load (expand-file-name "lpath.el" srcdir) nil t t)
225
226 (load (expand-file-name "gnus-clfns.el" srcdir) nil t t)
227
228 (when (boundp 'MULE)
229   ;; Load special macros for compiling canlock.el.
230   (load (expand-file-name "canlock-om.el" srcdir) nil t t))
231
232 (require 'custom)
233
234 ;; Bind functions defined by `defun-maybe'.
235 (put 'defun-maybe 'byte-hunk-handler 'byte-compile-file-form-defun-maybe)
236 (defun byte-compile-file-form-defun-maybe (form)
237   (if (and (not (fboundp (nth 1 form)))
238            (memq 'unresolved byte-compile-warnings))
239       (setq byte-compile-function-environment
240             (cons (cons (nth 1 form)
241                         (cons 'lambda (cdr (cdr form))))
242                   byte-compile-function-environment)))
243   form)
244
245 (condition-case nil
246     :symbol-for-testing-whether-colon-keyword-is-available-or-not
247   (void-variable
248    (defun dgnushack-bind-colon-keywords ()
249      "Bind all the colon keywords for old Emacsen."
250      (let ((cache (expand-file-name "dgnuskwds.el" srcdir))
251            (makefile (expand-file-name "Makefile" srcdir))
252            (buffer (get-buffer-create " *colon keywords*"))
253            keywords ignores files file dirs dir form elem make-backup-files)
254        (save-excursion
255          (set-buffer buffer)
256          (let (buffer-file-format
257                format-alist
258                insert-file-contents-post-hook
259                insert-file-contents-pre-hook
260                jam-zcat-filename-list
261                jka-compr-compression-info-list)
262            (if (and (file-exists-p cache)
263                     (file-exists-p makefile)
264                     (file-newer-than-file-p cache makefile))
265                (progn
266                  (insert-file-contents cache nil nil nil t)
267                  (setq keywords (read buffer)))
268              (setq
269               ignores
270               '(:symbol-for-testing-whether-colon-keyword-is-available-or-not
271                 ;; The following keywords will be bound by CUSTOM.
272                 :get :group :initialize :link :load :options :prefix
273                 :require :set :tag :type)
274               files (list (locate-library "semi-def")
275                           (locate-library "mailcap")
276                           (locate-library "mime-def")
277                           (locate-library "path-util")
278                           (locate-library "poem"))
279               dirs (list (file-name-as-directory (expand-file-name srcdir))))
280              (while files
281                (when (setq file (pop files))
282                  (setq dir (file-name-directory file))
283                  (unless (member dir dirs)
284                    (push dir dirs))))
285              (message "Searching for all the colon keywords in:")
286              (while dirs
287                (setq dir (pop dirs))
288                (message " %s..." dir)
289                (setq files (directory-files dir t
290                                             "\\.el\\(\\.gz\\|\\.bz2\\)?$"))
291                (while files
292                  (setq file (pop files))
293                  (if (string-match "\\(\\.gz$\\)\\|\\.bz2$" file)
294                      (let ((temp (expand-file-name "dgnustemp.el" srcdir)))
295                        (when
296                            (let* ((binary (if (boundp 'MULE)
297                                               '*noconv*
298                                             'binary))
299                                   (coding-system-for-read binary)
300                                   (coding-system-for-write binary)
301                                   (input-coding-system binary)
302                                   (output-coding-system binary)
303                                   (default-process-coding-system
304                                     (cons binary binary))
305                                   call-process-hook)
306                              (insert-file-contents file nil nil nil t)
307                              (when
308                                  (condition-case code
309                                      (progn
310                                        (if (match-beginning 1)
311                                            (call-process-region
312                                             (point-min) (point-max)
313                                             "gzip" t buffer nil "-cd")
314                                          (call-process-region
315                                           (point-min) (point-max)
316                                           "bzip2" t buffer nil "-d"))
317                                        t)
318                                    (error
319                                     (erase-buffer)
320                                     (message "In file %s: %s" file code)
321                                     nil))
322                                (write-region (point-min) (point-max) temp
323                                              nil 'silent)
324                                t))
325                          (unwind-protect
326                              (insert-file-contents temp nil nil nil t)
327                            (delete-file temp))))
328                    (insert-file-contents file nil nil nil t))
329                  (while (setq form (condition-case nil
330                                        (read buffer)
331                                      (error nil)))
332                    (when (listp form)
333                      (while form
334                        (setq elem (pop form))
335                        (unless (memq (car-safe elem)
336                                      '(\` backquote
337                                        defcustom defface defgroup
338                                        define-widget quote))
339                          (while (consp elem)
340                            (push (car elem) form)
341                            (setq elem (cdr elem)))
342                          (when (and elem
343                                     (symbolp elem)
344                                     (not (eq ': elem))
345                                     (eq ?: (aref (symbol-name elem) 0))
346                                     (not (memq elem ignores))
347                                     (not (memq elem keywords)))
348                            (push elem keywords))))))))
349              (setq keywords (sort keywords
350                                   (lambda (a b)
351                                     (string-lessp (symbol-name a)
352                                                   (symbol-name b)))))
353              (erase-buffer)
354              (insert (format "%s" keywords))
355              (write-region (point-min) (point) cache nil 'silent)
356              (message
357               "The following colon keywords will be bound at run-time:\n %s"
358               keywords))))
359        (kill-buffer buffer)
360        (defconst dgnushack-colon-keywords keywords)
361        (while keywords
362          (set (car keywords) (car keywords))
363          (setq keywords (cdr keywords)))))
364    (byte-compile 'dgnushack-bind-colon-keywords)
365    (dgnushack-bind-colon-keywords)))
366
367 (when (boundp 'MULE)
368   (setq :version ':version
369         :set-after ':set-after)
370   (require 'custom)
371   (defadvice custom-handle-keyword
372     (around dont-signal-an-error-even-if-unsupported-keyword-is-given
373             activate)
374     "Don't signal an error even if unsupported keyword is given."
375     (if (not (memq (ad-get-arg 1) '(:version :set-after)))
376         ad-do-it)))
377
378 (when (boundp 'MULE)
379   (put 'custom-declare-face 'byte-optimizer
380        'byte-optimize-ignore-unsupported-custom-keywords)
381   (put 'custom-declare-group 'byte-optimizer
382        'byte-optimize-ignore-unsupported-custom-keywords)
383   (defun byte-optimize-ignore-unsupported-custom-keywords (form)
384     (if (or (memq ':version (nthcdr 4 form))
385             (memq ':set-after (nthcdr 4 form)))
386         (let ((newform (list (car form) (nth 1 form)
387                              (nth 2 form) (nth 3 form)))
388               (args (nthcdr 4 form)))
389           (while args
390             (or (memq (car args) '(:version :set-after))
391                 (setq newform (nconc newform (list (car args)
392                                                    (car (cdr args))))))
393             (setq args (cdr (cdr args))))
394           newform)
395       form))
396
397   (put 'custom-declare-variable 'byte-hunk-handler
398        'byte-compile-file-form-custom-declare-variable)
399   (defun byte-compile-file-form-custom-declare-variable (form)
400     ;; Bind defcustom'ed variables.
401     (if (memq 'free-vars byte-compile-warnings)
402         (setq byte-compile-bound-variables
403               (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
404     (if (memq ':version (nthcdr 4 form))
405         ;; Make the variable uncustomizable.
406         `(defvar ,(nth 1 (nth 1 form)) ,(nth 1 (nth 2 form))
407            ,(substring (nth 3 form) (if (string-match "^[\t *]+" (nth 3 form))
408                                         (match-end 0)
409                                       0)))
410       ;; Ignore unsupported keyword(s).
411       (if (memq ':set-after (nthcdr 4 form))
412           (let ((newform (list (car form) (nth 1 form)
413                                (nth 2 form) (nth 3 form)))
414                 (args (nthcdr 4 form)))
415             (while args
416               (or (eq (car args) ':set-after)
417                   (setq newform (nconc newform (list (car args)
418                                                      (car (cdr args))))))
419               (setq args (cdr (cdr args))))
420             newform)
421         form)))
422
423   (defadvice byte-compile-inline-expand (around ignore-built-in-functions
424                                                 (form) activate)
425     "Ignore built-in functions."
426     (let* ((name (car form))
427            (fn (and (fboundp name)
428                     (symbol-function name))))
429       (if (subrp fn)
430           ;; Give up on inlining.
431           (setq ad-return-value form)
432         ad-do-it))))
433
434 ;; Unknown variables and functions.
435 (unless (boundp 'buffer-file-coding-system)
436   (defvar buffer-file-coding-system (symbol-value 'file-coding-system)))
437 (unless (featurep 'xemacs)
438   (defalias 'Custom-make-dependencies 'ignore)
439   (defalias 'update-autoloads-from-directory 'ignore))
440
441 (defalias 'device-sound-enabled-p 'ignore)
442 (defalias 'play-sound-file 'ignore)
443 (defalias 'nndb-request-article 'ignore)
444 (defalias 'efs-re-read-dir 'ignore)
445 (defalias 'ange-ftp-re-read-dir 'ignore)
446 (defalias 'define-mail-user-agent 'ignore)
447
448 (defconst dgnushack-unexporting-files
449   (append '("dgnushack.el" "dgnuspath.el" "dgnuskwds.el" "lpath.el")
450           (condition-case nil
451               (progn (require 'shimbun) nil)
452             (error '("nnshimbun.el")))
453           (unless (or (condition-case code
454                           (require 'w3-parse)
455                         (error
456                          (message "No w3: %s %s retrying..." code
457                                   (locate-library "w3-parse"))
458                          nil))
459                       ;; Maybe mis-configured Makefile is used (e.g.
460                       ;; configured for FSFmacs but XEmacs is running).
461                       (let ((lp (delete dgnushack-w3-directory
462                                         (copy-sequence load-path))))
463                         (if (let ((load-path lp))
464                               (condition-case nil
465                                   (require 'w3-parse)
466                                 (error nil)))
467                             ;; If success, fix `load-path' for compiling.
468                             (progn
469                               (setq load-path lp)
470                               (message " => fixed; W3DIR=%s"
471                                        (file-name-directory
472                                         (locate-library "w3-parse")))
473                               t)
474                           (message " => ignored")
475                           nil)))
476             '("nnultimate.el" "webmail.el" "nnwfm.el"))
477           (condition-case code
478               (progn (require 'mh-e) nil)
479             (error
480              (message "No mh-e: %s %s (ignored)" code (locate-library "mh-e"))
481              '("gnus-mh.el")))
482           (condition-case code
483               (progn (require 'xml) nil)
484             (error
485              (message "No xml: %s %s (ignored)" code (locate-library "xml"))
486              '("nnrss.el")))
487           (condition-case code
488               (progn (require 'bbdb) nil)
489             (error
490              (message "No bbdb: %s %s (ignored)" code (locate-library "bbdb"))
491              '("gnus-bbdb.el")))
492           (unless (featurep 'xemacs)
493             '("gnus-xmas.el" "messagexmas.el" "nnheaderxm.el" "smiley.el"))
494           (when (or (featurep 'xemacs) (<= emacs-major-version 20))
495             '("smiley-ems.el"))
496           (when (and (fboundp 'base64-decode-string)
497                      (subrp (symbol-function 'base64-decode-string)))
498             '("base64.el"))
499           (when (and (fboundp 'md5) (subrp (symbol-function 'md5)))
500             '("md5.el"))
501           (unless (boundp 'MULE)
502             '("canlock-om.el")))
503   "Files which will not be installed.")
504
505 (defconst dgnushack-exporting-files
506   (let ((files (directory-files srcdir nil "^[^=].*\\.el$" t)))
507     (dolist (file dgnushack-unexporting-files)
508       (setq files (delete file files)))
509     (sort files 'string-lessp))
510   "Files which will be compiled and installed.")
511
512 (defun dgnushack-exporting-files ()
513   "Print name of files which will be installed."
514   (princ (mapconcat 'identity dgnushack-exporting-files " ")))
515
516 (defun dgnushack-compile (&optional warn)
517   ;;(setq byte-compile-dynamic t)
518   (unless warn
519     (setq byte-compile-warnings
520           '(free-vars unresolved callargs redefine)))
521   (unless (locate-library "cus-edit")
522     (error "You do not seem to have Custom installed.
523 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
524 You also then need to add the following to the lisp/dgnushack.el file:
525
526      (push \"~/lisp/custom\" load-path)
527
528 Modify to suit your needs."))
529
530   ;; Show `load-path'.
531   (message "load-path=(\"%s\")"
532            (mapconcat 'identity load-path "\"\n           \""))
533
534   (dolist (file dgnushack-exporting-files)
535     (setq file (expand-file-name file srcdir))
536     (when (and (file-exists-p
537                 (setq elc (concat (file-name-nondirectory file) "c")))
538                (file-newer-than-file-p file elc))
539       (delete-file elc)))
540
541   (let ((files dgnushack-exporting-files)
542         ;;(byte-compile-generate-call-tree t)
543         file elc)
544     ;; Avoid barfing (from gnus-xmas) because the etc directory is not yet
545     ;; installed.
546     (when (featurep 'xemacs)
547       (setq gnus-xmas-glyph-directory "dummy"))
548     (while (setq file (pop files))
549       (setq file (expand-file-name file srcdir))
550       (when (or (not (file-exists-p
551                       (setq elc (concat (file-name-nondirectory file) "c"))))
552                 (file-newer-than-file-p file elc))
553         (ignore-errors
554           (byte-compile-file file))))))
555
556 (defun dgnushack-recompile ()
557   (require 'gnus)
558   (byte-recompile-directory "." 0))
559
560 (defvar dgnushack-gnus-load-file (expand-file-name "gnus-load.el" srcdir))
561 (defvar dgnushack-cus-load-file (expand-file-name "cus-load.el" srcdir))
562 (defvar dgnushack-auto-load-file (expand-file-name "auto-autoloads.el" srcdir))
563
564 (defun dgnushack-make-cus-load ()
565   (when (condition-case nil
566             (load "cus-dep")
567           (error nil))
568     (let ((cusload-base-file dgnushack-cus-load-file))
569       (if (fboundp 'custom-make-dependencies)
570           (custom-make-dependencies)
571         (Custom-make-dependencies)))))
572
573 (defun dgnushack-make-auto-load ()
574   (require 'autoload)
575   (unless (make-autoload '(define-derived-mode child parent name
576                             "docstring" body)
577                          "file")
578     (defadvice make-autoload (around handle-define-derived-mode activate)
579       "Handle `define-derived-mode'."
580       (if (eq (car-safe (ad-get-arg 0)) 'define-derived-mode)
581           (setq ad-return-value
582                 (list 'autoload
583                       (list 'quote (nth 1 (ad-get-arg 0)))
584                       (ad-get-arg 1)
585                       (nth 4 (ad-get-arg 0))
586                       t nil))
587         ad-do-it))
588     (put 'define-derived-mode 'doc-string-elt 3))
589   (let ((generated-autoload-file dgnushack-gnus-load-file)
590         (make-backup-files nil)
591         (autoload-package-name "gnus"))
592     (if (featurep 'xemacs)
593         (progn
594           (if (file-exists-p generated-autoload-file)
595               (delete-file generated-autoload-file))
596           (if (file-exists-p dgnushack-auto-load-file)
597               (delete-file dgnushack-auto-load-file)))
598       (with-temp-file generated-autoload-file
599         (insert ?\014)))
600     (if (featurep 'xemacs)
601         (let ((si:message (symbol-function 'message)))
602           (defun message (fmt &rest args)
603             (cond ((and (string-equal "Generating autoloads for %s..." fmt)
604                         (file-exists-p (file-name-nondirectory (car args))))
605                    (funcall si:message
606                             fmt (file-name-nondirectory (car args))))
607                   ((string-equal "No autoloads found in %s" fmt))
608                   ((string-equal "Generating autoloads for %s...done" fmt))
609                   (t (apply si:message fmt args))))
610           (unwind-protect
611               (batch-update-autoloads)
612             (fset 'message si:message)))
613       (batch-update-autoloads))))
614
615 (defun dgnushack-make-load ()
616   (message (format "Generating %s..." dgnushack-gnus-load-file))
617   (with-temp-file dgnushack-gnus-load-file
618     (if (file-exists-p dgnushack-cus-load-file)
619         (progn
620           (insert-file-contents dgnushack-cus-load-file)
621           (delete-file dgnushack-cus-load-file)
622           (goto-char (point-min))
623           (search-forward ";;; Code:")
624           (forward-line)
625           (delete-region (point-min) (point))
626           (unless (re-search-forward "\
627 ^[\t ]*(autoload[\t\n ]+\\('\\|(quote[\t\n ]+\\)custom-add-loads[\t\n ]"
628                                      nil t)
629             (insert "\n(autoload 'custom-add-loads \"cus-load\")\n"))
630           (goto-char (point-min))
631           (insert "\
632 ;;; gnus-load.el --- automatically extracted custom dependencies and autoload
633 ;;
634 ;;; Code:
635 ")
636           (goto-char (point-max))
637           (if (search-backward "custom-versions-load-alist" nil t)
638               (forward-line -1)
639             (forward-line -1)
640             (while (eq (char-after) ?\;)
641               (forward-line -1))
642             (forward-line))
643           (delete-region (point) (point-max))
644           (insert "\n"))
645       (insert "\
646 ;;; gnus-load.el --- automatically extracted autoload
647 ;;
648 ;;; Code:
649 "))
650     ;; smiley-* are duplicated. Remove them all.
651     (let ((point (point)))
652       (insert-file-contents dgnushack-gnus-load-file)
653       (goto-char point)
654       (while (search-forward "smiley-" nil t)
655         (beginning-of-line)
656         (if (looking-at "(autoload ")
657             (delete-region (point) (progn (forward-sexp) (point)))
658           (forward-line))))
659     ;;
660     (goto-char (point-max))
661     (when (search-backward "\n(provide " nil t)
662       (forward-line -1)
663       (delete-region (point) (point-max)))
664     (insert "\
665
666 \(provide 'gnus-load)
667
668 ;;; Local Variables:
669 ;;; version-control: never
670 ;;; no-byte-compile: t
671 ;;; no-update-autoloads: t
672 ;;; End:
673 ;;; gnus-load.el ends here
674 ")
675     ;; Workaround the bug in some version of XEmacs.
676     (when (featurep 'xemacs)
677       (condition-case nil
678           (require 'cus-load)
679         (error nil))
680       (goto-char (point-min))
681       (when (and (fboundp 'custom-add-loads)
682                  (not (search-forward "\n(autoload 'custom-add-loads " nil t)))
683         (search-forward "\n;;; Code:" nil t)
684         (forward-line 1)
685         (insert "\n(autoload 'custom-add-loads \"cus-load\")\n"))))
686   (message (format "Compiling %s..." dgnushack-gnus-load-file))
687   (byte-compile-file dgnushack-gnus-load-file))
688
689 \f
690 (defun dgnushack-compose-package ()
691   "Re-split the file gnus-load.el into custom-load.el and
692 auto-autoloads.el.  It is silly, should be improved!"
693   (message "
694 Re-splitting gnus-load.el into custom-load.el and auto-autoloads.el...")
695   (let ((customload (expand-file-name "custom-load.el" srcdir))
696         (autoloads (expand-file-name "auto-autoloads.el" srcdir))
697         start)
698     (with-temp-buffer
699       (insert-file-contents dgnushack-gnus-load-file)
700       (delete-file dgnushack-gnus-load-file)
701       (when (file-exists-p (concat dgnushack-gnus-load-file "c"))
702         (delete-file (concat dgnushack-gnus-load-file "c")))
703       (while (prog1
704                  (looking-at "[\t ;]")
705                (forward-line 1)))
706       (setq start (point))
707       (insert "\
708 ;;; custom-load.el --- automatically extracted custom dependencies\n
709 ;;; Code:\n\n")
710       (goto-char (point-max))
711       (while (progn
712                (forward-line -1)
713                (not (looking-at "[\t ]*(custom-add-loads[\t\n ]"))))
714       (forward-list 1)
715       (forward-line 1)
716       (insert "\n;;; custom-load.el ends here\n")
717       (write-region start (point) customload)
718       (while (looking-at "[\t ]*$")
719         (forward-line 1))
720       (setq start (point))
721       (if (re-search-forward "^[\t\n ]*(if[\t\n ]+(featurep[\t\n ]" nil t)
722           (let ((from (goto-char (match-beginning 0))))
723             (delete-region from (progn
724                                   (forward-list 1)
725                                   (forward-line 1)
726                                   (point))))
727         (while (looking-at "[\t ;]")
728           (forward-line 1)))
729       (insert "(if (featurep 'gnus-autoloads) (error \"Already loaded\"))\n")
730       (goto-char (point-max))
731       (while (progn
732                (forward-line -1)
733                (not (looking-at "[\t ]*(provide[\t\n ]"))))
734       (insert "(provide 'gnus-autoloads)\n")
735       (write-region start (point) autoloads))
736     (byte-compile-file customload)
737     (byte-compile-file autoloads))
738   (message "\
739 Re-splitting gnus-load.el into custom-load.el and auto-autoloads.el...done
740 \n"))
741
742 \f
743 (defconst dgnushack-info-file-regexp-en
744   (let ((names '("gnus" "message" "emacs-mime"))
745         regexp name)
746     (while (setq name (pop names))
747       (setq regexp (concat regexp "^" name "\\.info\\(-[0-9]+\\)?$"
748                            (when names "\\|"))))
749     regexp)
750   "Regexp matching English info files.")
751
752 (defconst dgnushack-info-file-regexp-ja
753   (let ((names '("gnus-ja" "message-ja"))
754         regexp name)
755     (while (setq name (pop names))
756       (setq regexp (concat regexp "^" name "\\.info\\(-[0-9]+\\)?$"
757                            (when names "\\|"))))
758     regexp)
759   "Regexp matching Japanese info files.")
760
761 (defun dgnushack-remove-extra-files-in-package ()
762   "Remove extra files in the lisp directory of the XEmacs package."
763   (let ((lisp-dir (expand-file-name (concat "lisp/"
764                                             ;; GNUS_PRODUCT_NAME
765                                             (cadr command-line-args-left)
766                                             "/")
767                                     ;; PACKAGEDIR
768                                     (car command-line-args-left))))
769     (when (file-directory-p lisp-dir)
770       (let (files)
771         (dolist (file dgnushack-exporting-files)
772           (setq files (nconc files (list file (concat file "c")))))
773         (dolist (file (directory-files lisp-dir nil nil t t))
774           (unless (member file files)
775             (setq file (expand-file-name file lisp-dir))
776             (message "Removing %s..." file)
777             (condition-case nil
778                 (delete-file file)
779               (error nil))))))))
780
781 (defun dgnushack-install-package-manifest ()
782   "Install MANIFEST file as an XEmacs package."
783   (let* ((package-dir (car command-line-args-left))
784          (product-name (cadr command-line-args-left))
785          (name (expand-file-name (concat "pkginfo/MANIFEST." product-name)
786                                  package-dir))
787          make-backup-files)
788     (message "Generating %s..." name)
789     (with-temp-file name
790       (insert "pkginfo/MANIFEST." product-name "\n")
791       (let ((lisp-dir (concat "lisp/" product-name "/"))
792             (files (sort (directory-files "." nil "\\.elc?$" t) 'string-lessp))
793             file)
794         (while (setq file (pop files))
795           (unless (member file dgnushack-unexporting-files)
796             (insert lisp-dir file "\n")))
797         (setq files
798               (sort (directory-files "../texi/" nil
799                                      (concat dgnushack-info-file-regexp-en
800                                              "\\|"
801                                              dgnushack-info-file-regexp-ja)
802                                      t)
803                     'string-lessp))
804         (while (setq file (pop files))
805           (insert "info/" file "\n"))))))
806
807 \f
808 (define-compiler-macro describe-key-briefly (&whole form key &optional insert)
809   (if (condition-case nil
810           (progn
811             (describe-key-briefly '((())) nil)
812             t)
813         (wrong-number-of-arguments nil);; Old Emacsen.
814         (error t))
815       form
816     (if insert
817         `(if ,insert
818              (insert (funcall 'describe-key-briefly ,key))
819            (funcall 'describe-key-briefly ,key))
820       `(funcall 'describe-key-briefly ,key))))
821
822 ;;; dgnushack.el ends here