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