* dgnushack.el: Don't require `emu'.
[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 (defalias 'device-sound-enabled-p 'ignore)
256 (defalias 'play-sound-file 'ignore)
257 (defalias 'nndb-request-article 'ignore)
258 (defalias 'efs-re-read-dir 'ignore)
259 (defalias 'ange-ftp-re-read-dir 'ignore)
260 (defalias 'define-mail-user-agent 'ignore)
261
262 (defconst dgnushack-unexporting-files
263   (append '("dgnushack.el" "dgnuspath.el" "lpath.el")
264           (unless (or (condition-case code
265                           (require 'w3-forms)
266                         (error
267                          (message "No w3: %s %s retrying..." code
268                                   (locate-library "w3-forms"))
269                          nil))
270                       ;; Maybe mis-configured Makefile is used (e.g.
271                       ;; configured for FSFmacs but XEmacs is running).
272                       (let ((lp (delete dgnushack-w3-directory
273                                         (copy-sequence load-path))))
274                         (if (let ((load-path lp))
275                               (condition-case nil
276                                   (require 'w3-forms)
277                                 (error nil)))
278                             ;; If success, fix `load-path' for compiling.
279                             (progn
280                               (setq load-path lp)
281                               (message " => fixed; W3DIR=%s"
282                                        (file-name-directory
283                                         (locate-library "w3-forms")))
284                               t)
285                           (message " => ignored")
286                           nil)))
287             '("nnweb.el" "nnlistserv.el" "nnultimate.el"
288               "nnslashdot.el" "nnwarchive.el" "webmail.el"
289               "nnwfm.el"))
290           (condition-case nil
291               (progn (require 'bbdb) nil)
292             (error '("gnus-bbdb.el")))
293           (unless (featurep 'xemacs)
294             '("gnus-xmas.el" "gnus-picon.el" "messagexmas.el"
295               "nnheaderxm.el" "smiley.el"))
296           (when (or (featurep 'xemacs) (<= emacs-major-version 20))
297             '("smiley-ems.el"))
298           (when (and (fboundp 'base64-decode-string)
299                      (subrp (symbol-function 'base64-decode-string)))
300             '("base64.el"))
301           (when (and (fboundp 'md5) (subrp (symbol-function 'md5)))
302             '("md5.el")))
303   "Files which will not be installed.")
304
305 (defconst dgnushack-exporting-files
306   (let ((files (directory-files srcdir nil "^[^=].*\\.el$" t)))
307     (dolist (file dgnushack-unexporting-files)
308       (setq files (delete file files)))
309     (sort files 'string-lessp))
310   "Files which will be compiled and installed.")
311
312 (defun dgnushack-exporting-files ()
313   "Print name of files which will be installed."
314   (princ (mapconcat 'identity dgnushack-exporting-files " ")))
315
316 (defun dgnushack-compile (&optional warn)
317   ;;(setq byte-compile-dynamic t)
318   (unless warn
319     (setq byte-compile-warnings
320           '(free-vars unresolved callargs redefine)))
321   (unless (locate-library "cus-edit")
322     (error "You do not seem to have Custom installed.
323 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
324 You also then need to add the following to the lisp/dgnushack.el file:
325
326      (push \"~/lisp/custom\" load-path)
327
328 Modify to suit your needs."))
329
330   ;; Show `load-path'.
331   (message "load-path=(\"%s\")"
332            (mapconcat 'identity load-path "\"\n           \""))
333
334   (dolist (file dgnushack-exporting-files)
335     (setq file (expand-file-name file srcdir))
336     (when (and (file-exists-p
337                 (setq elc (concat (file-name-nondirectory file) "c")))
338                (file-newer-than-file-p file elc))
339       (delete-file elc)))
340
341   (let ((files dgnushack-exporting-files)
342         ;;(byte-compile-generate-call-tree t)
343         file elc)
344     ;; Avoid barfing (from gnus-xmas) because the etc directory is not yet
345     ;; installed.
346     (when (featurep 'xemacs)
347       (setq gnus-xmas-glyph-directory "dummy"))
348     (while (setq file (pop files))
349       (setq file (expand-file-name file srcdir))
350       (when (or (not (file-exists-p
351                       (setq elc (concat (file-name-nondirectory file) "c"))))
352                 (file-newer-than-file-p file elc))
353         (ignore-errors
354           (byte-compile-file file))))))
355
356 (defun dgnushack-recompile ()
357   (require 'gnus)
358   (byte-recompile-directory "." 0))
359
360 (defvar dgnushack-gnus-load-file (expand-file-name "gnus-load.el" srcdir))
361 (defvar dgnushack-cus-load-file (expand-file-name "cus-load.el" srcdir))
362 (defvar dgnushack-auto-load-file (expand-file-name "auto-autoloads.el" srcdir))
363
364 (defun dgnushack-make-cus-load ()
365   (when (condition-case nil
366             (load "cus-dep")
367           (error nil))
368     (let ((cusload-base-file dgnushack-cus-load-file))
369       (if (fboundp 'custom-make-dependencies)
370           (custom-make-dependencies)
371         (Custom-make-dependencies)))))
372
373 (defun dgnushack-make-auto-load ()
374   (require 'autoload)
375   (let ((generated-autoload-file dgnushack-gnus-load-file)
376         (make-backup-files nil)
377         (autoload-package-name "gnus"))
378     (if (featurep 'xemacs)
379         (progn
380           (if (file-exists-p generated-autoload-file)
381               (delete-file generated-autoload-file))
382           (if (file-exists-p dgnushack-auto-load-file)
383               (delete-file dgnushack-auto-load-file)))
384       (with-temp-file generated-autoload-file
385         (insert ?\014)))
386     (if (featurep 'xemacs)
387         (let ((si:message (symbol-function 'message)))
388           (defun message (fmt &rest args)
389             (cond ((and (string-equal "Generating autoloads for %s..." fmt)
390                         (file-exists-p (file-name-nondirectory (car args))))
391                    (funcall si:message
392                             fmt (file-name-nondirectory (car args))))
393                   ((string-equal "No autoloads found in %s" fmt))
394                   ((string-equal "Generating autoloads for %s...done" fmt))
395                   (t (apply si:message fmt args))))
396           (unwind-protect
397               (batch-update-autoloads)
398             (fset 'message si:message)))
399       (batch-update-autoloads))))
400
401 (defun dgnushack-make-load ()
402   (message (format "Generating %s..." dgnushack-gnus-load-file))
403   (with-temp-file dgnushack-gnus-load-file
404     (if (file-exists-p dgnushack-cus-load-file)
405         (progn
406           (insert-file-contents dgnushack-cus-load-file)
407           (delete-file dgnushack-cus-load-file)
408           (goto-char (point-min))
409           (search-forward ";;; Code:")
410           (forward-line)
411           (delete-region (point-min) (point))
412           (insert "\
413 ;;; gnus-load.el --- automatically extracted custom dependencies and autoload
414 ;;
415 ;;; Code:
416 ")
417           (goto-char (point-max))
418           (if (search-backward "custom-versions-load-alist" nil t)
419               (forward-line -1)
420             (forward-line -1)
421             (while (eq (char-after) ?\;)
422               (forward-line -1))
423             (forward-line))
424           (delete-region (point) (point-max))
425           (insert "\n"))
426       (insert "\
427 ;;; gnus-load.el --- automatically extracted autoload
428 ;;
429 ;;; Code:
430 "))
431     ;; smiley-* are duplicated. Remove them all.
432     (let ((point (point)))
433       (insert-file-contents dgnushack-gnus-load-file)
434       (goto-char point)
435       (while (search-forward "smiley-" nil t)
436         (beginning-of-line)
437         (if (looking-at "(autoload ")
438             (delete-region (point) (progn (forward-sexp) (point)))
439           (forward-line))))
440     ;;
441     (goto-char (point-max))
442     (when (search-backward "\n(provide " nil t)
443       (forward-line -1)
444       (delete-region (point) (point-max)))
445     (insert "\
446
447 \(provide 'gnus-load)
448
449 ;;; Local Variables:
450 ;;; version-control: never
451 ;;; no-byte-compile: t
452 ;;; no-update-autoloads: t
453 ;;; End:
454 ;;; gnus-load.el ends here\n"))
455   (message (format "Compiling %s..." dgnushack-gnus-load-file))
456   (byte-compile-file dgnushack-gnus-load-file))
457
458 \f
459 (defconst dgnushack-info-file-regexp-en
460   (let ((names '("gnus" "message" "emacs-mime"))
461         regexp name)
462     (while (setq name (pop names))
463       (setq regexp (concat regexp "^" name "\\.info\\(-[0-9]+\\)?$"
464                            (when names "\\|"))))
465     regexp)
466   "Regexp matching English info files.")
467
468 (defconst dgnushack-info-file-regexp-ja
469   (let ((names '("gnus-ja" "message-ja"))
470         regexp name)
471     (while (setq name (pop names))
472       (setq regexp (concat regexp "^" name "\\.info\\(-[0-9]+\\)?$"
473                            (when names "\\|"))))
474     regexp)
475   "Regexp matching Japanese info files.")
476
477 (defun dgnushack-remove-extra-files-in-package ()
478   "Remove extra files in the lisp directory of the XEmacs package."
479   (let ((lisp-dir (expand-file-name (concat "lisp/"
480                                             ;; GNUS_PRODUCT_NAME
481                                             (cadr command-line-args-left)
482                                             "/")
483                                     ;; PACKAGEDIR
484                                     (car command-line-args-left))))
485     (when (file-directory-p lisp-dir)
486       (let (files)
487         (dolist (file dgnushack-exporting-files)
488           (setq files (nconc files (list file (concat file "c")))))
489         (dolist (file (directory-files lisp-dir nil nil t t))
490           (unless (member file files)
491             (setq file (expand-file-name file lisp-dir))
492             (message "Removing %s..." file)
493             (condition-case nil
494                 (delete-file file)
495               (error nil))))))))
496
497 (defun dgnushack-install-package-manifest ()
498   "Install MANIFEST file as an XEmacs package."
499   (let* ((package-dir (car command-line-args-left))
500          (product-name (cadr command-line-args-left))
501          (name (expand-file-name (concat "pkginfo/MANIFEST." product-name)
502                                  package-dir))
503          make-backup-files)
504     (message "Generating %s..." name)
505     (with-temp-file name
506       (insert "pkginfo/MANIFEST." product-name "\n")
507       (let ((lisp-dir (concat "lisp/" product-name "/"))
508             (files (sort (directory-files "." nil "\\.elc?$" t) 'string-lessp))
509             file)
510         (while (setq file (pop files))
511           (unless (member file dgnushack-unexporting-files)
512             (insert lisp-dir file "\n")))
513         (setq files
514               (sort (directory-files "../texi/" nil
515                                      (concat dgnushack-info-file-regexp-en
516                                              "\\|"
517                                              dgnushack-info-file-regexp-ja)
518                                      t)
519                     'string-lessp))
520         (while (setq file (pop files))
521           (insert "info/" file "\n"))))))
522
523 \f
524 (define-compiler-macro describe-key-briefly (&whole form key &optional insert)
525   (if (condition-case nil
526           (progn
527             (describe-key-briefly '((())) nil)
528             t)
529         (wrong-number-of-arguments nil);; Old Emacsen.
530         (error t))
531       form
532     (if insert
533         `(if ,insert
534              (insert (funcall 'describe-key-briefly ,key))
535            (funcall 'describe-key-briefly ,key))
536       `(funcall 'describe-key-briefly ,key))))
537
538 ;;; dgnushack.el ends here