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