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