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