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