(gnus-group-completing-read): Remove all newlines from group names. They mess up...
[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 (when (featurep 'xemacs)
241   (defadvice byte-optimize-apply (before use-mapcan (form) activate)
242     "Replace (apply 'nconc (mapcar ...)) with (mapcan ...)."
243     (let ((last (nth (1- (length form)) form)))
244       (when (and (eq last (third form))
245                  (consp last)
246                  (eq 'mapcar (car last))
247                  (equal (nth 1 form) ''nconc))
248         (setq form (cons 'mapcan (cdr last)))))))
249
250 (defun dgnushack-compile-verbosely ()
251   "Call dgnushack-compile with warnings ENABLED.  If you are compiling
252 patches to gnus, you should consider modifying make.bat to call
253 dgnushack-compile-verbosely.  All other users should continue to use
254 dgnushack-compile."
255   (dgnushack-compile t))
256
257 (defun dgnushack-compile (&optional warn)
258   ;;(setq byte-compile-dynamic t)
259   (unless warn
260     (setq byte-compile-warnings
261           '(free-vars unresolved callargs redefine suspicious)))
262   (let ((files (directory-files srcdir nil "^[^=].*\\.el$"))
263         ;;(byte-compile-generate-call-tree t)
264         file elc)
265     ;; Avoid barfing (from gnus-xmas) because the etc directory is not yet
266     ;; installed.
267     (when (featurep 'xemacs)
268       (setq gnus-xmas-glyph-directory "dummy"))
269     (dolist (file '(".dir-locals.el" "dgnushack.el" "lpath.el"))
270       (setq files (delete file files)))
271     (when (featurep 'base64)
272       (setq files (delete "base64.el" files)))
273     (condition-case code
274         (require 'w3-parse)
275       (error
276        (message "No w3: %s %s" (cadr code) (or (locate-library "w3-parse") ""))
277        (dolist (file '("webmail.el" "nnwfm.el"))
278          (setq files (delete file files)))))
279     (condition-case code
280         ;; Under XEmacs 21.4 this loads easy-mmode.elc that provides
281         ;; the Emacs functions `propertize' and `replace-regexp-in-string'.
282         (require 'mh-e)
283       (error
284        (message "No mh-e: %s %s" (cadr code) (or (locate-library "mh-e") ""))
285        (setq files (delete "gnus-mh.el" files))))
286     (condition-case code
287         (require 'xml)
288       (error
289        (message "No xml: %s %s" (cadr code) (or (locate-library "xml") ""))
290        (setq files (delete "nnrss.el" files))))
291     (dolist (file
292              (if (featurep 'xemacs)
293                  '("md5.el")
294                '("gnus-xmas.el" "messagexmas.el" "nnheaderxm.el")))
295       (setq files (delete file files)))
296     (unless (and (fboundp 'libxml-parse-html-region)
297                  ;; lpath.el binds it.
298                  (not (eq (symbol-function 'libxml-parse-html-region)
299                           'ignore)))
300       (setq files (delete "shr.el" files)))
301
302     (dolist (file files)
303       (setq file (expand-file-name file srcdir))
304       (when (and (file-exists-p
305                   (setq elc (concat (file-name-nondirectory file) "c")))
306                  (file-newer-than-file-p file elc))
307         (delete-file elc)))
308
309     (while (setq file (pop files))
310       (setq file (expand-file-name file srcdir))
311       (when (or (not (file-exists-p
312                       (setq elc (concat (file-name-nondirectory file) "c"))))
313                 (file-newer-than-file-p file elc))
314         (ignore-errors
315           (byte-compile-file file))))))
316
317 (defun dgnushack-recompile ()
318   (require 'gnus)
319   (byte-recompile-directory "." 0))
320
321 (defvar dgnushack-gnus-load-file
322   (if (featurep 'xemacs)
323       (expand-file-name "auto-autoloads.el")
324     (expand-file-name "gnus-load.el")))
325
326 (defvar dgnushack-cus-load-file
327   (if (featurep 'xemacs)
328       (expand-file-name "custom-load.el")
329     (expand-file-name "cus-load.el")))
330
331 (defun dgnushack-make-cus-load ()
332   (load "cus-dep")
333   (let ((cusload-base-file dgnushack-cus-load-file))
334     (defadvice directory-files (after exclude-dir-locals activate)
335       "Exclude .dir-locals.el file."
336       (dolist (file ad-return-value)
337         (if (string-match "\\(?:\\`\\|/\\)\\.dir-locals\\.el\\'" file)
338             (setq ad-return-value (delete file ad-return-value)))))
339     (unwind-protect
340         (if (fboundp 'custom-make-dependencies)
341             (custom-make-dependencies)
342           (Custom-make-dependencies))
343       (ad-unadvise 'directory-files))
344     (when (featurep 'xemacs)
345       (message "Compiling %s..." dgnushack-cus-load-file)
346       (byte-compile-file dgnushack-cus-load-file))))
347
348 (defun dgnushack-make-auto-load ()
349   (require 'autoload)
350   (let ((generated-autoload-file dgnushack-gnus-load-file)
351         (make-backup-files nil)
352         (autoload-package-name "gnus"))
353     (if (featurep 'xemacs)
354         (if (file-exists-p generated-autoload-file)
355             (delete-file generated-autoload-file))
356       (with-temp-file generated-autoload-file
357         (insert ?\014)))
358     (defadvice directory-files (after exclude-dir-locals activate)
359       "Exclude .dir-locals.el file."
360       (dolist (file ad-return-value)
361         (if (string-match "\\(?:\\`\\|/\\)\\.dir-locals\\.el\\'" file)
362             (setq ad-return-value (delete file ad-return-value)))))
363     (unwind-protect
364         (batch-update-autoloads)
365       (ad-unadvise 'directory-files))))
366
367 (defun dgnushack-make-load ()
368   (unless (featurep 'xemacs)
369     (message "Generating %s..." dgnushack-gnus-load-file)
370     (with-temp-file dgnushack-gnus-load-file
371       (insert-file-contents dgnushack-cus-load-file)
372       (delete-file dgnushack-cus-load-file)
373       (goto-char (point-min))
374       (search-forward ";;; Code:")
375       (forward-line)
376       (delete-region (point-min) (point))
377       (insert "\
378 ;;; gnus-load.el --- automatically extracted custom dependencies and autoload
379 ;;
380 ;;; Code:
381 ")
382       (goto-char (point-max))
383       (if (search-backward "custom-versions-load-alist" nil t)
384           (forward-line -1)
385         (forward-line -1)
386         (while (eq (char-after) ?\;)
387           (forward-line -1))
388         (forward-line))
389       (delete-region (point) (point-max))
390       (insert "\n")
391       ;; smiley-* are duplicated. Remove them all.
392       (let ((point (point)))
393         (insert-file-contents dgnushack-gnus-load-file)
394         (goto-char point)
395         (while (search-forward "smiley-" nil t)
396           (beginning-of-line)
397           (if (looking-at "(autoload ")
398               (delete-region (point) (progn (forward-sexp) (point)))
399             (forward-line))))
400       ;;
401       (goto-char (point-max))
402       (when (search-backward "\n(provide " nil t)
403         (forward-line -1)
404         (delete-region (point) (point-max)))
405       (insert "\
406
407 \(provide 'gnus-load)
408
409 ;;; Local Variables:
410 ;;; version-control: never
411 ;;; no-byte-compile: t
412 ;;; no-update-autoloads: t
413 ;;; End:
414 ;;; gnus-load.el ends here
415 ")
416       ))
417   (message "Compiling %s..." dgnushack-gnus-load-file)
418   (byte-compile-file dgnushack-gnus-load-file)
419   (when (featurep 'xemacs)
420     (message "Creating dummy gnus-load.el...")
421     (with-temp-file (expand-file-name "gnus-load.el")
422       (insert "\
423
424 \(provide 'gnus-load)
425
426 ;;; Local Variables:
427 ;;; version-control: never
428 ;;; no-byte-compile: t
429 ;;; no-update-autoloads: t
430 ;;; End:
431 ;;; gnus-load.el ends here"))))
432
433 (defun dgnushack-find-lisp-shadows (&optional lispdir)
434   "Return a list of directories in which other Gnus installations exist.
435 This function looks for the other Gnus installations which will shadow
436 the new Gnus Lisp modules which have been installed in LISPDIR, using
437 the default `load-path'.  The return value will make sense only when
438 LISPDIR is existent and is listed in the default `load-path'.  Assume
439 LISPDIR will be prepended to `load-path' by a user if the default
440 `load-path' does not contain it."
441   (unless lispdir
442     (setq lispdir (getenv "lispdir")))
443   (when (and lispdir (file-directory-p lispdir))
444     (setq lispdir (file-truename (directory-file-name lispdir)))
445     (let ((indices '("gnus.elc" "gnus.el" "gnus.el.bz2" "gnus.el.gz"
446                      "message.elc" "message.el" "message.el.bz2"
447                      "message.el.gz"))
448           (path (delq nil (mapcar
449                            (lambda (p)
450                              (condition-case nil
451                                  (when (and p (file-directory-p p))
452                                    (file-truename (directory-file-name p)))
453                                (error nil)))
454                            dgnushack-default-load-path)))
455           rest elcs)
456       (while path
457         (setq rest (cons (car path) rest)
458               path (delete (car rest) (cdr path))))
459       (setq path (nreverse (cdr (member lispdir rest)))
460             rest nil)
461       (while path
462         (setq elcs indices)
463         (while elcs
464           (when (file-exists-p (expand-file-name (pop elcs) (car path)))
465             (setq rest (cons (car path) rest)
466                   elcs nil)))
467         (setq path (cdr path)))
468       (prog1
469           (setq path (nreverse rest))
470         (when path
471           (let (print-level print-length)
472             (princ (concat "\n\
473 WARNING: The other Gnus installation" (if (cdr path) "s have" " has") "\
474  been detected in:\n\n  " (mapconcat 'identity path "\n  ") "\n\n\
475 You will need to modify the run-time `load-path', remove them manually,
476 or remove them using `make remove-installed-shadows'.\n\n"))))))))
477
478 (defun dgnushack-remove-lisp-shadows (&optional lispdir)
479   "Remove the other Gnus installations which shadow the recent one."
480   (let ((path (with-temp-buffer
481                 (let ((standard-output (current-buffer)))
482                   (dgnushack-find-lisp-shadows lispdir))))
483         elcs files shadows file)
484     (when path
485       (unless (setq elcs (directory-files srcdir nil "\\.elc\\'"))
486         (error "You should build .elc files first."))
487       (setq files
488             (apply
489              'append
490              (mapcar
491               (lambda (el)
492                 (list (concat el "c") el (concat el ".bz2") (concat el ".gz")))
493               (append
494                (list (file-name-nondirectory dgnushack-gnus-load-file)
495                      (file-name-nondirectory dgnushack-cus-load-file))
496                (mapcar (lambda (elc) (substring elc 0 -1)) elcs)))))
497       (while path
498         (setq shadows files)
499         (while shadows
500           (setq file (expand-file-name (pop shadows) (car path)))
501           (when (file-exists-p file)
502             (princ (concat "  Removing " file "..."))
503             (condition-case nil
504                 (progn
505                   (delete-file file)
506                   (princ "done\n"))
507               (error (princ "failed\n")))))
508         (setq path (cdr path))))))
509
510 ;;; dgnushack.el ends here