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