* Makefile (check): New rule
[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"
32   "The language of info file (\"ja\" or \"en\").")
33
34 ;; for Nemacs (dirty!)
35 (if (not (fboundp 'file-executable-p))
36     (fset 'file-executable-p 'file-exists-p))
37
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 \f
40 (require 'cl)
41 (defvar INFODIR nil)
42
43 (condition-case () (require 'custom) (error nil))
44 ;; for wl-vars.el
45 (unless (and (fboundp 'defgroup)
46              (fboundp 'defcustom)
47              ;; ignore broken module
48              (not (featurep 'tinycustom)))
49   (when (and (boundp 'emacs-major-version)
50              (eq emacs-major-version 19)
51              (>= emacs-minor-version 29))
52     (message "%s" "
53   Warning: You don't seem to have \"new custom\" package installed.
54            See README file of APEL package for more information.
55 "))
56   (require 'backquote)
57   (defmacro defgroup (&rest args))
58   (defmacro defcustom (symbol value &optional doc &rest args)
59     (let ((doc (concat "*" (or doc ""))))
60       (` (defvar (, symbol) (, value) (, doc))))))
61
62 (load "bytecomp" nil t)
63 (unless (fboundp 'byte-compile-file-form-custom-declare-variable)
64   ;; Bind defcustom'ed variables.
65   (put 'custom-declare-variable 'byte-hunk-handler
66        'byte-compile-file-form-custom-declare-variable)
67   (defun byte-compile-file-form-custom-declare-variable (form)
68     (if (memq 'free-vars byte-compile-warnings)
69         (setq byte-compile-bound-variables
70               (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
71     form))
72
73 (setq byte-compile-warnings '(free-vars unresolved callargs redefine))
74
75 ;; v18, v19
76 (if (or (boundp 'MULE)
77         (fboundp 'nemacs-version))
78     (setq max-lisp-eval-depth 400))
79
80 ;; FIXME: it is currently needed to byte-compile with Emacs 21.
81 (setq recursive-load-depth-limit nil)
82
83 (condition-case () (require 'easymenu) (error nil))
84
85 (defvar config-wl-package-done nil)
86
87 (defun config-wl-package-subr ()
88   (unless config-wl-package-done
89     (setq config-wl-package-done t)
90     (setq load-path (cons (expand-file-name ".") load-path))
91     (setq load-path (cons (expand-file-name WLDIR)
92                           (cons (expand-file-name ELMODIR) load-path)))
93     (setq wl-icon-dir (expand-file-name ICONDIR))
94     ;; load custom file if exists.  `WL-CFG.el' override for committer.
95     (load "./WL-CFG" t nil nil)
96     ;; load-path
97     (if wl-install-utils
98         (setq load-path (cons (expand-file-name UTILSDIR) load-path)))
99     (require 'install)
100     (load "./WL-ELS" nil nil t)
101     (condition-case ()
102         (require 'mime-setup)
103       (error (error "No MIME module was detected. Please install SEMI or tm.")))
104     ;; smtp.el version check.
105     (require 'smtp)
106     (if (not (fboundp 'smtp-send-buffer))
107         (error "Please install FLIM 1.14.0 or later."))))
108
109 (defun config-wl-pixmap-dir (&optional packagedir)
110   "Examine pixmap directory where icon files should go."
111   (let ((pixmap-dir (car command-line-args-left)))
112     (defvar PIXMAPDIR
113       (if (string= pixmap-dir "NONE")
114           (if packagedir
115               (expand-file-name "etc/wl/" packagedir)
116             (if (or (featurep 'xemacs)
117                     (and (boundp 'emacs-major-version)
118                          (>= emacs-major-version 21)))
119                 (expand-file-name "wl/icons/" data-directory)))
120         pixmap-dir)))
121   (if PIXMAPDIR
122       (princ (format "PIXMAPDIR is %s\n" PIXMAPDIR)))
123   (setq command-line-args-left (cdr command-line-args-left)))
124
125 (defun config-wl-package ()
126   (config-wl-package-subr)
127   ;; LISPDIR check.
128   (let ((elispdir (car command-line-args-left)))
129     (if (string= elispdir "NONE")
130         (defvar LISPDIR (install-detect-elisp-directory))
131       (defvar LISPDIR elispdir)))
132   (princ (format "LISPDIR is %s\n" LISPDIR))
133   (setq command-line-args-left (cdr command-line-args-left))
134   ;; PIXMAPDIR check.
135   (config-wl-pixmap-dir)
136   (princ "\n"))
137
138 (defun check-wl ()
139   (config-wl-package)
140   (require 'lunit)
141   (let ((files (directory-files "tests" t "^test-.*\\.el$"))
142         (suite (lunit-make-test-suite)))
143     (while files
144       (if (file-regular-p (car files))
145           (progn
146             (load-file (car files))
147             (lunit-test-suite-add-test
148              suite (lunit-make-test-suite-from-class
149                     (intern (file-name-sans-extension
150                              (file-name-nondirectory (car files))))))))
151       (setq files (cdr files)))
152     (lunit suite)))
153
154 (defun wl-scan-source (path)
155   (let (ret)
156     (mapcar
157      '(lambda (x)
158         (mapcar '(lambda (y)
159                    (setq ret (append (list y (concat y "c")) ret)))
160                 (directory-files x nil "\\(.+\\)\\.el$" t)))
161      path)
162     ret))
163
164
165 (defun wl-uninstall (objs path)
166   ;(message (mapconcat 'identity objs " "))
167   (mapcar
168    '(lambda (x)
169       (let ((filename (expand-file-name x path)))
170         (if (and (file-exists-p filename)
171                  (file-writable-p filename))
172             (progn
173               (princ (format "%s was uninstalled.\n" filename))
174               (delete-file filename)))))
175    objs))
176
177
178 (defun compile-wl-package ()
179   ;; For nemacs byte compiler's strange behavior(?).
180   (config-wl-package)
181   (if (fboundp 'nemacs-version)
182       (load (expand-file-name "wl.el" WLDIR)))
183   (mapcar
184    '(lambda (x)
185       (compile-elisp-modules (cdr x) (car x)))
186    modules-alist))
187
188 (defun install-wl-icons ()
189   (if (not (file-directory-p PIXMAPDIR))
190       (make-directory PIXMAPDIR t))
191   (let* ((case-fold-search t)
192          (icons (directory-files ICONDIR t "\\.x[bp]m$"))
193          icon dest)
194     (while icons
195       (setq icon (car icons)
196             icons (cdr icons)
197             dest (expand-file-name (file-name-nondirectory icon) PIXMAPDIR))
198       (princ (format "%s->%s\n" icon dest))
199       (copy-file icon dest t))))
200
201 (defun install-wl-package ()
202   (compile-wl-package)
203   (let ((wl-install-dir (expand-file-name WL_PREFIX LISPDIR))
204         (elmo-install-dir (expand-file-name ELMO_PREFIX LISPDIR)))
205     (mapcar
206      '(lambda (x)
207         (install-elisp-modules (cdr x) (car x)
208                                (if (string= (car x) ELMODIR)
209                                    elmo-install-dir
210                                  wl-install-dir)))
211      modules-alist))
212   (if PIXMAPDIR
213       (install-wl-icons)))
214
215
216 (defun uninstall-wl-package ()
217   (config-wl-package)
218   (let ((wl-install-dir (expand-file-name WL_PREFIX
219                                           LISPDIR))
220         (elmo-install-dir (expand-file-name ELMO_PREFIX
221                                             LISPDIR)))
222     (wl-uninstall (wl-scan-source (list WLDIR UTILSDIR))
223                   wl-install-dir)
224     (wl-uninstall (wl-scan-source (list ELMODIR))
225                   elmo-install-dir))
226   (if PIXMAPDIR
227       (let* ((case-fold-search t)
228              (icons (directory-files PIXMAPDIR t "\\.x[bp]m$"))
229              icon)
230         (while icons
231           (setq icon (car icons)
232                 icons (cdr icons))
233           (if (and (file-exists-p icon)
234                    (file-writable-p icon))
235               (progn
236                 (princ (format "%s was uninstalled.\n" icon))
237                 (delete-file icon)))))))
238
239
240 (defun config-wl-package-xmas ()
241   (if (not (featurep 'xemacs))
242       (error "This directive is only for XEmacs."))
243   (config-wl-package-subr)
244   ;; PACKAGEDIR check.
245   (let (package-dir)
246     (and (setq package-dir (car command-line-args-left))
247          (if (string= "NONE" package-dir)
248              (defvar PACKAGEDIR
249                (if (boundp 'early-packages)
250                    (let ((dirs (append (if early-package-load-path
251                                            early-packages)
252                                        (if late-package-load-path
253                                            late-packages)
254                                        (if last-package-load-path
255                                            last-packages)))
256                          dir)
257                      (while (not (file-exists-p
258                                   (setq dir (car dirs))))
259                        (setq dirs (cdr dirs)))
260                      dir)))
261            (defvar PACKAGEDIR package-dir)))
262     (princ (format "PACKAGEDIR is %s\n" PACKAGEDIR))
263     (setq command-line-args-left (cdr command-line-args-left)))
264   ;; PIXMAPDIR check.
265   (config-wl-pixmap-dir PACKAGEDIR)
266   (princ "\n"))
267
268 ;; from SEMI-MK
269 (defun compile-wl-package-xmas ()
270   (config-wl-package-xmas)
271   (setq autoload-package-name "wl")
272   (add-to-list 'command-line-args-left WLDIR)
273   (batch-update-directory)
274   (add-to-list 'command-line-args-left WLDIR)
275   (Custom-make-dependencies)
276   ;; WL-AUTOLOAD-MODULES
277   (compile-elisp-modules WL-AUTOLOAD-MODULES WLDIR)
278   (mapcar
279    '(lambda (x)
280       (compile-elisp-modules (cdr x) (car x)))
281    modules-alist))
282
283 (defun install-wl-package-xmas ()
284   (compile-wl-package-xmas)
285   (let ((LISPDIR (expand-file-name "wl"
286                                     (expand-file-name "lisp"
287                                                       PACKAGEDIR)))
288         (DATADIR  (expand-file-name "wl"
289                                     (expand-file-name "etc"
290                                                       PACKAGEDIR)))
291         (INFODIR  (expand-file-name "info" PACKAGEDIR)))
292     (or (file-exists-p DATADIR)
293         (make-directory DATADIR t))
294     (or (file-exists-p INFODIR)
295         (make-directory INFODIR t))
296     ;; copy xpm files
297     (install-wl-icons)
298
299     (mapcar '(lambda (x)
300                (install-elisp-modules (cdr x) (car x) LISPDIR))
301             modules-alist)
302     ;; WL-AUTOLOAD-MODULES
303     (install-elisp-modules WL-AUTOLOAD-MODULES WLDIR LISPDIR)
304     ;;
305     (wl-texinfo-format)
306     (wl-texinfo-install)))
307
308 \f
309 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
310 ;;; Texinfo stuff
311
312 (defun wl-texinfo-format-file (lang)
313   (let ((infofile (symbol-value (intern (format "wl-%s-info" lang))))
314         (texifile (symbol-value (intern (format "wl-%s-texi" lang)))))
315     (require 'wl-vars) ;; for 'wl-cs-local
316     (or (file-newer-than-file-p (expand-file-name infofile DOCDIR)
317                                 (expand-file-name texifile DOCDIR))
318         (let (obuf beg)
319           ;; Support old texinfmt.el
320           (require 'ptexinfmt (expand-file-name "ptexinfmt.el" UTILSDIR))
321           (find-file (expand-file-name texifile DOCDIR))
322           (setq obuf (current-buffer))
323           ;; We can't know file names if splitted.
324           (texinfo-format-buffer t)
325           ;; Emacs20.2's default is 'raw-text-unix.
326           (and (fboundp 'set-buffer-file-coding-system)
327                (set-buffer-file-coding-system wl-cs-local))
328           (save-buffer)
329           (kill-buffer (current-buffer)) ;; info
330           (kill-buffer obuf)) ;; texi
331         )))
332
333 (defun wl-texinfo-format ()
334   (unless INFODIR
335     (setq INFODIR (wl-detect-info-directory)))
336   (cond ((listp wl-info-lang)
337          (mapcar 'wl-texinfo-format-file wl-info-lang))
338         ((stringp wl-info-lang)
339          (wl-texinfo-format-file wl-info-lang))))
340
341 (defun wl-texinfo-install-file (lang)
342   (let ((infofile (symbol-value (intern (format "wl-%s-info" lang)))))
343     (install-file infofile DOCDIR INFODIR)))
344
345 (defun wl-texinfo-install ()
346   (cond ((listp wl-info-lang)
347          (mapcar 'wl-texinfo-install-file wl-info-lang))
348         ((stringp wl-info-lang)
349          (wl-texinfo-install-file wl-info-lang))))
350
351 (defun wl-primary-info-file ()
352   "Get primary info file (for wl-detect-info-directory)."
353   (cond
354    ((listp wl-info-lang)
355     (let ((wl-info-lang (car wl-info-lang)))
356       (wl-primary-info-file)))
357    ((stringp wl-info-lang)
358     (symbol-value (intern (format "wl-%s-info" wl-info-lang))))))
359
360 (defun wl-detect-info-directory ()
361   (config-wl-package-subr)
362   (if (fboundp 'nemacs-version)
363       (error "Cannot format info on Nemacs. Please use another formatter."))
364   ;; INFODIR check.
365   (require 'info)
366   (if (fboundp 'info-initialize)
367       (info-initialize))
368   (let ((infodir (car command-line-args-left))
369         (info (wl-primary-info-file))
370         previous INFODIR)
371     (setq INFODIR
372           (if (string= infodir "NONE")
373               (if (setq previous
374                         (exec-installed-p info Info-directory-list
375                                           COMPRESS-SUFFIX-LIST))
376                   ;;(progn
377                   ;;(condition-case nil (delete-file previous))
378                   (directory-file-name (file-name-directory previous));)
379                 (car Info-directory-list))
380             infodir))
381     (setq command-line-args-left (cdr command-line-args-left))
382     (princ (format "INFODIR is %s\n\n" INFODIR))
383     INFODIR))
384
385 (defun install-wl-info ()
386   (wl-texinfo-format)
387   (wl-texinfo-install))
388
389 \f
390 ;;; ToDo
391 ;;; * MORE refine code (^_^;
392
393 ;;; End