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