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