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