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