0f4041f2a97140c5c520dd36420821e3ffa90788
[gnus] / lisp / dgnushack.el
1 ;;; dgnushack.el --- a hack to set the load path for byte-compiling
2 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Version: 4.19
7 ;; Keywords: news, path
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (defalias 'facep 'ignore)
31
32 (require 'cl)
33
34 (defvar srcdir (or (getenv "srcdir") "."))
35
36 (defun my-getenv (str)
37   (let ((val (getenv str)))
38     (if (equal val "no") nil val)))
39
40 (if (my-getenv "lispdir")
41     (push (my-getenv "lispdir") load-path))
42
43 (push (or (my-getenv "URLDIR") (expand-file-name "../../url/lisp/" srcdir))
44       load-path)
45
46 (push (or (my-getenv "W3DIR") (expand-file-name "../../w3/lisp/" srcdir))
47       load-path)
48
49 ;(push "/usr/share/emacs/site-lisp" load-path)
50
51 (unless (featurep 'xemacs)
52   (define-compiler-macro merge (&whole form type seq1 seq2 pred &rest keys)
53     (if (and (fboundp 'merge)
54              (subrp (symbol-function 'merge)))
55         form
56       `(let ((type ,type)
57              (seq1 ,seq1)
58              (seq2 ,seq2)
59              (pred ,pred))
60          (or (listp seq1) (setq seq1 (append seq1 nil)))
61          (or (listp seq2) (setq seq2 (append seq2 nil)))
62          (let ((res nil))
63            (while (and seq1 seq2)
64              (if (funcall pred (car seq2) (car seq1))
65                  (push (pop seq2) res)
66                (push (pop seq1) res)))
67            (let ((x (nconc (nreverse res) seq1 seq2)))
68              (cond ((eq type 'list) (if (listp x) x (append x nil)))
69                    ((eq type 'vector) (if (vectorp x) x (vconcat x)))
70                    ((eq type 'string) (if (stringp x) x (concat x)))
71                    ((eq type 'array) (if (arrayp x) x (vconcat x)))
72                    ((and (eq type 'character) (stringp x) (= (length x) 1))
73                     (aref x 0))
74                    ((and (eq type 'character) (symbolp x)) 
75                     (aref (symbol-name x) 0))
76                    ((eq type 'float) (float x))
77                    ((typep x type) x)
78                    (t (error "Can't coerce %s to type %s" x type))))))))
79
80   (define-compiler-macro copy-list (&whole form list)
81     (if (and (fboundp 'copy-list)
82              (subrp (symbol-function 'copy-list)))
83         form
84       `(let ((list ,list))
85          (if (consp list)
86              (let ((res nil))
87                (while (consp list) (push (pop list) res))
88                (prog1 (nreverse res) (setcdr res list)))
89            (car list)))))
90
91   (define-compiler-macro remove (&whole form item seq)
92     (if (>= emacs-major-version 21)
93         form
94       `(delete ,item (copy-sequence ,seq)))))
95
96 ;; If we are building w3 in a different directory than the source
97 ;; directory, we must read *.el from source directory and write *.elc
98 ;; into the building directory.  For that, we define this function
99 ;; before loading bytecomp.  Bytecomp doesn't overwrite this function.
100 (defun byte-compile-dest-file (filename)
101   "Convert an Emacs Lisp source file name to a compiled file name.
102  In addition, remove directory name part from FILENAME."
103   (setq filename (byte-compiler-base-file-name filename))
104   (setq filename (file-name-sans-versions filename))
105   (setq filename (file-name-nondirectory filename))
106   (if (memq system-type '(win32 w32 mswindows windows-nt))
107       (setq filename (downcase filename)))
108   (cond ((eq system-type 'vax-vms)
109          (concat (substring filename 0 (string-match ";" filename)) "c"))
110         ((string-match emacs-lisp-file-regexp filename)
111          (concat (substring filename 0 (match-beginning 0)) ".elc"))
112         (t (concat filename ".elc"))))
113
114 (require 'bytecomp)
115 ;; To avoid having defsubsts and inlines happen.
116 ;(if (featurep 'xemacs)
117 ;    (require 'byte-optimize)
118 ;  (require 'byte-opt))
119 ;(defun byte-optimize-inline-handler (form)
120 ;  "byte-optimize-handler for the `inline' special-form."
121 ;  (cons 'progn (cdr form)))
122 ;(defalias 'byte-compile-file-form-defsubst 'byte-compile-file-form-defun)
123
124 (push srcdir load-path)
125 (load (expand-file-name "lpath.el" srcdir) nil t)
126
127 (defalias 'device-sound-enabled-p 'ignore)
128 (defalias 'play-sound-file 'ignore)
129 (defalias 'nndb-request-article 'ignore)
130 (defalias 'efs-re-read-dir 'ignore)
131 (defalias 'ange-ftp-re-read-dir 'ignore)
132 (defalias 'define-mail-user-agent 'ignore)
133
134 (eval-and-compile
135   (unless (featurep 'xemacs)
136     (defalias 'get-popup-menu-response 'ignore)
137     (defalias 'event-object 'ignore)
138     (defalias 'x-defined-colors 'ignore)
139     (defalias 'read-color 'ignore)))
140
141 (defun dgnushack-compile (&optional warn)
142   ;;(setq byte-compile-dynamic t)
143   (unless warn
144     (setq byte-compile-warnings
145           '(free-vars unresolved callargs redefine)))
146   (unless (locate-library "cus-edit")
147     (error "You do not seem to have Custom installed.
148 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
149 You also then need to add the following to the lisp/dgnushack.el file:
150
151      (push \"~/lisp/custom\" load-path)
152
153 Modify to suit your needs."))
154   (let ((files (directory-files srcdir nil "^[^=].*\\.el$"))
155         ;;(byte-compile-generate-call-tree t)
156         file elc)
157     ;; Avoid barfing (from gnus-xmas) because the etc directory is not yet
158     ;; installed.
159     (when (featurep 'xemacs)
160       (setq gnus-xmas-glyph-directory "dummy"))
161     (dolist (file '("dgnushack.el" "lpath.el"))
162       (setq files (delete file files)))
163     (when (featurep 'base64)
164       (setq files (delete "base64.el" files)))
165     (condition-case code
166         (require 'w3-parse)
167       (error
168        (message "No w3: %s %s" (cadr code) (or (locate-library "w3-parse") ""))
169        (dolist (file '("nnultimate.el" "webmail.el" "nnwfm.el"))
170          (setq files (delete file files)))))
171     (condition-case code
172         (require 'mh-e)
173       (error
174        (message "No mh-e: %s %s" (cadr code) (or (locate-library "mh-e") ""))
175        (setq files (delete "gnus-mh.el" files))))
176     (condition-case code
177         (require 'xml)
178       (error
179        (message "No xml: %s %s" (cadr code) (or (locate-library "xml") ""))
180        (setq files (delete "nnrss.el" files))))
181     (dolist (file
182              (if (featurep 'xemacs)
183                  '("md5.el")
184                '("gnus-xmas.el" "messagexmas.el" "nnheaderxm.el")))
185       (setq files (delete file files)))
186
187     (dolist (file files)
188       (setq file (expand-file-name file srcdir))
189       (when (and (file-exists-p
190                   (setq elc (concat (file-name-nondirectory file) "c")))
191                  (file-newer-than-file-p file elc))
192         (delete-file elc)))
193
194     (while (setq file (pop files))
195       (setq file (expand-file-name file srcdir))
196       (when (or (not (file-exists-p
197                       (setq elc (concat (file-name-nondirectory file) "c"))))
198                 (file-newer-than-file-p file elc))
199         (ignore-errors
200           (byte-compile-file file))))))
201
202 (defun dgnushack-recompile ()
203   (require 'gnus)
204   (byte-recompile-directory "." 0))
205
206 (defvar dgnushack-gnus-load-file (expand-file-name "gnus-load.el"))
207 (defvar dgnushack-cus-load-file (expand-file-name "cus-load.el"))
208
209 (defun dgnushack-make-cus-load ()
210   (load "cus-dep")
211   (let ((cusload-base-file dgnushack-cus-load-file))
212     (if (fboundp 'custom-make-dependencies)
213         (custom-make-dependencies)
214       (Custom-make-dependencies))))
215
216 (defun dgnushack-make-auto-load ()
217   (require 'autoload)
218   (unless (make-autoload '(define-derived-mode child parent name
219                             "docstring" body)
220                          "file")
221     (defadvice make-autoload (around handle-define-derived-mode activate)
222       "Handle `define-derived-mode'."
223       (if (eq (car-safe (ad-get-arg 0)) 'define-derived-mode)
224           (setq ad-return-value
225                 (list 'autoload
226                       (list 'quote (nth 1 (ad-get-arg 0)))
227                       (ad-get-arg 1)
228                       (nth 4 (ad-get-arg 0))
229                       t nil))
230         ad-do-it))
231     (put 'define-derived-mode 'doc-string-elt 3))
232   (let ((generated-autoload-file dgnushack-gnus-load-file)
233         (make-backup-files nil)
234         (autoload-package-name "gnus"))
235     (if (featurep 'xemacs)
236         (if (file-exists-p generated-autoload-file)
237             (delete-file generated-autoload-file))
238       (with-temp-file generated-autoload-file
239         (insert ?\014)))
240     (batch-update-autoloads)))
241
242 (defun dgnushack-make-load ()
243   (message (format "Generating %s..." dgnushack-gnus-load-file))
244   (with-temp-file dgnushack-gnus-load-file
245     (insert-file-contents dgnushack-cus-load-file)
246     (delete-file dgnushack-cus-load-file)
247     (goto-char (point-min))
248     (search-forward ";;; Code:")
249     (forward-line)
250     (delete-region (point-min) (point))
251     (insert "\
252 ;;; gnus-load.el --- automatically extracted custom dependencies and autoload
253 ;;
254 ;;; Code:
255 ")
256     (goto-char (point-max))
257     (if (search-backward "custom-versions-load-alist" nil t)
258         (forward-line -1)
259       (forward-line -1)
260       (while (eq (char-after) ?\;)
261         (forward-line -1))
262       (forward-line))
263     (delete-region (point) (point-max))
264     (insert "\n")
265     ;; smiley-* are duplicated. Remove them all.
266     (let ((point (point)))
267       (insert-file-contents dgnushack-gnus-load-file)
268       (goto-char point)
269       (while (search-forward "smiley-" nil t)
270         (beginning-of-line)
271         (if (looking-at "(autoload ")
272             (delete-region (point) (progn (forward-sexp) (point)))
273           (forward-line))))
274     ;;
275     (goto-char (point-max))
276     (when (search-backward "\n(provide " nil t)
277       (forward-line -1)
278       (delete-region (point) (point-max)))
279     (insert "\
280
281 \(provide 'gnus-load)
282
283 ;;; Local Variables:
284 ;;; version-control: never
285 ;;; no-byte-compile: t
286 ;;; no-update-autoloads: t
287 ;;; End:
288 ;;; gnus-load.el ends here
289 ")
290     ;; Workaround the bug in some version of XEmacs.
291     (when (featurep 'xemacs)
292       (condition-case nil
293           (require 'cus-load)
294         (error nil))
295       (goto-char (point-min))
296       (when (and (fboundp 'custom-add-loads)
297                  (not (search-forward "\n(autoload 'custom-add-loads " nil t)))
298         (search-forward "\n;;; Code:" nil t)
299         (forward-line 1)
300         (insert "\n(autoload 'custom-add-loads \"cus-load\")\n"))))
301   (message (format "Compiling %s..." dgnushack-gnus-load-file))
302   (byte-compile-file dgnushack-gnus-load-file))
303
304 ;;; dgnushack.el ends here