Synch with 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 (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             (append (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             (append (delq 'coding-category-sjis
48                           (copy-sequence coding-category-list))
49                     '(coding-category-sjis))))))
50
51 (fset 'facep 'ignore)
52
53 (require 'cl)
54
55 (defvar srcdir (or (getenv "srcdir") "."))
56
57 (push (or (getenv "W3DIR") (expand-file-name "../../w3/lisp/" srcdir))
58       load-path)
59 (load (expand-file-name "dgnuspath.el" srcdir) nil nil t)
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   (fset '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 err
110     (load "~/.lpath.el" t nil t)
111   (error (message "Error in \"~/.lpath.el\" file: %s" err)))
112
113 (condition-case nil
114     (char-after)
115   (wrong-number-of-arguments
116    ;; Optimize byte code for `char-after'.
117    (put 'char-after 'byte-optimizer 'byte-optimize-char-after)
118    (defun byte-optimize-char-after (form)
119      (if (null (cdr form))
120          '(char-after (point))
121        form))
122    ))
123
124 (condition-case nil
125     (char-before)
126   (wrong-number-of-arguments
127    ;; Optimize byte code for `char-before'.
128    (put 'char-before 'byte-optimizer 'byte-optimize-char-before)
129    (defun byte-optimize-char-before (form)
130      (if (null (cdr form))
131          '(char-before (point))
132        form))
133    ))
134
135 (push srcdir load-path)
136
137 ;; `char-after' and `char-before' must be well-behaved before lpath.el
138 ;; is loaded.  Because it requires `poe' via `path-util'.
139 (load (expand-file-name "lpath.el" srcdir) nil t t)
140
141 (unless (fboundp 'byte-compile-file-form-custom-declare-variable)
142   ;; Bind defcustom'ed variables.
143   (put 'custom-declare-variable 'byte-hunk-handler
144        'byte-compile-file-form-custom-declare-variable)
145   (defun byte-compile-file-form-custom-declare-variable (form)
146     (if (memq 'free-vars byte-compile-warnings)
147         (setq byte-compile-bound-variables
148               (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
149     form))
150
151 ;; Bind functions defined by `defun-maybe'.
152 (put 'defun-maybe 'byte-hunk-handler 'byte-compile-file-form-defun-maybe)
153 (defun byte-compile-file-form-defun-maybe (form)
154   (if (and (not (fboundp (nth 1 form)))
155            (memq 'unresolved byte-compile-warnings))
156       (setq byte-compile-function-environment
157             (cons (cons (nth 1 form)
158                         (cons 'lambda (cdr (cdr form))))
159                   byte-compile-function-environment)))
160   form)
161
162 (condition-case nil
163     :symbol-for-testing-whether-colon-keyword-is-available-or-not
164   (void-variable
165    ;; Bind keywords.
166    (mapcar (lambda (keyword) (set keyword keyword))
167            '(:button-keymap :data :file :mime-handle))))
168
169 ;; Unknown variables and functions.
170 (unless (boundp 'buffer-file-coding-system)
171   (defvar buffer-file-coding-system (symbol-value 'file-coding-system)))
172 (unless (featurep 'xemacs)
173   (defalias 'Custom-make-dependencies 'ignore)
174   (defalias 'update-autoloads-from-directory 'ignore))
175 (autoload 'texinfo-parse-line-arg "texinfmt")
176
177 (unless (fboundp 'with-temp-buffer)
178   ;; Pickup some macros.
179   (require 'emu))
180
181 (defalias 'device-sound-enabled-p 'ignore)
182 (defalias 'play-sound-file 'ignore)
183 (defalias 'nndb-request-article 'ignore)
184 (defalias 'efs-re-read-dir 'ignore)
185 (defalias 'ange-ftp-re-read-dir 'ignore)
186 (defalias 'define-mail-user-agent 'ignore)
187
188 (defconst dgnushack-tool-files
189   '("dgnushack.el" "dgnuspath.el" "lpath.el" "ptexinfmt.el"))
190 (defconst dgnushack-unexported-files
191   '("dgnuspath.el" "ptexinfmt.el"))
192
193 (defun dgnushack-compile (&optional warn)
194   ;;(setq byte-compile-dynamic t)
195   (unless warn
196     (setq byte-compile-warnings
197           '(free-vars unresolved callargs redefine)))
198   (unless (locate-library "cus-edit")
199     (error "You do not seem to have Custom installed.
200 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
201 You also then need to add the following to the lisp/dgnushack.el file:
202
203      (push \"~/lisp/custom\" load-path)
204
205 Modify to suit your needs."))
206   (let ((files (directory-files srcdir nil "^[^=].*\\.el$"))
207         ;;(byte-compile-generate-call-tree t)
208         file elc)
209     (mapcar
210      (lambda (el) (setq files (delete el files)))
211      (append
212       dgnushack-tool-files
213       (condition-case nil
214           (progn (require 'w3-forms) nil)
215         (error '("nnweb.el" "nnlistserv.el" "nnultimate.el"
216                  "nnslashdot.el" "nnwarchive.el" "webmail.el")))
217       (condition-case nil
218           (progn (require 'bbdb) nil)
219         (error '("gnus-bbdb.el")))
220       (unless (featurep 'xemacs)
221         '("gnus-xmas.el" "gnus-picon.el" "messagexmas.el"
222           "nnheaderxm.el" "smiley.el"))
223       (when (or (featurep 'xemacs) (<= emacs-major-version 20))
224         '("smiley-ems.el"))
225       (when (and (fboundp 'md5) (subrp (symbol-function 'md5)))
226         '("md5.el"))))
227     (while (setq file (pop files))
228       (setq file (expand-file-name file srcdir))
229       (when (or (not (file-exists-p (setq elc (concat file "c"))))
230                 (file-newer-than-file-p file elc))
231         (ignore-errors
232           (byte-compile-file file))))))
233
234 (defun dgnushack-recompile ()
235   (require 'gnus)
236   (byte-recompile-directory "." 0))
237
238 \f
239 ;; Avoid byte-compile warnings.
240 (defvar gnus-product-name)
241 (defvar early-package-load-path)
242 (defvar early-packages)
243 (defvar last-package-load-path)
244 (defvar last-packages)
245 (defvar late-package-load-path)
246 (defvar late-packages)
247
248 (defconst dgnushack-info-file-regexp
249   (concat "^\\(gnus\\|message\\|emacs-mime\\|gnus-ja\\|message-ja\\)"
250           "\\.info\\(-[0-9]+\\)?$"))
251
252 (defconst dgnushack-texi-file-regexp
253   "^\\(gnus\\|message\\|emacs-mime\\|gnus-ja\\|message-ja\\)\\.texi$")
254
255 (defun dgnushack-make-package ()
256   (require 'gnus)
257   (let* ((product-name (downcase gnus-product-name))
258          (lisp-dir (concat "lisp/" product-name "/"))
259          make-backup-files)
260
261     (message "Updating autoloads for directory %s..." default-directory)
262     (let ((generated-autoload-file "auto-autoloads.el")
263           noninteractive
264           (omsg (symbol-function 'message)))
265       (defun message (fmt &rest args)
266         (cond ((and (string-equal "Generating autoloads for %s..." fmt)
267                     (file-exists-p (file-name-nondirectory (car args))))
268                (funcall omsg fmt (file-name-nondirectory (car args))))
269               ((string-equal "No autoloads found in %s" fmt))
270               ((string-equal "Generating autoloads for %s...done" fmt))
271               (t (apply omsg fmt args))))
272       (unwind-protect
273           (update-autoloads-from-directory default-directory)
274         (fset 'message omsg)))
275     (byte-compile-file "auto-autoloads.el")
276
277     (with-temp-buffer
278       (let ((standard-output (current-buffer)))
279         (Custom-make-dependencies "."))
280       (message "%s" (buffer-string)))
281     (require 'cus-load)
282     (byte-compile-file "custom-load.el")
283
284     (message "Generating MANIFEST.%s for the package..." product-name)
285     (with-temp-buffer
286       (insert "pkginfo/MANIFEST." product-name "\n")
287       (mapcar
288        (lambda (file)
289          (unless (member file dgnushack-unexported-files)
290            (insert lisp-dir file "\n")))
291        (sort (directory-files "." nil "\\.elc?$") 'string-lessp))
292       (mapcar
293        (lambda (file) (insert "info/" file "\n"))
294        (sort (directory-files "../texi/" nil dgnushack-info-file-regexp)
295              'string-lessp))
296       (write-file (concat "../MANIFEST." product-name)))))
297
298 (defun dgnushack-install-package ()
299   (let ((package-dir (car command-line-args-left))
300         dirs info-dir pkginfo-dir product-name lisp-dir manifest files)
301     (unless package-dir
302       (when (boundp 'early-packages)
303         (setq dirs (delq nil (append (when early-package-load-path
304                                        early-packages)
305                                      (when late-package-load-path
306                                        late-packages)
307                                      (when last-package-load-path
308                                        last-packages))))
309         (while (and dirs (not package-dir))
310           (when (file-exists-p (car dirs))
311             (setq package-dir (car dirs)
312                   dirs (cdr dirs))))))
313     (unless package-dir
314       (error "%s" "
315 You must specify the name of the package path as follows:
316
317 % make install-package PACKAGEDIR=/usr/local/lib/xemacs/xemacs-packages
318 "
319              ))
320     (setq info-dir (expand-file-name "info/" package-dir)
321           pkginfo-dir (expand-file-name "pkginfo/" package-dir))
322     (require 'gnus)
323     (setq product-name (downcase gnus-product-name)
324           lisp-dir (expand-file-name (concat "lisp/" product-name "/")
325                                      package-dir)
326           manifest (concat "MANIFEST." product-name))
327
328     (unless (file-directory-p lisp-dir)
329       (make-directory lisp-dir t))
330     (unless (file-directory-p info-dir)
331       (make-directory info-dir))
332     (unless (file-directory-p pkginfo-dir)
333       (make-directory pkginfo-dir))
334
335     (setq files (sort (directory-files "." nil "\\.elc?$") 'string-lessp))
336     (mapcar (lambda (el) (setq files (delete el files)))
337             dgnushack-unexported-files)
338     (mapcar
339      (lambda (file)
340        (unless (or (member file files)
341                    (not (string-match "\\.elc?$" file)))
342          (setq file (expand-file-name file lisp-dir))
343          (message "Removing %s..." file)
344          (condition-case nil
345              (delete-file file)
346            (error nil))))
347      (directory-files lisp-dir nil nil nil t))
348     (mapcar
349      (lambda (file)
350        (message "Copying %s to %s..." file lisp-dir)
351        (copy-file file (expand-file-name file lisp-dir) t t))
352      files)
353
354     (mapcar
355      (lambda (file)
356        (message "Copying ../texi/%s to %s..." file info-dir)
357        (copy-file (expand-file-name file "../texi/")
358                   (expand-file-name file info-dir)
359                   t t))
360      (sort (directory-files "../texi/" nil dgnushack-info-file-regexp)
361            'string-lessp))
362
363     (message "Copying ../%s to %s..." manifest pkginfo-dir)
364     (copy-file (expand-file-name manifest "../")
365                (expand-file-name manifest pkginfo-dir) t t)
366
367     (message "Done")))
368
369 (defun dgnushack-texi-add-suffix-and-format ()
370   (dgnushack-texi-format t))
371
372 (defun dgnushack-texi-format (&optional addsuffix)
373   (if (not noninteractive)
374       (error "batch-texinfo-format may only be used -batch."))
375   (require 'ptexinfmt)
376   (let ((auto-save-default nil)
377         (find-file-run-dired nil)
378         coding-system-for-write
379         output-coding-system)
380     (let ((error 0)
381           file
382           (files ()))
383       (while command-line-args-left
384         (setq file (expand-file-name (car command-line-args-left)))
385         (cond ((not (file-exists-p file))
386                (message ">> %s does not exist!" file)
387                (setq error 1
388                      command-line-args-left (cdr command-line-args-left)))
389               ((file-directory-p file)
390                (setq command-line-args-left
391                      (nconc (directory-files file)
392                             (cdr command-line-args-left))))
393               (t
394                (setq files (cons file files)
395                      command-line-args-left (cdr command-line-args-left)))))
396       (while files
397         (setq file (car files)
398               files (cdr files))
399         (condition-case err
400             (progn
401               (if buffer-file-name (kill-buffer (current-buffer)))
402               (find-file file)
403               (if (boundp 'MULE)
404                   (setq output-coding-system (symbol-value
405                                               'file-coding-system))
406                 (setq coding-system-for-write buffer-file-coding-system))
407               (when (and addsuffix
408                          (re-search-forward
409                           "^@setfilename[\t ]+\\([^\t\n ]+\\)" nil t)
410                          (not (string-match "\\.info$" (match-string 1))))
411                 (insert ".info"))
412               (buffer-disable-undo (current-buffer))
413               ;; process @include before updating node
414               ;; This might produce some problem if we use @lowersection or
415               ;; such.
416               (let ((input-directory default-directory)
417                     (texinfo-command-end))
418                 (while (re-search-forward "^@include" nil t)
419                   (setq texinfo-command-end (point))
420                   (let ((filename (concat input-directory
421                                           (texinfo-parse-line-arg))))
422                     (re-search-backward "^@include")
423                     (delete-region (point) (save-excursion
424                                              (forward-line 1)
425                                              (point)))
426                     (message "Reading included file: %s" filename)
427                     (save-excursion
428                       (save-restriction
429                         (narrow-to-region
430                          (point)
431                          (+ (point)
432                             (car (cdr (insert-file-contents filename)))))
433                         (goto-char (point-min))
434                         ;; Remove `@setfilename' line from included file,
435                         ;; if any, so @setfilename command not duplicated.
436                         (if (re-search-forward "^@setfilename"
437                                                (save-excursion
438                                                  (forward-line 100)
439                                                  (point))
440                                                t)
441                             (progn
442                               (beginning-of-line)
443                               (delete-region (point) (save-excursion
444                                                        (forward-line 1)
445                                                        (point))))))))))
446               (texinfo-mode)
447               (texinfo-every-node-update)
448               (set-buffer-modified-p nil)
449               (message "texinfo formatting %s..." file)
450               (texinfo-format-buffer nil)
451               (if (buffer-modified-p)
452                   (progn (message "Saving modified %s" (buffer-file-name))
453                          (save-buffer))))
454           (error
455            (message ">> Error: %s" (prin1-to-string err))
456            (message ">>  point at")
457            (let ((s (buffer-substring (point)
458                                       (min (+ (point) 100)
459                                            (point-max))))
460                  (tem 0))
461              (while (setq tem (string-match "\n+" s tem))
462                (setq s (concat (substring s 0 (match-beginning 0))
463                                "\n>>  "
464                                (substring s (match-end 0)))
465                      tem (1+ tem)))
466              (message ">>  %s" s))
467            (setq error 1))))
468       (kill-emacs error))))
469
470 ;;; dgnushack.el ends here