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