b582dbf4729743f844a60dd1f53ce7830617e72c
[elisp/gnus.git-] / 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
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      Katsumi Yamaoka <yamaoka@jpl.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 2, 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., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 ;; Set coding priority of Shift-JIS to the bottom.
32 (if (featurep 'xemacs)
33     (defalias 'set-coding-priority 'ignore)
34   (defalias 'coding-priority-list 'ignore)
35   (defalias 'set-coding-priority-list 'ignore))
36 (cond ((and (featurep 'xemacs) (featurep 'mule))
37        (if (memq 'shift-jis (coding-priority-list))
38            (set-coding-priority-list
39             (append (delq 'shift-jis (coding-priority-list)) '(shift-jis)))))
40       ((boundp 'MULE)
41        (put '*coding-category-sjis* 'priority (length *predefined-category*)))
42       ((featurep 'mule)
43        (if (memq 'coding-category-sjis coding-category-list)
44            (set-coding-priority
45             (append (delq 'coding-category-sjis
46                           (copy-sequence coding-category-list))
47                     '(coding-category-sjis))))))
48
49 (defalias 'facep 'ignore)
50
51 (require 'cl)
52
53 (defvar srcdir (or (getenv "srcdir") "."))
54
55 (let ((urldir (getenv "URLDIR")))
56   (unless (zerop (length urldir))
57     (push (file-name-as-directory urldir) load-path)))
58
59 (defvar dgnushack-w3-directory (let ((w3dir (getenv "W3DIR")))
60                                  (unless (zerop (length w3dir))
61                                    (file-name-as-directory w3dir))))
62 (when dgnushack-w3-directory
63   (push dgnushack-w3-directory load-path))
64
65 ;; If we are building w3 in a different directory than the source
66 ;; directory, we must read *.el from source directory and write *.elc
67 ;; into the building directory.  For that, we define this function
68 ;; before loading bytecomp.  Bytecomp doesn't overwrite this function.
69 (defun byte-compile-dest-file (filename)
70   "Convert an Emacs Lisp source file name to a compiled file name.
71  In addition, remove directory name part from FILENAME."
72   (setq filename (byte-compiler-base-file-name filename))
73   (setq filename (file-name-sans-versions filename))
74   (setq filename (file-name-nondirectory filename))
75   (if (memq system-type '(win32 w32 mswindows windows-nt))
76       (setq filename (downcase filename)))
77   (cond ((eq system-type 'vax-vms)
78          (concat (substring filename 0 (string-match ";" filename)) "c"))
79         ((string-match emacs-lisp-file-regexp filename)
80          (concat (substring filename 0 (match-beginning 0)) ".elc"))
81         (t (concat filename ".elc"))))
82
83 (require 'bytecomp)
84
85 (unless (fboundp 'si:byte-optimize-form-code-walker)
86   (byte-optimize-form nil);; Load `byte-opt' or `byte-optimize'.
87   (setq max-specpdl-size 3000)
88   (defalias 'si:byte-optimize-form-code-walker
89     (symbol-function 'byte-optimize-form-code-walker))
90   (defun byte-optimize-form-code-walker (form for-effect)
91     (if (and for-effect (memq (car-safe form) '(and or)))
92         ;; Fix bug in and/or forms.
93         (let ((fn (car form))
94               (backwards (reverse (cdr form))))
95           (while (and backwards
96                       (null (setcar backwards
97                                     (byte-optimize-form (car backwards) t))))
98             (setq backwards (cdr backwards)))
99           (if (and (cdr form) (null backwards))
100               (byte-compile-log
101                "  all subforms of %s called for effect; deleted" form))
102           (if backwards
103               (let ((head backwards))
104                 (while (setq backwards (cdr backwards))
105                   (setcar backwards (byte-optimize-form (car backwards)
106                                                         nil)))
107                 (cons fn (nreverse head)))))
108       (si:byte-optimize-form-code-walker form for-effect)))
109   (byte-compile 'byte-optimize-form-code-walker))
110
111 (load (expand-file-name "gnus-clfns.el" srcdir) nil t t)
112
113 (condition-case nil
114     (char-after)
115   (wrong-number-of-arguments
116    ;; Optimize byte code for `char-after'.
117    (put 'char-after 'byte-optimizer 'byte-optimize-char-after)
118    (defun byte-optimize-char-after (form)
119      (if (null (cdr form))
120          '(char-after (point))
121        form))))
122
123 (condition-case nil
124     (char-before)
125   (wrong-number-of-arguments
126    ;; Optimize byte code for `char-before'.
127    (put 'char-before 'byte-optimizer 'byte-optimize-char-before)
128    (defun byte-optimize-char-before (form)
129      (if (null (cdr form))
130          '(char-before (point))
131        form))))
132
133 (load (expand-file-name "dgnuspath.el" srcdir) nil nil t)
134
135 (condition-case err
136     (load "~/.lpath.el" t nil t)
137   (error (message "Error in \"~/.lpath.el\" file: %s" err)))
138
139 ;; Don't load path-util until `char-after' and `char-before' have been
140 ;; optimized because it requires `poe' and then modify the functions.
141 (or (featurep 'path-util)
142     (load "apel/path-util"))
143 (add-path "apel")
144 (add-path "flim")
145 (unless (module-installed-p 'mel)
146   ;; FLIM 1.14 may have installed in two "flim" subdirectories.
147   (push (expand-file-name "flim"
148                           (file-name-directory (get-latest-path "^apel$" t)))
149         load-path)
150   (unless (module-installed-p 'mel)
151     (error "
152 FLIM package does not found in %s.
153 Try to re-configure with --with-addpath=FLIM_PATH and run make again.
154 "
155            (progn
156              (add-path "semi")
157              load-path))))
158 (add-path "semi")
159
160 (push srcdir load-path)
161 (load (expand-file-name "lpath.el" srcdir) nil t t)
162
163 (require 'custom)
164
165 ;; Bind functions defined by `defun-maybe'.
166 (put 'defun-maybe 'byte-hunk-handler 'byte-compile-file-form-defun-maybe)
167 (defun byte-compile-file-form-defun-maybe (form)
168   (if (and (not (fboundp (nth 1 form)))
169            (memq 'unresolved byte-compile-warnings))
170       (setq byte-compile-function-environment
171             (cons (cons (nth 1 form)
172                         (cons 'lambda (cdr (cdr form))))
173                   byte-compile-function-environment)))
174   form)
175
176 (condition-case nil
177     :symbol-for-testing-whether-colon-keyword-is-available-or-not
178   (void-variable
179    ;; Bind keywords.
180    (dolist (keyword '(:button-keymap :data :file :mime-handle
181                                      :key-type :value-type
182                                      :ascent :foreground :help))
183      (set keyword keyword))))
184
185 ;; If you are using Mule 2.3 based on Emacs 19.34, you may also put the
186 ;; following lines in your .emacs file, before gnus related modules are
187 ;; loaded.  It is not always necessary.  However if it is done, you will
188 ;; be able to load or evaluate gnus related *.el (not compiled) files.
189 ;; ------ cut here ------ cut here ------ cut here ------ cut here ------
190 (if (boundp 'MULE)
191     (progn
192       (setq :version ':version
193             :set-after ':set-after)
194       (require 'custom)
195       (defadvice custom-handle-keyword
196         (around dont-signal-an-error-even-if-unsupported-keyword-is-given
197                 activate)
198         "Don't signal an error even if unsupported keyword is given."
199         (if (not (memq (ad-get-arg 1) '(:version :set-after)))
200             ad-do-it))))
201 ;; ------ cut here ------ cut here ------ cut here ------ cut here ------
202
203 (when (boundp 'MULE)
204   (put 'custom-declare-face 'byte-optimizer
205        'byte-optimize-ignore-unsupported-custom-keywords)
206   (put 'custom-declare-group 'byte-optimizer
207        'byte-optimize-ignore-unsupported-custom-keywords)
208   (defun byte-optimize-ignore-unsupported-custom-keywords (form)
209     (if (or (memq ':version (nthcdr 4 form))
210             (memq ':set-after (nthcdr 4 form)))
211         (let ((newform (list (car form) (nth 1 form)
212                              (nth 2 form) (nth 3 form)))
213               (args (nthcdr 4 form)))
214           (while args
215             (or (memq (car args) '(:version :set-after))
216                 (setq newform (nconc newform (list (car args)
217                                                    (car (cdr args))))))
218             (setq args (cdr (cdr args))))
219           newform)
220       form))
221
222   (put 'custom-declare-variable 'byte-hunk-handler
223        'byte-compile-file-form-custom-declare-variable)
224   (defun byte-compile-file-form-custom-declare-variable (form)
225     ;; Bind defcustom'ed variables.
226     (if (memq 'free-vars byte-compile-warnings)
227         (setq byte-compile-bound-variables
228               (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
229     (if (memq ':version (nthcdr 4 form))
230         ;; Make the variable uncustomizable.
231         `(defvar ,(nth 1 (nth 1 form)) ,(nth 1 (nth 2 form))
232            ,(substring (nth 3 form) (if (string-match "^[\t *]+" (nth 3 form))
233                                         (match-end 0)
234                                       0)))
235       ;; Ignore unsupported keyword(s).
236       (if (memq ':set-after (nthcdr 4 form))
237           (let ((newform (list (car form) (nth 1 form)
238                                (nth 2 form) (nth 3 form)))
239                 (args (nthcdr 4 form)))
240             (while args
241               (or (eq (car args) ':set-after)
242                   (setq newform (nconc newform (list (car args)
243                                                      (car (cdr args))))))
244               (setq args (cdr (cdr args))))
245             newform)
246         form))))
247
248 ;; Unknown variables and functions.
249 (unless (boundp 'buffer-file-coding-system)
250   (defvar buffer-file-coding-system (symbol-value 'file-coding-system)))
251 (unless (featurep 'xemacs)
252   (defalias 'Custom-make-dependencies 'ignore)
253   (defalias 'update-autoloads-from-directory 'ignore))
254
255 (unless (fboundp 'with-temp-buffer)
256   ;; Pickup some macros.
257   (require 'emu))
258
259 (defalias 'device-sound-enabled-p 'ignore)
260 (defalias 'play-sound-file 'ignore)
261 (defalias 'nndb-request-article 'ignore)
262 (defalias 'efs-re-read-dir 'ignore)
263 (defalias 'ange-ftp-re-read-dir 'ignore)
264 (defalias 'define-mail-user-agent 'ignore)
265
266 (defconst dgnushack-unexporting-files
267   (append '("dgnushack.el" "dgnuspath.el" "lpath.el")
268           (unless (or (condition-case code
269                           (require 'w3-forms)
270                         (error
271                          (message "No w3: %s %s retrying..." code
272                                   (locate-library "w3-forms"))
273                          nil))
274                       ;; Maybe mis-configured Makefile is used (e.g.
275                       ;; configured for FSFmacs but XEmacs is running).
276                       (let ((lp (delete dgnushack-w3-directory
277                                         (copy-sequence load-path))))
278                         (if (let ((load-path lp))
279                               (condition-case nil
280                                   (require 'w3-forms)
281                                 (error nil)))
282                             ;; If success, fix `load-path' for compiling.
283                             (progn
284                               (setq load-path lp)
285                               (message " => fixed; W3DIR=%s"
286                                        (file-name-directory
287                                         (locate-library "w3-forms")))
288                               t)
289                           (message " => ignored")
290                           nil)))
291             '("nnweb.el" "nnlistserv.el" "nnultimate.el"
292               "nnslashdot.el" "nnwarchive.el" "webmail.el"
293               "nnwfm.el"))
294           (condition-case nil
295               (progn (require 'bbdb) nil)
296             (error '("gnus-bbdb.el")))
297           (unless (featurep 'xemacs)
298             '("gnus-xmas.el" "gnus-picon.el" "messagexmas.el"
299               "nnheaderxm.el" "smiley.el"))
300           (when (or (featurep 'xemacs) (<= emacs-major-version 20))
301             '("smiley-ems.el"))
302           (when (and (fboundp 'base64-decode-string)
303                      (subrp (symbol-function 'base64-decode-string)))
304             '("base64.el"))
305           (when (and (fboundp 'md5) (subrp (symbol-function 'md5)))
306             '("md5.el")))
307   "Files which will not be installed.")
308
309 (defconst dgnushack-exporting-files
310   (let ((files (directory-files srcdir nil "^[^=].*\\.el$" t)))
311     (dolist (file dgnushack-unexporting-files)
312       (setq files (delete file files)))
313     (sort files 'string-lessp))
314   "Files which will be compiled and installed.")
315
316 (defun dgnushack-exporting-files ()
317   "Print name of files which will be installed."
318   (princ (mapconcat 'identity dgnushack-exporting-files " ")))
319
320 (defun dgnushack-compile (&optional warn)
321   ;;(setq byte-compile-dynamic t)
322   (unless warn
323     (setq byte-compile-warnings
324           '(free-vars unresolved callargs redefine)))
325   (unless (locate-library "cus-edit")
326     (error "You do not seem to have Custom installed.
327 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
328 You also then need to add the following to the lisp/dgnushack.el file:
329
330      (push \"~/lisp/custom\" load-path)
331
332 Modify to suit your needs."))
333
334   ;; Show `load-path'.
335   (message "load-path=(\"%s\")"
336            (mapconcat 'identity load-path "\"\n           \""))
337
338   (dolist (file dgnushack-exporting-files)
339     (setq file (expand-file-name file srcdir))
340     (when (and (file-exists-p
341                 (setq elc (concat (file-name-nondirectory file) "c")))
342                (file-newer-than-file-p file elc))
343       (delete-file elc)))
344
345   (let ((files dgnushack-exporting-files)
346         ;;(byte-compile-generate-call-tree t)
347         file elc)
348     ;; Avoid barfing (from gnus-xmas) because the etc directory is not yet
349     ;; installed.
350     (when (featurep 'xemacs)
351       (setq gnus-xmas-glyph-directory "dummy"))
352     (while (setq file (pop files))
353       (setq file (expand-file-name file srcdir))
354       (when (or (not (file-exists-p
355                       (setq elc (concat (file-name-nondirectory file) "c"))))
356                 (file-newer-than-file-p file elc))
357         (ignore-errors
358           (byte-compile-file file))))))
359
360 (defun dgnushack-recompile ()
361   (require 'gnus)
362   (byte-recompile-directory "." 0))
363
364 (defvar dgnushack-gnus-load-file (expand-file-name "gnus-load.el" srcdir))
365 (defvar dgnushack-cus-load-file (expand-file-name "cus-load.el" srcdir))
366 (defvar dgnushack-auto-load-file (expand-file-name "auto-autoloads.el" srcdir))
367
368 (defun dgnushack-make-cus-load ()
369   (when (condition-case nil
370             (load "cus-dep")
371           (error nil))
372     (let ((cusload-base-file dgnushack-cus-load-file))
373       (if (fboundp 'custom-make-dependencies)
374           (custom-make-dependencies)
375         (Custom-make-dependencies)))))
376
377 (defun dgnushack-make-auto-load ()
378   (require 'autoload)
379   (let ((generated-autoload-file dgnushack-gnus-load-file)
380         (make-backup-files nil)
381         (autoload-package-name "gnus"))
382     (if (featurep 'xemacs)
383         (progn
384           (if (file-exists-p generated-autoload-file)
385               (delete-file generated-autoload-file))
386           (if (file-exists-p dgnushack-auto-load-file)
387               (delete-file dgnushack-auto-load-file)))
388       (with-temp-file generated-autoload-file
389         (insert ?\014)))
390     (if (featurep 'xemacs)
391         (let ((si:message (symbol-function 'message)))
392           (defun message (fmt &rest args)
393             (cond ((and (string-equal "Generating autoloads for %s..." fmt)
394                         (file-exists-p (file-name-nondirectory (car args))))
395                    (funcall si:message
396                             fmt (file-name-nondirectory (car args))))
397                   ((string-equal "No autoloads found in %s" fmt))
398                   ((string-equal "Generating autoloads for %s...done" fmt))
399                   (t (apply si:message fmt args))))
400           (unwind-protect
401               (batch-update-autoloads)
402             (fset 'message si:message)))
403       (batch-update-autoloads))))
404
405 (defun dgnushack-make-load ()
406   (message (format "Generating %s..." dgnushack-gnus-load-file))
407   (with-temp-file dgnushack-gnus-load-file
408     (if (file-exists-p dgnushack-cus-load-file)
409         (progn
410           (insert-file-contents dgnushack-cus-load-file)
411           (delete-file dgnushack-cus-load-file)
412           (goto-char (point-min))
413           (search-forward ";;; Code:")
414           (forward-line)
415           (delete-region (point-min) (point))
416           (insert "\
417 ;;; gnus-load.el --- automatically extracted custom dependencies and autoload
418 ;;
419 ;;; Code:
420 ")
421           (goto-char (point-max))
422           (if (search-backward "custom-versions-load-alist" nil t)
423               (forward-line -1)
424             (forward-line -1)
425             (while (eq (char-after) ?\;)
426               (forward-line -1))
427             (forward-line))
428           (delete-region (point) (point-max))
429           (insert "\n"))
430       (insert "\
431 ;;; gnus-load.el --- automatically extracted autoload
432 ;;
433 ;;; Code:
434 "))
435     ;; smiley-* are duplicated. Remove them all.
436     (let ((point (point)))
437       (insert-file-contents dgnushack-gnus-load-file)
438       (goto-char point)
439       (while (search-forward "smiley-" nil t)
440         (beginning-of-line)
441         (if (looking-at "(autoload ")
442             (delete-region (point) (progn (forward-sexp) (point)))
443           (forward-line))))
444     ;;
445     (goto-char (point-max))
446     (when (search-backward "\n(provide " nil t)
447       (forward-line -1)
448       (delete-region (point) (point-max)))
449     (insert "\
450
451 \(provide 'gnus-load)
452
453 ;;; Local Variables:
454 ;;; version-control: never
455 ;;; no-byte-compile: t
456 ;;; no-update-autoloads: t
457 ;;; End:
458 ;;; gnus-load.el ends here\n"))
459   (message (format "Compiling %s..." dgnushack-gnus-load-file))
460   (byte-compile-file dgnushack-gnus-load-file))
461
462 \f
463 (defconst dgnushack-info-file-regexp-en
464   (let ((names '("gnus" "message" "emacs-mime"))
465         regexp name)
466     (while (setq name (pop names))
467       (setq regexp (concat regexp "^" name "\\.info\\(-[0-9]+\\)?$"
468                            (when names "\\|"))))
469     regexp)
470   "Regexp matching English info files.")
471
472 (defconst dgnushack-info-file-regexp-ja
473   (let ((names '("gnus-ja" "message-ja"))
474         regexp name)
475     (while (setq name (pop names))
476       (setq regexp (concat regexp "^" name "\\.info\\(-[0-9]+\\)?$"
477                            (when names "\\|"))))
478     regexp)
479   "Regexp matching Japanese info files.")
480
481 (defun dgnushack-remove-extra-files-in-package ()
482   "Remove extra files in the lisp directory of the XEmacs package."
483   (let ((lisp-dir (expand-file-name (concat "lisp/"
484                                             ;; GNUS_PRODUCT_NAME
485                                             (cadr command-line-args-left)
486                                             "/")
487                                     ;; PACKAGEDIR
488                                     (car command-line-args-left))))
489     (when (file-directory-p lisp-dir)
490       (let (files)
491         (dolist (file dgnushack-exporting-files)
492           (setq files (nconc files (list file (concat file "c")))))
493         (dolist (file (directory-files lisp-dir nil nil t t))
494           (unless (member file files)
495             (setq file (expand-file-name file lisp-dir))
496             (message "Removing %s..." file)
497             (condition-case nil
498                 (delete-file file)
499               (error nil))))))))
500
501 (defun dgnushack-install-package-manifest ()
502   "Install MANIFEST file as an XEmacs package."
503   (let* ((package-dir (car command-line-args-left))
504          (product-name (cadr command-line-args-left))
505          (name (expand-file-name (concat "pkginfo/MANIFEST." product-name)
506                                  package-dir))
507          make-backup-files)
508     (message "Generating %s..." name)
509     (with-temp-file name
510       (insert "pkginfo/MANIFEST." product-name "\n")
511       (let ((lisp-dir (concat "lisp/" product-name "/"))
512             (files (sort (directory-files "." nil "\\.elc?$" t) 'string-lessp))
513             file)
514         (while (setq file (pop files))
515           (unless (member file dgnushack-unexporting-files)
516             (insert lisp-dir file "\n")))
517         (setq files
518               (sort (directory-files "../texi/" nil
519                                      (concat dgnushack-info-file-regexp-en
520                                              "\\|"
521                                              dgnushack-info-file-regexp-ja)
522                                      t)
523                     'string-lessp))
524         (while (setq file (pop files))
525           (insert "info/" file "\n"))))))
526
527 \f
528 (define-compiler-macro describe-key-briefly (&whole form key &optional insert)
529   (if (condition-case nil
530           (progn
531             (describe-key-briefly '((())) nil)
532             t)
533         (wrong-number-of-arguments nil);; Old Emacsen.
534         (error t))
535       form
536     (if insert
537         `(if ,insert
538              (insert (funcall 'describe-key-briefly ,key))
539            (funcall 'describe-key-briefly ,key))
540       `(funcall 'describe-key-briefly ,key))))
541
542 ;;; dgnushack.el ends here