* test-dist.el (test-elmo-modules-trailing-whitespace)
[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 (if (featurep 'mule) '("ja" "en") '("en"))
32   "The language of info file (\"ja\" or \"en\").")
33
34 ;;; NEWS
35 (defvar wl-news-lang (if (featurep 'mule) '("ja" "en") '("en"))
36   "The language of news file (\"ja\" or \"en\").")
37 (defconst wl-news-news-file '(("en" "NEWS")
38                               ("ja" "NEWS.ja")))
39 (defconst wl-news-search-regexp
40   '(("en" "^\\* Changes in \\([0-9.]*\\) from [0-9.]+x?")
41     ("ja" "^\\* [0-9.]+x? \e$B$+$i\e(B \\([0-9.]*\\) \e$B$X$NJQ99E@\e(B")))
42 (defconst wl-news-filename "wl-news.el")
43
44 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 \f
46 (require 'cl)
47 (defvar INFODIR nil)
48
49 (condition-case () (require 'custom) (error nil))
50 ;; for wl-vars.el
51 (unless (and (fboundp 'defgroup)
52              (fboundp 'defcustom)
53              ;; ignore broken module
54              (not (featurep 'tinycustom)))
55   (when (and (boundp 'emacs-major-version)
56              (= emacs-major-version 19)
57              (>= emacs-minor-version 29))
58     (message "%s" "
59   Warning: You don't seem to have \"new custom\" package installed.
60            See README file of APEL package for more information.
61 "))
62   (require 'backquote)
63   (defmacro defgroup (&rest args))
64   (defmacro defcustom (symbol value &optional doc &rest args)
65     (let ((doc (concat "*" (or doc ""))))
66       `(defvar ,symbol ,value ,doc))))
67
68 (load "bytecomp" nil t)
69
70 (unless (fboundp 'byte-compile-file-form-custom-declare-variable)
71   ;; Bind defcustom'ed variables.
72   (put 'custom-declare-variable 'byte-hunk-handler
73        'byte-compile-file-form-custom-declare-variable)
74   (defun byte-compile-file-form-custom-declare-variable (form)
75     (if (memq 'free-vars byte-compile-warnings)
76         (setq byte-compile-bound-variables
77               (cons (nth 1 (nth 1 form)) byte-compile-bound-variables)))
78     form))
79
80 (condition-case nil
81     (char-after)
82   (wrong-number-of-arguments
83    ;; Optimize byte code for `char-after'.
84    (put 'char-after 'byte-optimizer 'byte-optimize-char-after)
85    (defun byte-optimize-char-after (form)
86      (if (null (cdr form))
87          '(char-after (point))
88        form))))
89
90 (setq byte-compile-warnings '(free-vars unresolved callargs redefine))
91
92 ;; v18, v19
93 (if (boundp 'MULE)
94     (setq max-lisp-eval-depth 400))
95
96 ;; FIXME: it is currently needed to byte-compile with Emacs 21.
97 (setq recursive-load-depth-limit nil)
98
99 (condition-case () (require 'easymenu) (error nil))
100
101 (defvar config-wl-package-done nil)
102
103 (defun config-wl-package-subr ()
104   (unless config-wl-package-done
105     (setq config-wl-package-done t)
106     (setq load-path (cons (expand-file-name ".") load-path))
107     (setq load-path (cons (expand-file-name WLDIR)
108                           (cons (expand-file-name ELMODIR) load-path)))
109     ;; load custom file if exists.  `WL-CFG.el' override for committer.
110     (load "./WL-CFG" t nil nil)
111     ;; load-path
112     (if wl-install-utils
113         (setq load-path (cons (expand-file-name UTILSDIR) load-path)))
114     (require 'install)
115     (load "./WL-ELS" nil nil t)
116     ;; product.el version check
117     (require 'product)
118     (if (not (fboundp 'product-version-as-string))
119         (error "Please install new APEL.  See INSTALL or INSTALL.ja"))
120     ;; smtp.el version check.
121     (require 'smtp)
122     (if (not (fboundp 'smtp-send-buffer))
123         (error "Please install new FLIM.  See INSTALL or INSTALL.ja"))
124     (condition-case ()
125         (require 'mime-setup)
126       (error (error "Cannot load `mime-setup'.  Please install SEMI")))))
127
128 (defun config-wl-pixmap-dir (&optional packagedir)
129   "Examine pixmap directory where icon files should go."
130   (let ((pixmap-dir (car command-line-args-left)))
131     (defvar PIXMAPDIR
132       (if (string= pixmap-dir "NONE")
133           (if packagedir
134               (expand-file-name "etc/wl/" packagedir)
135             (expand-file-name "wl/icons/" data-directory))
136         pixmap-dir)))
137   (if PIXMAPDIR
138       (princ (format "PIXMAPDIR is %s\n" PIXMAPDIR)))
139   (setq command-line-args-left (cdr command-line-args-left)))
140
141 (defun config-wl-package ()
142   (config-wl-package-subr)
143   ;; LISPDIR check.
144   (let ((elispdir (car command-line-args-left)))
145     (if (string= elispdir "NONE")
146         (defvar LISPDIR (install-detect-elisp-directory))
147       (defvar LISPDIR elispdir)))
148   (princ (format "LISPDIR is %s\n" LISPDIR))
149   (setq command-line-args-left (cdr command-line-args-left))
150   ;; PIXMAPDIR check.
151   (config-wl-pixmap-dir)
152   (princ "\n"))
153
154 (defun update-version ()
155   "Update version number of documents."
156   (config-wl-package)
157   (load-file "elmo/elmo-version.el")
158   (let ((version (mapconcat
159                   'number-to-string
160                   (product-version (product-find 'elmo-version))
161                   "."))
162         (coding-system-for-write 'iso-latin-1-unix))
163     (princ (concat "Update version number to " version "\n"))
164     ;; generate version.tex
165     (with-temp-buffer
166       (insert "\\def\\versionnumber{" version "}\n")
167       (write-region (point-min) (point-max) (expand-file-name
168                                              "version.tex" "doc")))
169     ;; generate version.texi
170     (with-temp-buffer
171       (insert "@set VERSION " version "\n")
172       (write-region (point-min) (point-max) (expand-file-name
173                                              "version.texi" "doc")))))
174
175 (defun test-wl ()
176   "Run test suite for developer."
177   (config-wl-package)
178   (make-wl-news)
179   (require 'lunit)
180   (let ((files (directory-files "tests" t "^test-.*\\.el$"))
181         (suite (lunit-make-test-suite)))
182     (while files
183       (if (file-regular-p (car files))
184           (progn
185             (load-file (car files))
186             (lunit-test-suite-add-test
187              suite (lunit-make-test-suite-from-class
188                     (intern (file-name-sans-extension
189                              (file-name-nondirectory (car files))))))))
190       (setq files (cdr files)))
191     (lunit suite)))
192
193 (defun check-wl ()
194   "Check user environment.  Not for developer."
195   (config-wl-package)
196   ;; Avoid load error
197   (provide 'wl-news)
198   (load "wl-news.el.in")
199   (require 'lunit)
200   (let ((files (directory-files "tests" t "^check-.*\\.el$"))
201         (suite (lunit-make-test-suite)))
202     (while files
203       (if (file-regular-p (car files))
204           (progn
205             (load-file (car files))
206             (lunit-test-suite-add-test
207              suite (lunit-make-test-suite-from-class
208                     (intern (file-name-sans-extension
209                              (file-name-nondirectory (car files))))))))
210       (setq files (cdr files)))
211     (lunit suite)))
212
213 (defun wl-scan-source (path)
214   (let (ret)
215     (mapc
216      (lambda (x)
217        (mapc (lambda (y)
218                (setq ret (append (list y (concat y "c")) ret)))
219              (directory-files x nil "\\(.+\\)\\.el$" t)))
220      path)
221     ret))
222
223
224 (defun wl-uninstall (objs path)
225   ;(message (mapconcat 'identity objs " "))
226   (mapc
227    (lambda (x)
228      (let ((filename (expand-file-name x path)))
229        (if (and (file-exists-p filename)
230                 (file-writable-p filename))
231            (progn
232              (princ (format "%s was uninstalled.\n" filename))
233              (delete-file filename)))))
234    objs))
235
236
237 (defun wl-examine-modules ()
238   "Examine wl modules should be byte-compile'd."
239   (config-wl-package-subr)
240   (make-wl-news)
241   (dolist (module modules-alist)
242     (dolist (filename (cdr module))
243       (princ (format "%s/%s.elc " (car module) filename)))))
244
245
246 (defun compile-wl-package ()
247   (config-wl-package)
248   (make-wl-news)
249   (mapc
250    (lambda (x)
251      (compile-elisp-modules (cdr x) (car x)))
252    modules-alist))
253
254 (defun install-wl-icons ()
255   (if (not (file-directory-p PIXMAPDIR))
256       (make-directory PIXMAPDIR t))
257   (let* ((case-fold-search t)
258          (icons (directory-files ICONDIR nil
259                                  (cond ((featurep 'xemacs)
260                                         "\\.x[bp]m$")
261                                        ((and (boundp 'emacs-major-version)
262                                              (>= emacs-major-version 21))
263                                         "\\.img$\\|\\.x[bp]m$")
264                                        ((featurep 'mule)
265                                         "\\.img$\\|\\.xbm$")))))
266     (install-files icons ICONDIR PIXMAPDIR nil 'overwrite)))
267
268 (defun uninstall-wl-icons ()
269   (let* ((case-fold-search t)
270          (icons (directory-files PIXMAPDIR t "\\.img$\\|\\.x[bp]m$"))
271          icon)
272     (while icons
273       (setq icon (car icons)
274             icons (cdr icons))
275       (if (and (file-exists-p icon)
276                (file-writable-p icon))
277           (progn
278             (princ (format "%s was uninstalled.\n" icon))
279             (delete-file icon))))))
280
281 (defun install-wl-package ()
282   (compile-wl-package)
283   (let ((wl-install-dir (expand-file-name WL_PREFIX LISPDIR))
284         (elmo-install-dir (expand-file-name ELMO_PREFIX LISPDIR)))
285     (mapc
286      (lambda (x)
287        (install-elisp-modules (cdr x) (car x)
288                               (if (string= (car x) ELMODIR)
289                                   elmo-install-dir
290                                 wl-install-dir)))
291      modules-alist))
292   (if PIXMAPDIR
293       (install-wl-icons)))
294
295
296 (defun uninstall-wl-package ()
297   (config-wl-package)
298   (let ((wl-install-dir (expand-file-name WL_PREFIX
299                                           LISPDIR))
300         (elmo-install-dir (expand-file-name ELMO_PREFIX
301                                             LISPDIR)))
302     (wl-uninstall (wl-scan-source (list WLDIR UTILSDIR))
303                   wl-install-dir)
304     (wl-uninstall (wl-scan-source (list ELMODIR))
305                   elmo-install-dir))
306   (if PIXMAPDIR
307       (uninstall-wl-icons)))
308
309
310 (defun config-wl-package-xmas ()
311   (if (not (featurep 'xemacs))
312       (error "This directive is only for XEmacs"))
313   (config-wl-package-subr)
314   ;; PACKAGEDIR check.
315   (let (package-dir)
316     (defvar PACKAGEDIR
317       (if (and (setq package-dir (car command-line-args-left))
318                (not (string= "NONE" package-dir)))
319           package-dir
320         (require 'install)
321         (install-get-default-package-directory)))
322     (princ (format "PACKAGEDIR is %s\n" PACKAGEDIR))
323     (setq command-line-args-left (cdr command-line-args-left)))
324   ;; PIXMAPDIR check.
325   (config-wl-pixmap-dir PACKAGEDIR)
326   (princ "\n"))
327
328 ;; from SEMI-MK
329 (defun compile-wl-package-xmas ()
330   (config-wl-package-xmas)
331   (make-wl-news)
332   (setq autoload-package-name "wl")
333   (add-to-list 'command-line-args-left WLDIR)
334   (batch-update-directory)
335   (add-to-list 'command-line-args-left WLDIR)
336   (Custom-make-dependencies)
337   ;; WL-AUTOLOAD-MODULES
338   (compile-elisp-modules WL-AUTOLOAD-MODULES WLDIR)
339   (mapc
340    (lambda (x)
341      (compile-elisp-modules (cdr x) (car x)))
342    modules-alist))
343
344 (defun install-wl-package-xmas ()
345   (compile-wl-package-xmas)
346   (let ((LISPDIR (expand-file-name "wl"
347                                     (expand-file-name "lisp"
348                                                       PACKAGEDIR)))
349         (DATADIR  (expand-file-name "wl"
350                                     (expand-file-name "etc"
351                                                       PACKAGEDIR)))
352         (INFODIR  (expand-file-name "info" PACKAGEDIR)))
353     (or (file-exists-p DATADIR)
354         (make-directory DATADIR t))
355     (or (file-exists-p INFODIR)
356         (make-directory INFODIR t))
357     ;; copy xpm files
358     (install-wl-icons)
359
360     (mapc (lambda (x)
361             (install-elisp-modules (cdr x) (car x) LISPDIR))
362           modules-alist)
363     ;; WL-AUTOLOAD-MODULES
364     (install-elisp-modules WL-AUTOLOAD-MODULES WLDIR LISPDIR)
365     ;;
366     (wl-texinfo-format)
367     (wl-texinfo-install)))
368
369 \f
370 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
371 ;;; Texinfo stuff
372
373 (defun wl-texinfo-info-file (lang)
374   (symbol-value (intern (format "wl-%s-info" lang))))
375
376 (defun wl-texinfo-texi-file (lang)
377   (symbol-value (intern (format "wl-%s-texi" lang))))
378
379 (defun wl-texinfo-check-newer (lang)
380   (let ((info-file (expand-file-name (wl-texinfo-info-file lang) DOCDIR)))
381     (and
382      (file-newer-than-file-p info-file
383                              (expand-file-name "version.texi" DOCDIR))
384      (file-newer-than-file-p info-file
385                              (expand-file-name (wl-texinfo-texi-file lang) DOCDIR)))))
386
387 (defun wl-texinfo-format-file (lang)
388   (require 'wl-vars) ;; for 'wl-cs-local
389   (or (wl-texinfo-check-newer lang)
390       (let (obuf)
391         ;; Support old texinfmt.el
392         (require 'ptexinfmt (expand-file-name "ptexinfmt.el" UTILSDIR))
393         (find-file (expand-file-name (wl-texinfo-texi-file lang) DOCDIR))
394         (setq obuf (current-buffer))
395         ;; We can't know file names if splitted.
396         (texinfo-format-buffer t)
397         ;; Emacs20.2's default is 'raw-text-unix.
398         (and (fboundp 'set-buffer-file-coding-system)
399              (set-buffer-file-coding-system wl-cs-local))
400         (save-buffer)
401         (kill-buffer (current-buffer)) ;; info
402         (kill-buffer obuf)) ;; texi
403       ))
404
405 (defun wl-texinfo-format ()
406   (wl-detect-info-directory)
407   (cond ((null wl-info-lang))
408         ((listp wl-info-lang)
409          (mapc 'wl-texinfo-format-file wl-info-lang))
410         ((stringp wl-info-lang)
411          (wl-texinfo-format-file wl-info-lang))))
412
413 (defun wl-texinfo-install-file (lang)
414   (let ((infofile (wl-texinfo-info-file lang)))
415     (install-file infofile DOCDIR INFODIR nil 'overwrite)))
416
417 (defun wl-texinfo-install ()
418   (cond ((null wl-info-lang))
419         ((listp wl-info-lang)
420          (mapc 'wl-texinfo-install-file wl-info-lang))
421         ((stringp wl-info-lang)
422          (wl-texinfo-install-file wl-info-lang))))
423
424 (defun wl-primary-info-file ()
425   "Get primary info file (for wl-detect-info-directory)."
426   (cond ((null wl-info-lang))
427         ((listp wl-info-lang)
428          (let ((wl-info-lang (car wl-info-lang)))
429            (wl-primary-info-file)))
430         ((stringp wl-info-lang)
431          (wl-texinfo-info-file wl-info-lang))))
432
433 (defun wl-detect-info-directory ()
434   (config-wl-package-subr)
435   ;; INFODIR check.
436   (when wl-info-lang
437     (require 'info)
438     (if (fboundp 'info-initialize)
439         (info-initialize))
440     (unless INFODIR
441       (let ((infodir (car command-line-args-left))
442             (info (wl-primary-info-file))
443             previous)
444         (setq INFODIR
445               (if (string= infodir "NONE")
446                   (if (setq previous
447                             (exec-installed-p info Info-directory-list
448                                               COMPRESS-SUFFIX-LIST))
449                       (directory-file-name (file-name-directory previous))
450                     (car Info-directory-list))
451                 infodir))
452         (setq command-line-args-left (cdr command-line-args-left))))
453     (princ (format "INFODIR is %s\n\n" INFODIR))))
454
455 (defun install-wl-info ()
456   (wl-texinfo-format)
457   (wl-texinfo-install))
458
459 \f
460 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
461 ;;; wl-news stuff
462
463 (defun wl-news-news-file (lang)
464   (cadr (assoc lang wl-news-news-file)))
465
466 (defun wl-news-check-newer (out-filename news-lang)
467   (let ((lang news-lang)
468         ret)
469     (while (car lang)
470       (if (file-newer-than-file-p
471            (wl-news-news-file (car lang)) out-filename)
472           (setq ret t))
473       (setq lang (cdr lang)))
474     ret))
475
476 (defun make-wl-news ()
477   (let ((in-filename
478          (expand-file-name (concat wl-news-filename ".in") WLDIR))
479         (out-filename
480          (expand-file-name wl-news-filename WLDIR))
481         (wl-news-lang (if (listp wl-news-lang)
482                           wl-news-lang
483                         (list wl-news-lang))))
484     (if (or (file-newer-than-file-p in-filename out-filename)
485             (wl-news-check-newer out-filename wl-news-lang))
486         (with-temp-buffer
487           (save-excursion
488             (insert-file-contents in-filename)
489             (goto-char (point-min))
490             (unless (re-search-forward "^;;; -\\*- news-list -\\*-" nil t)
491               (error "Invalid wl-news.el.in"))
492             (forward-line 2)
493             (if wl-news-lang
494                 (progn
495                   (insert "(defconst wl-news-news-alist\n  '")
496                   (let ((p (point)))
497                     (prin1 (wl-news-parse-news wl-news-lang) (current-buffer))
498                     (save-excursion
499                       (narrow-to-region p (point))
500                       (goto-char (1+ p))
501                       (while (re-search-forward "^(" nil t)
502                         (replace-match "\\\\(")) ; avoid font-lock confusion
503                       (widen)))
504                   (insert ")\n"))
505               (insert "(defconst wl-news-news-alist nil)\n\n"))
506             (let ((buffer-file-coding-system (mime-charset-to-coding-system 'x-ctext)))
507               (write-region (point-min) (point-max) out-filename)))))))
508
509 (defun wl-news-parse-news (lang)
510   (let (news-list)
511   (while (car lang)
512     (setq news-list (cons
513                         (cons (car lang) (wl-news-parse-news-subr (car lang)))
514                         news-list))
515     (setq lang (cdr lang)))
516   news-list))
517
518 (defun wl-news-parse-news-subr (lang)
519   (let ((filename (wl-news-news-file lang))
520         (reg (cadr (assoc lang wl-news-search-regexp)))
521         news-list)
522     (if (and filename reg)
523         (with-temp-buffer
524           (insert-file-contents filename)
525           (while (re-search-forward reg nil t)
526             (let ((beg (match-beginning 0))
527                   (version-tmp (split-string (match-string 1) "\\."))
528                   version news-string end)
529               (while version-tmp
530                 (setq version (append version (list (string-to-number (car version-tmp)))))
531                 (setq version-tmp (cdr version-tmp)))
532               (re-search-forward "^\\(\\* \\|\f\\)" nil t)
533               (goto-char (- (match-beginning 0) 1))
534               (setq end (point))
535               (setq news-string (buffer-substring beg end))
536               (setq news-list
537                     (append news-list
538                             (list (cons version news-string))))))))
539     news-list))
540
541
542 \f
543 ;;; ToDo
544 ;;; * MORE refine code (^_^;
545
546 ;;; End