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