Import ptexinfmt.el from Wanderlust.
[elisp/gnus.git-] / lisp / dgnushack.el
1 ;;; dgnushack.el --- a hack to set the load path for byte-compiling
2 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
7 ;; Version: 4.19
8 ;; Keywords: news, path
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 ;; Set coding priority of Shift-JIS to the bottom.
32 (defvar *predefined-category*)
33 (defvar coding-category-list)
34 (if (featurep 'xemacs)
35     (fset 'set-coding-priority 'ignore)
36   (fset 'coding-priority-list 'ignore)
37   (fset 'set-coding-priority-list 'ignore))
38 (cond ((and (featurep 'xemacs) (featurep 'mule))
39        (if (memq 'shift-jis (coding-priority-list))
40            (set-coding-priority-list
41             (nconc (delq 'shift-jis (coding-priority-list)) '(shift-jis)))))
42       ((boundp 'MULE)
43        (put '*coding-category-sjis* 'priority (length *predefined-category*)))
44       ((featurep 'mule)
45        (if (memq 'coding-category-sjis coding-category-list)
46            (set-coding-priority
47             (nconc (delq 'coding-category-sjis coding-category-list)
48                    '(coding-category-sjis))))))
49
50 (fset 'facep 'ignore)
51
52 (require 'cl)
53
54 (defvar srcdir (or (getenv "srcdir") "."))
55
56 (push (or (getenv "W3DIR") (expand-file-name "../../w3/lisp/" srcdir))
57       load-path)
58 (load (expand-file-name "dgnuspath.el" srcdir) nil nil t)
59
60 ;; If we are building w3 in a different directory than the source
61 ;; directory, we must read *.el from source directory and write *.elc
62 ;; into the building directory.  For that, we define this function
63 ;; before loading bytecomp.  Bytecomp doesn't overwrite this function.
64 (defun byte-compile-dest-file (filename)
65   "Convert an Emacs Lisp source file name to a compiled file name.
66  In addition, remove directory name part from FILENAME."
67   (setq filename (byte-compiler-base-file-name filename))
68   (setq filename (file-name-sans-versions filename))
69   (setq filename (file-name-nondirectory filename))
70   (if (memq system-type '(win32 w32 mswindows windows-nt))
71       (setq filename (downcase filename)))
72   (cond ((eq system-type 'vax-vms)
73          (concat (substring filename 0 (string-match ";" filename)) "c"))
74         ((string-match emacs-lisp-file-regexp filename)
75          (concat (substring filename 0 (match-beginning 0)) ".elc"))
76         (t (concat filename ".elc"))))
77
78 (require 'bytecomp)
79
80 (unless (fboundp 'si:byte-optimize-form-code-walker)
81   (byte-optimize-form nil);; Load `byte-opt' or `byte-optimize'.
82   (setq max-specpdl-size 3000)
83   (fset 'si:byte-optimize-form-code-walker
84         (symbol-function 'byte-optimize-form-code-walker))
85   (defun byte-optimize-form-code-walker (form for-effect)
86     (if (and for-effect (memq (car-safe form) '(and or)))
87         ;; Fix bug in and/or forms.
88         (let ((fn (car form))
89               (backwards (reverse (cdr form))))
90           (while (and backwards
91                       (null (setcar backwards
92                                     (byte-optimize-form (car backwards) t))))
93             (setq backwards (cdr backwards)))
94           (if (and (cdr form) (null backwards))
95               (byte-compile-log
96                "  all subforms of %s called for effect; deleted" form))
97           (if backwards
98               (let ((head backwards))
99                 (while (setq backwards (cdr backwards))
100                   (setcar backwards (byte-optimize-form (car backwards)
101                                                         nil)))
102                 (cons fn (nreverse head)))))
103       (si:byte-optimize-form-code-walker form for-effect)))
104   (byte-compile 'byte-optimize-form-code-walker))
105
106 (load (expand-file-name "gnus-clfns.el" srcdir) nil t t)
107
108 (condition-case err
109     (load "~/.lpath.el" t nil t)
110   (error (message "Error in \"~/.lpath.el\" file: %s" err)))
111
112 (condition-case nil
113     (char-after)
114   (wrong-number-of-arguments
115    ;; Optimize byte code for `char-after'.
116    (put 'char-after 'byte-optimizer 'byte-optimize-char-after)
117    (defun byte-optimize-char-after (form)
118      (if (null (cdr form))
119          '(char-after (point))
120        form))
121    ))
122
123 (condition-case nil
124     (char-before)
125   (wrong-number-of-arguments
126    ;; Optimize byte code for `char-before'.
127    (put 'char-before 'byte-optimizer 'byte-optimize-char-before)
128    (defun byte-optimize-char-before (form)
129      (if (null (cdr form))
130          '(char-before (point))
131        form))
132    ))
133
134 ;; `char-after' and `char-before' must be well-behaved before lpath.el
135 ;; is loaded.  Because it requires `poe' via `path-util'.
136 (load (expand-file-name "lpath.el" srcdir) nil t t)
137
138 (unless (fboundp 'byte-compile-file-form-custom-declare-variable)
139   ;; Bind defcustom'ed variables.
140   (put 'custom-declare-variable 'byte-hunk-handler
141        'byte-compile-file-form-custom-declare-variable)
142   (defun byte-compile-file-form-custom-declare-variable (form)
143     (if (memq 'free-vars byte-compile-warnings)
144         (setq byte-compile-bound-variables
145               (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
146     form))
147
148 ;; Bind functions defined by `defun-maybe'.
149 (put 'defun-maybe 'byte-hunk-handler 'byte-compile-file-form-defun-maybe)
150 (defun byte-compile-file-form-defun-maybe (form)
151   (if (and (not (fboundp (nth 1 form)))
152            (memq 'unresolved byte-compile-warnings))
153       (setq byte-compile-function-environment
154             (cons (cons (nth 1 form)
155                         (cons 'lambda (cdr (cdr form))))
156                   byte-compile-function-environment)))
157   form)
158
159 (condition-case nil
160     :symbol-for-testing-whether-colon-keyword-is-available-or-not
161   (void-variable
162    ;; Bind keywords.
163    (mapcar (lambda (keyword) (set keyword keyword))
164            '(:button-keymap :data :file :mime-handle))))
165
166 ;; Unknown variables and functions.
167 (unless (boundp 'buffer-file-coding-system)
168   (defvar buffer-file-coding-system (symbol-value 'file-coding-system)))
169 (unless (featurep 'xemacs)
170   (defalias 'Custom-make-dependencies 'ignore)
171   (defalias 'update-autoloads-from-directory 'ignore))
172 (autoload 'texinfo-parse-line-arg "texinfmt")
173
174 (unless (fboundp 'with-temp-buffer)
175   ;; Pickup some macros.
176   (require 'emu))
177
178 (defalias 'device-sound-enabled-p 'ignore)
179 (defalias 'play-sound-file 'ignore)
180 (defalias 'nndb-request-article 'ignore)
181 (defalias 'efs-re-read-dir 'ignore)
182 (defalias 'ange-ftp-re-read-dir 'ignore)
183 (defalias 'define-mail-user-agent 'ignore)
184
185 (defconst dgnushack-tool-files
186   '("dgnushack.el" "dgnuspath.el" "ptexinfmt.el"))
187 (defconst dgnushack-unexported-files
188   '("dgnuspath.el" "ptexinfmt.el"))
189
190 (defun dgnushack-compile (&optional warn)
191   ;;(setq byte-compile-dynamic t)
192   (unless warn
193     (setq byte-compile-warnings
194           '(free-vars unresolved callargs redefine)))
195   (unless (locate-library "cus-edit")
196     (error "You do not seem to have Custom installed.
197 Fetch it from <URL:http://www.dina.kvl.dk/~abraham/custom/>.
198 You also then need to add the following to the lisp/dgnushack.el file:
199
200      (push \"~/lisp/custom\" load-path)
201
202 Modify to suit your needs."))
203   (let ((files (directory-files srcdir nil "^[^=].*\\.el$"))
204         ;;(byte-compile-generate-call-tree t)
205         file elc)
206     (mapcar
207      (lambda (el) (setq files (delete el files)))
208      (nconc
209       dgnushack-tool-files
210       (condition-case nil
211           (progn (require 'w3-forms) nil)
212         (error '("nnweb.el" "nnlistserv.el" "nnultimate.el"
213                  "nnslashdot.el" "nnwarchive.el" "webmail.el")))
214       (condition-case nil
215           (progn (require 'bbdb) nil)
216         (error '("gnus-bbdb.el")))
217       (unless (featurep 'xemacs)
218         '("gnus-xmas.el" "gnus-picon.el" "messagexmas.el"
219           "nnheaderxm.el" "smiley.el"))
220       (when (and (fboundp 'md5) (subrp (symbol-function 'md5)))
221         '("md5.el"))))
222     (while (setq file (pop files))
223       (setq file (expand-file-name file srcdir))
224       (when (or (not (file-exists-p (setq elc (concat file "c"))))
225                 (file-newer-than-file-p file elc))
226         (ignore-errors
227           (byte-compile-file file))))))
228
229 (defun dgnushack-recompile ()
230   (require 'gnus)
231   (byte-recompile-directory "." 0))
232
233 \f
234 ;; Avoid byte-compile warnings.
235 (defvar gnus-product-name)
236 (defvar early-package-load-path)
237 (defvar early-packages)
238 (defvar last-package-load-path)
239 (defvar last-packages)
240 (defvar late-package-load-path)
241 (defvar late-packages)
242
243 (defconst dgnushack-info-file-regexp
244   (concat "^\\(gnus\\|message\\|emacs-mime\\|gnus-ja\\|message-ja\\)"
245           "\\.info\\(-[0-9]+\\)?$"))
246
247 (defconst dgnushack-texi-file-regexp
248   "^\\(gnus\\|message\\|emacs-mime\\|gnus-ja\\|message-ja\\)\\.texi$")
249
250 (defun dgnushack-make-package ()
251   (require 'gnus)
252   (let* ((product-name (downcase gnus-product-name))
253          (lisp-dir (concat "lisp/" product-name "/"))
254          make-backup-files)
255
256     (message "Updating autoloads for directory %s..." default-directory)
257     (let ((generated-autoload-file "auto-autoloads.el")
258           noninteractive
259           (omsg (symbol-function 'message)))
260       (defun message (fmt &rest args)
261         (cond ((and (string-equal "Generating autoloads for %s..." fmt)
262                     (file-exists-p (file-name-nondirectory (car args))))
263                (funcall omsg fmt (file-name-nondirectory (car args))))
264               ((string-equal "No autoloads found in %s" fmt))
265               ((string-equal "Generating autoloads for %s...done" fmt))
266               (t (apply omsg fmt args))))
267       (unwind-protect
268           (update-autoloads-from-directory default-directory)
269         (fset 'message omsg)))
270     (byte-compile-file "auto-autoloads.el")
271
272     (with-temp-buffer
273       (let ((standard-output (current-buffer)))
274         (Custom-make-dependencies "."))
275       (message "%s" (buffer-string)))
276     (require 'cus-load)
277     (byte-compile-file "custom-load.el")
278
279     (message "Generating MANIFEST.%s for the package..." product-name)
280     (with-temp-buffer
281       (insert "pkginfo/MANIFEST." product-name "\n")
282       (mapcar
283        (lambda (file)
284          (unless (member file dgnushack-unexported-files)
285            (insert lisp-dir file "\n")))
286        (sort (directory-files "." nil "\\.elc?$") 'string-lessp))
287       (mapcar
288        (lambda (file) (insert "info/" file "\n"))
289        (sort (directory-files "../texi/" nil dgnushack-info-file-regexp)
290              'string-lessp))
291       (write-file (concat "../MANIFEST." product-name)))))
292
293 (defun dgnushack-install-package ()
294   (let ((package-dir (car command-line-args-left))
295         dirs info-dir pkginfo-dir product-name lisp-dir manifest files)
296     (unless package-dir
297       (when (boundp 'early-packages)
298         (setq dirs (delq nil (append (when early-package-load-path
299                                        early-packages)
300                                      (when late-package-load-path
301                                        late-packages)
302                                      (when last-package-load-path
303                                        last-packages))))
304         (while (and dirs (not package-dir))
305           (when (file-exists-p (car dirs))
306             (setq package-dir (car dirs)
307                   dirs (cdr dirs))))))
308     (unless package-dir
309       (error "%s" "
310 You must specify the name of the package path as follows:
311
312 % make install-package PACKAGEDIR=/usr/local/lib/xemacs/xemacs-packages
313 "
314              ))
315     (setq info-dir (expand-file-name "info/" package-dir)
316           pkginfo-dir (expand-file-name "pkginfo/" package-dir))
317     (require 'gnus)
318     (setq product-name (downcase gnus-product-name)
319           lisp-dir (expand-file-name (concat "lisp/" product-name "/")
320                                      package-dir)
321           manifest (concat "MANIFEST." product-name))
322
323     (unless (file-directory-p lisp-dir)
324       (make-directory lisp-dir t))
325     (unless (file-directory-p info-dir)
326       (make-directory info-dir))
327     (unless (file-directory-p pkginfo-dir)
328       (make-directory pkginfo-dir))
329
330     (setq files (sort (directory-files "." nil "\\.elc?$") 'string-lessp))
331     (mapcar (lambda (el) (setq files (delete el files)))
332             dgnushack-unexported-files)
333     (mapcar
334      (lambda (file)
335        (unless (or (member file files)
336                    (not (string-match "\\.elc?$" file)))
337          (setq file (expand-file-name file lisp-dir))
338          (message "Removing %s..." file)
339          (condition-case nil
340              (delete-file file)
341            (error nil))))
342      (directory-files lisp-dir nil nil nil t))
343     (mapcar
344      (lambda (file)
345        (message "Copying %s to %s..." file lisp-dir)
346        (copy-file file (expand-file-name file lisp-dir) t t))
347      files)
348
349     (mapcar
350      (lambda (file)
351        (message "Copying ../texi/%s to %s..." file info-dir)
352        (copy-file (expand-file-name file "../texi/")
353                   (expand-file-name file info-dir)
354                   t t))
355      (sort (directory-files "../texi/" nil dgnushack-info-file-regexp)
356            'string-lessp))
357
358     (message "Copying ../%s to %s..." manifest pkginfo-dir)
359     (copy-file (expand-file-name manifest "../")
360                (expand-file-name manifest pkginfo-dir) t t)
361
362     (message "Done")))
363
364 (defun dgnushack-texi-add-suffix-and-format ()
365   (dgnushack-texi-format t))
366
367 (defun dgnushack-texi-format (&optional addsuffix)
368   (if (not noninteractive)
369       (error "batch-texinfo-format may only be used -batch."))
370   (let ((auto-save-default nil)
371         (find-file-run-dired nil)
372         coding-system-for-write
373         output-coding-system)
374     (let ((error 0)
375           file
376           (files ()))
377       (while command-line-args-left
378         (setq file (expand-file-name (car command-line-args-left)))
379         (cond ((not (file-exists-p file))
380                (message ">> %s does not exist!" file)
381                (setq error 1
382                      command-line-args-left (cdr command-line-args-left)))
383               ((file-directory-p file)
384                (setq command-line-args-left
385                      (nconc (directory-files file)
386                             (cdr command-line-args-left))))
387               (t
388                (setq files (cons file files)
389                      command-line-args-left (cdr command-line-args-left)))))
390       (while files
391         (setq file (car files)
392               files (cdr files))
393         (condition-case err
394             (progn
395               (if buffer-file-name (kill-buffer (current-buffer)))
396               (find-file file)
397               (if (boundp 'MULE)
398                   (setq output-coding-system (symbol-value
399                                               'file-coding-system))
400                 (setq coding-system-for-write buffer-file-coding-system))
401               (when (and addsuffix
402                          (re-search-forward
403                           "^@setfilename[\t ]+\\([^\t\n ]+\\)" nil t)
404                          (not (string-match "\\.info$" (match-string 1))))
405                 (insert ".info"))
406               (buffer-disable-undo (current-buffer))
407               ;; process @include before updating node
408               ;; This might produce some problem if we use @lowersection or
409               ;; such.
410               (let ((input-directory default-directory)
411                     (texinfo-command-end))
412                 (while (re-search-forward "^@include" nil t)
413                   (setq texinfo-command-end (point))
414                   (let ((filename (concat input-directory
415                                           (texinfo-parse-line-arg))))
416                     (re-search-backward "^@include")
417                     (delete-region (point) (save-excursion
418                                              (forward-line 1)
419                                              (point)))
420                     (message "Reading included file: %s" filename)
421                     (save-excursion
422                       (save-restriction
423                         (narrow-to-region
424                          (point)
425                          (+ (point)
426                             (car (cdr (insert-file-contents filename)))))
427                         (goto-char (point-min))
428                         ;; Remove `@setfilename' line from included file,
429                         ;; if any, so @setfilename command not duplicated.
430                         (if (re-search-forward "^@setfilename"
431                                                (save-excursion
432                                                  (forward-line 100)
433                                                  (point))
434                                                t)
435                             (progn
436                               (beginning-of-line)
437                               (delete-region (point) (save-excursion
438                                                        (forward-line 1)
439                                                        (point))))))))))
440               (texinfo-mode)
441               (texinfo-every-node-update)
442               (set-buffer-modified-p nil)
443               (message "texinfo formatting %s..." file)
444               (texinfo-format-buffer nil)
445               (if (buffer-modified-p)
446                   (progn (message "Saving modified %s" (buffer-file-name))
447                          (save-buffer))))
448           (error
449            (message ">> Error: %s" (prin1-to-string err))
450            (message ">>  point at")
451            (let ((s (buffer-substring (point)
452                                       (min (+ (point) 100)
453                                            (point-max))))
454                  (tem 0))
455              (while (setq tem (string-match "\n+" s tem))
456                (setq s (concat (substring s 0 (match-beginning 0))
457                                "\n>>  "
458                                (substring s (match-end 0)))
459                      tem (1+ tem)))
460              (message ">>  %s" s))
461            (setq error 1))))
462       (kill-emacs error))))
463
464 ;;; dgnushack.el ends here