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