ptexinfmt.el; Fix last change
[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-path (path scanner)
214   (let ((dirs (if (listp path)
215                   path
216                 (list path))))
217     (apply 'append (mapcar scanner dirs))))
218
219 (defun wl-scan-source (path)
220   (wl-scan-path path
221                 (lambda (x)
222                   (apply 'append
223                          (mapcar (lambda (y)
224                                    (list y (concat y "c")))
225                                  (directory-files x nil "\\(.+\\)\\.el$" t))))))
226
227 (defun wl-scan-icons (path)
228   (wl-scan-path path
229                 (lambda (x)
230                   (let ((case-fold-search t))
231                     (directory-files x nil "\\.img$\\|\\.x[bp]m$")))))
232
233 (defun wl-uninstall (objs path)
234   ;(message (mapconcat 'identity objs " "))
235   (mapc
236    (lambda (x)
237      (let ((filename (expand-file-name x path)))
238        (if (and (file-exists-p filename)
239                 (file-writable-p filename))
240            (progn
241              (princ (format "%s was uninstalled.\n" filename))
242              (delete-file filename)))))
243    objs))
244
245
246 (defun wl-examine-modules ()
247   "Examine wl modules should be byte-compile'd."
248   (config-wl-package-subr)
249   (make-wl-news)
250   (dolist (module modules-alist)
251     (dolist (filename (cdr module))
252       (princ (format "%s/%s.elc " (car module) filename)))))
253
254
255 (defun compile-wl-package ()
256   (config-wl-package)
257   (make-wl-news)
258   (mapc
259    (lambda (x)
260      (compile-elisp-modules (cdr x) (car x)))
261    modules-alist))
262
263 (defun install-wl-icons ()
264   (if (not (file-directory-p PIXMAPDIR))
265       (make-directory PIXMAPDIR t))
266   (let* ((case-fold-search t)
267          (icons (directory-files ICONDIR nil
268                                  (cond ((featurep 'xemacs)
269                                         "\\.x[bp]m$")
270                                        ((and (boundp 'emacs-major-version)
271                                              (>= emacs-major-version 21))
272                                         "\\.img$\\|\\.x[bp]m$")
273                                        ((featurep 'mule)
274                                         "\\.img$\\|\\.xbm$")))))
275     (install-files icons ICONDIR PIXMAPDIR nil 'overwrite)))
276
277 (defun uninstall-wl-icons ()
278   (if (not (file-directory-p PIXMAPDIR))
279       ()
280     (wl-uninstall (wl-scan-icons PIXMAPDIR)
281                   PIXMAPDIR)))
282
283 (defun install-wl-package ()
284   (compile-wl-package)
285   (let ((wl-install-dir (expand-file-name WL_PREFIX LISPDIR))
286         (elmo-install-dir (expand-file-name ELMO_PREFIX LISPDIR)))
287     (mapc
288      (lambda (x)
289        (install-elisp-modules (cdr x) (car x)
290                               (if (string= (car x) ELMODIR)
291                                   elmo-install-dir
292                                 wl-install-dir)))
293      modules-alist))
294   (if PIXMAPDIR
295       (install-wl-icons)))
296
297
298 (defun uninstall-wl-package ()
299   (config-wl-package)
300   (let ((wl-install-dir (expand-file-name WL_PREFIX
301                                           LISPDIR))
302         (elmo-install-dir (expand-file-name ELMO_PREFIX
303                                             LISPDIR)))
304     (wl-uninstall (wl-scan-source (list WLDIR UTILSDIR))
305                   wl-install-dir)
306     (wl-uninstall (wl-scan-source (list ELMODIR))
307                   elmo-install-dir))
308   (if PIXMAPDIR
309       (uninstall-wl-icons)))
310
311
312 (defun config-wl-package-xmas ()
313   (if (not (featurep 'xemacs))
314       (error "This directive is only for XEmacs"))
315   (config-wl-package-subr)
316   ;; PACKAGEDIR check.
317   (let (package-dir)
318     (defvar PACKAGEDIR
319       (if (and (setq package-dir (car command-line-args-left))
320                (not (string= "NONE" package-dir)))
321           package-dir
322         (require 'install)
323         (install-get-default-package-directory)))
324     (princ (format "PACKAGEDIR is %s\n" PACKAGEDIR))
325     (setq command-line-args-left (cdr command-line-args-left)))
326   ;; PIXMAPDIR check.
327   (config-wl-pixmap-dir PACKAGEDIR)
328   (princ "\n"))
329
330 ;; from SEMI-MK
331 (defun compile-wl-package-xmas ()
332   (config-wl-package-xmas)
333   (make-wl-news)
334   (setq autoload-package-name "wl")
335   (add-to-list 'command-line-args-left WLDIR)
336   (batch-update-directory)
337   (add-to-list 'command-line-args-left WLDIR)
338   (Custom-make-dependencies)
339   ;; WL-AUTOLOAD-MODULES
340   (compile-elisp-modules WL-AUTOLOAD-MODULES WLDIR)
341   (mapc
342    (lambda (x)
343      (compile-elisp-modules (cdr x) (car x)))
344    modules-alist))
345
346 (defun install-wl-package-xmas ()
347   (compile-wl-package-xmas)
348   (let ((LISPDIR (expand-file-name "wl"
349                                     (expand-file-name "lisp"
350                                                       PACKAGEDIR)))
351         (DATADIR  (expand-file-name "wl"
352                                     (expand-file-name "etc"
353                                                       PACKAGEDIR)))
354         (INFODIR  (expand-file-name "info" PACKAGEDIR)))
355     (or (file-exists-p DATADIR)
356         (make-directory DATADIR t))
357     (or (file-exists-p INFODIR)
358         (make-directory INFODIR t))
359     ;; copy xpm files
360     (install-wl-icons)
361
362     (mapc (lambda (x)
363             (install-elisp-modules (cdr x) (car x) LISPDIR))
364           modules-alist)
365     ;; WL-AUTOLOAD-MODULES
366     (install-elisp-modules WL-AUTOLOAD-MODULES WLDIR LISPDIR)
367     ;;
368     (wl-texinfo-format)
369     (wl-texinfo-install)))
370
371 \f
372 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
373 ;;; Texinfo stuff
374
375 (defun wl-texinfo-info-file (lang)
376   (symbol-value (intern (format "wl-%s-info" lang))))
377
378 (defun wl-texinfo-texi-file (lang)
379   (symbol-value (intern (format "wl-%s-texi" lang))))
380
381 (defun wl-texinfo-check-newer (lang)
382   (let ((info-file (expand-file-name (wl-texinfo-info-file lang) DOCDIR)))
383     (and
384      (file-newer-than-file-p info-file
385                              (expand-file-name "version.texi" DOCDIR))
386      (file-newer-than-file-p info-file
387                              (expand-file-name (wl-texinfo-texi-file lang) DOCDIR)))))
388
389 (defun wl-texinfo-format-file (lang)
390   (require 'wl-vars) ;; for 'wl-cs-local
391   (or (wl-texinfo-check-newer lang)
392       (let (obuf)
393         ;; Support old texinfmt.el
394         (require 'ptexinfmt (expand-file-name "ptexinfmt.el" UTILSDIR))
395         (find-file (expand-file-name (wl-texinfo-texi-file lang) DOCDIR))
396         (setq obuf (current-buffer))
397         ;; We can't know file names if splitted.
398         (texinfo-format-buffer t)
399         ;; Emacs20.2's default is 'raw-text-unix.
400         (and (fboundp 'set-buffer-file-coding-system)
401              (set-buffer-file-coding-system wl-cs-local))
402         (save-buffer)
403         (kill-buffer (current-buffer)) ;; info
404         (kill-buffer obuf)) ;; texi
405       ))
406
407 (defun wl-texinfo-format ()
408   (wl-detect-info-directory)
409   (cond ((null wl-info-lang))
410         ((listp wl-info-lang)
411          (mapc 'wl-texinfo-format-file wl-info-lang))
412         ((stringp wl-info-lang)
413          (wl-texinfo-format-file wl-info-lang))))
414
415 (defun wl-texinfo-install-file (lang)
416   (let ((infofile (wl-texinfo-info-file lang)))
417     (install-file infofile DOCDIR INFODIR nil 'overwrite)))
418
419 (defun wl-texinfo-install ()
420   (cond ((null wl-info-lang))
421         ((listp wl-info-lang)
422          (mapc 'wl-texinfo-install-file wl-info-lang))
423         ((stringp wl-info-lang)
424          (wl-texinfo-install-file wl-info-lang))))
425
426 (defun wl-primary-info-file ()
427   "Get primary info file (for wl-detect-info-directory)."
428   (cond ((null wl-info-lang))
429         ((listp wl-info-lang)
430          (let ((wl-info-lang (car wl-info-lang)))
431            (wl-primary-info-file)))
432         ((stringp wl-info-lang)
433          (wl-texinfo-info-file wl-info-lang))))
434
435 (defun wl-detect-info-directory ()
436   (config-wl-package-subr)
437   ;; INFODIR check.
438   (when wl-info-lang
439     (require 'info)
440     (if (fboundp 'info-initialize)
441         (info-initialize))
442     (unless INFODIR
443       (let ((infodir (car command-line-args-left))
444             (info (wl-primary-info-file))
445             previous)
446         (setq INFODIR
447               (if (string= infodir "NONE")
448                   (if (setq previous
449                             (exec-installed-p info Info-directory-list
450                                               COMPRESS-SUFFIX-LIST))
451                       (directory-file-name (file-name-directory previous))
452                     (car Info-directory-list))
453                 infodir))
454         (setq command-line-args-left (cdr command-line-args-left))))
455     (princ (format "INFODIR is %s\n\n" INFODIR))))
456
457 (defun install-wl-info ()
458   (wl-texinfo-format)
459   (wl-texinfo-install))
460
461 \f
462 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
463 ;;; wl-news stuff
464
465 (defun wl-news-news-file (lang)
466   (cadr (assoc lang wl-news-news-file)))
467
468 (defun wl-news-check-newer (out-filename news-lang)
469   (let ((lang news-lang)
470         ret)
471     (while (car lang)
472       (if (file-newer-than-file-p
473            (wl-news-news-file (car lang)) out-filename)
474           (setq ret t))
475       (setq lang (cdr lang)))
476     ret))
477
478 (defun make-wl-news ()
479   (let ((in-filename
480          (expand-file-name (concat wl-news-filename ".in") WLDIR))
481         (out-filename
482          (expand-file-name wl-news-filename WLDIR))
483         (wl-news-lang (if (listp wl-news-lang)
484                           wl-news-lang
485                         (list wl-news-lang))))
486     (if (or (file-newer-than-file-p in-filename out-filename)
487             (wl-news-check-newer out-filename wl-news-lang))
488         (with-temp-buffer
489           (save-excursion
490             (insert-file-contents in-filename)
491             (goto-char (point-min))
492             (unless (re-search-forward "^;;; -\\*- news-list -\\*-" nil t)
493               (error "Invalid wl-news.el.in"))
494             (forward-line 2)
495             (if wl-news-lang
496                 (progn
497                   (insert "(defconst wl-news-news-alist\n  '")
498                   (let ((p (point)))
499                     (prin1 (wl-news-parse-news wl-news-lang) (current-buffer))
500                     (save-excursion
501                       (narrow-to-region p (point))
502                       (goto-char (1+ p))
503                       (while (re-search-forward "^(" nil t)
504                         (replace-match "\\\\(")) ; avoid font-lock confusion
505                       (widen)))
506                   (insert ")\n"))
507               (insert "(defconst wl-news-news-alist nil)\n\n"))
508             (let ((buffer-file-coding-system (mime-charset-to-coding-system 'x-ctext)))
509               (write-region (point-min) (point-max) out-filename)))))))
510
511 (defun wl-news-parse-news (lang)
512   (let (news-list)
513   (while (car lang)
514     (setq news-list (cons
515                         (cons (car lang) (wl-news-parse-news-subr (car lang)))
516                         news-list))
517     (setq lang (cdr lang)))
518   news-list))
519
520 (defun wl-news-parse-news-subr (lang)
521   (let ((filename (wl-news-news-file lang))
522         (reg (cadr (assoc lang wl-news-search-regexp)))
523         news-list)
524     (if (and filename reg)
525         (with-temp-buffer
526           (insert-file-contents filename)
527           (while (re-search-forward reg nil t)
528             (let ((beg (match-beginning 0))
529                   (version-tmp (split-string (match-string 1) "\\."))
530                   version news-string end)
531               (while version-tmp
532                 (setq version (append version (list (string-to-number (car version-tmp)))))
533                 (setq version-tmp (cdr version-tmp)))
534               (re-search-forward "^\\(\\* \\|\f\\)" nil t)
535               (goto-char (- (match-beginning 0) 1))
536               (setq end (point))
537               (setq news-string (buffer-substring beg end))
538               (setq news-list
539                     (append news-list
540                             (list (cons version news-string))))))))
541     news-list))
542
543
544 \f
545 ;;; ToDo
546 ;;; * MORE refine code (^_^;
547
548 ;;; End