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