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