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