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