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