T-gnus 6.14.3.
[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 (defvar *predefined-category*)
33 (defvar coding-category-list)
34 (if (featurep 'xemacs)
35     (fset 'set-coding-priority 'ignore)
36   (fset 'coding-priority-list 'ignore)
37   (fset 'set-coding-priority-list 'ignore))
38 (cond ((and (featurep 'xemacs) (featurep 'mule))
39        (if (memq 'shift-jis (coding-priority-list))
40            (set-coding-priority-list
41             (nconc (delq 'shift-jis (coding-priority-list)) '(shift-jis)))))
42       ((boundp 'MULE)
43        (put '*coding-category-sjis* 'priority (length *predefined-category*)))
44       ((featurep 'mule)
45        (if (memq 'coding-category-sjis coding-category-list)
46            (set-coding-priority
47             (nconc (delq 'coding-category-sjis coding-category-list)
48                    '(coding-category-sjis))))))
49
50 (fset 'facep 'ignore)
51
52 (require 'cl)
53
54 (push "/usr/share/emacs/site-lisp" load-path)
55
56 (unless (featurep 'xemacs)
57   (define-compiler-macro last (&whole form x &optional n)
58     (if (and (fboundp 'last)
59              (subrp (symbol-function 'last)))
60         form
61       (if n
62           `(let* ((x ,x)
63                   (n ,n)
64                   (m 0)
65                   (p x))
66              (while (consp p)
67                (incf m)
68                (pop p))
69              (if (<= n 0)
70                  p
71                (if (< n m)
72                    (nthcdr (- m n) x)
73                  x)))
74         `(let ((x ,x))
75            (while (consp (cdr x))
76              (pop x))
77            x))))
78
79   (define-compiler-macro mapcon (&whole form fn seq &rest rest)
80     (if (and (fboundp 'mapcon)
81              (subrp (symbol-function 'mapcon)))
82         form
83       (if rest
84           `(let (res
85                  (args (list ,seq ,@rest))
86                  p)
87              (while (not (memq nil args))
88                (push (apply ,fn args) res)
89                (setq p args)
90                (while p
91                  (setcar p (cdr (pop p)))
92                  ))
93              (apply (function nconc) (nreverse res)))
94         `(let (res
95                (arg ,seq))
96            (while arg
97              (push (funcall ,fn arg) res)
98              (setq arg (cdr arg)))
99            (apply (function nconc) (nreverse res))))))
100
101   (define-compiler-macro member-if (&whole form pred list)
102     (if (and (fboundp 'member-if)
103              (subrp (symbol-function 'member-if)))
104         form
105       `(let ((fn ,pred)
106              (seq ,list))
107          (while (and seq
108                      (not (funcall fn (car seq))))
109            (pop seq))
110          seq)))
111
112   (define-compiler-macro union (&whole form list1 list2)
113     (if (and (fboundp 'union)
114              (subrp (symbol-function 'union)))
115         form
116       `(let ((a ,list1)
117              (b ,list2))
118          (cond ((null a) b)
119                ((null b) a)
120                ((equal a b) a)
121                (t
122                 (or (>= (length a) (length b))
123                     (setq a (prog1 b (setq b a))))
124                 (while b
125                   (or (memq (car b) a)
126                       (push (car b) a))
127                   (pop b))
128                 a)))))
129   )
130
131 ;; If we are building w3 in a different directory than the source
132 ;; directory, we must read *.el from source directory and write *.elc
133 ;; into the building directory.  For that, we define this function
134 ;; before loading bytecomp.  Bytecomp doesn't overwrite this function.
135 (defun byte-compile-dest-file (filename)
136   "Convert an Emacs Lisp source file name to a compiled file name.
137  In addition, remove directory name part from FILENAME."
138   (setq filename (byte-compiler-base-file-name filename))
139   (setq filename (file-name-sans-versions filename))
140   (setq filename (file-name-nondirectory filename))
141   (if (memq system-type '(win32 w32 mswindows windows-nt))
142       (setq filename (downcase filename)))
143   (cond ((eq system-type 'vax-vms)
144          (concat (substring filename 0 (string-match ";" filename)) "c"))
145         ((string-match emacs-lisp-file-regexp filename)
146          (concat (substring filename 0 (match-beginning 0)) ".elc"))
147         (t (concat filename ".elc"))))
148
149 (require 'bytecomp)
150
151 (unless (fboundp 'si:byte-optimize-form-code-walker)
152   (byte-optimize-form nil);; Load `byte-opt' or `byte-optimize'.
153   (setq max-specpdl-size 3000)
154   (fset 'si:byte-optimize-form-code-walker
155         (symbol-function 'byte-optimize-form-code-walker))
156   (defun byte-optimize-form-code-walker (form for-effect)
157     (if (and for-effect (memq (car-safe form) '(and or)))
158         ;; Fix bug in and/or forms.
159         (let ((fn (car form))
160               (backwards (reverse (cdr form))))
161           (while (and backwards
162                       (null (setcar backwards
163                                     (byte-optimize-form (car backwards) t))))
164             (setq backwards (cdr backwards)))
165           (if (and (cdr form) (null backwards))
166               (byte-compile-log
167                "  all subforms of %s called for effect; deleted" form))
168           (if backwards
169               (let ((head backwards))
170                 (while (setq backwards (cdr backwards))
171                   (setcar backwards (byte-optimize-form (car backwards)
172                                                         nil)))
173                 (cons fn (nreverse head)))))
174       (si:byte-optimize-form-code-walker form for-effect)))
175   (byte-compile 'byte-optimize-form-code-walker))
176
177 (defvar srcdir (or (getenv "srcdir") "."))
178
179 (load (expand-file-name "gnus-clfns.el" srcdir) nil t t)
180
181 ;(push "/usr/share/emacs/site-lisp" load-path)
182
183 ;; Attempt to pickup the additional load-path(s).
184 (load (expand-file-name "dgnuspath.el" srcdir) nil nil t)
185 (condition-case err
186     (load "~/.lpath.el" t nil t)
187   (error (message "Error in \"~/.lpath.el\" file: %s" err)))
188
189 (condition-case nil
190     (char-after)
191   (wrong-number-of-arguments
192    ;; Optimize byte code for `char-after'.
193    (put 'char-after 'byte-optimizer 'byte-optimize-char-after)
194    (defun byte-optimize-char-after (form)
195      (if (null (cdr form))
196          '(char-after (point))
197        form))
198    ))
199
200 (condition-case nil
201     (char-before)
202   (wrong-number-of-arguments
203    ;; Optimize byte code for `char-before'.
204    (put 'char-before 'byte-optimizer 'byte-optimize-char-before)
205    (defun byte-optimize-char-before (form)
206      (if (null (cdr form))
207          '(char-before (point))
208        form))
209    ))
210
211 ;; `char-after' and `char-before' must be well-behaved before lpath.el
212 ;; is loaded.  Because it requires `poe' via `path-util'.
213 (load (expand-file-name "lpath.el" srcdir) nil t t)
214
215 (unless (fboundp 'byte-compile-file-form-custom-declare-variable)
216   ;; Bind defcustom'ed variables.
217   (put 'custom-declare-variable 'byte-hunk-handler
218        'byte-compile-file-form-custom-declare-variable)
219   (defun byte-compile-file-form-custom-declare-variable (form)
220     (if (memq 'free-vars byte-compile-warnings)
221         (setq byte-compile-bound-variables
222               (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
223     form))
224
225 ;; Bind functions defined by `defun-maybe'.
226 (put 'defun-maybe 'byte-hunk-handler 'byte-compile-file-form-defun-maybe)
227 (defun byte-compile-file-form-defun-maybe (form)
228   (if (and (not (fboundp (nth 1 form)))
229            (memq 'unresolved byte-compile-warnings))
230       (setq byte-compile-function-environment
231             (cons (cons (nth 1 form)
232                         (cons 'lambda (cdr (cdr form))))
233                   byte-compile-function-environment)))
234   form)
235
236 (condition-case nil
237     :symbol-for-testing-whether-colon-keyword-is-available-or-not
238   (void-variable
239    ;; Bind keywords.
240    (mapcar (lambda (keyword) (set keyword keyword))
241            '(:button-keymap :data :file :mime-handle))))
242
243 ;; Unknown variables and functions.
244 (unless (boundp 'buffer-file-coding-system)
245   (defvar buffer-file-coding-system (symbol-value 'file-coding-system)))
246 (unless (featurep 'xemacs)
247   (defalias 'Custom-make-dependencies 'ignore)
248   (defalias 'update-autoloads-from-directory 'ignore))
249 (autoload 'texinfo-parse-line-arg "texinfmt")
250
251 (unless (fboundp 'with-temp-buffer)
252   ;; Pickup some macros.
253   (require 'emu))
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 (defun dgnushack-compile (&optional warn)
263   ;;(setq byte-compile-dynamic t)
264   (unless warn
265     (setq byte-compile-warnings
266           '(free-vars unresolved callargs redefine)))
267   (unless (locate-library "cus-edit")
268     (error "You do not seem to have Custom installed.
269 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
270 You also then need to add the following to the lisp/dgnushack.el file:
271
272      (push \"~/lisp/custom\" load-path)
273
274 Modify to suit your needs."))
275   (let ((files (delete "dgnuspath.el"
276                        (directory-files srcdir nil "^[^=].*\\.el$")))
277         (xemacs (string-match "XEmacs" emacs-version))
278         ;;(byte-compile-generate-call-tree t)
279         file elc)
280     (condition-case ()
281         (require 'w3-forms)
282       (error
283        (dolist (file '("nnweb.el" "nnlistserv.el" "nnultimate.el"
284                        "nnslashdot.el" "nnwarchive.el" "webmail.el"))
285          (setq files (delete file files)))))
286     (condition-case ()
287         (require 'bbdb)
288       (error (setq files (delete "gnus-bbdb.el" files))))
289     (while (setq file (pop files))
290       (unless (or (and (not xemacs)
291                        (member file
292                                '("gnus-xmas.el" "gnus-picon.el"
293                                  "messagexmas.el" "nnheaderxm.el"
294                                  "smiley.el" "x-overlay.el")))
295                   (and (string-equal file "md5.el")
296                        (not (and (fboundp 'md5)
297                                  (subrp (symbol-function 'md5))))))
298         (setq file (expand-file-name file srcdir))
299         (when (or (not (file-exists-p (setq elc (concat file "c"))))
300                   (file-newer-than-file-p file elc))
301           (ignore-errors
302             (byte-compile-file file)))))))
303
304 (defun dgnushack-recompile ()
305   (require 'gnus)
306   (byte-recompile-directory "." 0))
307
308 \f
309 ;; Avoid byte-compile warnings.
310 (defvar gnus-product-name)
311 (defvar early-package-load-path)
312 (defvar early-packages)
313 (defvar last-package-load-path)
314 (defvar last-packages)
315 (defvar late-package-load-path)
316 (defvar late-packages)
317
318 (defconst dgnushack-info-file-regexp
319   (concat "^\\(gnus\\|message\\|emacs-mime\\|gnus-ja\\|message-ja\\)"
320           "\\.info\\(-[0-9]+\\)?$"))
321
322 (defconst dgnushack-texi-file-regexp
323   "^\\(gnus\\|message\\|emacs-mime\\|gnus-ja\\|message-ja\\)\\.texi$")
324
325 (defun dgnushack-make-package ()
326   (require 'gnus)
327   (let* ((product-name (downcase gnus-product-name))
328          (lisp-dir (concat "lisp/" product-name "/"))
329          make-backup-files)
330
331     (message "Updating autoloads for directory %s..." default-directory)
332     (let ((generated-autoload-file "auto-autoloads.el")
333           noninteractive
334           (omsg (symbol-function 'message)))
335       (defun message (fmt &rest args)
336         (cond ((and (string-equal "Generating autoloads for %s..." fmt)
337                     (file-exists-p (file-name-nondirectory (car args))))
338                (funcall omsg fmt (file-name-nondirectory (car args))))
339               ((string-equal "No autoloads found in %s" fmt))
340               ((string-equal "Generating autoloads for %s...done" fmt))
341               (t (apply omsg fmt args))))
342       (unwind-protect
343           (update-autoloads-from-directory default-directory)
344         (fset 'message omsg)))
345     (byte-compile-file "auto-autoloads.el")
346
347     (with-temp-buffer
348       (let ((standard-output (current-buffer)))
349         (Custom-make-dependencies "."))
350       (message "%s" (buffer-string)))
351     (require 'cus-load)
352     (byte-compile-file "custom-load.el")
353
354     (message "Generating MANIFEST.%s for the package..." product-name)
355     (with-temp-buffer
356       (insert "pkginfo/MANIFEST." product-name "\n"
357               lisp-dir
358               (mapconcat
359                'identity
360                (sort (delete "dgnuspath.el"
361                              (delete "patchs.elc"
362                                      (directory-files "." nil "\\.elc?$")))
363                      'string-lessp)
364                (concat "\n" lisp-dir))
365               "\ninfo/"
366               (mapconcat
367                'identity
368                (sort (directory-files "../texi/"
369                                       nil dgnushack-info-file-regexp)
370                      'string-lessp)
371                "\ninfo/")
372               "\n")
373       (write-file (concat "../MANIFEST." product-name)))))
374
375 (defun dgnushack-install-package ()
376   (let ((package-dir (car command-line-args-left))
377         dirs info-dir pkginfo-dir product-name lisp-dir manifest files)
378     (unless package-dir
379       (when (boundp 'early-packages)
380         (setq dirs (delq nil (append (when early-package-load-path
381                                        early-packages)
382                                      (when late-package-load-path
383                                        late-packages)
384                                      (when last-package-load-path
385                                        last-packages))))
386         (while (and dirs (not package-dir))
387           (when (file-exists-p (car dirs))
388             (setq package-dir (car dirs)
389                   dirs (cdr dirs))))))
390     (unless package-dir
391       (error "%s" "
392 You must specify the name of the package path as follows:
393
394 % make install-package PACKAGEDIR=/usr/local/lib/xemacs/xemacs-packages
395 "
396              ))
397     (setq info-dir (expand-file-name "info/" package-dir)
398           pkginfo-dir (expand-file-name "pkginfo/" package-dir))
399     (require 'gnus)
400     (setq product-name (downcase gnus-product-name)
401           lisp-dir (expand-file-name (concat "lisp/" product-name "/")
402                                      package-dir)
403           manifest (concat "MANIFEST." product-name))
404
405     (unless (file-directory-p lisp-dir)
406       (make-directory lisp-dir t))
407     (unless (file-directory-p info-dir)
408       (make-directory info-dir))
409     (unless (file-directory-p pkginfo-dir)
410       (make-directory pkginfo-dir))
411
412     (setq files
413           (sort (delete "dgnuspath.el"
414                         (delete "dgnuspath.elc"
415                                 (directory-files "." nil "\\.elc?$")))
416                 'string-lessp))
417     (mapcar
418      (lambda (file)
419        (unless (or (member file files)
420                    (not (string-match "\\.elc?$" file)))
421          (setq file (expand-file-name file lisp-dir))
422          (message "Removing %s..." file)
423          (condition-case nil
424              (delete-file file)
425            (error nil))))
426      (directory-files lisp-dir nil nil nil t))
427     (mapcar
428      (lambda (file)
429        (message "Copying %s to %s..." file lisp-dir)
430        (copy-file file (expand-file-name file lisp-dir) t t))
431      files)
432
433     (mapcar
434      (lambda (file)
435        (message "Copying ../texi/%s to %s..." file info-dir)
436        (copy-file (expand-file-name file "../texi/")
437                   (expand-file-name file info-dir)
438                   t t))
439      (sort (directory-files "../texi/" nil dgnushack-info-file-regexp)
440            'string-lessp))
441
442     (message "Copying ../%s to %s..." manifest pkginfo-dir)
443     (copy-file (expand-file-name manifest "../")
444                (expand-file-name manifest pkginfo-dir) t t)
445
446     (message "Done")))
447
448 (defun dgnushack-texi-add-suffix-and-format ()
449   (dgnushack-texi-format t))
450
451 (defun dgnushack-texi-format (&optional addsuffix)
452   (if (not noninteractive)
453       (error "batch-texinfo-format may only be used -batch."))
454   (require 'texinfmt)
455   (let ((auto-save-default nil)
456         (find-file-run-dired nil)
457         coding-system-for-write)
458     (let ((error 0)
459           file
460           (files ()))
461       (while command-line-args-left
462         (setq file (expand-file-name (car command-line-args-left)))
463         (cond ((not (file-exists-p file))
464                (message ">> %s does not exist!" file)
465                (setq error 1
466                      command-line-args-left (cdr command-line-args-left)))
467               ((file-directory-p file)
468                (setq command-line-args-left
469                      (nconc (directory-files file)
470                             (cdr command-line-args-left))))
471               (t
472                (setq files (cons file files)
473                      command-line-args-left (cdr command-line-args-left)))))
474       (while files
475         (setq file (car files)
476               files (cdr files))
477         (condition-case err
478             (progn
479               (if buffer-file-name (kill-buffer (current-buffer)))
480               (find-file file)
481               (setq coding-system-for-write buffer-file-coding-system)
482               (when (and addsuffix
483                          (re-search-forward
484                           "^@setfilename[\t ]+\\([^\t\n ]+\\)" nil t)
485                          (not (string-match "\\.info$" (match-string 1))))
486                 (insert ".info"))
487               (buffer-disable-undo (current-buffer))
488               ;; process @include before updating node
489               ;; This might produce some problem if we use @lowersection or
490               ;; such.
491               (let ((input-directory default-directory)
492                     (texinfo-command-end))
493                 (while (re-search-forward "^@include" nil t)
494                   (setq texinfo-command-end (point))
495                   (let ((filename (concat input-directory
496                                           (texinfo-parse-line-arg))))
497                     (re-search-backward "^@include")
498                     (delete-region (point) (save-excursion
499                                              (forward-line 1)
500                                              (point)))
501                     (message "Reading included file: %s" filename)
502                     (save-excursion
503                       (save-restriction
504                         (narrow-to-region
505                          (point)
506                          (+ (point)
507                             (car (cdr (insert-file-contents filename)))))
508                         (goto-char (point-min))
509                         ;; Remove `@setfilename' line from included file,
510                         ;; if any, so @setfilename command not duplicated.
511                         (if (re-search-forward "^@setfilename"
512                                                (save-excursion
513                                                  (forward-line 100)
514                                                  (point))
515                                                t)
516                             (progn
517                               (beginning-of-line)
518                               (delete-region (point) (save-excursion
519                                                        (forward-line 1)
520                                                        (point))))))))))
521               (texinfo-mode)
522               (texinfo-every-node-update)
523               (set-buffer-modified-p nil)
524               (message "texinfo formatting %s..." file)
525               (texinfo-format-buffer nil)
526               (if (buffer-modified-p)
527                   (progn (message "Saving modified %s" (buffer-file-name))
528                          (save-buffer))))
529           (error
530            (message ">> Error: %s" (prin1-to-string err))
531            (message ">>  point at")
532            (let ((s (buffer-substring (point)
533                                       (min (+ (point) 100)
534                                            (point-max))))
535                  (tem 0))
536              (while (setq tem (string-match "\n+" s tem))
537                (setq s (concat (substring s 0 (match-beginning 0))
538                                "\n>>  "
539                                (substring s (match-end 0)))
540                      tem (1+ tem)))
541              (message ">>  %s" s))
542            (setq error 1))))
543       (kill-emacs error))))
544
545 ;;; dgnushack.el ends here