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