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