Fix some compilation warnings.
[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
287     (dolist (file files)
288       (setq file (expand-file-name file srcdir))
289       (when (and (file-exists-p
290                   (setq elc (concat (file-name-nondirectory file) "c")))
291                  (file-newer-than-file-p file elc))
292         (delete-file elc)))
293
294     (while (setq file (pop files))
295       (setq file (expand-file-name file srcdir))
296       (when (or (not (file-exists-p
297                       (setq elc (concat (file-name-nondirectory file) "c"))))
298                 (file-newer-than-file-p file elc))
299         (ignore-errors
300           (byte-compile-file file))))))
301
302 (defun dgnushack-recompile ()
303   (require 'gnus)
304   (byte-recompile-directory "." 0))
305
306 (defvar dgnushack-gnus-load-file
307   (if (featurep 'xemacs)
308       (expand-file-name "auto-autoloads.el")
309     (expand-file-name "gnus-load.el")))
310
311 (defvar dgnushack-cus-load-file
312   (if (featurep 'xemacs)
313       (expand-file-name "custom-load.el")
314     (expand-file-name "cus-load.el")))
315
316 (defun dgnushack-make-cus-load ()
317   (load "cus-dep")
318   (let ((cusload-base-file dgnushack-cus-load-file))
319     (defadvice directory-files (after exclude-dir-locals activate)
320       "Exclude .dir-locals.el file."
321       (dolist (file ad-return-value)
322         (if (string-match "\\(?:\\`\\|/\\)\\.dir-locals\\.el\\'" file)
323             (setq ad-return-value (delete file ad-return-value)))))
324     (unwind-protect
325         (if (fboundp 'custom-make-dependencies)
326             (custom-make-dependencies)
327           (Custom-make-dependencies))
328       (ad-unadvise 'directory-files))
329     (when (featurep 'xemacs)
330       (message "Compiling %s..." dgnushack-cus-load-file)
331       (byte-compile-file dgnushack-cus-load-file))))
332
333 (defun dgnushack-make-auto-load ()
334   (require 'autoload)
335   (let ((generated-autoload-file dgnushack-gnus-load-file)
336         (make-backup-files nil)
337         (autoload-package-name "gnus"))
338     (if (featurep 'xemacs)
339         (if (file-exists-p generated-autoload-file)
340             (delete-file generated-autoload-file))
341       (with-temp-file generated-autoload-file
342         (insert ?\014)))
343     (defadvice directory-files (after exclude-dir-locals activate)
344       "Exclude .dir-locals.el file."
345       (dolist (file ad-return-value)
346         (if (string-match "\\(?:\\`\\|/\\)\\.dir-locals\\.el\\'" file)
347             (setq ad-return-value (delete file ad-return-value)))))
348     (unwind-protect
349         (batch-update-autoloads)
350       (ad-unadvise 'directory-files))))
351
352 (defun dgnushack-make-load ()
353   (unless (featurep 'xemacs)
354     (message "Generating %s..." dgnushack-gnus-load-file)
355     (with-temp-file dgnushack-gnus-load-file
356       (insert-file-contents dgnushack-cus-load-file)
357       (delete-file dgnushack-cus-load-file)
358       (goto-char (point-min))
359       (search-forward ";;; Code:")
360       (forward-line)
361       (delete-region (point-min) (point))
362       (insert "\
363 ;;; gnus-load.el --- automatically extracted custom dependencies and autoload
364 ;;
365 ;;; Code:
366 ")
367       (goto-char (point-max))
368       (if (search-backward "custom-versions-load-alist" nil t)
369           (forward-line -1)
370         (forward-line -1)
371         (while (eq (char-after) ?\;)
372           (forward-line -1))
373         (forward-line))
374       (delete-region (point) (point-max))
375       (insert "\n")
376       ;; smiley-* are duplicated. Remove them all.
377       (let ((point (point)))
378         (insert-file-contents dgnushack-gnus-load-file)
379         (goto-char point)
380         (while (search-forward "smiley-" nil t)
381           (beginning-of-line)
382           (if (looking-at "(autoload ")
383               (delete-region (point) (progn (forward-sexp) (point)))
384             (forward-line))))
385       ;;
386       (goto-char (point-max))
387       (when (search-backward "\n(provide " nil t)
388         (forward-line -1)
389         (delete-region (point) (point-max)))
390       (insert "\
391
392 \(provide 'gnus-load)
393
394 ;;; Local Variables:
395 ;;; version-control: never
396 ;;; no-byte-compile: t
397 ;;; no-update-autoloads: t
398 ;;; End:
399 ;;; gnus-load.el ends here
400 ")
401       ))
402   (message "Compiling %s..." dgnushack-gnus-load-file)
403   (byte-compile-file dgnushack-gnus-load-file)
404   (when (featurep 'xemacs)
405     (message "Creating dummy gnus-load.el...")
406     (with-temp-file (expand-file-name "gnus-load.el")
407       (insert "\
408
409 \(provide 'gnus-load)
410
411 ;;; Local Variables:
412 ;;; version-control: never
413 ;;; no-byte-compile: t
414 ;;; no-update-autoloads: t
415 ;;; End:
416 ;;; gnus-load.el ends here"))))
417
418 (defun dgnushack-find-lisp-shadows (&optional lispdir)
419   "Return a list of directories in which other Gnus installations exist.
420 This function looks for the other Gnus installations which will shadow
421 the new Gnus Lisp modules which have been installed in LISPDIR, using
422 the default `load-path'.  The return value will make sense only when
423 LISPDIR is existent and is listed in the default `load-path'.  Assume
424 LISPDIR will be prepended to `load-path' by a user if the default
425 `load-path' does not contain it."
426   (unless lispdir
427     (setq lispdir (getenv "lispdir")))
428   (when (and lispdir (file-directory-p lispdir))
429     (setq lispdir (file-truename (directory-file-name lispdir)))
430     (let ((indices '("gnus.elc" "gnus.el" "gnus.el.bz2" "gnus.el.gz"
431                      "message.elc" "message.el" "message.el.bz2"
432                      "message.el.gz"))
433           (path (delq nil (mapcar
434                            (lambda (p)
435                              (condition-case nil
436                                  (when (and p (file-directory-p p))
437                                    (file-truename (directory-file-name p)))
438                                (error nil)))
439                            dgnushack-default-load-path)))
440           rest elcs)
441       (while path
442         (setq rest (cons (car path) rest)
443               path (delete (car rest) (cdr path))))
444       (setq path (nreverse (cdr (member lispdir rest)))
445             rest nil)
446       (while path
447         (setq elcs indices)
448         (while elcs
449           (when (file-exists-p (expand-file-name (pop elcs) (car path)))
450             (setq rest (cons (car path) rest)
451                   elcs nil)))
452         (setq path (cdr path)))
453       (prog1
454           (setq path (nreverse rest))
455         (when path
456           (let (print-level print-length)
457             (princ (concat "\n\
458 WARNING: The other Gnus installation" (if (cdr path) "s have" " has") "\
459  been detected in:\n\n  " (mapconcat 'identity path "\n  ") "\n\n\
460 You will need to modify the run-time `load-path', remove them manually,
461 or remove them using `make remove-installed-shadows'.\n\n"))))))))
462
463 (defun dgnushack-remove-lisp-shadows (&optional lispdir)
464   "Remove the other Gnus installations which shadow the recent one."
465   (let ((path (with-temp-buffer
466                 (let ((standard-output (current-buffer)))
467                   (dgnushack-find-lisp-shadows lispdir))))
468         elcs files shadows file)
469     (when path
470       (unless (setq elcs (directory-files srcdir nil "\\.elc\\'"))
471         (error "You should build .elc files first."))
472       (setq files
473             (apply
474              'append
475              (mapcar
476               (lambda (el)
477                 (list (concat el "c") el (concat el ".bz2") (concat el ".gz")))
478               (append
479                (list (file-name-nondirectory dgnushack-gnus-load-file)
480                      (file-name-nondirectory dgnushack-cus-load-file))
481                (mapcar (lambda (elc) (substring elc 0 -1)) elcs)))))
482       (while path
483         (setq shadows files)
484         (while shadows
485           (setq file (expand-file-name (pop shadows) (car path)))
486           (when (file-exists-p file)
487             (princ (concat "  Removing " file "..."))
488             (condition-case nil
489                 (progn
490                   (delete-file file)
491                   (princ "done\n"))
492               (error (princ "failed\n")))))
493         (setq path (cdr path))))))
494
495 ;;; dgnushack.el ends here