(dgnushack-emacs-compile-defcustom-p): New function;
[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, 2006, 2007
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 3, 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 (defvar dgnushack-default-load-path (copy-sequence load-path))
32
33 (defalias 'facep 'ignore)
34
35 (require 'cl)
36
37 (defvar srcdir (or (getenv "srcdir") "."))
38 (defvar loaddir (and load-file-name (file-name-directory load-file-name)))
39
40 (defun my-getenv (str)
41   (let ((val (getenv str)))
42     (if (equal val "no") nil val)))
43
44 (if (my-getenv "lispdir")
45     (push (my-getenv "lispdir") load-path))
46
47 (push (or (my-getenv "URLDIR") (expand-file-name "../../url/lisp/" loaddir))
48       load-path)
49
50 (push (or (my-getenv "W3DIR") (expand-file-name "../../w3/lisp/" loaddir))
51       load-path)
52
53 ;(push "/usr/share/emacs/site-lisp" load-path)
54
55 ;; If we are building w3 in a different directory than the source
56 ;; directory, we must read *.el from source directory and write *.elc
57 ;; into the building directory.  For that, we define this function
58 ;; before loading bytecomp.  Bytecomp doesn't overwrite this function.
59 (defun byte-compile-dest-file (filename)
60   "Convert an Emacs Lisp source file name to a compiled file name.
61  In addition, remove directory name part from FILENAME."
62   (setq filename (byte-compiler-base-file-name filename))
63   (setq filename (file-name-sans-versions filename))
64   (setq filename (file-name-nondirectory filename))
65   (if (memq system-type '(win32 w32 mswindows windows-nt))
66       (setq filename (downcase filename)))
67   (cond ((eq system-type 'vax-vms)
68          (concat (substring filename 0 (string-match ";" filename)) "c"))
69         ((string-match emacs-lisp-file-regexp filename)
70          (concat (substring filename 0 (match-beginning 0)) ".elc"))
71         (t (concat filename ".elc"))))
72
73 (require 'bytecomp)
74 ;; To avoid having defsubsts and inlines happen.
75 ;(if (featurep 'xemacs)
76 ;    (require 'byte-optimize)
77 ;  (require 'byte-opt))
78 ;(defun byte-optimize-inline-handler (form)
79 ;  "byte-optimize-handler for the `inline' special-form."
80 ;  (cons 'progn (cdr form)))
81 ;(defalias 'byte-compile-file-form-defsubst 'byte-compile-file-form-defun)
82
83 (when (and (not (featurep 'xemacs))
84            (= emacs-major-version 21)
85            (>= emacs-minor-version 3)
86            (condition-case code
87                (let ((byte-compile-error-on-warn t))
88                  (byte-optimize-form (quote (pop x)) t)
89                  nil)
90              (error (string-match "called for effect"
91                                   (error-message-string code)))))
92   (defadvice byte-optimize-form-code-walker (around silence-warn-for-pop
93                                                     (form for-effect)
94                                                     activate)
95     "Silence the warning \"...called for effect\" for the `pop' form.
96 It is effective only when the `pop' macro is defined by cl.el rather
97 than subr.el."
98     (let (tmp)
99       (if (and (eq (car-safe form) 'car)
100                for-effect
101                (setq tmp (get 'car 'side-effect-free))
102                (not byte-compile-delete-errors)
103                (not (eq tmp 'error-free))
104                (eq (car-safe (cadr form)) 'prog1)
105                (let ((var (cadr (cadr form)))
106                      (last (nth 2 (cadr form))))
107                  (and (symbolp var)
108                       (null (nthcdr 3 (cadr form)))
109                       (eq (car-safe last) 'setq)
110                       (eq (cadr last) var)
111                       (eq (car-safe (nth 2 last)) 'cdr)
112                       (eq (cadr (nth 2 last)) var))))
113           (progn
114             (put 'car 'side-effect-free 'error-free)
115             (unwind-protect
116                 ad-do-it
117               (put 'car 'side-effect-free tmp)))
118         ad-do-it))))
119
120 (when (and (not (featurep 'xemacs))
121            (byte-optimize-form
122             '(and (> 0 1)
123                   (message "This should not appear in the byte-code."))
124             t))
125   (defadvice byte-optimize-form-code-walker
126     (around fix-bug-in-and/or-forms (form for-effect) activate)
127     "Optimize the rest of the and/or forms.
128 It has been fixed in XEmacs before releasing 21.4 and also has been
129 fixed in Emacs 22."
130     (if (and for-effect (memq (car-safe form) '(and or)))
131         (let ((fn (car form))
132               (backwards (reverse (cdr form))))
133           (while (and backwards
134                       (null (setcar backwards
135                                     (byte-optimize-form (car backwards) t))))
136             (setq backwards (cdr backwards)))
137           (if (and (cdr form) (null backwards))
138               (byte-compile-log
139                "  all subforms of %s called for effect; deleted" form))
140           (when backwards
141             (setcdr backwards
142                     (mapcar 'byte-optimize-form (cdr backwards))))
143           (setq ad-return-value (cons fn (nreverse backwards))))
144       ad-do-it)))
145
146 ;; Work around for an incompatibility (XEmacs 21.4 vs. 21.5), see the
147 ;; following threads:
148 ;;
149 ;; http://thread.gmane.org/gmane.emacs.gnus.general/56414
150 ;; Subject: attachment problems found but not fixed
151 ;;
152 ;; http://thread.gmane.org/gmane.emacs.gnus.general/56459
153 ;; Subject: Splitting mail -- XEmacs 21.4 vs 21.5
154 ;;
155 ;; http://thread.gmane.org/gmane.emacs.xemacs.beta/20519
156 ;; Subject: XEmacs 21.5 and Gnus fancy splitting.
157 ;;
158 ;; Should be fixed in XEmacs (March 2007).
159 ;; http://thread.gmane.org/gmane.emacs.xemacs.patches/8124
160 ;; When should we remove this workaround?
161 ;;
162 (when (and (featurep 'xemacs)
163            (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
164              (modify-syntax-entry ?= " " table)
165              (with-temp-buffer
166                (with-syntax-table table
167                  (insert "foo=bar")
168                  (goto-char (point-min))
169                  (forward-sexp 1)
170                  (eolp)))))
171   ;; The original `with-syntax-table' uses `copy-syntax-table' which
172   ;; doesn't seem to copy modified syntax entries in XEmacs 21.5.
173   (defmacro with-syntax-table (syntab &rest body)
174     "Evaluate BODY with the SYNTAB as the current syntax table."
175     `(let ((stab (syntax-table)))
176        (unwind-protect
177            (progn
178              ;;(set-syntax-table (copy-syntax-table ,syntab))
179              (set-syntax-table ,syntab)
180              ,@body)
181          (set-syntax-table stab)))))
182
183 (push srcdir load-path)
184 (push loaddir load-path)
185 (load (expand-file-name "lpath.el" loaddir) nil t)
186
187 (defalias 'device-sound-enabled-p 'ignore)
188 (defalias 'play-sound-file 'ignore)
189 (defalias 'nndb-request-article 'ignore)
190 (defalias 'efs-re-read-dir 'ignore)
191 (defalias 'ange-ftp-re-read-dir 'ignore)
192 (defalias 'define-mail-user-agent 'ignore)
193
194 (eval-and-compile
195   (unless (featurep 'xemacs)
196     (defalias 'get-popup-menu-response 'ignore)
197     (defalias 'event-object 'ignore)
198     (defalias 'x-defined-colors 'ignore)
199     (defalias 'read-color 'ignore)))
200
201 (eval-and-compile
202   (when (featurep 'xemacs)
203     (unless (fboundp 'defadvice)
204       (autoload 'defadvice "advice" nil nil 'macro))
205     (autoload 'Info-directory "info" nil t)
206     (autoload 'Info-menu "info" nil t)
207     (autoload 'ad-add-advice "advice")
208     (autoload 'annotations-at "annotations")
209     (autoload 'apropos "apropos" nil t)
210     (autoload 'apropos-command "apropos" nil t)
211     (autoload 'bbdb-complete-name "bbdb-com" nil t)
212     (autoload 'browse-url "browse-url" nil t)
213     (autoload 'browse-url-of-file "browse-url" nil t)
214     (autoload 'c-mode "cc-mode" nil t)
215     (autoload 'customize-apropos "cus-edit" nil t)
216     (autoload 'customize-group "cus-edit" nil t)
217     (autoload 'customize-save-variable "cus-edit" nil t)
218     (autoload 'customize-set-variable "cus-edit" nil t)
219     (autoload 'customize-variable "cus-edit" nil t)
220     (if (featurep 'mule)
221         (unless (locate-library "mule-ccl")
222           (autoload 'define-ccl-program "ccl" nil nil 'macro))
223       (defalias 'define-ccl-program 'ignore))
224     (autoload 'delete-annotation "annotations")
225     (autoload 'dolist "cl-macs" nil nil 'macro)
226     (autoload 'enriched-decode "enriched")
227     (autoload 'executable-find "executable")
228     (autoload 'font-lock-fontify-buffer "font-lock" nil t)
229     (autoload 'info "info" nil t)
230     (autoload 'mail-extract-address-components "mail-extr")
231     (autoload 'mail-fetch-field "mail-utils")
232     (autoload 'make-annotation "annotations")
233     (autoload 'make-display-table "disp-table")
234     (autoload 'pp "pp")
235     (autoload 'ps-despool "ps-print" nil t)
236     (autoload 'ps-spool-buffer "ps-print" nil t)
237     (autoload 'ps-spool-buffer-with-faces "ps-print" nil t)
238     (autoload 'read-passwd "passwd")
239     (autoload 'regexp-opt "regexp-opt")
240     (autoload 'reporter-submit-bug-report "reporter")
241     (if (and (emacs-version>= 21 5)
242              (not (featurep 'sxemacs)))
243         (autoload 'setenv "process" nil t)
244       (autoload 'setenv "env" nil t))
245     (autoload 'sgml-mode "psgml" nil t)
246     (autoload 'smtpmail-send-it "smtpmail")
247     (autoload 'sort-numeric-fields "sort" nil t)
248     (autoload 'sort-subr "sort")
249     (autoload 'toggle-truncate-lines "view-less" nil t)
250     (autoload 'trace-function-background "trace" nil t)
251     (autoload 'unmorse-region "morse" nil t)
252     (autoload 'w3-do-setup "w3")
253     (autoload 'w3-prepare-buffer "w3-display")
254     (autoload 'w3-region "w3-display" nil t)
255     (defalias 'frame-char-height 'frame-height)
256     (defalias 'frame-char-width 'frame-width)
257     (defalias 'frame-parameter 'frame-property)
258     (defalias 'make-overlay 'ignore)
259     (defalias 'overlay-end 'ignore)
260     (defalias 'overlay-get 'ignore)
261     (defalias 'overlay-put 'ignore)
262     (defalias 'overlay-start 'ignore)
263     (defalias 'overlays-in 'ignore)
264     (defalias 'replace-dehighlight 'ignore)
265     (defalias 'replace-highlight 'ignore)
266     (defalias 'w3-coding-system-for-mime-charset 'ignore)))
267
268 (defun dgnushack-emacs-compile-defcustom-p ()
269   "Return non-nil if Emacs byte compiles `defcustom' forms.
270 Those Emacsen will warn against undefined variables and functions used
271 in `defcustom' forms."
272   (let ((outbuf (with-temp-buffer
273                   (insert "(defcustom foo (1+ (random)) \"\" :group 'emacs)\n")
274                   (byte-compile-from-buffer (current-buffer) "foo.el"))))
275     (when outbuf
276       (prog1
277           (with-current-buffer outbuf
278             (goto-char (point-min))
279             (search-forward " 'foo '(byte-code " nil t))
280         (kill-buffer outbuf)))))
281
282 (when (dgnushack-emacs-compile-defcustom-p)
283   (maybe-fbind '(defined-colors face-attribute))
284   (maybe-bind '(idna-program installation-directory)))
285
286 (defun dgnushack-compile-verbosely ()
287   "Call dgnushack-compile with warnings ENABLED.  If you are compiling
288 patches to gnus, you should consider modifying make.bat to call
289 dgnushack-compile-verbosely.  All other users should continue to use
290 dgnushack-compile."
291   (dgnushack-compile t))
292
293 (defun dgnushack-compile (&optional warn)
294   ;;(setq byte-compile-dynamic t)
295   (unless warn
296     (setq byte-compile-warnings
297           '(free-vars unresolved callargs redefine)))
298   (let ((files (directory-files srcdir nil "^[^=].*\\.el$"))
299         ;;(byte-compile-generate-call-tree t)
300         file elc)
301     ;; Avoid barfing (from gnus-xmas) because the etc directory is not yet
302     ;; installed.
303     (when (featurep 'xemacs)
304       (setq gnus-xmas-glyph-directory "dummy"))
305     (dolist (file '("dgnushack.el" "lpath.el"))
306       (setq files (delete file files)))
307     (when (featurep 'base64)
308       (setq files (delete "base64.el" files)))
309     (condition-case code
310         (require 'w3-parse)
311       (error
312        (message "No w3: %s %s" (cadr code) (or (locate-library "w3-parse") ""))
313        (dolist (file '("nnultimate.el" "webmail.el" "nnwfm.el"))
314          (setq files (delete file files)))))
315     (condition-case code
316         (require 'mh-e)
317       (error
318        (message "No mh-e: %s %s" (cadr code) (or (locate-library "mh-e") ""))
319        (setq files (delete "gnus-mh.el" files))))
320     (condition-case code
321         (require 'xml)
322       (error
323        (message "No xml: %s %s" (cadr code) (or (locate-library "xml") ""))
324        (setq files (delete "nnrss.el" files))))
325     (dolist (file
326              (if (featurep 'xemacs)
327                  '("md5.el")
328                '("gnus-xmas.el" "messagexmas.el" "nnheaderxm.el")))
329       (setq files (delete file files)))
330
331     (dolist (file files)
332       (setq file (expand-file-name file srcdir))
333       (when (and (file-exists-p
334                   (setq elc (concat (file-name-nondirectory file) "c")))
335                  (file-newer-than-file-p file elc))
336         (delete-file elc)))
337
338     (while (setq file (pop files))
339       (setq file (expand-file-name file srcdir))
340       (when (or (not (file-exists-p
341                       (setq elc (concat (file-name-nondirectory file) "c"))))
342                 (file-newer-than-file-p file elc))
343         (ignore-errors
344           (byte-compile-file file))))))
345
346 (defun dgnushack-recompile ()
347   (require 'gnus)
348   (byte-recompile-directory "." 0))
349
350 (defvar dgnushack-gnus-load-file
351   (if (featurep 'xemacs)
352       (expand-file-name "auto-autoloads.el")
353     (expand-file-name "gnus-load.el")))
354
355 (defvar dgnushack-cus-load-file 
356   (if (featurep 'xemacs)
357       (expand-file-name "custom-load.el")
358     (expand-file-name "cus-load.el")))
359
360 (defun dgnushack-make-cus-load ()
361   (load "cus-dep")
362   (let ((cusload-base-file dgnushack-cus-load-file))
363     (if (fboundp 'custom-make-dependencies)
364         (custom-make-dependencies)
365       (Custom-make-dependencies))
366     (when (featurep 'xemacs)
367       (message "Compiling %s..." dgnushack-cus-load-file)
368       (byte-compile-file dgnushack-cus-load-file))))
369
370 (defun dgnushack-make-auto-load ()
371   (require 'autoload)
372   (unless (make-autoload '(define-derived-mode child parent name
373                             "docstring" body)
374                          "file")
375     (defadvice make-autoload (around handle-define-derived-mode activate)
376       "Handle `define-derived-mode'."
377       (if (eq (car-safe (ad-get-arg 0)) 'define-derived-mode)
378           (setq ad-return-value
379                 (list 'autoload
380                       (list 'quote (nth 1 (ad-get-arg 0)))
381                       (ad-get-arg 1)
382                       (nth 4 (ad-get-arg 0))
383                       t nil))
384         ad-do-it))
385     (put 'define-derived-mode 'doc-string-elt 3))
386   (let ((generated-autoload-file dgnushack-gnus-load-file)
387         (make-backup-files nil)
388         (autoload-package-name "gnus"))
389     (if (featurep 'xemacs)
390         (if (file-exists-p generated-autoload-file)
391             (delete-file generated-autoload-file))
392       (with-temp-file generated-autoload-file
393         (insert ?\014)))
394     (batch-update-autoloads)))
395
396 (defun dgnushack-make-load ()
397   (unless (featurep 'xemacs)
398     (message "Generating %s..." dgnushack-gnus-load-file)
399     (with-temp-file dgnushack-gnus-load-file
400       (insert-file-contents dgnushack-cus-load-file)
401       (delete-file dgnushack-cus-load-file)
402       (goto-char (point-min))
403       (search-forward ";;; Code:")
404       (forward-line)
405       (delete-region (point-min) (point))
406       (insert "\
407 ;;; gnus-load.el --- automatically extracted custom dependencies and autoload
408 ;;
409 ;;; Code:
410 ")
411       (goto-char (point-max))
412       (if (search-backward "custom-versions-load-alist" nil t)
413           (forward-line -1)
414         (forward-line -1)
415         (while (eq (char-after) ?\;)
416           (forward-line -1))
417         (forward-line))
418       (delete-region (point) (point-max))
419       (insert "\n")
420       ;; smiley-* are duplicated. Remove them all.
421       (let ((point (point)))
422         (insert-file-contents dgnushack-gnus-load-file)
423         (goto-char point)
424         (while (search-forward "smiley-" nil t)
425           (beginning-of-line)
426           (if (looking-at "(autoload ")
427               (delete-region (point) (progn (forward-sexp) (point)))
428             (forward-line))))
429       ;;
430       (goto-char (point-max))
431       (when (search-backward "\n(provide " nil t)
432         (forward-line -1)
433         (delete-region (point) (point-max)))
434       (insert "\
435
436 \(provide 'gnus-load)
437
438 ;;; Local Variables:
439 ;;; version-control: never
440 ;;; no-byte-compile: t
441 ;;; no-update-autoloads: t
442 ;;; End:
443 ;;; gnus-load.el ends here
444 ")
445       ))
446   (message "Compiling %s..." dgnushack-gnus-load-file)
447   (byte-compile-file dgnushack-gnus-load-file)
448   (when (featurep 'xemacs)
449     (message "Creating dummy gnus-load.el...")
450     (with-temp-file (expand-file-name "gnus-load.el")
451       (insert "\
452
453 \(provide 'gnus-load)
454
455 ;;; Local Variables:
456 ;;; version-control: never
457 ;;; no-byte-compile: t
458 ;;; no-update-autoloads: t
459 ;;; End:
460 ;;; gnus-load.el ends here"))))
461
462 (defun dgnushack-find-lisp-shadows (&optional lispdir)
463   "Return a list of directories in which other Gnus installations exist.
464 This function looks for the other Gnus installations which will shadow
465 the new Gnus Lisp modules which have been installed in LISPDIR, using
466 the default `load-path'.  The return value will make sense only when
467 LISPDIR is existent and is listed in the default `load-path'.  Assume
468 LISPDIR will be prepended to `load-path' by a user if the default
469 `load-path' does not contain it."
470   (unless lispdir
471     (setq lispdir (getenv "lispdir")))
472   (when (and lispdir (file-directory-p lispdir))
473     (setq lispdir (file-truename (directory-file-name lispdir)))
474     (let ((indices '("gnus.elc" "gnus.el" "gnus.el.bz2" "gnus.el.gz"
475                      "message.elc" "message.el" "message.el.bz2"
476                      "message.el.gz"))
477           (path (delq nil (mapcar
478                            (lambda (p)
479                              (condition-case nil
480                                  (when (and p (file-directory-p p))
481                                    (file-truename (directory-file-name p)))
482                                (error nil)))
483                            dgnushack-default-load-path)))
484           rest elcs)
485       (while path
486         (setq rest (cons (car path) rest)
487               path (delete (car rest) (cdr path))))
488       (setq path (nreverse (cdr (member lispdir rest)))
489             rest nil)
490       (while path
491         (setq elcs indices)
492         (while elcs
493           (when (file-exists-p (expand-file-name (pop elcs) (car path)))
494             (setq rest (cons (car path) rest)
495                   elcs nil)))
496         (setq path (cdr path)))
497       (prog1
498           (setq path (nreverse rest))
499         (when path
500           (let (print-level print-length)
501             (princ (concat "\n\
502 WARNING: The other Gnus installation" (if (cdr path) "s have" " has") "\
503  been detected in:\n\n  " (mapconcat 'identity path "\n  ") "\n\n\
504 You will need to modify the run-time `load-path', remove them manually,
505 or remove them using `make remove-installed-shadows'.\n\n"))))))))
506
507 (defun dgnushack-remove-lisp-shadows (&optional lispdir)
508   "Remove the other Gnus installations which shadow the recent one."
509   (let ((path (with-temp-buffer
510                 (let ((standard-output (current-buffer)))
511                   (dgnushack-find-lisp-shadows lispdir))))
512         elcs files shadows file)
513     (when path
514       (unless (setq elcs (directory-files srcdir nil "\\.elc\\'"))
515         (error "You should build .elc files first."))
516       (setq files
517             (apply
518              'append
519              (mapcar
520               (lambda (el)
521                 (list (concat el "c") el (concat el ".bz2") (concat el ".gz")))
522               (append
523                (list (file-name-nondirectory dgnushack-gnus-load-file)
524                      (file-name-nondirectory dgnushack-cus-load-file))
525                (mapcar (lambda (elc) (substring elc 0 -1)) elcs)))))
526       (while path
527         (setq shadows files)
528         (while shadows
529           (setq file (expand-file-name (pop shadows) (car path)))
530           (when (file-exists-p file)
531             (princ (concat "  Removing " file "..."))
532             (condition-case nil
533                 (progn
534                   (delete-file file)
535                   (princ "done\n"))
536               (error (princ "failed\n")))))
537         (setq path (cdr path))))))
538
539 ;;; dgnushack.el ends here
540
541 ;;; arch-tag: 579f585a-24eb-4e1c-8d34-4808e11b68f2