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