Move compiler macros to gnus-clfns.el; load gnus-clfns.el.
[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 ;; If we are building w3 in a different directory than the source
54 ;; directory, we must read *.el from source directory and write *.elc
55 ;; into the building directory.  For that, we define this function
56 ;; before loading bytecomp.  Bytecomp doesn't overwrite this function.
57 (defun byte-compile-dest-file (filename)
58   "Convert an Emacs Lisp source file name to a compiled file name.
59  In addition, remove directory name part from FILENAME."
60   (setq filename (byte-compiler-base-file-name filename))
61   (setq filename (file-name-sans-versions filename))
62   (setq filename (file-name-nondirectory filename))
63   (if (memq system-type '(win32 w32 mswindows windows-nt))
64       (setq filename (downcase filename)))
65   (cond ((eq system-type 'vax-vms)
66          (concat (substring filename 0 (string-match ";" filename)) "c"))
67         ((string-match emacs-lisp-file-regexp filename)
68          (concat (substring filename 0 (match-beginning 0)) ".elc"))
69         (t (concat filename ".elc"))))
70
71 (require 'bytecomp)
72
73 (unless (fboundp 'si:byte-optimize-form-code-walker)
74   (byte-optimize-form nil);; Load `byte-opt' or `byte-optimize'.
75   (setq max-specpdl-size 3000)
76   (fset 'si:byte-optimize-form-code-walker
77         (symbol-function 'byte-optimize-form-code-walker))
78   (defun byte-optimize-form-code-walker (form for-effect)
79     (if (and for-effect (memq (car-safe form) '(and or)))
80         ;; Fix bug in and/or forms.
81         (let ((fn (car form))
82               (backwards (reverse (cdr form))))
83           (while (and backwards
84                       (null (setcar backwards
85                                     (byte-optimize-form (car backwards) t))))
86             (setq backwards (cdr backwards)))
87           (if (and (cdr form) (null backwards))
88               (byte-compile-log
89                "  all subforms of %s called for effect; deleted" form))
90           (if backwards
91               (let ((head backwards))
92                 (while (setq backwards (cdr backwards))
93                   (setcar backwards (byte-optimize-form (car backwards)
94                                                         nil)))
95                 (cons fn (nreverse head)))))
96       (si:byte-optimize-form-code-walker form for-effect)))
97   (byte-compile 'byte-optimize-form-code-walker))
98
99 (defvar srcdir (or (getenv "srcdir") "."))
100
101 (load (expand-file-name "gnus-clfns.el" srcdir) nil t t)
102
103 ;(push "/usr/share/emacs/site-lisp" load-path)
104
105 ;; Attempt to pickup the additional load-path(s).
106 (load (expand-file-name "dgnuspath.el" srcdir) nil nil t)
107 (condition-case err
108     (load "~/.lpath.el" t nil t)
109   (error (message "Error in \"~/.lpath.el\" file: %s" err)))
110
111 (condition-case nil
112     (char-after)
113   (wrong-number-of-arguments
114    ;; Optimize byte code for `char-after'.
115    (put 'char-after 'byte-optimizer 'byte-optimize-char-after)
116    (defun byte-optimize-char-after (form)
117      (if (null (cdr form))
118          '(char-after (point))
119        form))
120    ))
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
133 ;; `char-after' and `char-before' must be well-behaved before lpath.el
134 ;; is loaded.  Because it requires `poe' via `path-util'.
135 (load (expand-file-name "lpath.el" srcdir) nil t t)
136
137 (unless (fboundp 'byte-compile-file-form-custom-declare-variable)
138   ;; Bind defcustom'ed variables.
139   (put 'custom-declare-variable 'byte-hunk-handler
140        'byte-compile-file-form-custom-declare-variable)
141   (defun byte-compile-file-form-custom-declare-variable (form)
142     (if (memq 'free-vars byte-compile-warnings)
143         (setq byte-compile-bound-variables
144               (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
145     form))
146
147 ;; Bind functions defined by `defun-maybe'.
148 (put 'defun-maybe 'byte-hunk-handler 'byte-compile-file-form-defun-maybe)
149 (defun byte-compile-file-form-defun-maybe (form)
150   (if (and (not (fboundp (nth 1 form)))
151            (memq 'unresolved byte-compile-warnings))
152       (setq byte-compile-function-environment
153             (cons (cons (nth 1 form)
154                         (cons 'lambda (cdr (cdr form))))
155                   byte-compile-function-environment)))
156   form)
157
158 (condition-case nil
159     :symbol-for-testing-whether-colon-keyword-is-available-or-not
160   (void-variable
161    ;; Bind keywords.
162    (mapcar (lambda (keyword) (set keyword keyword))
163            '(:button-keymap :data :file :mime-handle))))
164
165 ;; Unknown variables and functions.
166 (unless (boundp 'buffer-file-coding-system)
167   (defvar buffer-file-coding-system (symbol-value 'file-coding-system)))
168 (unless (featurep 'xemacs)
169   (defalias 'Custom-make-dependencies 'ignore)
170   (defalias 'update-autoloads-from-directory 'ignore))
171 (autoload 'texinfo-parse-line-arg "texinfmt")
172
173 (unless (fboundp 'with-temp-buffer)
174   ;; Pickup some macros.
175   (require 'emu))
176
177 (defalias 'device-sound-enabled-p 'ignore)
178 (defalias 'play-sound-file 'ignore)
179 (defalias 'nndb-request-article 'ignore)
180 (defalias 'efs-re-read-dir 'ignore)
181 (defalias 'ange-ftp-re-read-dir 'ignore)
182 (defalias 'define-mail-user-agent 'ignore)
183
184 (defun dgnushack-compile (&optional warn)
185   ;;(setq byte-compile-dynamic t)
186   (unless warn
187     (setq byte-compile-warnings
188           '(free-vars unresolved callargs redefine)))
189   (unless (locate-library "cus-edit")
190     (error "You do not seem to have Custom installed.
191 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
192 You also then need to add the following to the lisp/dgnushack.el file:
193
194      (push \"~/lisp/custom\" load-path)
195
196 Modify to suit your needs."))
197   (let ((files (delete "dgnuspath.el"
198                        (directory-files srcdir nil "^[^=].*\\.el$")))
199         (xemacs (string-match "XEmacs" emacs-version))
200         ;;(byte-compile-generate-call-tree t)
201         file elc)
202     (condition-case ()
203         (require 'w3-forms)
204       (error
205        (dolist (file '("nnweb.el" "nnlistserv.el" "nnultimate.el"
206                        "nnslashdot.el" "nnwarchive.el" "webmail.el"))
207          (setq files (delete file files)))))
208     (condition-case ()
209         (require 'bbdb)
210       (error (setq files (delete "gnus-bbdb.el" files))))
211     (while (setq file (pop files))
212       (unless (or (and (not xemacs)
213                        (member file
214                                '("gnus-xmas.el" "gnus-picon.el"
215                                  "messagexmas.el" "nnheaderxm.el"
216                                  "smiley.el" "x-overlay.el")))
217                   (and (string-equal file "md5.el")
218                        (not (and (fboundp 'md5)
219                                  (subrp (symbol-function 'md5))))))
220         (setq file (expand-file-name file srcdir))
221         (when (or (not (file-exists-p (setq elc (concat file "c"))))
222                   (file-newer-than-file-p file elc))
223           (ignore-errors
224             (byte-compile-file file)))))))
225
226 (defun dgnushack-recompile ()
227   (require 'gnus)
228   (byte-recompile-directory "." 0))
229
230 \f
231 ;; Avoid byte-compile warnings.
232 (defvar gnus-product-name)
233 (defvar early-package-load-path)
234 (defvar early-packages)
235 (defvar last-package-load-path)
236 (defvar last-packages)
237 (defvar late-package-load-path)
238 (defvar late-packages)
239
240 (defconst dgnushack-info-file-regexp
241   (concat "^\\(gnus\\|message\\|emacs-mime\\|gnus-ja\\|message-ja\\)"
242           "\\.info\\(-[0-9]+\\)?$"))
243
244 (defconst dgnushack-texi-file-regexp
245   "^\\(gnus\\|message\\|emacs-mime\\|gnus-ja\\|message-ja\\)\\.texi$")
246
247 (defun dgnushack-make-package ()
248   (require 'gnus)
249   (let* ((product-name (downcase gnus-product-name))
250          (lisp-dir (concat "lisp/" product-name "/"))
251          make-backup-files)
252
253     (message "Updating autoloads for directory %s..." default-directory)
254     (let ((generated-autoload-file "auto-autoloads.el")
255           noninteractive
256           (omsg (symbol-function 'message)))
257       (defun message (fmt &rest args)
258         (cond ((and (string-equal "Generating autoloads for %s..." fmt)
259                     (file-exists-p (file-name-nondirectory (car args))))
260                (funcall omsg fmt (file-name-nondirectory (car args))))
261               ((string-equal "No autoloads found in %s" fmt))
262               ((string-equal "Generating autoloads for %s...done" fmt))
263               (t (apply omsg fmt args))))
264       (unwind-protect
265           (update-autoloads-from-directory default-directory)
266         (fset 'message omsg)))
267     (byte-compile-file "auto-autoloads.el")
268
269     (with-temp-buffer
270       (let ((standard-output (current-buffer)))
271         (Custom-make-dependencies "."))
272       (message (buffer-string)))
273     (require 'cus-load)
274     (byte-compile-file "custom-load.el")
275
276     (message "Generating MANIFEST.%s for the package..." product-name)
277     (with-temp-buffer
278       (insert "pkginfo/MANIFEST." product-name "\n"
279               lisp-dir
280               (mapconcat
281                'identity
282                (sort (delete "dgnuspath.el"
283                              (delete "patchs.elc"
284                                      (directory-files "." nil "\\.elc?$")))
285                      'string-lessp)
286                (concat "\n" lisp-dir))
287               "\ninfo/"
288               (mapconcat
289                'identity
290                (sort (directory-files "../texi/"
291                                       nil dgnushack-info-file-regexp)
292                      'string-lessp)
293                "\ninfo/")
294               "\n")
295       (write-file (concat "../MANIFEST." product-name)))))
296
297 (defun dgnushack-install-package ()
298   (let ((package-dir (car command-line-args-left))
299         dirs info-dir pkginfo-dir product-name lisp-dir manifest files)
300     (unless package-dir
301       (when (boundp 'early-packages)
302         (setq dirs (delq nil (append (when early-package-load-path
303                                        early-packages)
304                                      (when late-package-load-path
305                                        late-packages)
306                                      (when last-package-load-path
307                                        last-packages))))
308         (while (and dirs (not package-dir))
309           (when (file-exists-p (car dirs))
310             (setq package-dir (car dirs)
311                   dirs (cdr dirs))))))
312     (unless package-dir
313       (error "%s" "
314 You must specify the name of the package path as follows:
315
316 % make install-package PACKAGEDIR=/usr/local/lib/xemacs/xemacs-packages
317 "
318              ))
319     (setq info-dir (expand-file-name "info/" package-dir)
320           pkginfo-dir (expand-file-name "pkginfo/" package-dir))
321     (require 'gnus)
322     (setq product-name (downcase gnus-product-name)
323           lisp-dir (expand-file-name (concat "lisp/" product-name "/")
324                                      package-dir)
325           manifest (concat "MANIFEST." product-name))
326
327     (unless (file-directory-p lisp-dir)
328       (make-directory lisp-dir t))
329     (unless (file-directory-p info-dir)
330       (make-directory info-dir))
331     (unless (file-directory-p pkginfo-dir)
332       (make-directory pkginfo-dir))
333
334     (setq files
335           (sort (delete "dgnuspath.el"
336                         (delete "dgnuspath.elc"
337                                 (directory-files "." nil "\\.elc?$")))
338                 'string-lessp))
339     (mapcar
340      (lambda (file)
341        (unless (or (member file files)
342                    (not (string-match "\\.elc?$" file)))
343          (setq file (expand-file-name file lisp-dir))
344          (message "Removing %s..." file)
345          (condition-case nil
346              (delete-file file)
347            (error nil))))
348      (directory-files lisp-dir nil nil nil t))
349     (mapcar
350      (lambda (file)
351        (message "Copying %s to %s..." file lisp-dir)
352        (copy-file file (expand-file-name file lisp-dir) t t))
353      files)
354
355     (mapcar
356      (lambda (file)
357        (message "Copying ../texi/%s to %s..." file info-dir)
358        (copy-file (expand-file-name file "../texi/")
359                   (expand-file-name file info-dir)
360                   t t))
361      (sort (directory-files "../texi/" nil dgnushack-info-file-regexp)
362            'string-lessp))
363
364     (message "Copying ../%s to %s..." manifest pkginfo-dir)
365     (copy-file (expand-file-name manifest "../")
366                (expand-file-name manifest pkginfo-dir) t t)
367
368     (message "Done")))
369
370 (defun dgnushack-texi-add-suffix-and-format ()
371   (dgnushack-texi-format t))
372
373 (defun dgnushack-texi-format (&optional addsuffix)
374   (if (not noninteractive)
375       (error "batch-texinfo-format may only be used -batch."))
376   (require 'texinfmt)
377   (let ((auto-save-default nil)
378         (find-file-run-dired nil)
379         coding-system-for-write)
380     (let ((error 0)
381           file
382           (files ()))
383       (while command-line-args-left
384         (setq file (expand-file-name (car command-line-args-left)))
385         (cond ((not (file-exists-p file))
386                (message ">> %s does not exist!" file)
387                (setq error 1
388                      command-line-args-left (cdr command-line-args-left)))
389               ((file-directory-p file)
390                (setq command-line-args-left
391                      (nconc (directory-files file)
392                             (cdr command-line-args-left))))
393               (t
394                (setq files (cons file files)
395                      command-line-args-left (cdr command-line-args-left)))))
396       (while files
397         (setq file (car files)
398               files (cdr files))
399         (condition-case err
400             (progn
401               (if buffer-file-name (kill-buffer (current-buffer)))
402               (find-file file)
403               (setq coding-system-for-write buffer-file-coding-system)
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               (buffer-disable-undo (current-buffer))
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 ;;; dgnushack.el ends here