T-gnus 6.14.6; synch up with Gnus v5.8.8.
[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 ;; Bind functions defined by `defun-maybe'.
160 (put 'defun-maybe 'byte-hunk-handler 'byte-compile-file-form-defun-maybe)
161 (defun byte-compile-file-form-defun-maybe (form)
162   (if (and (not (fboundp (nth 1 form)))
163            (memq 'unresolved byte-compile-warnings))
164       (setq byte-compile-function-environment
165             (cons (cons (nth 1 form)
166                         (cons 'lambda (cdr (cdr form))))
167                   byte-compile-function-environment)))
168   form)
169
170 (condition-case nil
171     :symbol-for-testing-whether-colon-keyword-is-available-or-not
172   (void-variable
173    ;; Bind keywords.
174    (dolist (keyword '(:button-keymap :data :file :mime-handle
175                                      :key-type :value-type))
176      (set keyword keyword))))
177
178 ;; If you are using Mule 2.3 based on Emacs 19.34, you may also put the
179 ;; following lines in your .emacs file, before gnus related modules are
180 ;; loaded.  It is not always necessary.  However if it is done, you will
181 ;; be able to load or evaluate gnus related *.el (not compiled) files.
182 ;; ------ cut here ------ cut here ------ cut here ------ cut here ------
183 (if (boundp 'MULE)
184     (progn
185       (setq :version ':version
186             :set-after ':set-after)
187       (require 'custom)
188       (defadvice custom-handle-keyword
189         (around dont-signal-an-error-even-if-unsupported-keyword-is-given
190                 activate)
191         "Don't signal an error even if unsupported keyword is given."
192         (if (not (memq (ad-get-arg 1) '(:version :set-after)))
193             ad-do-it))))
194 ;; ------ cut here ------ cut here ------ cut here ------ cut here ------
195
196 (when (boundp 'MULE)
197   (put 'custom-declare-face 'byte-optimizer
198        'byte-optimize-ignore-unsupported-custom-keywords)
199   (put 'custom-declare-group 'byte-optimizer
200        'byte-optimize-ignore-unsupported-custom-keywords)
201   (defun byte-optimize-ignore-unsupported-custom-keywords (form)
202     (if (or (memq ':version (nthcdr 4 form))
203             (memq ':set-after (nthcdr 4 form)))
204         (let ((newform (list (car form) (nth 1 form)
205                              (nth 2 form) (nth 3 form)))
206               (args (nthcdr 4 form)))
207           (while args
208             (or (memq (car args) '(:version :set-after))
209                 (setq newform (nconc newform (list (car args)
210                                                    (car (cdr args))))))
211             (setq args (cdr (cdr args))))
212           newform)
213       form))
214
215   (put 'custom-declare-variable 'byte-hunk-handler
216        'byte-compile-file-form-custom-declare-variable)
217   (defun byte-compile-file-form-custom-declare-variable (form)
218     ;; Bind defcustom'ed variables.
219     (if (memq 'free-vars byte-compile-warnings)
220         (setq byte-compile-bound-variables
221               (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
222     (if (memq ':version (nthcdr 4 form))
223         ;; Make the variable uncustomizable.
224         `(defvar ,(nth 1 (nth 1 form)) ,(nth 1 (nth 2 form))
225            ,(substring (nth 3 form) (if (string-match "^[\t *]+" (nth 3 form))
226                                         (match-end 0)
227                                       0)))
228       ;; Ignore unsupported keyword(s).
229       (if (memq ':set-after (nthcdr 4 form))
230           (let ((newform (list (car form) (nth 1 form)
231                                (nth 2 form) (nth 3 form)))
232                 (args (nthcdr 4 form)))
233             (while args
234               (or (eq (car args) ':set-after)
235                   (setq newform (nconc newform (list (car args)
236                                                      (car (cdr args))))))
237               (setq args (cdr (cdr args))))
238             newform)
239         form))))
240
241 ;; Unknown variables and functions.
242 (unless (boundp 'buffer-file-coding-system)
243   (defvar buffer-file-coding-system (symbol-value 'file-coding-system)))
244 (unless (featurep 'xemacs)
245   (defalias 'Custom-make-dependencies 'ignore)
246   (defalias 'update-autoloads-from-directory 'ignore))
247 (autoload 'texinfo-parse-line-arg "texinfmt")
248
249 (unless (fboundp 'with-temp-buffer)
250   ;; Pickup some macros.
251   (require 'emu))
252
253 (defalias 'device-sound-enabled-p 'ignore)
254 (defalias 'play-sound-file 'ignore)
255 (defalias 'nndb-request-article 'ignore)
256 (defalias 'efs-re-read-dir 'ignore)
257 (defalias 'ange-ftp-re-read-dir 'ignore)
258 (defalias 'define-mail-user-agent 'ignore)
259
260 (defconst dgnushack-unexporting-files
261   (append '("dgnushack.el" "dgnuspath.el" "lpath.el" "ptexinfmt.el")
262           (unless (or (condition-case nil
263                           (require 'w3-forms)
264                         (error nil))
265                       ;; Maybe mis-configured Makefile is used (e.g.
266                       ;; configured for FSFmacs but XEmacs is running).
267                       (let ((lp (delete dgnushack-w3-dir
268                                         (copy-sequence load-path))))
269                         (when (condition-case nil
270                                   (let ((load-path lp))
271                                     (require 'w3-forms))
272                                 (error nil))
273                           ;; If success, fix `load-path' for compiling.
274                           (setq load-path lp))))
275             '("nnweb.el" "nnlistserv.el" "nnultimate.el"
276               "nnslashdot.el" "nnwarchive.el" "webmail.el"
277               "nnwfm.el"))
278           (condition-case nil
279               (progn (require 'bbdb) nil)
280             (error '("gnus-bbdb.el")))
281           (unless (featurep 'xemacs)
282             '("gnus-xmas.el" "gnus-picon.el" "messagexmas.el"
283               "nnheaderxm.el" "smiley.el"))
284           (when (or (featurep 'xemacs) (<= emacs-major-version 20))
285             '("smiley-ems.el"))
286           (when (and (fboundp 'base64-decode-string)
287                      (subrp (symbol-function 'base64-decode-string)))
288             '("base64.el"))
289           (when (and (fboundp 'md5) (subrp (symbol-function 'md5)))
290             '("md5.el")))
291   "Files which will not be installed.")
292
293 (defconst dgnushack-exporting-files
294   (let ((files (directory-files srcdir nil "^[^=].*\\.el$" t)))
295     (dolist (file dgnushack-unexporting-files)
296       (setq files (delete file files)))
297     (sort files 'string-lessp))
298   "Files which will be compiled and installed.")
299
300 (defun dgnushack-exporting-files ()
301   "Print name of files which will be installed."
302   (princ (mapconcat 'identity dgnushack-exporting-files " ")))
303
304 (defun dgnushack-compile (&optional warn)
305   ;;(setq byte-compile-dynamic t)
306   (unless warn
307     (setq byte-compile-warnings
308           '(free-vars unresolved callargs redefine)))
309   (unless (locate-library "cus-edit")
310     (error "You do not seem to have Custom installed.
311 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
312 You also then need to add the following to the lisp/dgnushack.el file:
313
314      (push \"~/lisp/custom\" load-path)
315
316 Modify to suit your needs."))
317
318   ;; Show `load-path'.
319   (message "load-path=(\"%s\")"
320            (mapconcat 'identity load-path "\"\n           \""))
321
322   (dolist (file dgnushack-exporting-files)
323     (setq file (expand-file-name file srcdir))
324     (when (and (file-exists-p
325                 (setq elc (concat (file-name-nondirectory file) "c")))
326                (file-newer-than-file-p file elc))
327       (delete-file elc)))
328
329   (let ((files dgnushack-exporting-files)
330         ;;(byte-compile-generate-call-tree t)
331         file elc)
332     (while (setq file (pop files))
333       (setq file (expand-file-name file srcdir))
334       (when (or (not (file-exists-p
335                       (setq elc (concat (file-name-nondirectory file) "c"))))
336                 (file-newer-than-file-p file elc))
337         (ignore-errors
338           (byte-compile-file file))))))
339
340 (defun dgnushack-recompile ()
341   (require 'gnus)
342   (byte-recompile-directory "." 0))
343
344 \f
345 (defun dgnushack-texi-add-suffix-and-format ()
346   (dgnushack-texi-format t))
347
348 (defun dgnushack-texi-format (&optional addsuffix)
349   (if (not noninteractive)
350       (error "batch-texinfo-format may only be used -batch."))
351   (require 'ptexinfmt)
352   (let ((auto-save-default nil)
353         (find-file-run-dired nil)
354         coding-system-for-write
355         output-coding-system)
356     (let ((error 0)
357           file
358           (files ()))
359       (while command-line-args-left
360         (setq file (expand-file-name (car command-line-args-left)))
361         (cond ((not (file-exists-p file))
362                (message ">> %s does not exist!" file)
363                (setq error 1
364                      command-line-args-left (cdr command-line-args-left)))
365               ((file-directory-p file)
366                (setq command-line-args-left
367                      (nconc (directory-files file nil nil t)
368                             (cdr command-line-args-left))))
369               (t
370                (setq files (cons file files)
371                      command-line-args-left (cdr command-line-args-left)))))
372       (while (setq file (pop files))
373         (condition-case err
374             (progn
375               (if buffer-file-name (kill-buffer (current-buffer)))
376               (find-file file)
377               (buffer-disable-undo (current-buffer))
378               (if (boundp 'MULE)
379                   (setq output-coding-system (symbol-value
380                                               'file-coding-system))
381                 (setq coding-system-for-write buffer-file-coding-system))
382               ;; Remove ignored areas first.
383               (while (re-search-forward "^@ignore[\t\r ]*$" nil t)
384                 (delete-region (match-beginning 0)
385                                (if (re-search-forward
386                                     "^@end[\t ]+ignore[\t\r ]*$" nil t)
387                                    (1+ (match-end 0))
388                                  (point-max))))
389               (goto-char (point-min))
390               ;; Add suffix if it is needed.
391               (when (and addsuffix
392                          (re-search-forward
393                           "^@setfilename[\t ]+\\([^\t\n ]+\\)" nil t)
394                          (not (string-match "\\.info$" (match-string 1))))
395                 (insert ".info")
396                 (goto-char (point-min)))
397               ;; process @include before updating node
398               ;; This might produce some problem if we use @lowersection or
399               ;; such.
400               (let ((input-directory default-directory)
401                     (texinfo-command-end))
402                 (while (re-search-forward "^@include" nil t)
403                   (setq texinfo-command-end (point))
404                   (let ((filename (concat input-directory
405                                           (texinfo-parse-line-arg))))
406                     (re-search-backward "^@include")
407                     (delete-region (point) (save-excursion
408                                              (forward-line 1)
409                                              (point)))
410                     (message "Reading included file: %s" filename)
411                     (save-excursion
412                       (save-restriction
413                         (narrow-to-region
414                          (point)
415                          (+ (point)
416                             (car (cdr (insert-file-contents filename)))))
417                         (goto-char (point-min))
418                         ;; Remove `@setfilename' line from included file,
419                         ;; if any, so @setfilename command not duplicated.
420                         (if (re-search-forward "^@setfilename"
421                                                (save-excursion
422                                                  (forward-line 100)
423                                                  (point))
424                                                t)
425                             (progn
426                               (beginning-of-line)
427                               (delete-region (point) (save-excursion
428                                                        (forward-line 1)
429                                                        (point))))))))))
430               (texinfo-mode)
431               (texinfo-every-node-update)
432               (set-buffer-modified-p nil)
433               (message "texinfo formatting %s..." file)
434               (texinfo-format-buffer nil)
435               (if (buffer-modified-p)
436                   (progn (message "Saving modified %s" (buffer-file-name))
437                          (save-buffer))))
438           (error
439            (message ">> Error: %s" (prin1-to-string err))
440            (message ">>  point at")
441            (let ((s (buffer-substring (point)
442                                       (min (+ (point) 100)
443                                            (point-max))))
444                  (tem 0))
445              (while (setq tem (string-match "\n+" s tem))
446                (setq s (concat (substring s 0 (match-beginning 0))
447                                "\n>>  "
448                                (substring s (match-end 0)))
449                      tem (1+ tem)))
450              (message ">>  %s" s))
451            (setq error 1))))
452       (kill-emacs error))))
453
454 \f
455 (defconst dgnushack-info-file-regexp-en
456   (let ((names '("gnus" "message" "emacs-mime"))
457         regexp name)
458     (while (setq name (pop names))
459       (setq regexp (concat regexp "^" name "\\.info\\(-[0-9]+\\)?$"
460                            (when names "\\|"))))
461     regexp)
462   "Regexp matching English info files.")
463
464 (defconst dgnushack-info-file-regexp-ja
465   (let ((names '("gnus-ja" "message-ja"))
466         regexp name)
467     (while (setq name (pop names))
468       (setq regexp (concat regexp "^" name "\\.info\\(-[0-9]+\\)?$"
469                            (when names "\\|"))))
470     regexp)
471   "Regexp matching Japanese info files.")
472
473 (defun dgnushack-make-autoloads ()
474   "Make auto-autoloads.el, custom-load.el and then compile them."
475   (let (make-backup-files)
476     (message "Updating autoloads for directory %s..." default-directory)
477     (let ((generated-autoload-file "auto-autoloads.el")
478           (si:message (symbol-function 'message))
479           noninteractive)
480       (defun message (fmt &rest args)
481         (cond ((and (string-equal "Generating autoloads for %s..." fmt)
482                     (file-exists-p (file-name-nondirectory (car args))))
483                (funcall si:message fmt (file-name-nondirectory (car args))))
484               ((string-equal "No autoloads found in %s" fmt))
485               ((string-equal "Generating autoloads for %s...done" fmt))
486               (t (apply si:message fmt args))))
487       (unwind-protect
488           (update-autoloads-from-directory default-directory)
489         (fset 'message si:message)))
490     (byte-compile-file "auto-autoloads.el")
491     (with-temp-buffer
492       (let ((standard-output (current-buffer)))
493         (Custom-make-dependencies "."))
494       (message "%s" (buffer-string)))
495     (require 'cus-load)
496     (byte-compile-file "custom-load.el")))
497
498 (defun dgnushack-remove-extra-files-in-package ()
499   "Remove extra files in the lisp directory of the XEmacs package."
500   (let ((lisp-dir (expand-file-name (concat "lisp/"
501                                             ;; GNUS_PRODUCT_NAME
502                                             (cadr command-line-args-left)
503                                             "/")
504                                     ;; PACKAGEDIR
505                                     (car command-line-args-left))))
506     (when (file-directory-p lisp-dir)
507       (let (files)
508         (dolist (file dgnushack-exporting-files)
509           (setq files (nconc files (list file (concat file "c")))))
510         (dolist (file (directory-files lisp-dir nil nil t t))
511           (unless (member file files)
512             (setq file (expand-file-name file lisp-dir))
513             (message "Removing %s..." file)
514             (condition-case nil
515                 (delete-file file)
516               (error nil))))))))
517
518 (defun dgnushack-install-package-manifest ()
519   "Install MANIFEST file as an XEmacs package."
520   (let* ((package-dir (car command-line-args-left))
521          (product-name (cadr command-line-args-left))
522          (name (expand-file-name (concat "pkginfo/MANIFEST." product-name)
523                                  package-dir))
524          make-backup-files)
525     (message "Generating %s..." name)
526     (with-temp-file name
527       (insert "pkginfo/MANIFEST." product-name "\n")
528       (let ((lisp-dir (concat "lisp/" product-name "/"))
529             (files (sort (directory-files "." nil "\\.elc?$" t) 'string-lessp))
530             file)
531         (while (setq file (pop files))
532           (unless (member file dgnushack-unexporting-files)
533             (insert lisp-dir file "\n")))
534         (setq files
535               (sort (directory-files "../texi/" nil
536                                      (concat dgnushack-info-file-regexp-en
537                                              "\\|"
538                                              dgnushack-info-file-regexp-ja)
539                                      t)
540                     'string-lessp))
541         (while (setq file (pop files))
542           (insert "info/" file "\n"))))))
543
544 \f
545 (define-compiler-macro describe-key-briefly (&whole form key &optional insert)
546   (if (condition-case nil
547           (progn
548             (describe-key-briefly '((())) nil)
549             t)
550         (wrong-number-of-arguments nil);; Old Emacsen.
551         (error t))
552       form
553     (if insert
554         `(if ,insert
555              (insert (funcall 'describe-key-briefly ,key))
556            (funcall 'describe-key-briefly ,key))
557       `(funcall 'describe-key-briefly ,key))))
558
559 ;;; dgnushack.el ends here