Merge from trunk
[elisp/wanderlust.git] / WL-MK
1 ;;; -*- Emacs-Lisp -*-
2 ;;; WL-MK for byte-compile, install, uninstall
3 ;;;
4 ;;; Original by OKUNISHI Fujikazu <fuji0924@mbox.kyoto-inet.or.jp>
5 ;;; Modified by Yuuichi Teranishi <teranisi@gohome.org>
6
7 ;;;;;;;;;;;;;;;;;;;;;   DO NOT EDIT THIS FILE   ;;;;;;;;;;;;;;;;;;;;;
8 ;;;;;;;;;;;;;;;;;;;;;     INTERNAL USE ONLY     ;;;;;;;;;;;;;;;;;;;;;
9
10 ;;; Code
11
12 (defvar WLDIR "./wl")
13 (defvar ELMODIR "./elmo")
14 (defvar DOCDIR "./doc")
15 (defvar ICONDIR "./etc/icons")
16 (defvar UTILSDIR "./utils")
17 (defvar WL_PREFIX "wl")
18 (defvar ELMO_PREFIX "wl")
19
20 (defvar COMPRESS-SUFFIX-LIST '("" ".gz" ".Z" ".bz2"))
21
22 (defvar wl-install-utils nil
23   "If Non-nil, install `wl-utils-modules'.")
24
25 ;;; INFO
26 (defconst wl-ja-info "wl-ja.info")
27 (defconst wl-ja-texi "wl-ja.texi")
28 (defconst wl-en-info "wl.info")
29 (defconst wl-en-texi "wl.texi")
30
31 (defvar wl-info-lang '("ja" "en")
32   "The language of info file (\"ja\" or \"en\").")
33
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 \f
36 (require 'cl)
37 (defvar INFODIR nil)
38
39 (condition-case () (require 'custom) (error nil))
40 ;; for wl-vars.el
41 (unless (and (fboundp 'defgroup)
42              (fboundp 'defcustom)
43              ;; ignore broken module
44              (not (featurep 'tinycustom)))
45   (when (and (boundp 'emacs-major-version)
46              (= emacs-major-version 19)
47              (>= emacs-minor-version 29))
48     (message "%s" "
49   Warning: You don't seem to have \"new custom\" package installed.
50            See README file of APEL package for more information.
51 "))
52   (require 'backquote)
53   (defmacro defgroup (&rest args))
54   (defmacro defcustom (symbol value &optional doc &rest args)
55     (let ((doc (concat "*" (or doc ""))))
56       (` (defvar (, symbol) (, value) (, doc))))))
57
58 (load "bytecomp" nil t)
59
60 (unless (fboundp 'byte-compile-file-form-custom-declare-variable)
61   ;; Bind defcustom'ed variables.
62   (put 'custom-declare-variable 'byte-hunk-handler
63        'byte-compile-file-form-custom-declare-variable)
64   (defun byte-compile-file-form-custom-declare-variable (form)
65     (if (memq 'free-vars byte-compile-warnings)
66         (setq byte-compile-bound-variables
67               (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
68     form))
69
70 (condition-case nil
71     (char-after)
72   (wrong-number-of-arguments
73    ;; Optimize byte code for `char-after'.
74    (put 'char-after 'byte-optimizer 'byte-optimize-char-after)
75    (defun byte-optimize-char-after (form)
76      (if (null (cdr form))
77          '(char-after (point))
78        form))))
79
80 (setq byte-compile-warnings '(free-vars unresolved callargs redefine))
81
82 ;; v18, v19
83 (if (boundp 'MULE)
84     (setq max-lisp-eval-depth 400))
85
86 ;; FIXME: it is currently needed to byte-compile with Emacs 21.
87 (setq recursive-load-depth-limit nil)
88
89 (condition-case () (require 'easymenu) (error nil))
90
91 (defvar config-wl-package-done nil)
92
93 (defun config-wl-package-subr ()
94   (unless config-wl-package-done
95     (setq config-wl-package-done t)
96     (setq load-path (cons (expand-file-name ".") load-path))
97     (setq load-path (cons (expand-file-name WLDIR)
98                           (cons (expand-file-name ELMODIR) load-path)))
99     ;; load custom file if exists.  `WL-CFG.el' override for committer.
100     (load "./WL-CFG" t nil nil)
101     ;; load-path
102     (if wl-install-utils
103         (setq load-path (cons (expand-file-name UTILSDIR) load-path)))
104     (require 'install)
105     (load "./WL-ELS" nil nil t)
106     ;; product.el version check
107     (require 'product)
108     (if (not (fboundp 'product-version-as-string))
109         (error "Please install new APEL.  See INSTALL or INSTALL.ja"))
110     ;; smtp.el version check.
111     (require 'smtp)
112     (if (not (fboundp 'smtp-send-buffer))
113         (error "Please install new FLIM.  See INSTALL or INSTALL.ja"))
114     (condition-case ()
115         (require 'mime-setup)
116       (error (error "Cannot load `mime-setup'.  Please install SEMI")))))
117
118 (defun config-wl-pixmap-dir (&optional packagedir)
119   "Examine pixmap directory where icon files should go."
120   (let ((pixmap-dir (car command-line-args-left)))
121     (defvar PIXMAPDIR
122       (if (string= pixmap-dir "NONE")
123           (if packagedir
124               (expand-file-name "etc/wl/" packagedir)
125             (expand-file-name "wl/icons/" data-directory))
126         pixmap-dir)))
127   (if PIXMAPDIR
128       (princ (format "PIXMAPDIR is %s\n" PIXMAPDIR)))
129   (setq command-line-args-left (cdr command-line-args-left)))
130
131 (defun config-wl-package ()
132   (config-wl-package-subr)
133   ;; LISPDIR check.
134   (let ((elispdir (car command-line-args-left)))
135     (if (string= elispdir "NONE")
136         (defvar LISPDIR (install-detect-elisp-directory))
137       (defvar LISPDIR elispdir)))
138   (princ (format "LISPDIR is %s\n" LISPDIR))
139   (setq command-line-args-left (cdr command-line-args-left))
140   ;; PIXMAPDIR check.
141   (config-wl-pixmap-dir)
142   (princ "\n"))
143
144 (defun update-version ()
145   "Update version number of documents."
146   (config-wl-package)
147   (load-file "elmo/elmo-version.el")
148   (let ((version (mapconcat
149                   'number-to-string
150                   (product-version (product-find 'elmo-version))
151                   ".")))
152     (princ (concat "Update version number to " version "\n"))
153     ;; generate version.tex
154     (with-temp-buffer
155       (insert "\\def\\versionnumber{" version "}\n")
156       (write-region (point-min) (point-max) (expand-file-name
157                                              "version.tex" "doc")))
158     ;; generate version.texi
159     (with-temp-buffer
160       (insert "@set VERSION " version "\n")
161       (write-region (point-min) (point-max) (expand-file-name
162                                              "version.texi" "doc")))))
163
164 (defun test-wl ()
165   "Run test suite for developer."
166   (config-wl-package)
167   (require 'lunit)
168   (let ((files (directory-files "tests" t "^test-.*\\.el$"))
169         (suite (lunit-make-test-suite)))
170     (while files
171       (if (file-regular-p (car files))
172           (progn
173             (load-file (car files))
174             (lunit-test-suite-add-test
175              suite (lunit-make-test-suite-from-class
176                     (intern (file-name-sans-extension
177                              (file-name-nondirectory (car files))))))))
178       (setq files (cdr files)))
179     (lunit suite)))
180
181 (defun check-wl ()
182   "Check user environment.  Not for developer."
183   (config-wl-package)
184   (require 'lunit)
185   (let ((files (directory-files "tests" t "^check-.*\\.el$"))
186         (suite (lunit-make-test-suite)))
187     (while files
188       (if (file-regular-p (car files))
189           (progn
190             (load-file (car files))
191             (lunit-test-suite-add-test
192              suite (lunit-make-test-suite-from-class
193                     (intern (file-name-sans-extension
194                              (file-name-nondirectory (car files))))))))
195       (setq files (cdr files)))
196     (lunit suite)))
197
198 (defun wl-scan-source (path)
199   (let (ret)
200     (mapcar
201      '(lambda (x)
202         (mapcar '(lambda (y)
203                    (setq ret (append (list y (concat y "c")) ret)))
204                 (directory-files x nil "\\(.+\\)\\.el$" t)))
205      path)
206     ret))
207
208
209 (defun wl-uninstall (objs path)
210   ;(message (mapconcat 'identity objs " "))
211   (mapcar
212    '(lambda (x)
213       (let ((filename (expand-file-name x path)))
214         (if (and (file-exists-p filename)
215                  (file-writable-p filename))
216             (progn
217               (princ (format "%s was uninstalled.\n" filename))
218               (delete-file filename)))))
219    objs))
220
221
222 (defun compile-wl-package ()
223   (config-wl-package)
224   (mapcar
225    '(lambda (x)
226       (compile-elisp-modules (cdr x) (car x)))
227    modules-alist))
228
229 (defun install-wl-icons ()
230   (if (not (file-directory-p PIXMAPDIR))
231       (make-directory PIXMAPDIR t))
232   (let* ((case-fold-search t)
233          (icons (directory-files ICONDIR t
234                                  (cond ((featurep 'xemacs)
235                                         "\\.x[bp]m$")
236                                        ((and (boundp 'emacs-major-version)
237                                              (>= emacs-major-version 21))
238                                         "\\.img$\\|\\.x[bp]m$")
239                                        ((featurep 'mule)
240                                         "\\.img$\\|\\.xbm$"))))
241          icon dest)
242     (while icons
243       (setq icon (car icons)
244             icons (cdr icons)
245             dest (expand-file-name (file-name-nondirectory icon) PIXMAPDIR))
246       (princ (format "%s -> %s\n"
247                      (file-name-nondirectory icon)
248                      (substring (file-name-directory dest) 0 -1)))
249       (copy-file icon dest t))))
250
251 (defun install-wl-package ()
252   (compile-wl-package)
253   (let ((wl-install-dir (expand-file-name WL_PREFIX LISPDIR))
254         (elmo-install-dir (expand-file-name ELMO_PREFIX LISPDIR)))
255     (mapcar
256      '(lambda (x)
257         (install-elisp-modules (cdr x) (car x)
258                                (if (string= (car x) ELMODIR)
259                                    elmo-install-dir
260                                  wl-install-dir)))
261      modules-alist))
262   (if PIXMAPDIR
263       (install-wl-icons)))
264
265
266 (defun uninstall-wl-package ()
267   (config-wl-package)
268   (let ((wl-install-dir (expand-file-name WL_PREFIX
269                                           LISPDIR))
270         (elmo-install-dir (expand-file-name ELMO_PREFIX
271                                             LISPDIR)))
272     (wl-uninstall (wl-scan-source (list WLDIR UTILSDIR))
273                   wl-install-dir)
274     (wl-uninstall (wl-scan-source (list ELMODIR))
275                   elmo-install-dir))
276   (if PIXMAPDIR
277       (let* ((case-fold-search t)
278              (icons (directory-files PIXMAPDIR t "\\.x[bp]m$"))
279              icon)
280         (while icons
281           (setq icon (car icons)
282                 icons (cdr icons))
283           (if (and (file-exists-p icon)
284                    (file-writable-p icon))
285               (progn
286                 (princ (format "%s was uninstalled.\n" icon))
287                 (delete-file icon)))))))
288
289
290 (defun config-wl-package-xmas ()
291   (if (not (featurep 'xemacs))
292       (error "This directive is only for XEmacs"))
293   (config-wl-package-subr)
294   ;; PACKAGEDIR check.
295   (let (package-dir)
296     (and (setq package-dir (car command-line-args-left))
297          (if (string= "NONE" package-dir)
298              (defvar PACKAGEDIR
299                (if (boundp 'early-packages)
300                    (let ((dirs (append (if early-package-load-path
301                                            early-packages)
302                                        (if late-package-load-path
303                                            late-packages)
304                                        (if last-package-load-path
305                                            last-packages)))
306                          dir)
307                      (while (not (file-exists-p
308                                   (setq dir (car dirs))))
309                        (setq dirs (cdr dirs)))
310                      dir)))
311            (defvar PACKAGEDIR package-dir)))
312     (princ (format "PACKAGEDIR is %s\n" PACKAGEDIR))
313     (setq command-line-args-left (cdr command-line-args-left)))
314   ;; PIXMAPDIR check.
315   (config-wl-pixmap-dir PACKAGEDIR)
316   (princ "\n"))
317
318 ;; from SEMI-MK
319 (defun compile-wl-package-xmas ()
320   (config-wl-package-xmas)
321   (setq autoload-package-name "wl")
322   (add-to-list 'command-line-args-left WLDIR)
323   (batch-update-directory)
324   (add-to-list 'command-line-args-left WLDIR)
325   (Custom-make-dependencies)
326   ;; WL-AUTOLOAD-MODULES
327   (compile-elisp-modules WL-AUTOLOAD-MODULES WLDIR)
328   (mapcar
329    '(lambda (x)
330       (compile-elisp-modules (cdr x) (car x)))
331    modules-alist))
332
333 (defun install-wl-package-xmas ()
334   (compile-wl-package-xmas)
335   (let ((LISPDIR (expand-file-name "wl"
336                                     (expand-file-name "lisp"
337                                                       PACKAGEDIR)))
338         (DATADIR  (expand-file-name "wl"
339                                     (expand-file-name "etc"
340                                                       PACKAGEDIR)))
341         (INFODIR  (expand-file-name "info" PACKAGEDIR)))
342     (or (file-exists-p DATADIR)
343         (make-directory DATADIR t))
344     (or (file-exists-p INFODIR)
345         (make-directory INFODIR t))
346     ;; copy xpm files
347     (install-wl-icons)
348
349     (mapcar '(lambda (x)
350                (install-elisp-modules (cdr x) (car x) LISPDIR))
351             modules-alist)
352     ;; WL-AUTOLOAD-MODULES
353     (install-elisp-modules WL-AUTOLOAD-MODULES WLDIR LISPDIR)
354     ;;
355     (wl-texinfo-format)
356     (wl-texinfo-install)))
357
358 \f
359 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
360 ;;; Texinfo stuff
361
362 (defun wl-texinfo-format-file (lang)
363   (let ((infofile (symbol-value (intern (format "wl-%s-info" lang))))
364         (texifile (symbol-value (intern (format "wl-%s-texi" lang)))))
365     (require 'wl-vars) ;; for 'wl-cs-local
366     (or (file-newer-than-file-p (expand-file-name infofile DOCDIR)
367                                 (expand-file-name texifile DOCDIR))
368         (let (obuf beg)
369           ;; Support old texinfmt.el
370           (require 'ptexinfmt (expand-file-name "ptexinfmt.el" UTILSDIR))
371           (find-file (expand-file-name texifile DOCDIR))
372           (setq obuf (current-buffer))
373           ;; We can't know file names if splitted.
374           (texinfo-format-buffer t)
375           ;; Emacs20.2's default is 'raw-text-unix.
376           (and (fboundp 'set-buffer-file-coding-system)
377                (set-buffer-file-coding-system wl-cs-local))
378           (save-buffer)
379           (kill-buffer (current-buffer)) ;; info
380           (kill-buffer obuf)) ;; texi
381         )))
382
383 (defun wl-texinfo-format ()
384   (wl-detect-info-directory)
385   (cond ((listp wl-info-lang)
386          (mapcar 'wl-texinfo-format-file wl-info-lang))
387         ((stringp wl-info-lang)
388          (wl-texinfo-format-file wl-info-lang))))
389
390 (defun wl-texinfo-install-file (lang)
391   (let ((infofile (symbol-value (intern (format "wl-%s-info" lang)))))
392     (install-file infofile DOCDIR INFODIR nil 'overwrite)))
393
394 (defun wl-texinfo-install ()
395   (cond ((listp wl-info-lang)
396          (mapcar 'wl-texinfo-install-file wl-info-lang))
397         ((stringp wl-info-lang)
398          (wl-texinfo-install-file wl-info-lang))))
399
400 (defun wl-primary-info-file ()
401   "Get primary info file (for wl-detect-info-directory)."
402   (cond
403    ((listp wl-info-lang)
404     (let ((wl-info-lang (car wl-info-lang)))
405       (wl-primary-info-file)))
406    ((stringp wl-info-lang)
407     (symbol-value (intern (format "wl-%s-info" wl-info-lang))))))
408
409 (defun wl-detect-info-directory ()
410   (config-wl-package-subr)
411   ;; INFODIR check.
412   (require 'info)
413   (if (fboundp 'info-initialize)
414       (info-initialize))
415   (unless INFODIR
416     (let ((infodir (car command-line-args-left))
417           (info (wl-primary-info-file))
418           previous)
419       (setq INFODIR
420             (if (string= infodir "NONE")
421                 (if (setq previous
422                           (exec-installed-p info Info-directory-list
423                                             COMPRESS-SUFFIX-LIST))
424                     ;;(progn
425                     ;;(condition-case nil (delete-file previous))
426                     (directory-file-name (file-name-directory previous));)
427                   (car Info-directory-list))
428               infodir))
429       (setq command-line-args-left (cdr command-line-args-left))))
430   (princ (format "INFODIR is %s\n\n" INFODIR)))
431
432 (defun install-wl-info ()
433   (wl-texinfo-format)
434   (wl-texinfo-install))
435
436 \f
437 ;;; ToDo
438 ;;; * MORE refine code (^_^;
439
440 ;;; End