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