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