fb64339f290229f0140b40dbcf3e8426ff8d77a3
[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 'c-mode "cc-mode" nil t)
191     (autoload 'customize-apropos "cus-edit" nil t)
192     (autoload 'customize-save-variable "cus-edit" nil t)
193     (autoload 'customize-variable "cus-edit" nil t)
194     (autoload 'delete-annotation "annotations")
195     (autoload 'dolist "cl-macs" nil nil 'macro)
196     (autoload 'enriched-decode "enriched")
197     (autoload 'executable-find "executable")
198     (autoload 'font-lock-fontify-buffer "font-lock" nil t)
199     (autoload 'info "info" nil t)
200     (autoload 'make-annotation "annotations")
201     (autoload 'make-display-table "disp-table")
202     (autoload 'pp "pp")
203     (autoload 'ps-despool "ps-print" nil t)
204     (autoload 'ps-spool-buffer "ps-print" nil t)
205     (autoload 'ps-spool-buffer-with-faces "ps-print" nil t)
206     (autoload 'read-passwd "passwd")
207     (autoload 'regexp-opt "regexp-opt")
208     (autoload 'reporter-submit-bug-report "reporter")
209     (if (emacs-version>= 21 5)
210         (autoload 'setenv "process" nil t)
211       (autoload 'setenv "env" nil t))
212     (autoload 'sgml-mode "psgml" nil t)
213     (autoload 'smtpmail-send-it "smtpmail")
214     (autoload 'sort-numeric-fields "sort" nil t)
215     (autoload 'sort-subr "sort")
216     (autoload 'trace-function-background "trace" nil t)
217     (autoload 'w3-do-setup "w3")
218     (autoload 'w3-prepare-buffer "w3-display")
219     (autoload 'w3-region "w3-display" nil t)
220     (defalias 'frame-char-height 'frame-height)
221     (defalias 'frame-char-width 'frame-width)
222     (defalias 'frame-parameter 'frame-property)
223     (defalias 'make-overlay 'ignore)
224     (defalias 'overlay-end 'ignore)
225     (defalias 'overlay-get 'ignore)
226     (defalias 'overlay-put 'ignore)
227     (defalias 'overlay-start 'ignore)
228     (defalias 'overlays-in 'ignore)
229     (defalias 'replace-dehighlight 'ignore)
230     (defalias 'replace-highlight 'ignore)
231     (defalias 'run-with-idle-timer 'ignore)
232     (defalias 'w3-coding-system-for-mime-charset 'ignore)))
233
234 (defun dgnushack-compile-verbosely ()
235   "Call dgnushack-compile with warnings ENABLED.  If you are compiling
236 patches to gnus, you should consider modifying make.bat to call
237 dgnushack-compile-verbosely.  All other users should continue to use
238 dgnushack-compile."
239   (dgnushack-compile t))
240
241 (defun dgnushack-compile (&optional warn)
242   ;;(setq byte-compile-dynamic t)
243   (unless warn
244     (setq byte-compile-warnings
245           '(free-vars unresolved callargs redefine)))
246   (let ((files (directory-files srcdir nil "^[^=].*\\.el$"))
247         ;;(byte-compile-generate-call-tree t)
248         file elc)
249     ;; Avoid barfing (from gnus-xmas) because the etc directory is not yet
250     ;; installed.
251     (when (featurep 'xemacs)
252       (setq gnus-xmas-glyph-directory "dummy"))
253     (dolist (file '("dgnushack.el" "lpath.el"))
254       (setq files (delete file files)))
255     (when (featurep 'base64)
256       (setq files (delete "base64.el" files)))
257     (condition-case code
258         (require 'w3-parse)
259       (error
260        (message "No w3: %s %s" (cadr code) (or (locate-library "w3-parse") ""))
261        (dolist (file '("nnultimate.el" "webmail.el" "nnwfm.el"))
262          (setq files (delete file files)))))
263     (condition-case code
264         (require 'mh-e)
265       (error
266        (message "No mh-e: %s %s" (cadr code) (or (locate-library "mh-e") ""))
267        (setq files (delete "gnus-mh.el" files))))
268     (condition-case code
269         (require 'xml)
270       (error
271        (message "No xml: %s %s" (cadr code) (or (locate-library "xml") ""))
272        (setq files (delete "nnrss.el" files))))
273     (dolist (file
274              (if (featurep 'xemacs)
275                  '("md5.el")
276                '("gnus-xmas.el" "messagexmas.el" "nnheaderxm.el"
277                  "run-at-time.el")))
278       (setq files (delete file files)))
279
280     (dolist (file files)
281       (setq file (expand-file-name file srcdir))
282       (when (and (file-exists-p
283                   (setq elc (concat (file-name-nondirectory file) "c")))
284                  (file-newer-than-file-p file elc))
285         (delete-file elc)))
286
287     (while (setq file (pop files))
288       (setq file (expand-file-name file srcdir))
289       (when (or (not (file-exists-p
290                       (setq elc (concat (file-name-nondirectory file) "c"))))
291                 (file-newer-than-file-p file elc))
292         (ignore-errors
293           (byte-compile-file file))))))
294
295 (defun dgnushack-recompile ()
296   (require 'gnus)
297   (byte-recompile-directory "." 0))
298
299 (defvar dgnushack-gnus-load-file
300   (if (featurep 'xemacs)
301       (expand-file-name "auto-autoloads.el")
302     (expand-file-name "gnus-load.el")))
303
304 (defvar dgnushack-cus-load-file 
305   (if (featurep 'xemacs)
306       (expand-file-name "custom-load.el")
307     (expand-file-name "cus-load.el")))
308
309 (defun dgnushack-make-cus-load ()
310   (load "cus-dep")
311   (let ((cusload-base-file dgnushack-cus-load-file))
312     (if (fboundp 'custom-make-dependencies)
313         (custom-make-dependencies)
314       (Custom-make-dependencies))
315     (when (featurep 'xemacs)
316       (message "Compiling %s..." dgnushack-cus-load-file)
317       (byte-compile-file dgnushack-cus-load-file))))
318
319 (defun dgnushack-make-auto-load ()
320   (require 'autoload)
321   (unless (make-autoload '(define-derived-mode child parent name
322                             "docstring" body)
323                          "file")
324     (defadvice make-autoload (around handle-define-derived-mode activate)
325       "Handle `define-derived-mode'."
326       (if (eq (car-safe (ad-get-arg 0)) 'define-derived-mode)
327           (setq ad-return-value
328                 (list 'autoload
329                       (list 'quote (nth 1 (ad-get-arg 0)))
330                       (ad-get-arg 1)
331                       (nth 4 (ad-get-arg 0))
332                       t nil))
333         ad-do-it))
334     (put 'define-derived-mode 'doc-string-elt 3))
335   (let ((generated-autoload-file dgnushack-gnus-load-file)
336         (make-backup-files nil)
337         (autoload-package-name "gnus"))
338     (if (featurep 'xemacs)
339         (if (file-exists-p generated-autoload-file)
340             (delete-file generated-autoload-file))
341       (with-temp-file generated-autoload-file
342         (insert ?\014)))
343     (batch-update-autoloads)))
344
345 (defun dgnushack-make-load ()
346   (unless (featurep 'xemacs)
347     (message "Generating %s..." dgnushack-gnus-load-file)
348     (with-temp-file dgnushack-gnus-load-file
349       (insert-file-contents dgnushack-cus-load-file)
350       (delete-file dgnushack-cus-load-file)
351       (goto-char (point-min))
352       (search-forward ";;; Code:")
353       (forward-line)
354       (delete-region (point-min) (point))
355       (insert "\
356 ;;; gnus-load.el --- automatically extracted custom dependencies and autoload
357 ;;
358 ;;; Code:
359 ")
360       (goto-char (point-max))
361       (if (search-backward "custom-versions-load-alist" nil t)
362           (forward-line -1)
363         (forward-line -1)
364         (while (eq (char-after) ?\;)
365           (forward-line -1))
366         (forward-line))
367       (delete-region (point) (point-max))
368       (insert "\n")
369       ;; smiley-* are duplicated. Remove them all.
370       (let ((point (point)))
371         (insert-file-contents dgnushack-gnus-load-file)
372         (goto-char point)
373         (while (search-forward "smiley-" nil t)
374           (beginning-of-line)
375           (if (looking-at "(autoload ")
376               (delete-region (point) (progn (forward-sexp) (point)))
377             (forward-line))))
378       ;;
379       (goto-char (point-max))
380       (when (search-backward "\n(provide " nil t)
381         (forward-line -1)
382         (delete-region (point) (point-max)))
383       (insert "\
384
385 \(provide 'gnus-load)
386
387 ;;; Local Variables:
388 ;;; version-control: never
389 ;;; no-byte-compile: t
390 ;;; no-update-autoloads: t
391 ;;; End:
392 ;;; gnus-load.el ends here
393 ")
394       ))
395   (message "Compiling %s..." dgnushack-gnus-load-file)
396   (byte-compile-file dgnushack-gnus-load-file)
397   (when (featurep 'xemacs)
398     (message "Creating dummy gnus-load.el...")
399     (with-temp-file (expand-file-name "gnus-load.el")
400       (insert "\
401
402 \(provide 'gnus-load)
403
404 ;;; Local Variables:
405 ;;; version-control: never
406 ;;; no-byte-compile: t
407 ;;; no-update-autoloads: t
408 ;;; End:
409 ;;; gnus-load.el ends here"))))
410
411
412 ;;; dgnushack.el ends here