(mapc): Add the compiler macro for Emacs 20.
[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 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Version: 4.19
7 ;; Keywords: news, path
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (defalias 'facep 'ignore)
31
32 (require 'cl)
33
34 (defvar srcdir (or (getenv "srcdir") "."))
35
36 (defun my-getenv (str)
37   (let ((val (getenv str)))
38     (if (equal val "no") nil val)))
39
40 (if (my-getenv "lispdir")
41     (push (my-getenv "lispdir") load-path))
42
43 (push (or (my-getenv "URLDIR") (expand-file-name "../../url/lisp/" srcdir))
44       load-path)
45
46 (push (or (my-getenv "W3DIR") (expand-file-name "../../w3/lisp/" srcdir))
47       load-path)
48
49 ;(push "/usr/share/emacs/site-lisp" load-path)
50
51 ;; Define compiler macros for the functions provided by cl in old Emacsen.
52 (unless (featurep 'xemacs)
53   (define-compiler-macro butlast (&whole form x &optional n)
54     (if (>= emacs-major-version 21)
55         form
56       (if n
57           `(let ((x ,x)
58                  (n ,n))
59              (if (and n (<= n 0))
60                  x
61                (let ((m (length x)))
62                  (or n (setq n 1))
63                  (and (< n m)
64                       (progn
65                         (if (> n 0)
66                             (progn
67                               (setq x (copy-sequence x))
68                               (setcdr (nthcdr (- (1- m) n) x) nil)))
69                         x)))))
70         `(let* ((x ,x)
71                 (m (length x)))
72            (and (< 1 m)
73                 (progn
74                   (setq x (copy-sequence x))
75                   (setcdr (nthcdr (- m 2) x) nil)
76                   x))))))
77
78   (define-compiler-macro remove (&whole form item seq)
79     (if (>= emacs-major-version 21)
80         form
81       `(delete ,item (copy-sequence ,seq))))
82
83   (define-compiler-macro mapc (&whole form fn seq &rest rest)
84     (if (>= emacs-major-version 21)
85         form
86       (if rest
87           `(let* ((fn ,fn)
88                   (seq ,seq)
89                   (args (list seq ,@rest))
90                   (m (apply (function min) (mapcar (function length) args)))
91                   (n 0))
92              (while (< n m)
93                (apply fn (mapcar (function (lambda (arg) (nth n arg))) args))
94                (setq n (1+ n)))
95              seq)
96         `(let ((seq ,seq))
97            (mapcar ,fn seq)
98            seq)))))
99
100 ;; If we are building w3 in a different directory than the source
101 ;; directory, we must read *.el from source directory and write *.elc
102 ;; into the building directory.  For that, we define this function
103 ;; before loading bytecomp.  Bytecomp doesn't overwrite this function.
104 (defun byte-compile-dest-file (filename)
105   "Convert an Emacs Lisp source file name to a compiled file name.
106  In addition, remove directory name part from FILENAME."
107   (setq filename (byte-compiler-base-file-name filename))
108   (setq filename (file-name-sans-versions filename))
109   (setq filename (file-name-nondirectory filename))
110   (if (memq system-type '(win32 w32 mswindows windows-nt))
111       (setq filename (downcase filename)))
112   (cond ((eq system-type 'vax-vms)
113          (concat (substring filename 0 (string-match ";" filename)) "c"))
114         ((string-match emacs-lisp-file-regexp filename)
115          (concat (substring filename 0 (match-beginning 0)) ".elc"))
116         (t (concat filename ".elc"))))
117
118 (require 'bytecomp)
119 ;; To avoid having defsubsts and inlines happen.
120 ;(if (featurep 'xemacs)
121 ;    (require 'byte-optimize)
122 ;  (require 'byte-opt))
123 ;(defun byte-optimize-inline-handler (form)
124 ;  "byte-optimize-handler for the `inline' special-form."
125 ;  (cons 'progn (cdr form)))
126 ;(defalias 'byte-compile-file-form-defsubst 'byte-compile-file-form-defun)
127
128 (push srcdir load-path)
129 (load (expand-file-name "lpath.el" srcdir) nil t)
130
131 (defalias 'device-sound-enabled-p 'ignore)
132 (defalias 'play-sound-file 'ignore)
133 (defalias 'nndb-request-article 'ignore)
134 (defalias 'efs-re-read-dir 'ignore)
135 (defalias 'ange-ftp-re-read-dir 'ignore)
136 (defalias 'define-mail-user-agent 'ignore)
137
138 (eval-and-compile
139   (unless (featurep 'xemacs)
140     (defalias 'get-popup-menu-response 'ignore)
141     (defalias 'event-object 'ignore)
142     (defalias 'x-defined-colors 'ignore)
143     (defalias 'read-color 'ignore)))
144
145 (eval-and-compile
146   (when (featurep 'xemacs)
147     ;; XEmacs 21.1 needs some extra hand holding
148     (when (eq emacs-minor-version 1)
149       (autoload 'custom-declare-face "cus-face" nil t)
150       (autoload 'cl-compile-time-init "cl-macs" nil t)
151       (autoload 'defadvice "advice" nil nil 'macro))
152     (unless (fboundp 'defadvice)
153       (autoload 'defadvice "advice" nil nil 'macro))
154     (autoload 'Info-directory "info" nil t)
155     (autoload 'Info-menu "info" nil t)
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 'customize-apropos "cus-edit" nil t)
162     (autoload 'customize-save-variable "cus-edit" nil t)
163     (autoload 'customize-variable "cus-edit" nil t)
164     (autoload 'delete-annotation "annotations")
165     (autoload 'dolist "cl-macs" nil nil 'macro)
166     (autoload 'enriched-decode "enriched")
167     (autoload 'info "info" nil t)
168     (autoload 'make-annotation "annotations")
169     (autoload 'make-display-table "disp-table")
170     (autoload 'pp "pp")
171     (autoload 'ps-despool "ps-print" nil t)
172     (autoload 'ps-spool-buffer "ps-print" nil t)
173     (autoload 'ps-spool-buffer-with-faces "ps-print" nil t)
174     (autoload 'read-passwd "passwd")
175     (autoload 'regexp-opt "regexp-opt")
176     (autoload 'reporter-submit-bug-report "reporter")
177     (if (emacs-version>= 21 5)
178         (autoload 'setenv "process" nil t)
179       (autoload 'setenv "env" nil t))
180     (autoload 'smtpmail-send-it "smtpmail")
181     (autoload 'sort-numeric-fields "sort" nil t)
182     (autoload 'sort-subr "sort")
183     (autoload 'trace-function-background "trace" nil t)
184     (autoload 'w3-do-setup "w3")
185     (autoload 'w3-prepare-buffer "w3-display")
186     (autoload 'w3-region "w3-display" nil t)
187     (defalias 'frame-char-height 'frame-height)
188     (defalias 'frame-char-width 'frame-width)
189     (defalias 'frame-parameter 'frame-property)
190     (defalias 'make-overlay 'ignore)
191     (defalias 'overlay-end 'ignore)
192     (defalias 'overlay-get 'ignore)
193     (defalias 'overlay-put 'ignore)
194     (defalias 'overlay-start 'ignore)
195     (defalias 'overlays-in 'ignore)
196     (defalias 'replace-dehighlight 'ignore)
197     (defalias 'replace-highlight 'ignore)
198     (defalias 'run-with-idle-timer 'ignore)
199     (defalias 'w3-coding-system-for-mime-charset 'ignore)))
200
201 (defun dgnushack-compile-verbosely ()
202   "Call dgnushack-compile with warnings ENABLED.  If you are compiling
203 patches to gnus, you should consider modifying make.bat to call
204 dgnushack-compile-verbosely.  All other users should continue to use
205 dgnushack-compile."
206   (dgnushack-compile t))
207
208 (defun dgnushack-compile (&optional warn)
209   ;;(setq byte-compile-dynamic t)
210   (unless warn
211     (setq byte-compile-warnings
212           '(free-vars unresolved callargs redefine)))
213   (unless (locate-library "cus-edit")
214     (error "You do not seem to have Custom installed.
215 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
216 You also then need to add the following to the lisp/dgnushack.el file:
217
218      (push \"~/lisp/custom\" load-path)
219
220 Modify to suit your needs."))
221   (let ((files (directory-files srcdir nil "^[^=].*\\.el$"))
222         ;;(byte-compile-generate-call-tree t)
223         file elc)
224     ;; Avoid barfing (from gnus-xmas) because the etc directory is not yet
225     ;; installed.
226     (when (featurep 'xemacs)
227       (setq gnus-xmas-glyph-directory "dummy"))
228     (dolist (file '("dgnushack.el" "lpath.el"))
229       (setq files (delete file files)))
230     (when (featurep 'base64)
231       (setq files (delete "base64.el" files)))
232     (condition-case code
233         (require 'w3-parse)
234       (error
235        (message "No w3: %s %s" (cadr code) (or (locate-library "w3-parse") ""))
236        (dolist (file '("nnultimate.el" "webmail.el" "nnwfm.el"))
237          (setq files (delete file files)))))
238     (condition-case code
239         (require 'mh-e)
240       (error
241        (message "No mh-e: %s %s" (cadr code) (or (locate-library "mh-e") ""))
242        (setq files (delete "gnus-mh.el" files))))
243     (condition-case code
244         (require 'xml)
245       (error
246        (message "No xml: %s %s" (cadr code) (or (locate-library "xml") ""))
247        (setq files (delete "nnrss.el" files))))
248     (dolist (file
249              (if (featurep 'xemacs)
250                  '("md5.el")
251                '("gnus-xmas.el" "messagexmas.el" "nnheaderxm.el")))
252       (setq files (delete file files)))
253
254     (dolist (file files)
255       (setq file (expand-file-name file srcdir))
256       (when (and (file-exists-p
257                   (setq elc (concat (file-name-nondirectory file) "c")))
258                  (file-newer-than-file-p file elc))
259         (delete-file elc)))
260
261     (while (setq file (pop files))
262       (setq file (expand-file-name file srcdir))
263       (when (or (not (file-exists-p
264                       (setq elc (concat (file-name-nondirectory file) "c"))))
265                 (file-newer-than-file-p file elc))
266         (ignore-errors
267           (byte-compile-file file))))))
268
269 (defun dgnushack-recompile ()
270   (require 'gnus)
271   (byte-recompile-directory "." 0))
272
273 (defvar dgnushack-gnus-load-file
274   (if (featurep 'xemacs)
275       (expand-file-name "auto-autoloads.el")
276     (expand-file-name "gnus-load.el")))
277
278 (defvar dgnushack-cus-load-file 
279   (if (featurep 'xemacs)
280       (expand-file-name "custom-load.el")
281     (expand-file-name "cus-load.el")))
282
283 (defun dgnushack-make-cus-load ()
284   (load "cus-dep")
285   (let ((cusload-base-file dgnushack-cus-load-file))
286     (if (fboundp 'custom-make-dependencies)
287         (custom-make-dependencies)
288       (Custom-make-dependencies))
289     (when (featurep 'xemacs)
290       (message "Compiling %s..." dgnushack-cus-load-file)
291       (byte-compile-file dgnushack-cus-load-file))))
292
293 (defun dgnushack-make-auto-load ()
294   (require 'autoload)
295   (unless (make-autoload '(define-derived-mode child parent name
296                             "docstring" body)
297                          "file")
298     (defadvice make-autoload (around handle-define-derived-mode activate)
299       "Handle `define-derived-mode'."
300       (if (eq (car-safe (ad-get-arg 0)) 'define-derived-mode)
301           (setq ad-return-value
302                 (list 'autoload
303                       (list 'quote (nth 1 (ad-get-arg 0)))
304                       (ad-get-arg 1)
305                       (nth 4 (ad-get-arg 0))
306                       t nil))
307         ad-do-it))
308     (put 'define-derived-mode 'doc-string-elt 3))
309   (let ((generated-autoload-file dgnushack-gnus-load-file)
310         (make-backup-files nil)
311         (autoload-package-name "gnus"))
312     (if (featurep 'xemacs)
313         (if (file-exists-p generated-autoload-file)
314             (delete-file generated-autoload-file))
315       (with-temp-file generated-autoload-file
316         (insert ?\014)))
317     (batch-update-autoloads)))
318
319 (defun dgnushack-make-load ()
320   (unless (featurep 'xemacs)
321     (message "Generating %s..." dgnushack-gnus-load-file)
322     (with-temp-file dgnushack-gnus-load-file
323       (insert-file-contents dgnushack-cus-load-file)
324       (delete-file dgnushack-cus-load-file)
325       (goto-char (point-min))
326       (search-forward ";;; Code:")
327       (forward-line)
328       (delete-region (point-min) (point))
329       (insert "\
330 ;;; gnus-load.el --- automatically extracted custom dependencies and autoload
331 ;;
332 ;;; Code:
333 ")
334       (goto-char (point-max))
335       (if (search-backward "custom-versions-load-alist" nil t)
336           (forward-line -1)
337         (forward-line -1)
338         (while (eq (char-after) ?\;)
339           (forward-line -1))
340         (forward-line))
341       (delete-region (point) (point-max))
342       (insert "\n")
343       ;; smiley-* are duplicated. Remove them all.
344       (let ((point (point)))
345         (insert-file-contents dgnushack-gnus-load-file)
346         (goto-char point)
347         (while (search-forward "smiley-" nil t)
348           (beginning-of-line)
349           (if (looking-at "(autoload ")
350               (delete-region (point) (progn (forward-sexp) (point)))
351             (forward-line))))
352       ;;
353       (goto-char (point-max))
354       (when (search-backward "\n(provide " nil t)
355         (forward-line -1)
356         (delete-region (point) (point-max)))
357       (insert "\
358
359 \(provide 'gnus-load)
360
361 ;;; Local Variables:
362 ;;; version-control: never
363 ;;; no-byte-compile: t
364 ;;; no-update-autoloads: t
365 ;;; End:
366 ;;; gnus-load.el ends here
367 ")
368       ))
369   (message "Compiling %s..." dgnushack-gnus-load-file)
370   (byte-compile-file dgnushack-gnus-load-file)
371   (when (featurep 'xemacs)
372     (message "Creating dummy gnus-load.el...")
373     (with-temp-file (expand-file-name "gnus-load.el")
374       (insert "\
375
376 \(provide 'gnus-load)
377
378 ;;; Local Variables:
379 ;;; version-control: never
380 ;;; no-byte-compile: t
381 ;;; no-update-autoloads: t
382 ;;; End:
383 ;;; gnus-load.el ends here"))))
384
385
386 ;;; dgnushack.el ends here