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