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