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