(pop3-open-ssl-stream): Do away with w32-related
[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                                      '(defcustom defface defgroup
349                                        define-widget quote))
350                          (while (consp elem)
351                            (push (car elem) form)
352                            (setq elem (cdr elem)))
353                          (when (and elem
354                                     (symbolp elem)
355                                     (not (eq ': elem))
356                                     (eq ?: (aref (symbol-name elem) 0))
357                                     (not (memq elem ignores))
358                                     (not (memq elem keywords)))
359                            (push elem keywords))))))))
360              (setq keywords (sort keywords
361                                   (lambda (a b)
362                                     (string-lessp (symbol-name a)
363                                                   (symbol-name b)))))
364              (erase-buffer)
365              (insert (format "%s" keywords))
366              (write-region (point-min) (point) cache nil 'silent)
367              (message
368               "The following colon keywords will be bound at run-time:\n %s"
369               keywords))))
370        (kill-buffer buffer)
371        (defconst dgnushack-colon-keywords keywords)
372        (while keywords
373          (set (car keywords) (car keywords))
374          (setq keywords (cdr keywords)))))
375    (byte-compile 'dgnushack-bind-colon-keywords)
376    (dgnushack-bind-colon-keywords)))
377
378 (when (boundp 'MULE)
379   (setq :version ':version
380         :set-after ':set-after)
381   (require 'custom)
382   (defadvice custom-handle-keyword
383     (around dont-signal-an-error-even-if-unsupported-keyword-is-given
384             activate)
385     "Don't signal an error even if unsupported keyword is given."
386     (if (not (memq (ad-get-arg 1) '(:version :set-after)))
387         ad-do-it)))
388
389 (when (boundp 'MULE)
390   (put 'custom-declare-face 'byte-optimizer
391        'byte-optimize-ignore-unsupported-custom-keywords)
392   (put 'custom-declare-group 'byte-optimizer
393        'byte-optimize-ignore-unsupported-custom-keywords)
394   (defun byte-optimize-ignore-unsupported-custom-keywords (form)
395     (if (or (memq ':version (nthcdr 4 form))
396             (memq ':set-after (nthcdr 4 form)))
397         (let ((newform (list (car form) (nth 1 form)
398                              (nth 2 form) (nth 3 form)))
399               (args (nthcdr 4 form)))
400           (while args
401             (or (memq (car args) '(:version :set-after))
402                 (setq newform (nconc newform (list (car args)
403                                                    (car (cdr args))))))
404             (setq args (cdr (cdr args))))
405           newform)
406       form))
407
408   (put 'custom-declare-variable 'byte-hunk-handler
409        'byte-compile-file-form-custom-declare-variable)
410   (defun byte-compile-file-form-custom-declare-variable (form)
411     ;; Bind defcustom'ed variables.
412     (if (memq 'free-vars byte-compile-warnings)
413         (setq byte-compile-bound-variables
414               (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
415     (if (memq ':version (nthcdr 4 form))
416         ;; Make the variable uncustomizable.
417         `(defvar ,(nth 1 (nth 1 form)) ,(nth 1 (nth 2 form))
418            ,(substring (nth 3 form) (if (string-match "^[\t *]+" (nth 3 form))
419                                         (match-end 0)
420                                       0)))
421       ;; Ignore unsupported keyword(s).
422       (if (memq ':set-after (nthcdr 4 form))
423           (let ((newform (list (car form) (nth 1 form)
424                                (nth 2 form) (nth 3 form)))
425                 (args (nthcdr 4 form)))
426             (while args
427               (or (eq (car args) ':set-after)
428                   (setq newform (nconc newform (list (car args)
429                                                      (car (cdr args))))))
430               (setq args (cdr (cdr args))))
431             newform)
432         form)))
433
434   (defadvice byte-compile-inline-expand (around ignore-built-in-functions
435                                                 (form) activate)
436     "Ignore built-in functions."
437     (let* ((name (car form))
438            (fn (and (fboundp name)
439                     (symbol-function name))))
440       (if (subrp fn)
441           ;; Give up on inlining.
442           (setq ad-return-value form)
443         ad-do-it))))
444
445 ;; Unknown variables and functions.
446 (unless (boundp 'buffer-file-coding-system)
447   (defvar buffer-file-coding-system (symbol-value 'file-coding-system)))
448 (unless (featurep 'xemacs)
449   (defalias 'Custom-make-dependencies 'ignore)
450   (defalias 'update-autoloads-from-directory 'ignore))
451
452 (defalias 'device-sound-enabled-p 'ignore)
453 (defalias 'play-sound-file 'ignore)
454 (defalias 'nndb-request-article 'ignore)
455 (defalias 'efs-re-read-dir 'ignore)
456 (defalias 'ange-ftp-re-read-dir 'ignore)
457 (defalias 'define-mail-user-agent 'ignore)
458
459 (defconst dgnushack-unexporting-files
460   (append '("dgnushack.el" "dgnuspath.el" "dgnuskwds.el" "lpath.el")
461           (condition-case nil
462               (progn (require 'shimbun) nil)
463             (error '("nnshimbun.el")))
464           (unless (or (condition-case code
465                           (require 'w3-parse)
466                         (error
467                          (message "No w3: %s%s, retrying..."
468                                   (error-message-string code)
469                                   (if (setq code (locate-library "w3-parse"))
470                                       (concat " (" code ")")
471                                     ""))
472                          nil))
473                       ;; Maybe mis-configured Makefile is used (e.g.
474                       ;; configured for FSFmacs but XEmacs is running).
475                       (let ((lp (delete dgnushack-w3-directory
476                                         (copy-sequence load-path))))
477                         (if (let ((load-path lp))
478                               (condition-case nil
479                                   (require 'w3-parse)
480                                 (error nil)))
481                             ;; If success, fix `load-path' for compiling.
482                             (progn
483                               (setq load-path lp)
484                               (message " => fixed; W3DIR=%s"
485                                        (file-name-directory
486                                         (locate-library "w3-parse")))
487                               t)
488                           (message " => ignored")
489                           nil)))
490             '("nnultimate.el" "webmail.el" "nnwfm.el"))
491           (condition-case code
492               (progn (require 'mh-e) nil)
493             (error
494              (message "No mh-e: %s%s (ignored)"
495                       (error-message-string code)
496                       (if (setq code (locate-library "mh-e"))
497                           (concat " (" code ")")
498                         ""))
499              '("gnus-mh.el")))
500           (condition-case code
501               (progn (require 'xml) nil)
502             (error
503              (message "No xml: %s%s (ignored)"
504                       (error-message-string code)
505                       (if (setq code (locate-library "xml"))
506                           (concat " (" code ")")
507                         ""))
508              '("nnrss.el")))
509           (condition-case code
510               (progn (require 'bbdb) nil)
511             (error
512              (message "No bbdb: %s%s (ignored)"
513                       (error-message-string code)
514                       (if (setq code (locate-library "bbdb"))
515                           (concat " (" code ")")
516                         ""))
517              '("gnus-bbdb.el")))
518           (unless (featurep 'xemacs)
519             '("gnus-xmas.el" "messagexmas.el" "nnheaderxm.el"))
520           (when (and (not (featurep 'xemacs))
521                      (<= emacs-major-version 20))
522             '("smiley.el"))
523           (when (and (fboundp 'base64-decode-string)
524                      (subrp (symbol-function 'base64-decode-string)))
525             '("base64.el"))
526           (when (and (fboundp 'md5) (subrp (symbol-function 'md5)))
527             '("md5.el"))
528           (unless (boundp 'MULE)
529             '("canlock-om.el")))
530   "Files which will not be installed.")
531
532 (defconst dgnushack-exporting-files
533   (let ((files (directory-files srcdir nil "^[^=].*\\.el$" t)))
534     (dolist (file dgnushack-unexporting-files)
535       (setq files (delete file files)))
536     (sort files 'string-lessp))
537   "Files which will be compiled and installed.")
538
539 (defun dgnushack-exporting-files ()
540   "Print name of files which will be installed."
541   (princ (mapconcat 'identity dgnushack-exporting-files " ")))
542
543 (defun dgnushack-compile (&optional warn)
544   ;;(setq byte-compile-dynamic t)
545   (unless warn
546     (setq byte-compile-warnings
547           '(free-vars unresolved callargs redefine)))
548   (unless (locate-library "cus-edit")
549     (error "You do not seem to have Custom installed.
550 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
551 You also then need to add the following to the lisp/dgnushack.el file:
552
553      (push \"~/lisp/custom\" load-path)
554
555 Modify to suit your needs."))
556
557   ;; Show `load-path'.
558   (message "load-path=(\"%s\")"
559            (mapconcat 'identity load-path "\"\n           \""))
560
561   (dolist (file dgnushack-exporting-files)
562     (setq file (expand-file-name file srcdir))
563     (when (and (file-exists-p
564                 (setq elc (concat (file-name-nondirectory file) "c")))
565                (file-newer-than-file-p file elc))
566       (delete-file elc)))
567
568   (let ((files dgnushack-exporting-files)
569         ;;(byte-compile-generate-call-tree t)
570         file elc)
571     ;; Avoid barfing (from gnus-xmas) because the etc directory is not yet
572     ;; installed.
573     (when (featurep 'xemacs)
574       (setq gnus-xmas-glyph-directory "dummy"))
575     (while (setq file (pop files))
576       (setq file (expand-file-name file srcdir))
577       (when (or (not (file-exists-p
578                       (setq elc (concat (file-name-nondirectory file) "c"))))
579                 (file-newer-than-file-p file elc))
580         (ignore-errors
581           (byte-compile-file file))))))
582
583 (defun dgnushack-recompile ()
584   (require 'gnus)
585   (byte-recompile-directory "." 0))
586
587 (defvar dgnushack-gnus-load-file (expand-file-name "gnus-load.el" srcdir))
588 (defvar dgnushack-cus-load-file (expand-file-name "cus-load.el" srcdir))
589 (defvar dgnushack-auto-load-file (expand-file-name "auto-autoloads.el" srcdir))
590
591 (defun dgnushack-make-cus-load ()
592   (when (condition-case nil
593             (load "cus-dep")
594           (error nil))
595     (let ((cusload-base-file dgnushack-cus-load-file))
596       (if (fboundp 'custom-make-dependencies)
597           (custom-make-dependencies)
598         (Custom-make-dependencies)))))
599
600 (defun dgnushack-make-auto-load ()
601   (require 'autoload)
602   (unless (make-autoload '(define-derived-mode child parent name
603                             "docstring" body)
604                          "file")
605     (defadvice make-autoload (around handle-define-derived-mode activate)
606       "Handle `define-derived-mode'."
607       (if (eq (car-safe (ad-get-arg 0)) 'define-derived-mode)
608           (setq ad-return-value
609                 (list 'autoload
610                       (list 'quote (nth 1 (ad-get-arg 0)))
611                       (ad-get-arg 1)
612                       (nth 4 (ad-get-arg 0))
613                       t nil))
614         ad-do-it))
615     (put 'define-derived-mode 'doc-string-elt 3))
616   (let ((generated-autoload-file dgnushack-gnus-load-file)
617         (make-backup-files nil)
618         (autoload-package-name "gnus"))
619     (if (featurep 'xemacs)
620         (progn
621           (if (file-exists-p generated-autoload-file)
622               (delete-file generated-autoload-file))
623           (if (file-exists-p dgnushack-auto-load-file)
624               (delete-file dgnushack-auto-load-file)))
625       (with-temp-file generated-autoload-file
626         (insert ?\014)))
627     (if (featurep 'xemacs)
628         (let ((si:message (symbol-function 'message)))
629           (defun message (fmt &rest args)
630             (cond ((and (string-equal "Generating autoloads for %s..." fmt)
631                         (file-exists-p (file-name-nondirectory (car args))))
632                    (funcall si:message
633                             fmt (file-name-nondirectory (car args))))
634                   ((string-equal "No autoloads found in %s" fmt))
635                   ((string-equal "Generating autoloads for %s...done" fmt))
636                   (t (apply si:message fmt args))))
637           (unwind-protect
638               (batch-update-autoloads)
639             (fset 'message si:message)))
640       (batch-update-autoloads))))
641
642 (defun dgnushack-make-load ()
643   (message (format "Generating %s..." dgnushack-gnus-load-file))
644   (with-temp-file dgnushack-gnus-load-file
645     (if (file-exists-p dgnushack-cus-load-file)
646         (progn
647           (insert-file-contents dgnushack-cus-load-file)
648           (delete-file dgnushack-cus-load-file)
649           (goto-char (point-min))
650           (search-forward ";;; Code:")
651           (forward-line)
652           (delete-region (point-min) (point))
653           (unless (re-search-forward "\
654 ^[\t ]*(autoload[\t\n ]+\\('\\|(quote[\t\n ]+\\)custom-add-loads[\t\n ]"
655                                      nil t)
656             (insert "\n(autoload 'custom-add-loads \"cus-load\")\n"))
657           (goto-char (point-min))
658           (insert "\
659 ;;; gnus-load.el --- automatically extracted custom dependencies and autoload
660 ;;
661 ;;; Code:
662 ")
663           (goto-char (point-max))
664           (if (search-backward "custom-versions-load-alist" nil t)
665               (forward-line -1)
666             (forward-line -1)
667             (while (eq (char-after) ?\;)
668               (forward-line -1))
669             (forward-line))
670           (delete-region (point) (point-max))
671           (insert "\n"))
672       (insert "\
673 ;;; gnus-load.el --- automatically extracted autoload
674 ;;
675 ;;; Code:
676 "))
677     ;; smiley-* are duplicated. Remove them all.
678     (let ((point (point)))
679       (insert-file-contents dgnushack-gnus-load-file)
680       (goto-char point)
681       (while (search-forward "smiley-" nil t)
682         (beginning-of-line)
683         (if (looking-at "(autoload ")
684             (delete-region (point) (progn (forward-sexp) (point)))
685           (forward-line))))
686     ;;
687     (goto-char (point-max))
688     (when (search-backward "\n(provide " nil t)
689       (forward-line -1)
690       (delete-region (point) (point-max)))
691     (insert "\
692
693 \(provide 'gnus-load)
694
695 ;;; Local Variables:
696 ;;; version-control: never
697 ;;; no-byte-compile: t
698 ;;; no-update-autoloads: t
699 ;;; End:
700 ;;; gnus-load.el ends here
701 ")
702     ;; Workaround the bug in some version of XEmacs.
703     (when (featurep 'xemacs)
704       (condition-case nil
705           (require 'cus-load)
706         (error nil))
707       (goto-char (point-min))
708       (when (and (fboundp 'custom-add-loads)
709                  (not (search-forward "\n(autoload 'custom-add-loads " nil t)))
710         (search-forward "\n;;; Code:" nil t)
711         (forward-line 1)
712         (insert "\n(autoload 'custom-add-loads \"cus-load\")\n"))))
713   (message (format "Compiling %s..." dgnushack-gnus-load-file))
714   (byte-compile-file dgnushack-gnus-load-file))
715
716 \f
717 (defun dgnushack-compose-package ()
718   "Re-split the file gnus-load.el into custom-load.el and
719 auto-autoloads.el.  It is silly, should be improved!"
720   (message "
721 Re-splitting gnus-load.el into custom-load.el and auto-autoloads.el...")
722   (let ((customload (expand-file-name "custom-load.el" srcdir))
723         (autoloads (expand-file-name "auto-autoloads.el" srcdir))
724         start)
725     (with-temp-buffer
726       (insert-file-contents dgnushack-gnus-load-file)
727       (delete-file dgnushack-gnus-load-file)
728       (when (file-exists-p (concat dgnushack-gnus-load-file "c"))
729         (delete-file (concat dgnushack-gnus-load-file "c")))
730       (while (prog1
731                  (looking-at "[\t ;]")
732                (forward-line 1)))
733       (setq start (point))
734       (insert "\
735 ;;; custom-load.el --- automatically extracted custom dependencies\n
736 ;;; Code:\n\n")
737       (goto-char (point-max))
738       (while (progn
739                (forward-line -1)
740                (not (looking-at "[\t ]*(custom-add-loads[\t\n ]"))))
741       (forward-list 1)
742       (forward-line 1)
743       (insert "\n;;; custom-load.el ends here\n")
744       (write-region start (point) customload)
745       (while (looking-at "[\t ]*$")
746         (forward-line 1))
747       (setq start (point))
748       (if (re-search-forward "^[\t\n ]*(if[\t\n ]+(featurep[\t\n ]" nil t)
749           (let ((from (goto-char (match-beginning 0))))
750             (delete-region from (progn
751                                   (forward-list 1)
752                                   (forward-line 1)
753                                   (point))))
754         (while (looking-at "[\t ;]")
755           (forward-line 1)))
756       (insert "(if (featurep 'gnus-autoloads) (error \"Already loaded\"))\n")
757       (goto-char (point-max))
758       (while (progn
759                (forward-line -1)
760                (not (looking-at "[\t ]*(provide[\t\n ]"))))
761       (insert "(provide 'gnus-autoloads)\n")
762       (write-region start (point) autoloads))
763     (byte-compile-file customload)
764     (byte-compile-file autoloads))
765   (message "\
766 Re-splitting gnus-load.el into custom-load.el and auto-autoloads.el...done
767 \n"))
768
769 \f
770 (defconst dgnushack-info-file-regexp-en
771   (let ((names '("gnus" "message" "emacs-mime"))
772         regexp name)
773     (while (setq name (pop names))
774       (setq regexp (concat regexp "^" name "\\.info\\(-[0-9]+\\)?$"
775                            (when names "\\|"))))
776     regexp)
777   "Regexp matching English info files.")
778
779 (defconst dgnushack-info-file-regexp-ja
780   (let ((names '("gnus-ja" "message-ja"))
781         regexp name)
782     (while (setq name (pop names))
783       (setq regexp (concat regexp "^" name "\\.info\\(-[0-9]+\\)?$"
784                            (when names "\\|"))))
785     regexp)
786   "Regexp matching Japanese info files.")
787
788 (defun dgnushack-remove-extra-files-in-package ()
789   "Remove extra files in the lisp directory of the XEmacs package."
790   (let ((lisp-dir (expand-file-name (concat "lisp/"
791                                             ;; GNUS_PRODUCT_NAME
792                                             (cadr command-line-args-left)
793                                             "/")
794                                     ;; PACKAGEDIR
795                                     (car command-line-args-left))))
796     (when (file-directory-p lisp-dir)
797       (let (files)
798         (dolist (file dgnushack-exporting-files)
799           (setq files (nconc files (list file (concat file "c")))))
800         (dolist (file (directory-files lisp-dir nil nil t t))
801           (unless (member file files)
802             (setq file (expand-file-name file lisp-dir))
803             (message "Removing %s..." file)
804             (condition-case nil
805                 (delete-file file)
806               (error nil))))))))
807
808 (defun dgnushack-install-package-manifest ()
809   "Install MANIFEST file as an XEmacs package."
810   (let* ((package-dir (car command-line-args-left))
811          (product-name (cadr command-line-args-left))
812          (name (expand-file-name (concat "pkginfo/MANIFEST." product-name)
813                                  package-dir))
814          make-backup-files)
815     (message "Generating %s..." name)
816     (with-temp-file name
817       (insert "pkginfo/MANIFEST." product-name "\n")
818       (let ((lisp-dir (concat "lisp/" product-name "/"))
819             (files (sort (directory-files "." nil "\\.elc?$" t) 'string-lessp))
820             file)
821         (while (setq file (pop files))
822           (unless (member file dgnushack-unexporting-files)
823             (insert lisp-dir file "\n")))
824         (setq files
825               (sort (directory-files "../texi/" nil
826                                      (concat dgnushack-info-file-regexp-en
827                                              "\\|"
828                                              dgnushack-info-file-regexp-ja)
829                                      t)
830                     'string-lessp))
831         (while (setq file (pop files))
832           (insert "info/" file "\n"))))))
833
834 \f
835 (define-compiler-macro describe-key-briefly (&whole form key &optional insert)
836   (if (condition-case nil
837           (progn
838             (describe-key-briefly '((())) nil)
839             t)
840         (wrong-number-of-arguments nil);; Old Emacsen.
841         (error t))
842       form
843     (if insert
844         `(if ,insert
845              (insert (funcall 'describe-key-briefly ,key))
846            (funcall 'describe-key-briefly ,key))
847       `(funcall 'describe-key-briefly ,key))))
848
849 ;;; dgnushack.el ends here