Add `byte-optimizer' to `buffer-string', `backward-char' and `char-before'.
[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 (fset 'facep 'ignore)
31
32 (require 'cl)
33 (require 'bytecomp)
34 (push "~/lisp/custom" load-path)
35 (push "." load-path)
36 (load "./lpath.el" nil t)
37
38 (defalias 'device-sound-enabled-p 'ignore)
39 (defalias 'play-sound-file 'ignore)
40 (defalias 'nndb-request-article 'ignore)
41 (defalias 'efs-re-read-dir 'ignore)
42 (defalias 'ange-ftp-re-read-dir 'ignore)
43 (defalias 'define-mail-user-agent 'ignore)
44
45 (eval-and-compile
46   (unless (string-match "XEmacs" emacs-version)
47     (fset 'get-popup-menu-response 'ignore)
48     (fset 'event-object 'ignore)
49     (fset 'x-defined-colors 'ignore)
50     (fset 'read-color 'ignore)))
51
52 (eval-and-compile
53   (put 'buffer-string 'byte-optimizer 'byte-optimize-buffer-string)
54   (defun byte-optimize-buffer-string (form)
55     (if (null (cdr form))
56         (list 'buffer-substring (list 'point-min)(list 'point-max))
57       form))
58
59   (put 'backward-char 'byte-optimizer 'byte-optimize-backward-char)
60   (defun byte-optimize-backward-char (form)
61     (if (null (cdr form))
62         (list 'forward-char -1)
63       (list 'forward-char (list '- (nth 1 form)))))
64
65   (when (or (featurep 'xemacs)
66             (and (= emacs-major-version 20)
67                  (> emacs-minor-version 2)))
68     ;; this optimization is not safe under Emacs 20.2 and before.
69     (put 'char-before 'byte-optimizer 'byte-optimize-char-before)
70     (defun byte-optimize-char-before (form)
71       (if (null (cdr form))
72           (list 'char-after (list '1- (list 'point)))
73         (list 'char-after (list '1- (nth 1 form)))))))
74
75 (defun dgnushack-compile (&optional warn)
76   ;;(setq byte-compile-dynamic t)
77   (unless warn
78     (setq byte-compile-warnings
79           '(free-vars unresolved callargs redefine)))
80   (unless (locate-library "cus-edit")
81     (error "You do not seem to have Custom installed.
82 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
83 You also then need to add the following to the lisp/dgnushack.el file:
84
85      (push \"~/lisp/custom\" load-path)
86
87 Modify to suit your needs."))
88   (let ((files (directory-files "." nil "^[^=].*\\.el$"))
89         (xemacs (string-match "XEmacs" emacs-version))
90         ;;(byte-compile-generate-call-tree t)
91         file elc)
92     (condition-case ()
93         (require 'w3-forms)
94       (error (setq files (delete "nnweb.el" (delete "nnlistserv.el" files)))))
95     (condition-case ()
96         (require 'bbdb)
97       (error (setq files (delete "gnus-bbdb.el"
98                                  (delete "mess-bbdb.el" files)))))
99     (while (setq file (pop files))
100       (when (or (and (not xemacs)
101                      (not (member file '("gnus-xmas.el" "gnus-picon.el"
102                                          "messagexmas.el" "nnheaderxm.el"
103                                          "smiley.el" "x-overlay.el"))))
104                 (and xemacs
105                      (not (member file '("md5.el")))))
106         (when (or (not (file-exists-p (setq elc (concat file "c"))))
107                   (file-newer-than-file-p file elc))
108           (ignore-errors
109             (byte-compile-file file)))))))
110
111 (defun dgnushack-recompile ()
112   (require 'gnus)
113   (byte-recompile-directory "." 0))
114
115 \f
116 ;; Avoid byte-compile warnings.
117 (defvar gnus-product-name)
118 (defvar early-package-load-path)
119 (defvar early-packages)
120 (defvar last-package-load-path)
121 (defvar last-packages)
122 (defvar late-package-load-path)
123 (defvar late-packages)
124
125 (defconst dgnushack-info-file-regexp
126   "^\\(gnus\\|message\\|gnus-ja\\|message-ja\\)\\.info\\(-[0-9]+\\)?$")
127
128 (defconst dgnushack-texi-file-regexp
129   "^\\(gnus\\|message\\|gnus-ja\\|message-ja\\)\\.texi$")
130
131 (defun dgnushack-make-package ()
132   (require 'gnus)
133   (let* ((product-name (downcase gnus-product-name))
134          (lisp-dir (concat "lisp/" product-name "/"))
135          make-backup-files)
136
137     (message "Updating autoloads for directory %s..." default-directory)
138     (let ((generated-autoload-file "auto-autoloads.el")
139           noninteractive)
140       (update-autoloads-from-directory default-directory))
141     (byte-compile-file "auto-autoloads.el")
142
143     (with-temp-buffer
144       (let ((standard-output (current-buffer)))
145         (Custom-make-dependencies "."))
146       (message (buffer-string)))
147     (require 'cus-load)
148     (byte-compile-file "custom-load.el")
149
150     (message "Generating MANIFEST.%s for the package..." product-name)
151     (with-temp-buffer
152       (insert "pkginfo/MANIFEST." product-name "\n"
153               lisp-dir
154               (mapconcat
155                'identity
156                (sort (directory-files "." nil "\\.elc?$")
157                      'string-lessp)
158                (concat "\n" lisp-dir))
159               "\ninfo/"
160               (mapconcat
161                'identity
162                (sort (directory-files "../texi/"
163                                       nil dgnushack-info-file-regexp)
164                      'string-lessp)
165                "\ninfo/")
166               "\n")
167       (write-file (concat "../MANIFEST." product-name)))))
168
169 (defun dgnushack-install-package ()
170   (let ((package-dir (car command-line-args-left))
171         dirs info-dir pkginfo-dir product-name lisp-dir manifest files)
172     (unless package-dir
173       (when (boundp 'early-packages)
174         (setq dirs (delq nil (append (when early-package-load-path
175                                        early-packages)
176                                      (when late-package-load-path
177                                        late-packages)
178                                      (when last-package-load-path
179                                        last-packages))))
180         (while (and dirs (not package-dir))
181           (when (file-exists-p (car dirs))
182             (setq package-dir (car dirs)
183                   dirs (cdr dirs))))))
184     (unless package-dir
185       (error "%s" "
186 You must specify the name of the package path as follows:
187
188 % make install-package PACKAGEDIR=/usr/local/lib/xemacs/xemacs-packages
189 "
190              ))
191     (setq info-dir (expand-file-name "info/" package-dir)
192           pkginfo-dir (expand-file-name "pkginfo/" package-dir))
193     (require 'gnus)
194     (setq product-name (downcase gnus-product-name)
195           lisp-dir (expand-file-name (concat "lisp/" product-name "/")
196                                      package-dir)
197           manifest (concat "MANIFEST." product-name))
198
199     (unless (file-directory-p lisp-dir)
200       (make-directory lisp-dir t))
201     (unless (file-directory-p info-dir)
202       (make-directory info-dir))
203     (unless (file-directory-p pkginfo-dir)
204       (make-directory pkginfo-dir))
205
206     (setq files (sort (directory-files "." nil "\\.elc?$") 'string-lessp))
207     (mapcar
208      (lambda (file)
209        (unless (member file files)
210          (setq file (expand-file-name file lisp-dir))
211          (message "Removing %s..." file)
212          (condition-case nil
213              (delete-file file)
214            (error nil))))
215      (directory-files lisp-dir nil nil nil t))
216     (mapcar
217      (lambda (file)
218        (message "Copying %s to %s..." file lisp-dir)
219        (copy-file file (expand-file-name file lisp-dir) t t))
220      files)
221
222     (mapcar
223      (lambda (file)
224        (message "Copying ../texi/%s to %s..." file info-dir)
225        (copy-file (expand-file-name file "../texi/")
226                   (expand-file-name file info-dir)
227                   t t))
228      (sort (directory-files "../texi/" nil dgnushack-info-file-regexp)
229            'string-lessp))
230
231     (message "Copying ../%s to %s..." manifest pkginfo-dir)
232     (copy-file (expand-file-name manifest "../")
233                (expand-file-name manifest pkginfo-dir) t t)
234
235     (message "Done")))
236
237 (defun dgnushack-add-info-suffix-maybe ()
238   ;; This function must be invoked from lisp directory.
239   (setq default-directory "../texi/")
240   (let ((coding-system-for-read 'raw-text)
241         (coding-system-for-write 'raw-text)
242         (files (directory-files "." nil dgnushack-texi-file-regexp))
243         file make-backup-files)
244     (while (setq file (pop files))
245       (find-file file)
246       (when (and (re-search-forward
247                   "^@setfilename[\t ]+\\([^\t\n ]+\\)" nil t)
248                  (not (string-match "\\.info$" (match-string 1))))
249         (copy-file file (concat file "_") nil t)
250         (insert ".info")
251         (save-buffer))
252       (kill-buffer (current-buffer)))))
253
254 ;;; dgnushack.el ends here