384db333e1bd6b0db905c29237233cb7d2074f96
[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     (defalias 'get-popup-menu-response 'ignore)
134     (defalias 'event-object 'ignore)
135     (autoload 'iswitchb-read-buffer "iswitchb")
136     (autoload 'netrc-credentials "netrc")
137     (defalias 'x-defined-colors 'ignore)
138     (defalias 'read-color 'ignore)))
139
140 (eval-and-compile
141   (when (featurep 'xemacs)
142     (unless (fboundp 'defadvice)
143       (autoload 'defadvice "advice" nil nil 'macro))
144     (autoload 'Info-directory "info" nil t)
145     (autoload 'Info-index "info" nil t)
146     (autoload 'Info-index-next "info" nil t)
147     (autoload 'Info-menu "info" nil t)
148     (autoload 'ad-add-advice "advice")
149     (unless (and (emacs-version>= 21 5)
150                  (not (featurep 'sxemacs)))
151       ;; calendar/auto-autoloads.el provides it.
152       (autoload 'add-to-invisibility-spec "dummy"))
153     (autoload 'annotations-at "annotations")
154     (autoload 'apropos "apropos" nil t)
155     (autoload 'apropos-command "apropos" nil t)
156     (autoload 'bbdb-complete-name "bbdb-com" nil t)
157     (autoload 'browse-url "browse-url" nil t)
158     (autoload 'browse-url-of-file "browse-url" nil t)
159     (autoload 'c-mode "cc-mode" nil t)
160     (autoload 'customize-apropos "cus-edit" nil t)
161     (autoload 'customize-group "cus-edit" nil t)
162     (autoload 'customize-save-variable "cus-edit" nil t)
163     (autoload 'customize-set-variable "cus-edit" nil t)
164     (autoload 'customize-variable "cus-edit" nil t)
165     (autoload 'debug "debug" nil t)
166     (if (featurep 'mule)
167         (unless (locate-library "mule-ccl")
168           (autoload 'define-ccl-program "ccl" nil nil 'macro))
169       (defalias 'define-ccl-program 'ignore))
170     (autoload 'delete-annotation "annotations")
171     (autoload 'dolist "cl-macs" nil nil 'macro)
172     (autoload 'enriched-decode "enriched")
173     (autoload 'eudc-expand-inline "eudc" nil t)
174     (autoload 'executable-find "executable")
175     (autoload 'font-lock-fontify-buffer "font-lock" nil t)
176     (when (and (emacs-version>= 21 5)
177                (not (featurep 'sxemacs)))
178       (autoload 'get-display-table "disp-table")
179       (autoload 'put-display-table "disp-table"))
180     (autoload 'info "info" nil t)
181     (autoload 'mail-extract-address-components "mail-extr")
182     (autoload 'mail-fetch-field "mail-utils")
183     (autoload 'make-annotation "annotations")
184     (autoload 'make-display-table "disp-table")
185     (autoload 'pgg-snarf-keys-region "pgg" nil t)
186     (autoload 'pp "pp")
187     (autoload 'ps-despool "ps-print" nil t)
188     (autoload 'ps-spool-buffer "ps-print" nil t)
189     (autoload 'ps-spool-buffer-with-faces "ps-print" nil t)
190     (autoload 'read-passwd "passwd")
191     (autoload 'regexp-opt "regexp-opt")
192     (autoload 'reporter-submit-bug-report "reporter")
193     (if (and (emacs-version>= 21 5)
194              (not (featurep 'sxemacs)))
195         (autoload 'setenv "process" nil t)
196       (autoload 'setenv "env" nil t))
197     (autoload 'sgml-mode "psgml" nil t)
198     (autoload 'smtpmail-send-it "smtpmail")
199     (autoload 'sort-numeric-fields "sort" nil t)
200     (autoload 'sort-subr "sort")
201     (autoload 'thing-at-point "thingatpt")
202     (autoload 'toggle-truncate-lines "view-less" nil t)
203     (autoload 'trace-function-background "trace" nil t)
204     (autoload 'unmorse-region "morse" nil t)
205     (autoload 'w3-do-setup "w3")
206     (autoload 'w3-prepare-buffer "w3-display")
207     (autoload 'w3-region "w3-display" nil t)
208     (defalias 'frame-char-height 'frame-height)
209     (defalias 'frame-char-width 'frame-width)
210     (defalias 'frame-parameter 'frame-property)
211     (defalias 'make-overlay 'ignore)
212     (defalias 'overlay-end 'ignore)
213     (defalias 'overlay-get 'ignore)
214     (defalias 'overlay-put 'ignore)
215     (defalias 'overlay-start 'ignore)
216     (defalias 'overlays-in 'ignore)
217     (defalias 'replace-dehighlight 'ignore)
218     (defalias 'replace-highlight 'ignore)
219     (defalias 'w3-coding-system-for-mime-charset 'ignore)))
220
221 (defun dgnushack-emacs-compile-defcustom-p ()
222   "Return non-nil if Emacs byte compiles `defcustom' forms.
223 Those Emacsen will warn against undefined variables and functions used
224 in `defcustom' forms."
225   (let ((outbuf (with-temp-buffer
226                   (insert "(defcustom foo (1+ (random)) \"\" :group 'emacs)\n")
227                   (byte-compile-from-buffer (current-buffer) "foo.el"))))
228     (when outbuf
229       (prog1
230           (with-current-buffer outbuf
231             (goto-char (point-min))
232             (search-forward " 'foo '(byte-code " nil t))
233         (kill-buffer outbuf)))))
234
235 (when (dgnushack-emacs-compile-defcustom-p)
236   (maybe-fbind '(defined-colors face-attribute))
237   (maybe-bind '(idna-program installation-directory)))
238
239 (when (featurep 'xemacs)
240   (defadvice byte-optimize-apply (before use-mapcan (form) activate)
241     "Replace (apply 'nconc (mapcar ...)) with (mapcan ...)."
242     (let ((last (nth (1- (length form)) form)))
243       (when (and (eq last (third form))
244                  (consp last)
245                  (eq 'mapcar (car last))
246                  (equal (nth 1 form) ''nconc))
247         (setq form (cons 'mapcan (cdr last)))))))
248
249 (defun dgnushack-compile-verbosely ()
250   "Call dgnushack-compile with warnings ENABLED.  If you are compiling
251 patches to gnus, you should consider modifying make.bat to call
252 dgnushack-compile-verbosely.  All other users should continue to use
253 dgnushack-compile."
254   (dgnushack-compile t))
255
256 (defun dgnushack-compile (&optional warn)
257   ;;(setq byte-compile-dynamic t)
258   (unless warn
259     (setq byte-compile-warnings
260           '(free-vars unresolved callargs redefine suspicious)))
261   (let ((files (directory-files srcdir nil "^[^=].*\\.el$"))
262         ;;(byte-compile-generate-call-tree t)
263         file elc)
264     ;; Avoid barfing (from gnus-xmas) because the etc directory is not yet
265     ;; installed.
266     (when (featurep 'xemacs)
267       (setq gnus-xmas-glyph-directory "dummy"))
268     (dolist (file '(".dir-locals.el" "dgnushack.el" "lpath.el"))
269       (setq files (delete file files)))
270     (when (featurep 'base64)
271       (setq files (delete "base64.el" files)))
272     (condition-case code
273         (require 'w3-parse)
274       (error
275        (message "No w3: %s %s" (cadr code) (or (locate-library "w3-parse") ""))
276        (dolist (file '("webmail.el" "nnwfm.el"))
277          (setq files (delete file files)))))
278     (condition-case code
279         ;; Under XEmacs 21.4 this loads easy-mmode.elc that provides
280         ;; the Emacs functions `propertize' and `replace-regexp-in-string'.
281         (require 'mh-e)
282       (error
283        (message "No mh-e: %s %s" (cadr code) (or (locate-library "mh-e") ""))
284        (setq files (delete "gnus-mh.el" files))))
285     (condition-case code
286         (require 'xml)
287       (error
288        (message "No xml: %s %s" (cadr code) (or (locate-library "xml") ""))
289        (setq files (delete "nnrss.el" files))))
290     (dolist (file
291              (if (featurep 'xemacs)
292                  '("md5.el")
293                '("gnus-xmas.el" "messagexmas.el" "nnheaderxm.el")))
294       (setq files (delete file files)))
295     (unless (and (fboundp 'libxml-parse-html-region)
296                  ;; lpath.el binds it.
297                  (not (eq (symbol-function 'libxml-parse-html-region)
298                           'ignore)))
299       (setq files (delete "shr.el" files)))
300
301     (dolist (file files)
302       (setq file (expand-file-name file srcdir))
303       (when (and (file-exists-p
304                   (setq elc (concat (file-name-nondirectory file) "c")))
305                  (file-newer-than-file-p file elc))
306         (delete-file elc)))
307
308     (while (setq file (pop files))
309       (setq file (expand-file-name file srcdir))
310       (when (or (not (file-exists-p
311                       (setq elc (concat (file-name-nondirectory file) "c"))))
312                 (file-newer-than-file-p file elc))
313         (ignore-errors
314           (byte-compile-file file))))))
315
316 (defun dgnushack-recompile ()
317   (require 'gnus)
318   (byte-recompile-directory "." 0))
319
320 (defvar dgnushack-gnus-load-file
321   (if (featurep 'xemacs)
322       (expand-file-name "auto-autoloads.el")
323     (expand-file-name "gnus-load.el")))
324
325 (defvar dgnushack-cus-load-file
326   (if (featurep 'xemacs)
327       (expand-file-name "custom-load.el")
328     (expand-file-name "cus-load.el")))
329
330 (defun dgnushack-make-cus-load ()
331   (load "cus-dep")
332   (let ((cusload-base-file dgnushack-cus-load-file))
333     (defadvice directory-files (after exclude-dir-locals activate)
334       "Exclude .dir-locals.el file."
335       (dolist (file ad-return-value)
336         (if (string-match "\\(?:\\`\\|/\\)\\.dir-locals\\.el\\'" file)
337             (setq ad-return-value (delete file ad-return-value)))))
338     (unwind-protect
339         (if (fboundp 'custom-make-dependencies)
340             (custom-make-dependencies)
341           (Custom-make-dependencies))
342       (ad-unadvise 'directory-files))
343     (when (featurep 'xemacs)
344       (message "Compiling %s..." dgnushack-cus-load-file)
345       (byte-compile-file dgnushack-cus-load-file))))
346
347 (defun dgnushack-make-auto-load ()
348   (require 'autoload)
349   (let ((generated-autoload-file dgnushack-gnus-load-file)
350         (make-backup-files nil)
351         (autoload-package-name "gnus"))
352     (if (featurep 'xemacs)
353         (if (file-exists-p generated-autoload-file)
354             (delete-file generated-autoload-file))
355       (with-temp-file generated-autoload-file
356         (insert ?\014)))
357     (defadvice directory-files (after exclude-dir-locals activate)
358       "Exclude .dir-locals.el file."
359       (dolist (file ad-return-value)
360         (if (string-match "\\(?:\\`\\|/\\)\\.dir-locals\\.el\\'" file)
361             (setq ad-return-value (delete file ad-return-value)))))
362     (unwind-protect
363         (batch-update-autoloads)
364       (ad-unadvise 'directory-files))))
365
366 (defun dgnushack-make-load ()
367   (unless (featurep 'xemacs)
368     (message "Generating %s..." dgnushack-gnus-load-file)
369     (with-temp-file dgnushack-gnus-load-file
370       (insert-file-contents dgnushack-cus-load-file)
371       (delete-file dgnushack-cus-load-file)
372       (goto-char (point-min))
373       (search-forward ";;; Code:")
374       (forward-line)
375       (delete-region (point-min) (point))
376       (insert "\
377 ;;; gnus-load.el --- automatically extracted custom dependencies and autoload
378 ;;
379 ;;; Code:
380 ")
381       (goto-char (point-max))
382       (if (search-backward "custom-versions-load-alist" nil t)
383           (forward-line -1)
384         (forward-line -1)
385         (while (eq (char-after) ?\;)
386           (forward-line -1))
387         (forward-line))
388       (delete-region (point) (point-max))
389       (insert "\n")
390       ;; smiley-* are duplicated. Remove them all.
391       (let ((point (point)))
392         (insert-file-contents dgnushack-gnus-load-file)
393         (goto-char point)
394         (while (search-forward "smiley-" nil t)
395           (beginning-of-line)
396           (if (looking-at "(autoload ")
397               (delete-region (point) (progn (forward-sexp) (point)))
398             (forward-line))))
399       ;;
400       (goto-char (point-max))
401       (when (search-backward "\n(provide " nil t)
402         (forward-line -1)
403         (delete-region (point) (point-max)))
404       (insert "\
405
406 \(provide 'gnus-load)
407
408 ;;; Local Variables:
409 ;;; version-control: never
410 ;;; no-byte-compile: t
411 ;;; no-update-autoloads: t
412 ;;; End:
413 ;;; gnus-load.el ends here
414 ")
415       ))
416   (message "Compiling %s..." dgnushack-gnus-load-file)
417   (byte-compile-file dgnushack-gnus-load-file)
418   (when (featurep 'xemacs)
419     (message "Creating dummy gnus-load.el...")
420     (with-temp-file (expand-file-name "gnus-load.el")
421       (insert "\
422
423 \(provide 'gnus-load)
424
425 ;;; Local Variables:
426 ;;; version-control: never
427 ;;; no-byte-compile: t
428 ;;; no-update-autoloads: t
429 ;;; End:
430 ;;; gnus-load.el ends here"))))
431
432 (defun dgnushack-find-lisp-shadows (&optional lispdir)
433   "Return a list of directories in which other Gnus installations exist.
434 This function looks for the other Gnus installations which will shadow
435 the new Gnus Lisp modules which have been installed in LISPDIR, using
436 the default `load-path'.  The return value will make sense only when
437 LISPDIR is existent and is listed in the default `load-path'.  Assume
438 LISPDIR will be prepended to `load-path' by a user if the default
439 `load-path' does not contain it."
440   (unless lispdir
441     (setq lispdir (getenv "lispdir")))
442   (when (and lispdir (file-directory-p lispdir))
443     (setq lispdir (file-truename (directory-file-name lispdir)))
444     (let ((indices '("gnus.elc" "gnus.el" "gnus.el.bz2" "gnus.el.gz"
445                      "message.elc" "message.el" "message.el.bz2"
446                      "message.el.gz"))
447           (path (delq nil (mapcar
448                            (lambda (p)
449                              (condition-case nil
450                                  (when (and p (file-directory-p p))
451                                    (file-truename (directory-file-name p)))
452                                (error nil)))
453                            dgnushack-default-load-path)))
454           rest elcs)
455       (while path
456         (setq rest (cons (car path) rest)
457               path (delete (car rest) (cdr path))))
458       (setq path (nreverse (cdr (member lispdir rest)))
459             rest nil)
460       (while path
461         (setq elcs indices)
462         (while elcs
463           (when (file-exists-p (expand-file-name (pop elcs) (car path)))
464             (setq rest (cons (car path) rest)
465                   elcs nil)))
466         (setq path (cdr path)))
467       (prog1
468           (setq path (nreverse rest))
469         (when path
470           (let (print-level print-length)
471             (princ (concat "\n\
472 WARNING: The other Gnus installation" (if (cdr path) "s have" " has") "\
473  been detected in:\n\n  " (mapconcat 'identity path "\n  ") "\n\n\
474 You will need to modify the run-time `load-path', remove them manually,
475 or remove them using `make remove-installed-shadows'.\n\n"))))))))
476
477 (defun dgnushack-remove-lisp-shadows (&optional lispdir)
478   "Remove the other Gnus installations which shadow the recent one."
479   (let ((path (with-temp-buffer
480                 (let ((standard-output (current-buffer)))
481                   (dgnushack-find-lisp-shadows lispdir))))
482         elcs files shadows file)
483     (when path
484       (unless (setq elcs (directory-files srcdir nil "\\.elc\\'"))
485         (error "You should build .elc files first."))
486       (setq files
487             (apply
488              'append
489              (mapcar
490               (lambda (el)
491                 (list (concat el "c") el (concat el ".bz2") (concat el ".gz")))
492               (append
493                (list (file-name-nondirectory dgnushack-gnus-load-file)
494                      (file-name-nondirectory dgnushack-cus-load-file))
495                (mapcar (lambda (elc) (substring elc 0 -1)) elcs)))))
496       (while path
497         (setq shadows files)
498         (while shadows
499           (setq file (expand-file-name (pop shadows) (car path)))
500           (when (file-exists-p file)
501             (princ (concat "  Removing " file "..."))
502             (condition-case nil
503                 (progn
504                   (delete-file file)
505                   (princ "done\n"))
506               (error (princ "failed\n")))))
507         (setq path (cdr path))))))
508
509 ;;; dgnushack.el ends here