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