Synch with Oort Gnus.
[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
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..." code
469                                   (locate-library "w3-parse"))
470                          nil))
471                       ;; Maybe mis-configured Makefile is used (e.g.
472                       ;; configured for FSFmacs but XEmacs is running).
473                       (let ((lp (delete dgnushack-w3-directory
474                                         (copy-sequence load-path))))
475                         (if (let ((load-path lp))
476                               (condition-case nil
477                                   (require 'w3-parse)
478                                 (error nil)))
479                             ;; If success, fix `load-path' for compiling.
480                             (progn
481                               (setq load-path lp)
482                               (message " => fixed; W3DIR=%s"
483                                        (file-name-directory
484                                         (locate-library "w3-parse")))
485                               t)
486                           (message " => ignored")
487                           nil)))
488             '("nnultimate.el" "webmail.el" "nnwfm.el"))
489           (condition-case code
490               (progn (require 'mh-e) nil)
491             (error
492              (message "No mh-e: %s %s (ignored)" code (locate-library "mh-e"))
493              '("gnus-mh.el")))
494           (condition-case code
495               (progn (require 'xml) nil)
496             (error
497              (message "No xml: %s %s (ignored)" code (locate-library "xml"))
498              '("nnrss.el")))
499           (condition-case code
500               (progn (require 'bbdb) nil)
501             (error
502              (message "No bbdb: %s %s (ignored)" code (locate-library "bbdb"))
503              '("gnus-bbdb.el")))
504           (unless (featurep 'xemacs)
505             '("gnus-xmas.el" "messagexmas.el" "nnheaderxm.el"))
506           (when (and (not (featurep 'xemacs))
507                      (<= emacs-major-version 20))
508             '("smiley.el"))
509           (when (and (fboundp 'base64-decode-string)
510                      (subrp (symbol-function 'base64-decode-string)))
511             '("base64.el"))
512           (when (and (fboundp 'md5) (subrp (symbol-function 'md5)))
513             '("md5.el"))
514           (unless (boundp 'MULE)
515             '("canlock-om.el")))
516   "Files which will not be installed.")
517
518 (defconst dgnushack-exporting-files
519   (let ((files (directory-files srcdir nil "^[^=].*\\.el$" t)))
520     (dolist (file dgnushack-unexporting-files)
521       (setq files (delete file files)))
522     (sort files 'string-lessp))
523   "Files which will be compiled and installed.")
524
525 (defun dgnushack-exporting-files ()
526   "Print name of files which will be installed."
527   (princ (mapconcat 'identity dgnushack-exporting-files " ")))
528
529 (defun dgnushack-compile (&optional warn)
530   ;;(setq byte-compile-dynamic t)
531   (unless warn
532     (setq byte-compile-warnings
533           '(free-vars unresolved callargs redefine)))
534   (unless (locate-library "cus-edit")
535     (error "You do not seem to have Custom installed.
536 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
537 You also then need to add the following to the lisp/dgnushack.el file:
538
539      (push \"~/lisp/custom\" load-path)
540
541 Modify to suit your needs."))
542
543   ;; Show `load-path'.
544   (message "load-path=(\"%s\")"
545            (mapconcat 'identity load-path "\"\n           \""))
546
547   (dolist (file dgnushack-exporting-files)
548     (setq file (expand-file-name file srcdir))
549     (when (and (file-exists-p
550                 (setq elc (concat (file-name-nondirectory file) "c")))
551                (file-newer-than-file-p file elc))
552       (delete-file elc)))
553
554   (let ((files dgnushack-exporting-files)
555         ;;(byte-compile-generate-call-tree t)
556         file elc)
557     ;; Avoid barfing (from gnus-xmas) because the etc directory is not yet
558     ;; installed.
559     (when (featurep 'xemacs)
560       (setq gnus-xmas-glyph-directory "dummy"))
561     (while (setq file (pop files))
562       (setq file (expand-file-name file srcdir))
563       (when (or (not (file-exists-p
564                       (setq elc (concat (file-name-nondirectory file) "c"))))
565                 (file-newer-than-file-p file elc))
566         (ignore-errors
567           (byte-compile-file file))))))
568
569 (defun dgnushack-recompile ()
570   (require 'gnus)
571   (byte-recompile-directory "." 0))
572
573 (defvar dgnushack-gnus-load-file (expand-file-name "gnus-load.el" srcdir))
574 (defvar dgnushack-cus-load-file (expand-file-name "cus-load.el" srcdir))
575 (defvar dgnushack-auto-load-file (expand-file-name "auto-autoloads.el" srcdir))
576
577 (defun dgnushack-make-cus-load ()
578   (when (condition-case nil
579             (load "cus-dep")
580           (error nil))
581     (let ((cusload-base-file dgnushack-cus-load-file))
582       (if (fboundp 'custom-make-dependencies)
583           (custom-make-dependencies)
584         (Custom-make-dependencies)))))
585
586 (defun dgnushack-make-auto-load ()
587   (require 'autoload)
588   (unless (make-autoload '(define-derived-mode child parent name
589                             "docstring" body)
590                          "file")
591     (defadvice make-autoload (around handle-define-derived-mode activate)
592       "Handle `define-derived-mode'."
593       (if (eq (car-safe (ad-get-arg 0)) 'define-derived-mode)
594           (setq ad-return-value
595                 (list 'autoload
596                       (list 'quote (nth 1 (ad-get-arg 0)))
597                       (ad-get-arg 1)
598                       (nth 4 (ad-get-arg 0))
599                       t nil))
600         ad-do-it))
601     (put 'define-derived-mode 'doc-string-elt 3))
602   (let ((generated-autoload-file dgnushack-gnus-load-file)
603         (make-backup-files nil)
604         (autoload-package-name "gnus"))
605     (if (featurep 'xemacs)
606         (progn
607           (if (file-exists-p generated-autoload-file)
608               (delete-file generated-autoload-file))
609           (if (file-exists-p dgnushack-auto-load-file)
610               (delete-file dgnushack-auto-load-file)))
611       (with-temp-file generated-autoload-file
612         (insert ?\014)))
613     (if (featurep 'xemacs)
614         (let ((si:message (symbol-function 'message)))
615           (defun message (fmt &rest args)
616             (cond ((and (string-equal "Generating autoloads for %s..." fmt)
617                         (file-exists-p (file-name-nondirectory (car args))))
618                    (funcall si:message
619                             fmt (file-name-nondirectory (car args))))
620                   ((string-equal "No autoloads found in %s" fmt))
621                   ((string-equal "Generating autoloads for %s...done" fmt))
622                   (t (apply si:message fmt args))))
623           (unwind-protect
624               (batch-update-autoloads)
625             (fset 'message si:message)))
626       (batch-update-autoloads))))
627
628 (defun dgnushack-make-load ()
629   (message (format "Generating %s..." dgnushack-gnus-load-file))
630   (with-temp-file dgnushack-gnus-load-file
631     (if (file-exists-p dgnushack-cus-load-file)
632         (progn
633           (insert-file-contents dgnushack-cus-load-file)
634           (delete-file dgnushack-cus-load-file)
635           (goto-char (point-min))
636           (search-forward ";;; Code:")
637           (forward-line)
638           (delete-region (point-min) (point))
639           (unless (re-search-forward "\
640 ^[\t ]*(autoload[\t\n ]+\\('\\|(quote[\t\n ]+\\)custom-add-loads[\t\n ]"
641                                      nil t)
642             (insert "\n(autoload 'custom-add-loads \"cus-load\")\n"))
643           (goto-char (point-min))
644           (insert "\
645 ;;; gnus-load.el --- automatically extracted custom dependencies and autoload
646 ;;
647 ;;; Code:
648 ")
649           (goto-char (point-max))
650           (if (search-backward "custom-versions-load-alist" nil t)
651               (forward-line -1)
652             (forward-line -1)
653             (while (eq (char-after) ?\;)
654               (forward-line -1))
655             (forward-line))
656           (delete-region (point) (point-max))
657           (insert "\n"))
658       (insert "\
659 ;;; gnus-load.el --- automatically extracted autoload
660 ;;
661 ;;; Code:
662 "))
663     ;; smiley-* are duplicated. Remove them all.
664     (let ((point (point)))
665       (insert-file-contents dgnushack-gnus-load-file)
666       (goto-char point)
667       (while (search-forward "smiley-" nil t)
668         (beginning-of-line)
669         (if (looking-at "(autoload ")
670             (delete-region (point) (progn (forward-sexp) (point)))
671           (forward-line))))
672     ;;
673     (goto-char (point-max))
674     (when (search-backward "\n(provide " nil t)
675       (forward-line -1)
676       (delete-region (point) (point-max)))
677     (insert "\
678
679 \(provide 'gnus-load)
680
681 ;;; Local Variables:
682 ;;; version-control: never
683 ;;; no-byte-compile: t
684 ;;; no-update-autoloads: t
685 ;;; End:
686 ;;; gnus-load.el ends here
687 ")
688     ;; Workaround the bug in some version of XEmacs.
689     (when (featurep 'xemacs)
690       (condition-case nil
691           (require 'cus-load)
692         (error nil))
693       (goto-char (point-min))
694       (when (and (fboundp 'custom-add-loads)
695                  (not (search-forward "\n(autoload 'custom-add-loads " nil t)))
696         (search-forward "\n;;; Code:" nil t)
697         (forward-line 1)
698         (insert "\n(autoload 'custom-add-loads \"cus-load\")\n"))))
699   (message (format "Compiling %s..." dgnushack-gnus-load-file))
700   (byte-compile-file dgnushack-gnus-load-file))
701
702 \f
703 (defun dgnushack-compose-package ()
704   "Re-split the file gnus-load.el into custom-load.el and
705 auto-autoloads.el.  It is silly, should be improved!"
706   (message "
707 Re-splitting gnus-load.el into custom-load.el and auto-autoloads.el...")
708   (let ((customload (expand-file-name "custom-load.el" srcdir))
709         (autoloads (expand-file-name "auto-autoloads.el" srcdir))
710         start)
711     (with-temp-buffer
712       (insert-file-contents dgnushack-gnus-load-file)
713       (delete-file dgnushack-gnus-load-file)
714       (when (file-exists-p (concat dgnushack-gnus-load-file "c"))
715         (delete-file (concat dgnushack-gnus-load-file "c")))
716       (while (prog1
717                  (looking-at "[\t ;]")
718                (forward-line 1)))
719       (setq start (point))
720       (insert "\
721 ;;; custom-load.el --- automatically extracted custom dependencies\n
722 ;;; Code:\n\n")
723       (goto-char (point-max))
724       (while (progn
725                (forward-line -1)
726                (not (looking-at "[\t ]*(custom-add-loads[\t\n ]"))))
727       (forward-list 1)
728       (forward-line 1)
729       (insert "\n;;; custom-load.el ends here\n")
730       (write-region start (point) customload)
731       (while (looking-at "[\t ]*$")
732         (forward-line 1))
733       (setq start (point))
734       (if (re-search-forward "^[\t\n ]*(if[\t\n ]+(featurep[\t\n ]" nil t)
735           (let ((from (goto-char (match-beginning 0))))
736             (delete-region from (progn
737                                   (forward-list 1)
738                                   (forward-line 1)
739                                   (point))))
740         (while (looking-at "[\t ;]")
741           (forward-line 1)))
742       (insert "(if (featurep 'gnus-autoloads) (error \"Already loaded\"))\n")
743       (goto-char (point-max))
744       (while (progn
745                (forward-line -1)
746                (not (looking-at "[\t ]*(provide[\t\n ]"))))
747       (insert "(provide 'gnus-autoloads)\n")
748       (write-region start (point) autoloads))
749     (byte-compile-file customload)
750     (byte-compile-file autoloads))
751   (message "\
752 Re-splitting gnus-load.el into custom-load.el and auto-autoloads.el...done
753 \n"))
754
755 \f
756 (defconst dgnushack-info-file-regexp-en
757   (let ((names '("gnus" "message" "emacs-mime"))
758         regexp name)
759     (while (setq name (pop names))
760       (setq regexp (concat regexp "^" name "\\.info\\(-[0-9]+\\)?$"
761                            (when names "\\|"))))
762     regexp)
763   "Regexp matching English info files.")
764
765 (defconst dgnushack-info-file-regexp-ja
766   (let ((names '("gnus-ja" "message-ja"))
767         regexp name)
768     (while (setq name (pop names))
769       (setq regexp (concat regexp "^" name "\\.info\\(-[0-9]+\\)?$"
770                            (when names "\\|"))))
771     regexp)
772   "Regexp matching Japanese info files.")
773
774 (defun dgnushack-remove-extra-files-in-package ()
775   "Remove extra files in the lisp directory of the XEmacs package."
776   (let ((lisp-dir (expand-file-name (concat "lisp/"
777                                             ;; GNUS_PRODUCT_NAME
778                                             (cadr command-line-args-left)
779                                             "/")
780                                     ;; PACKAGEDIR
781                                     (car command-line-args-left))))
782     (when (file-directory-p lisp-dir)
783       (let (files)
784         (dolist (file dgnushack-exporting-files)
785           (setq files (nconc files (list file (concat file "c")))))
786         (dolist (file (directory-files lisp-dir nil nil t t))
787           (unless (member file files)
788             (setq file (expand-file-name file lisp-dir))
789             (message "Removing %s..." file)
790             (condition-case nil
791                 (delete-file file)
792               (error nil))))))))
793
794 (defun dgnushack-install-package-manifest ()
795   "Install MANIFEST file as an XEmacs package."
796   (let* ((package-dir (car command-line-args-left))
797          (product-name (cadr command-line-args-left))
798          (name (expand-file-name (concat "pkginfo/MANIFEST." product-name)
799                                  package-dir))
800          make-backup-files)
801     (message "Generating %s..." name)
802     (with-temp-file name
803       (insert "pkginfo/MANIFEST." product-name "\n")
804       (let ((lisp-dir (concat "lisp/" product-name "/"))
805             (files (sort (directory-files "." nil "\\.elc?$" t) 'string-lessp))
806             file)
807         (while (setq file (pop files))
808           (unless (member file dgnushack-unexporting-files)
809             (insert lisp-dir file "\n")))
810         (setq files
811               (sort (directory-files "../texi/" nil
812                                      (concat dgnushack-info-file-regexp-en
813                                              "\\|"
814                                              dgnushack-info-file-regexp-ja)
815                                      t)
816                     'string-lessp))
817         (while (setq file (pop files))
818           (insert "info/" file "\n"))))))
819
820 \f
821 (define-compiler-macro describe-key-briefly (&whole form key &optional insert)
822   (if (condition-case nil
823           (progn
824             (describe-key-briefly '((())) nil)
825             t)
826         (wrong-number-of-arguments nil);; Old Emacsen.
827         (error t))
828       form
829     (if insert
830         `(if ,insert
831              (insert (funcall 'describe-key-briefly ,key))
832            (funcall 'describe-key-briefly ,key))
833       `(funcall 'describe-key-briefly ,key))))
834
835 ;;; dgnushack.el ends here