2004-01-06 Steve Youngs <sryoungs@bigpond.net.au>
[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, 2003
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 ;; If we are building w3 in a different directory than the source
52 ;; directory, we must read *.el from source directory and write *.elc
53 ;; into the building directory.  For that, we define this function
54 ;; before loading bytecomp.  Bytecomp doesn't overwrite this function.
55 (defun byte-compile-dest-file (filename)
56   "Convert an Emacs Lisp source file name to a compiled file name.
57  In addition, remove directory name part from FILENAME."
58   (setq filename (byte-compiler-base-file-name filename))
59   (setq filename (file-name-sans-versions filename))
60   (setq filename (file-name-nondirectory filename))
61   (if (memq system-type '(win32 w32 mswindows windows-nt))
62       (setq filename (downcase filename)))
63   (cond ((eq system-type 'vax-vms)
64          (concat (substring filename 0 (string-match ";" filename)) "c"))
65         ((string-match emacs-lisp-file-regexp filename)
66          (concat (substring filename 0 (match-beginning 0)) ".elc"))
67         (t (concat filename ".elc"))))
68
69 (require 'bytecomp)
70 ;; To avoid having defsubsts and inlines happen.
71 ;(if (featurep 'xemacs)
72 ;    (require 'byte-optimize)
73 ;  (require 'byte-opt))
74 ;(defun byte-optimize-inline-handler (form)
75 ;  "byte-optimize-handler for the `inline' special-form."
76 ;  (cons 'progn (cdr form)))
77 ;(defalias 'byte-compile-file-form-defsubst 'byte-compile-file-form-defun)
78
79 (when (and (not (featurep 'xemacs))
80            (= emacs-major-version 21)
81            (= emacs-minor-version 3)
82            (condition-case code
83                (let ((byte-compile-error-on-warn t))
84                  (byte-optimize-form (quote (pop x)) t)
85                  nil)
86              (error (string-match "called for effect"
87                                   (error-message-string code)))))
88   (defadvice byte-optimize-form-code-walker (around silence-warn-for-pop
89                                                     (form for-effect)
90                                                     activate)
91     "Silence the warning \"...called for effect\" for the `pop' form.
92 It is effective only when the `pop' macro is defined by cl.el rather
93 than subr.el."
94     (let (tmp)
95       (if (and (eq (car-safe form) 'car)
96                for-effect
97                (setq tmp (get 'car 'side-effect-free))
98                (not byte-compile-delete-errors)
99                (not (eq tmp 'error-free))
100                (eq (car-safe (cadr form)) 'prog1)
101                (let ((var (cadr (cadr form)))
102                      (last (nth 2 (cadr form))))
103                  (and (symbolp var)
104                       (null (nthcdr 3 (cadr form)))
105                       (eq (car-safe last) 'setq)
106                       (eq (cadr last) var)
107                       (eq (car-safe (nth 2 last)) 'cdr)
108                       (eq (cadr (nth 2 last)) var))))
109           (progn
110             (put 'car 'side-effect-free 'error-free)
111             (unwind-protect
112                 ad-do-it
113               (put 'car 'side-effect-free tmp)))
114         ad-do-it))))
115
116 (push srcdir load-path)
117 (load (expand-file-name "lpath.el" srcdir) nil t)
118
119 (defalias 'device-sound-enabled-p 'ignore)
120 (defalias 'play-sound-file 'ignore)
121 (defalias 'nndb-request-article 'ignore)
122 (defalias 'efs-re-read-dir 'ignore)
123 (defalias 'ange-ftp-re-read-dir 'ignore)
124 (defalias 'define-mail-user-agent 'ignore)
125
126 (eval-and-compile
127   (unless (featurep 'xemacs)
128     (defalias 'get-popup-menu-response 'ignore)
129     (defalias 'event-object 'ignore)
130     (defalias 'x-defined-colors 'ignore)
131     (defalias 'read-color 'ignore)))
132
133 (eval-and-compile
134   (when (featurep 'xemacs)
135     (unless (fboundp 'defadvice)
136       (autoload 'defadvice "advice" nil nil 'macro))
137     (autoload 'Info-directory "info" nil t)
138     (autoload 'Info-menu "info" nil t)
139     (autoload 'annotations-at "annotations")
140     (autoload 'apropos "apropos" nil t)
141     (autoload 'apropos-command "apropos" nil t)
142     (autoload 'bbdb-complete-name "bbdb-com" nil t)
143     (autoload 'browse-url "browse-url" nil t)
144     (autoload 'customize-apropos "cus-edit" nil t)
145     (autoload 'customize-save-variable "cus-edit" nil t)
146     (autoload 'customize-variable "cus-edit" nil t)
147     (autoload 'delete-annotation "annotations")
148     (autoload 'dolist "cl-macs" nil nil 'macro)
149     (autoload 'enriched-decode "enriched")
150     (autoload 'executable-find "executable")
151     (autoload 'info "info" nil t)
152     (autoload 'make-annotation "annotations")
153     (autoload 'make-display-table "disp-table")
154     (autoload 'pp "pp")
155     (autoload 'ps-despool "ps-print" nil t)
156     (autoload 'ps-spool-buffer "ps-print" nil t)
157     (autoload 'ps-spool-buffer-with-faces "ps-print" nil t)
158     (autoload 'read-passwd "passwd")
159     (autoload 'regexp-opt "regexp-opt")
160     (autoload 'reporter-submit-bug-report "reporter")
161     (if (emacs-version>= 21 5)
162         (autoload 'setenv "process" nil t)
163       (autoload 'setenv "env" nil t))
164     (autoload 'smtpmail-send-it "smtpmail")
165     (autoload 'sort-numeric-fields "sort" nil t)
166     (autoload 'sort-subr "sort")
167     (autoload 'trace-function-background "trace" nil t)
168     (autoload 'w3-do-setup "w3")
169     (autoload 'w3-prepare-buffer "w3-display")
170     (autoload 'w3-region "w3-display" nil t)
171     (defalias 'frame-char-height 'frame-height)
172     (defalias 'frame-char-width 'frame-width)
173     (defalias 'frame-parameter 'frame-property)
174     (defalias 'make-overlay 'ignore)
175     (defalias 'overlay-end 'ignore)
176     (defalias 'overlay-get 'ignore)
177     (defalias 'overlay-put 'ignore)
178     (defalias 'overlay-start 'ignore)
179     (defalias 'overlays-in 'ignore)
180     (defalias 'replace-dehighlight 'ignore)
181     (defalias 'replace-highlight 'ignore)
182     (defalias 'run-with-idle-timer 'ignore)
183     (defalias 'w3-coding-system-for-mime-charset 'ignore)))
184
185 (defun dgnushack-compile-verbosely ()
186   "Call dgnushack-compile with warnings ENABLED.  If you are compiling
187 patches to gnus, you should consider modifying make.bat to call
188 dgnushack-compile-verbosely.  All other users should continue to use
189 dgnushack-compile."
190   (dgnushack-compile t))
191
192 (defun dgnushack-compile (&optional warn)
193   ;;(setq byte-compile-dynamic t)
194   (unless warn
195     (setq byte-compile-warnings
196           '(free-vars unresolved callargs redefine)))
197   (unless (locate-library "cus-edit")
198     (error "You do not seem to have Custom installed.
199 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
200 You also then need to add the following to the lisp/dgnushack.el file:
201
202      (push \"~/lisp/custom\" load-path)
203
204 Modify to suit your needs."))
205   (let ((files (directory-files srcdir nil "^[^=].*\\.el$"))
206         ;;(byte-compile-generate-call-tree t)
207         file elc)
208     ;; Avoid barfing (from gnus-xmas) because the etc directory is not yet
209     ;; installed.
210     (when (featurep 'xemacs)
211       (setq gnus-xmas-glyph-directory "dummy"))
212     (dolist (file '("dgnushack.el" "lpath.el"))
213       (setq files (delete file files)))
214     (when (featurep 'base64)
215       (setq files (delete "base64.el" files)))
216     (condition-case code
217         (require 'w3-parse)
218       (error
219        (message "No w3: %s %s" (cadr code) (or (locate-library "w3-parse") ""))
220        (dolist (file '("nnultimate.el" "webmail.el" "nnwfm.el"))
221          (setq files (delete file files)))))
222     (condition-case code
223         (require 'mh-e)
224       (error
225        (message "No mh-e: %s %s" (cadr code) (or (locate-library "mh-e") ""))
226        (setq files (delete "gnus-mh.el" files))))
227     (condition-case code
228         (require 'xml)
229       (error
230        (message "No xml: %s %s" (cadr code) (or (locate-library "xml") ""))
231        (setq files (delete "nnrss.el" files))))
232     (dolist (file
233              (if (featurep 'xemacs)
234                  '("md5.el")
235                '("gnus-xmas.el" "messagexmas.el" "nnheaderxm.el")))
236       (setq files (delete file files)))
237
238     (dolist (file files)
239       (setq file (expand-file-name file srcdir))
240       (when (and (file-exists-p
241                   (setq elc (concat (file-name-nondirectory file) "c")))
242                  (file-newer-than-file-p file elc))
243         (delete-file elc)))
244
245     (while (setq file (pop files))
246       (setq file (expand-file-name file srcdir))
247       (when (or (not (file-exists-p
248                       (setq elc (concat (file-name-nondirectory file) "c"))))
249                 (file-newer-than-file-p file elc))
250         (ignore-errors
251           (byte-compile-file file))))))
252
253 (defun dgnushack-recompile ()
254   (require 'gnus)
255   (byte-recompile-directory "." 0))
256
257 (defvar dgnushack-gnus-load-file
258   (if (featurep 'xemacs)
259       (expand-file-name "auto-autoloads.el")
260     (expand-file-name "gnus-load.el")))
261
262 (defvar dgnushack-cus-load-file 
263   (if (featurep 'xemacs)
264       (expand-file-name "custom-load.el")
265     (expand-file-name "cus-load.el")))
266
267 (defun dgnushack-make-cus-load ()
268   (load "cus-dep")
269   (let ((cusload-base-file dgnushack-cus-load-file))
270     (if (fboundp 'custom-make-dependencies)
271         (custom-make-dependencies)
272       (Custom-make-dependencies))
273     (when (featurep 'xemacs)
274       (message "Compiling %s..." dgnushack-cus-load-file)
275       (byte-compile-file dgnushack-cus-load-file))))
276
277 (defun dgnushack-make-auto-load ()
278   (require 'autoload)
279   (unless (make-autoload '(define-derived-mode child parent name
280                             "docstring" body)
281                          "file")
282     (defadvice make-autoload (around handle-define-derived-mode activate)
283       "Handle `define-derived-mode'."
284       (if (eq (car-safe (ad-get-arg 0)) 'define-derived-mode)
285           (setq ad-return-value
286                 (list 'autoload
287                       (list 'quote (nth 1 (ad-get-arg 0)))
288                       (ad-get-arg 1)
289                       (nth 4 (ad-get-arg 0))
290                       t nil))
291         ad-do-it))
292     (put 'define-derived-mode 'doc-string-elt 3))
293   (let ((generated-autoload-file dgnushack-gnus-load-file)
294         (make-backup-files nil)
295         (autoload-package-name "gnus"))
296     (if (featurep 'xemacs)
297         (if (file-exists-p generated-autoload-file)
298             (delete-file generated-autoload-file))
299       (with-temp-file generated-autoload-file
300         (insert ?\014)))
301     (batch-update-autoloads)))
302
303 (defun dgnushack-make-load ()
304   (unless (featurep 'xemacs)
305     (message "Generating %s..." dgnushack-gnus-load-file)
306     (with-temp-file dgnushack-gnus-load-file
307       (insert-file-contents dgnushack-cus-load-file)
308       (delete-file dgnushack-cus-load-file)
309       (goto-char (point-min))
310       (search-forward ";;; Code:")
311       (forward-line)
312       (delete-region (point-min) (point))
313       (insert "\
314 ;;; gnus-load.el --- automatically extracted custom dependencies and autoload
315 ;;
316 ;;; Code:
317 ")
318       (goto-char (point-max))
319       (if (search-backward "custom-versions-load-alist" nil t)
320           (forward-line -1)
321         (forward-line -1)
322         (while (eq (char-after) ?\;)
323           (forward-line -1))
324         (forward-line))
325       (delete-region (point) (point-max))
326       (insert "\n")
327       ;; smiley-* are duplicated. Remove them all.
328       (let ((point (point)))
329         (insert-file-contents dgnushack-gnus-load-file)
330         (goto-char point)
331         (while (search-forward "smiley-" nil t)
332           (beginning-of-line)
333           (if (looking-at "(autoload ")
334               (delete-region (point) (progn (forward-sexp) (point)))
335             (forward-line))))
336       ;;
337       (goto-char (point-max))
338       (when (search-backward "\n(provide " nil t)
339         (forward-line -1)
340         (delete-region (point) (point-max)))
341       (insert "\
342
343 \(provide 'gnus-load)
344
345 ;;; Local Variables:
346 ;;; version-control: never
347 ;;; no-byte-compile: t
348 ;;; no-update-autoloads: t
349 ;;; End:
350 ;;; gnus-load.el ends here
351 ")
352       ))
353   (message "Compiling %s..." dgnushack-gnus-load-file)
354   (byte-compile-file dgnushack-gnus-load-file)
355   (when (featurep 'xemacs)
356     (message "Creating dummy gnus-load.el...")
357     (with-temp-file (expand-file-name "gnus-load.el")
358       (insert "\
359
360 \(provide 'gnus-load)
361
362 ;;; Local Variables:
363 ;;; version-control: never
364 ;;; no-byte-compile: t
365 ;;; no-update-autoloads: t
366 ;;; End:
367 ;;; gnus-load.el ends here"))))
368
369
370 ;;; dgnushack.el ends here