Fix the comment why loading cl-macs is needed.
[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, 2001
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 (unless (dolist (var nil t))
53   ;; Override the macro `dolist' which may have been defined in egg.el.
54   (load "cl-macs" nil t))
55
56 (defvar srcdir (or (getenv "srcdir") "."))
57
58 (defvar dgnushack-w3-dir (let ((w3dir (getenv "W3DIR")))
59                            (unless (zerop (length w3dir))
60                              (file-name-as-directory w3dir))))
61 (when dgnushack-w3-dir
62   (push dgnushack-w3-dir load-path))
63
64 ;; If we are building w3 in a different directory than the source
65 ;; directory, we must read *.el from source directory and write *.elc
66 ;; into the building directory.  For that, we define this function
67 ;; before loading bytecomp.  Bytecomp doesn't overwrite this function.
68 (defun byte-compile-dest-file (filename)
69   "Convert an Emacs Lisp source file name to a compiled file name.
70  In addition, remove directory name part from FILENAME."
71   (setq filename (byte-compiler-base-file-name filename))
72   (setq filename (file-name-sans-versions filename))
73   (setq filename (file-name-nondirectory filename))
74   (if (memq system-type '(win32 w32 mswindows windows-nt))
75       (setq filename (downcase filename)))
76   (cond ((eq system-type 'vax-vms)
77          (concat (substring filename 0 (string-match ";" filename)) "c"))
78         ((string-match emacs-lisp-file-regexp filename)
79          (concat (substring filename 0 (match-beginning 0)) ".elc"))
80         (t (concat filename ".elc"))))
81
82 (require 'bytecomp)
83
84 (unless (fboundp 'si:byte-optimize-form-code-walker)
85   (byte-optimize-form nil);; Load `byte-opt' or `byte-optimize'.
86   (setq max-specpdl-size 3000)
87   (defalias 'si:byte-optimize-form-code-walker
88     (symbol-function 'byte-optimize-form-code-walker))
89   (defun byte-optimize-form-code-walker (form for-effect)
90     (if (and for-effect (memq (car-safe form) '(and or)))
91         ;; Fix bug in and/or forms.
92         (let ((fn (car form))
93               (backwards (reverse (cdr form))))
94           (while (and backwards
95                       (null (setcar backwards
96                                     (byte-optimize-form (car backwards) t))))
97             (setq backwards (cdr backwards)))
98           (if (and (cdr form) (null backwards))
99               (byte-compile-log
100                "  all subforms of %s called for effect; deleted" form))
101           (if backwards
102               (let ((head backwards))
103                 (while (setq backwards (cdr backwards))
104                   (setcar backwards (byte-optimize-form (car backwards)
105                                                         nil)))
106                 (cons fn (nreverse head)))))
107       (si:byte-optimize-form-code-walker form for-effect)))
108   (byte-compile 'byte-optimize-form-code-walker))
109
110 (load (expand-file-name "gnus-clfns.el" srcdir) nil t t)
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 (condition-case nil
123     (char-before)
124   (wrong-number-of-arguments
125    ;; Optimize byte code for `char-before'.
126    (put 'char-before 'byte-optimizer 'byte-optimize-char-before)
127    (defun byte-optimize-char-before (form)
128      (if (null (cdr form))
129          '(char-before (point))
130        form))))
131
132 (load (expand-file-name "dgnuspath.el" srcdir) nil nil t)
133
134 (condition-case err
135     (load "~/.lpath.el" t nil t)
136   (error (message "Error in \"~/.lpath.el\" file: %s" err)))
137
138 ;; Don't load path-util until `char-after' and `char-before' have been
139 ;; optimized because it requires `poe' and then modify the functions.
140 (or (featurep 'path-util)
141     (load "apel/path-util"))
142 (add-path "apel")
143 (add-path "flim")
144 (unless (module-installed-p 'mel)
145   ;; FLIM 1.14 may have installed in two "flim" subdirectories.
146   (push (expand-file-name "flim"
147                           (file-name-directory (get-latest-path "^apel$" t)))
148         load-path)
149   (unless (module-installed-p 'mel)
150     (error "
151 FLIM package does not found in %s.
152 Try to re-configure with --with-addpath=FLIM_PATH and run make again.
153 "
154            (progn
155              (add-path "semi")
156              load-path))))
157 (add-path "semi")
158
159 (push srcdir load-path)
160 (load (expand-file-name "lpath.el" srcdir) nil t t)
161
162 ;; Bind functions defined by `defun-maybe'.
163 (put 'defun-maybe 'byte-hunk-handler 'byte-compile-file-form-defun-maybe)
164 (defun byte-compile-file-form-defun-maybe (form)
165   (if (and (not (fboundp (nth 1 form)))
166            (memq 'unresolved byte-compile-warnings))
167       (setq byte-compile-function-environment
168             (cons (cons (nth 1 form)
169                         (cons 'lambda (cdr (cdr form))))
170                   byte-compile-function-environment)))
171   form)
172
173 (condition-case nil
174     :symbol-for-testing-whether-colon-keyword-is-available-or-not
175   (void-variable
176    ;; Bind keywords.
177    (dolist (keyword '(:button-keymap :data :file :mime-handle
178                                      :key-type :value-type))
179      (set keyword keyword))))
180
181 ;; If you are using Mule 2.3 based on Emacs 19.34, you may also put the
182 ;; following lines in your .emacs file, before gnus related modules are
183 ;; loaded.  It is not always necessary.  However if it is done, you will
184 ;; be able to load or evaluate gnus related *.el (not compiled) files.
185 ;; ------ cut here ------ cut here ------ cut here ------ cut here ------
186 (if (boundp 'MULE)
187     (progn
188       (setq :version ':version
189             :set-after ':set-after)
190       (require 'custom)
191       (defadvice custom-handle-keyword
192         (around dont-signal-an-error-even-if-unsupported-keyword-is-given
193                 activate)
194         "Don't signal an error even if unsupported keyword is given."
195         (if (not (memq (ad-get-arg 1) '(:version :set-after)))
196             ad-do-it))))
197 ;; ------ cut here ------ cut here ------ cut here ------ cut here ------
198
199 (when (boundp 'MULE)
200   (put 'custom-declare-face 'byte-optimizer
201        'byte-optimize-ignore-unsupported-custom-keywords)
202   (put 'custom-declare-group 'byte-optimizer
203        'byte-optimize-ignore-unsupported-custom-keywords)
204   (defun byte-optimize-ignore-unsupported-custom-keywords (form)
205     (if (or (memq ':version (nthcdr 4 form))
206             (memq ':set-after (nthcdr 4 form)))
207         (let ((newform (list (car form) (nth 1 form)
208                              (nth 2 form) (nth 3 form)))
209               (args (nthcdr 4 form)))
210           (while args
211             (or (memq (car args) '(:version :set-after))
212                 (setq newform (nconc newform (list (car args)
213                                                    (car (cdr args))))))
214             (setq args (cdr (cdr args))))
215           newform)
216       form))
217
218   (put 'custom-declare-variable 'byte-hunk-handler
219        'byte-compile-file-form-custom-declare-variable)
220   (defun byte-compile-file-form-custom-declare-variable (form)
221     ;; Bind defcustom'ed variables.
222     (if (memq 'free-vars byte-compile-warnings)
223         (setq byte-compile-bound-variables
224               (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
225     (if (memq ':version (nthcdr 4 form))
226         ;; Make the variable uncustomizable.
227         `(defvar ,(nth 1 (nth 1 form)) ,(nth 1 (nth 2 form))
228            ,(substring (nth 3 form) (if (string-match "^[\t *]+" (nth 3 form))
229                                         (match-end 0)
230                                       0)))
231       ;; Ignore unsupported keyword(s).
232       (if (memq ':set-after (nthcdr 4 form))
233           (let ((newform (list (car form) (nth 1 form)
234                                (nth 2 form) (nth 3 form)))
235                 (args (nthcdr 4 form)))
236             (while args
237               (or (eq (car args) ':set-after)
238                   (setq newform (nconc newform (list (car args)
239                                                      (car (cdr args))))))
240               (setq args (cdr (cdr args))))
241             newform)
242         form))))
243
244 ;; Unknown variables and functions.
245 (unless (boundp 'buffer-file-coding-system)
246   (defvar buffer-file-coding-system (symbol-value 'file-coding-system)))
247 (unless (featurep 'xemacs)
248   (defalias 'Custom-make-dependencies 'ignore)
249   (defalias 'update-autoloads-from-directory 'ignore))
250 (autoload 'texinfo-parse-line-arg "texinfmt")
251
252 (defalias 'device-sound-enabled-p 'ignore)
253 (defalias 'play-sound-file 'ignore)
254 (defalias 'nndb-request-article 'ignore)
255 (defalias 'efs-re-read-dir 'ignore)
256 (defalias 'ange-ftp-re-read-dir 'ignore)
257 (defalias 'define-mail-user-agent 'ignore)
258
259 (defconst dgnushack-unexporting-files
260   (append '("dgnushack.el" "dgnuspath.el" "lpath.el" "ptexinfmt.el")
261           (unless (or (condition-case code
262                           (require 'w3-forms)
263                         (error
264                          (message "No w3: %s %s retrying..." code
265                                   (locate-library "w3-forms"))
266                          nil))
267                       ;; Maybe mis-configured Makefile is used (e.g.
268                       ;; configured for FSFmacs but XEmacs is running).
269                       (let ((lp (delete dgnushack-w3-dir
270                                         (copy-sequence load-path))))
271                         (if (let ((load-path lp))
272                               (condition-case nil
273                                   (require 'w3-forms)
274                                 (error nil)))
275                             ;; If success, fix `load-path' for compiling.
276                             (progn
277                               (setq load-path lp)
278                               (message " => fixed; W3DIR=%s"
279                                        (file-name-directory
280                                         (locate-library "w3-forms")))
281                               t)
282                           (message " => ignored")
283                           nil)))
284             '("nnweb.el" "nnlistserv.el" "nnultimate.el"
285               "nnslashdot.el" "nnwarchive.el" "webmail.el"
286               "nnwfm.el"))
287           (condition-case nil
288               (progn (require 'bbdb) nil)
289             (error '("gnus-bbdb.el")))
290           (unless (featurep 'xemacs)
291             '("gnus-xmas.el" "gnus-picon.el" "messagexmas.el"
292               "nnheaderxm.el" "smiley.el"))
293           (when (or (featurep 'xemacs) (<= emacs-major-version 20))
294             '("smiley-ems.el"))
295           (when (and (fboundp 'base64-decode-string)
296                      (subrp (symbol-function 'base64-decode-string)))
297             '("base64.el"))
298           (when (and (fboundp 'md5) (subrp (symbol-function 'md5)))
299             '("md5.el")))
300   "Files which will not be installed.")
301
302 (defconst dgnushack-exporting-files
303   (let ((files (directory-files srcdir nil "^[^=].*\\.el$" t)))
304     (dolist (file dgnushack-unexporting-files)
305       (setq files (delete file files)))
306     (sort files 'string-lessp))
307   "Files which will be compiled and installed.")
308
309 (defun dgnushack-exporting-files ()
310   "Print name of files which will be installed."
311   (princ (mapconcat 'identity dgnushack-exporting-files " ")))
312
313 (defun dgnushack-compile (&optional warn)
314   ;;(setq byte-compile-dynamic t)
315   (unless warn
316     (setq byte-compile-warnings
317           '(free-vars unresolved callargs redefine)))
318   (unless (locate-library "cus-edit")
319     (error "You do not seem to have Custom installed.
320 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
321 You also then need to add the following to the lisp/dgnushack.el file:
322
323      (push \"~/lisp/custom\" load-path)
324
325 Modify to suit your needs."))
326
327   ;; Show `load-path'.
328   (message "load-path=(\"%s\")"
329            (mapconcat 'identity load-path "\"\n           \""))
330
331   (dolist (file dgnushack-exporting-files)
332     (setq file (expand-file-name file srcdir))
333     (when (and (file-exists-p
334                 (setq elc (concat (file-name-nondirectory file) "c")))
335                (file-newer-than-file-p file elc))
336       (delete-file elc)))
337
338   (let ((files dgnushack-exporting-files)
339         ;;(byte-compile-generate-call-tree t)
340         file elc)
341     (while (setq file (pop files))
342       (setq file (expand-file-name file srcdir))
343       (when (or (not (file-exists-p
344                       (setq elc (concat (file-name-nondirectory file) "c"))))
345                 (file-newer-than-file-p file elc))
346         (ignore-errors
347           (byte-compile-file file))))))
348
349 (defun dgnushack-recompile ()
350   (require 'gnus)
351   (byte-recompile-directory "." 0))
352
353 \f
354 (defun dgnushack-texi-add-suffix-and-format ()
355   (dgnushack-texi-format t))
356
357 (defun dgnushack-texi-format (&optional addsuffix)
358   (if (not noninteractive)
359       (error "batch-texinfo-format may only be used -batch."))
360   (require 'ptexinfmt)
361   (let ((auto-save-default nil)
362         (find-file-run-dired nil)
363         coding-system-for-write
364         output-coding-system)
365     (let ((error 0)
366           file
367           (files ()))
368       (while command-line-args-left
369         (setq file (expand-file-name (car command-line-args-left)))
370         (cond ((not (file-exists-p file))
371                (message ">> %s does not exist!" file)
372                (setq error 1
373                      command-line-args-left (cdr command-line-args-left)))
374               ((file-directory-p file)
375                (setq command-line-args-left
376                      (nconc (directory-files file nil nil t)
377                             (cdr command-line-args-left))))
378               (t
379                (setq files (cons file files)
380                      command-line-args-left (cdr command-line-args-left)))))
381       (while (setq file (pop files))
382         (condition-case err
383             (progn
384               (if buffer-file-name (kill-buffer (current-buffer)))
385               (find-file file)
386               (buffer-disable-undo (current-buffer))
387               (if (boundp 'MULE)
388                   (setq output-coding-system (symbol-value
389                                               'file-coding-system))
390                 (setq coding-system-for-write buffer-file-coding-system))
391               ;; Remove ignored areas first.
392               (while (re-search-forward "^@ignore[\t\r ]*$" nil t)
393                 (delete-region (match-beginning 0)
394                                (if (re-search-forward
395                                     "^@end[\t ]+ignore[\t\r ]*$" nil t)
396                                    (1+ (match-end 0))
397                                  (point-max))))
398               (goto-char (point-min))
399               ;; formerly EMACSINFOHACK in texi/Makefile.
400               (while (re-search-forward "@\\(end \\)?ifnottex\n*" nil t)
401                 (replace-match ""))
402               (goto-char (point-min))
403               ;; Add suffix if it is needed.
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                 (goto-char (point-min)))
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 \f
468 (defconst dgnushack-info-file-regexp-en
469   (let ((names '("gnus" "message" "emacs-mime"))
470         regexp name)
471     (while (setq name (pop names))
472       (setq regexp (concat regexp "^" name "\\.info\\(-[0-9]+\\)?$"
473                            (when names "\\|"))))
474     regexp)
475   "Regexp matching English info files.")
476
477 (defconst dgnushack-info-file-regexp-ja
478   (let ((names '("gnus-ja" "message-ja"))
479         regexp name)
480     (while (setq name (pop names))
481       (setq regexp (concat regexp "^" name "\\.info\\(-[0-9]+\\)?$"
482                            (when names "\\|"))))
483     regexp)
484   "Regexp matching Japanese info files.")
485
486 (defun dgnushack-make-autoloads ()
487   "Make auto-autoloads.el, custom-load.el and then compile them."
488   (let ((auto-autoloads (expand-file-name "auto-autoloads.el" srcdir))
489         (custom-load (expand-file-name "custom-load.el" srcdir)))
490     (unless (and (file-exists-p auto-autoloads)
491                  (file-exists-p (concat auto-autoloads "c"))
492                  (file-newer-than-file-p (concat auto-autoloads "c")
493                                          auto-autoloads)
494                  (file-exists-p custom-load)
495                  (file-exists-p (concat custom-load "c"))
496                  (file-newer-than-file-p (concat custom-load "c")
497                                          custom-load))
498       (let (make-backup-files)
499         (message "Updating autoloads for directory %s..." default-directory)
500         (let ((generated-autoload-file auto-autoloads)
501               (si:message (symbol-function 'message))
502               noninteractive)
503           (defun message (fmt &rest args)
504             (cond ((and (string-equal "Generating autoloads for %s..." fmt)
505                         (file-exists-p (file-name-nondirectory (car args))))
506                    (funcall si:message
507                             fmt (file-name-nondirectory (car args))))
508                   ((string-equal "No autoloads found in %s" fmt))
509                   ((string-equal "Generating autoloads for %s...done" fmt))
510                   (t (apply si:message fmt args))))
511           (unwind-protect
512               (update-autoloads-from-directory default-directory)
513             (fset 'message si:message)))
514         (byte-compile-file auto-autoloads)
515         (with-temp-buffer
516           (let ((standard-output (current-buffer)))
517             (Custom-make-dependencies "."))
518           (message "%s" (buffer-string)))
519         (require 'cus-load)
520         (byte-compile-file custom-load)))))
521
522 (defun dgnushack-remove-extra-files-in-package ()
523   "Remove extra files in the lisp directory of the XEmacs package."
524   (let ((lisp-dir (expand-file-name (concat "lisp/"
525                                             ;; GNUS_PRODUCT_NAME
526                                             (cadr command-line-args-left)
527                                             "/")
528                                     ;; PACKAGEDIR
529                                     (car command-line-args-left))))
530     (when (file-directory-p lisp-dir)
531       (let (files)
532         (dolist (file dgnushack-exporting-files)
533           (setq files (nconc files (list file (concat file "c")))))
534         (dolist (file (directory-files lisp-dir nil nil t t))
535           (unless (member file files)
536             (setq file (expand-file-name file lisp-dir))
537             (message "Removing %s..." file)
538             (condition-case nil
539                 (delete-file file)
540               (error nil))))))))
541
542 (defun dgnushack-install-package-manifest ()
543   "Install MANIFEST file as an XEmacs package."
544   (let* ((package-dir (car command-line-args-left))
545          (product-name (cadr command-line-args-left))
546          (name (expand-file-name (concat "pkginfo/MANIFEST." product-name)
547                                  package-dir))
548          make-backup-files)
549     (message "Generating %s..." name)
550     (with-temp-file name
551       (insert "pkginfo/MANIFEST." product-name "\n")
552       (let ((lisp-dir (concat "lisp/" product-name "/"))
553             (files (sort (directory-files "." nil "\\.elc?$" t) 'string-lessp))
554             file)
555         (while (setq file (pop files))
556           (unless (member file dgnushack-unexporting-files)
557             (insert lisp-dir file "\n")))
558         (setq files
559               (sort (directory-files "../texi/" nil
560                                      (concat dgnushack-info-file-regexp-en
561                                              "\\|"
562                                              dgnushack-info-file-regexp-ja)
563                                      t)
564                     'string-lessp))
565         (while (setq file (pop files))
566           (insert "info/" file "\n"))))))
567
568 \f
569 (define-compiler-macro describe-key-briefly (&whole form key &optional insert)
570   (if (condition-case nil
571           (progn
572             (describe-key-briefly '((())) nil)
573             t)
574         (wrong-number-of-arguments nil);; Old Emacsen.
575         (error t))
576       form
577     (if insert
578         `(if ,insert
579              (insert (funcall 'describe-key-briefly ,key))
580            (funcall 'describe-key-briefly ,key))
581       `(funcall 'describe-key-briefly ,key))))
582
583 ;;; dgnushack.el ends here