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