078ca16cbd687158d4018f946aeca084cddde0b0
[elisp/wanderlust.git] / wl / wl-demo.el
1 ;;; wl-demo.el --- Opening demo on Wanderlust
2
3 ;; Copyright (C) 1998,1999,2000,2001 Yuuichi Teranishi <teranisi@gohome.org>
4 ;; Copyright (C) 2000,2001 Katsumi Yamaoka <yamaoka@jpl.org>
5
6 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
7 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
8 ;; Keywords: mail, net news
9
10 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
11
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (defconst wl-demo-copyright-notice
32   "Copyright (C) 1998-2001 Yuuichi Teranishi <teranisi@gohome.org>"
33   "A declaration of the copyright on Wanderlust.")
34
35 (eval-when-compile
36   (require 'cl))
37 (require 'path-util)
38 (require 'wl-vars)
39 (require 'wl-version)
40 (require 'wl-highlight)
41
42 (defconst wl-demo-icon-name
43   (concat "wl-" (wl-version-status)
44           (if (string-match "^... Dec \\([ 1][0-9]\\|2[0-4]\\)"
45                             (current-time-string))
46               "-xmas-logo"
47             "-logo"))
48   "Basename of the logo file.")
49
50 (defvar wl-logo-ascii "\
51         o$                  oo$$$$$$ooo
52      oo$$$      o$$      o$$$\"\"\"\"\"\"$$$$$o
53   $$$$$$\"     o$$$\"    o$\"\"          \"$$$
54     $$\"      o$\"\"    o$\"              $$$
55    $\"      oo$\"     $\"                $$$
56  o$     oo\"\"$$     $                  $$
57 o$$  oo$\"  \"$$$o  $                 o$$
58 $$$$\"\"       \"$$oo$    o          o$\"
59                \"$$o   \"$$$o oooo$\"\"
60                  $$       \"\"\"\"
61                Wanderlust
62                   \"$
63 Yet Another Message Interface On Emacsen"
64   "Ascii picture used to splash the startup screen.")
65
66 (eval-and-compile
67   (when wl-on-emacs21
68     ;; `display-images-p' has not been available in Emacs versions
69     ;; prior to Emacs 21.0.105.
70     (defalias-maybe 'display-images-p 'display-graphic-p)))
71
72 ;; Avoid byte compile warnings.
73 (eval-when-compile
74   (autoload 'bitmap-insert-xbm-file "bitmap" nil t)
75   (autoload 'create-image "image")
76   (autoload 'device-on-window-system-p "device")
77   (autoload 'image-type-available-p "image")
78   (autoload 'insert-image "image")
79   (autoload 'make-glyph "glyphs")
80   (autoload 'set-glyph-face "glyphs")
81   (autoload 'set-specifier "specifier")
82   (defalias-maybe 'frame-char-height 'ignore)
83   (defalias-maybe 'frame-char-width 'ignore)
84   (defalias-maybe 'glyph-height 'ignore)
85   (defalias-maybe 'glyph-width 'ignore)
86   (defalias-maybe 'image-size 'ignore)
87   (defalias-maybe 'make-extent 'ignore)
88   (defalias-maybe 'propertize 'ignore)
89   (defalias-maybe 'set-extent-end-glyph 'ignore)
90   (defalias-maybe 'window-pixel-height 'ignore)
91   (defalias-maybe 'window-pixel-width 'ignore))
92
93 (defvar wl-demo-bitmap-mule-available-p 'unknown
94   "Internal variable to say whether the BITMAP-MULE package is available.")
95
96 (defun wl-demo-image-type-alist ()
97   "Return an alist of available logo image types on the current frame."
98   (if (or (and (featurep 'xemacs)
99                (device-on-window-system-p))
100           window-system)
101       (append
102        (when (or (and (featurep 'xemacs)
103                       (featurep 'xpm))
104                  (and wl-on-emacs21
105                       (display-images-p)
106                       (image-type-available-p 'xpm)))
107          '(("xpm" . xpm)))
108        (when (and (not (or (featurep 'xemacs)
109                            ;; *.img files won't fit for Emacs 21.
110                            wl-on-emacs21))
111                   (or (eq t wl-demo-bitmap-mule-available-p)
112                       (and (eq 'unknown wl-demo-bitmap-mule-available-p)
113                            (module-installed-p 'bitmap)
114                            (setq wl-demo-bitmap-mule-available-p t))))
115          '(("bitmap" . bitmap)))
116        (when (or (featurep 'xemacs)
117                  (and wl-on-emacs21
118                       (display-images-p)
119                       (image-type-available-p 'xbm))
120                  (eq t wl-demo-bitmap-mule-available-p)
121                  (and (eq 'unknown wl-demo-bitmap-mule-available-p)
122                       (module-installed-p 'bitmap)
123                       (setq wl-demo-bitmap-mule-available-p t)))
124          '(("xbm" . xbm)))
125        '(("ascii")))
126     '(("ascii"))))
127
128 (defun wl-demo-insert-image (image-type)
129   "Insert a logo image at the point and position it to be centered.
130 IMAGE-TYPE specifies what a type of image should be displayed.
131 Return a number of lines that an image occupies in the buffer."
132   (let ((file (cond ((eq 'xpm image-type)
133                      (concat wl-demo-icon-name ".xpm"))
134                     ((eq 'bitmap image-type)
135                      (concat wl-demo-icon-name ".img"))
136                     ((eq 'xbm image-type)
137                      (concat wl-demo-icon-name ".xbm"))))
138         image width height)
139     (when (featurep 'xemacs)
140       (when (boundp 'default-gutter-visible-p)
141         (set-specifier (symbol-value 'default-gutter-visible-p)
142                        nil (current-buffer)))
143       (set-specifier (symbol-value 'scrollbar-height) 0 (current-buffer))
144       (set-specifier (symbol-value 'scrollbar-width) 0 (current-buffer)))
145     (if (and file
146              (if (and wl-icon-directory
147                       (file-directory-p wl-icon-directory))
148                  (setq file (expand-file-name file wl-icon-directory))
149                (message "You have to specify the value of `wl-icon-directory'")
150                nil)
151              (if (file-exists-p file)
152                  (if (file-readable-p file)
153                      t
154                    (message "Permission denied: %s" file)
155                    nil)
156                (message "File not found: %s" file)
157                nil))
158         (progn
159           (cond ((featurep 'xemacs)
160                  (setq width (window-pixel-width)
161                        height (window-pixel-height)
162                        image (make-glyph (vector image-type ':file file)))
163                  (when (eq 'xbm image-type)
164                    (set-glyph-face image 'wl-highlight-logo-face))
165                  (insert-char ?\  (max 0 (/ (+ (* (- width (glyph-width image))
166                                                   (window-width)) width)
167                                             (* 2 width))))
168                  (set-extent-end-glyph (make-extent (point) (point)) image)
169                  (insert "\n")
170                  (/ (+ (* 2 (glyph-height image) (window-height)) height)
171                     (* 2 height)))
172                 ((eq 'bitmap image-type)
173                  (require 'bitmap)
174                  (let ((coding-system-for-read 'iso-2022-7bit)
175                        (input-coding-system '*iso-2022-jp*))
176                    (insert-file-contents file))
177                  (end-of-line)
178                  (indent-rigidly (point-min) (point-max)
179                                  (max 0 (/ (1+ (- (window-width)
180                                                   (current-column)))
181                                            2)))
182                  (put-text-property (point-min) (point-max)
183                                     'face 'wl-highlight-logo-face)
184                  (count-lines (point-min) (goto-char (point-max))))
185                 ((>= emacs-major-version 21)
186                  (setq image (create-image file image-type)
187                        width (image-size image)
188                        height (cdr width)
189                        width (car width))
190                  (when (eq 'xbm image-type)
191                    (let ((bg (face-background 'wl-highlight-logo-face))
192                          (fg (face-foreground 'wl-highlight-logo-face)))
193                      (when (stringp bg)
194                        (plist-put (cdr image) ':background bg))
195                      (when (stringp fg)
196                        (plist-put (cdr image) ':foreground fg))))
197                  (insert (propertize " " 'display
198                                      (list 'space ':align-to
199                                            (max 0 (round (- (window-width)
200                                                             width)
201                                                          2)))))
202                  (insert-image image)
203                  (insert "\n")
204                  (round height))
205                 ((eq 'xbm image-type)
206                  (message "Composing a bitmap image...")
207                  (require 'bitmap)
208                  (bitmap-insert-xbm-file file)
209                  (backward-char)
210                  (indent-rigidly (point-min) (point-max)
211                                  (max 0 (/ (1+ (- (window-width)
212                                                   (current-column)))
213                                            2)))
214                  (put-text-property (point-min) (point-max)
215                                     'face 'wl-highlight-logo-face)
216                  (message "Composing a bitmap image...done")
217                  (count-lines (point-min) (goto-char (point-max))))))
218       (insert wl-logo-ascii)
219       (put-text-property (point-min) (point) 'face 'wl-highlight-logo-face)
220       (unless (bolp)
221         (insert "\n"))
222       (setq width 0)
223       (while (progn
224                (end-of-line 0)
225                (not (bobp)))
226         (setq width (max width (current-column))))
227       (indent-rigidly (point-min) (point-max)
228                       (max 0 (/ (1+ (- (window-width) width)) 2)))
229       (count-lines (point-min) (goto-char (point-max))))))
230
231 (defun wl-demo-insert-text (height)
232   "Insert a version and the copyright message after a logo image.  HEIGHT
233 should be a number of lines that an image occupies in the buffer."
234   (let* ((height (- (window-height) height 1))
235          (text (format (cond ((<= height 2)
236                               "version %s - \"%s\"\n%s")
237                              ((eq height 3)
238                               "version %s - \"%s\"\n\n%s")
239                              (t
240                               "\nversion %s - \"%s\"\n\n%s"))
241                        (product-version-string (product-find 'wl-version))
242                        (product-code-name (product-find 'wl-version))
243                        wl-demo-copyright-notice))
244          start)
245     (goto-char (point-min))
246     (insert-char ?\n (max 0 (/ (- height 4) 2)))
247     (setq start (goto-char (point-max)))
248     (if wl-on-emacs21
249         (let ((bg (face-background 'wl-highlight-demo-face))
250               (fg (face-foreground 'wl-highlight-demo-face)))
251           (insert (propertize text
252                               'face (nconc '(variable-pitch :slant oblique)
253                                            (when (stringp bg)
254                                              (list ':background bg))
255                                            (when (stringp fg)
256                                              (list ':foreground fg))))))
257       (insert text)
258       (put-text-property start (point) 'face 'wl-highlight-demo-face))
259     (let ((fill-column (window-width)))
260       (center-region start (point)))))
261
262 (defun wl-demo (&optional image-type)
263   "Demo on the startup screen.  IMAGE-TYPE should be a symbol which
264 overrides the variable `wl-demo-display-logo'.  It will prompt user
265 for the type of image when it is called interactively with a prefix
266 argument."
267   (interactive "P")
268   (let ((selection (wl-demo-image-type-alist))
269         type)
270     (if (and image-type (interactive-p))
271         (setq type (completing-read "Image type: " selection nil t)
272               image-type (when (assoc type selection)
273                            (cdr (assoc type selection))))
274       (if (setq type (assoc (format "%s" (or image-type wl-demo-display-logo))
275                             selection))
276           (setq image-type (cdr type))
277         (setq image-type (when wl-demo-display-logo
278                            (cdr (car selection)))))))
279   (let ((buffer (let ((default-enable-multibyte-characters t)
280                       (default-mc-flag t)
281                       (default-line-spacing 0))
282                   (get-buffer-create "*WL Demo*"))))
283     (switch-to-buffer buffer)
284     (setq buffer-read-only nil)
285     (erase-buffer)
286     (setq truncate-lines t
287           tab-width 8)
288     (set (make-local-variable 'tab-stop-list)
289          '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120))
290     (wl-demo-insert-text (wl-demo-insert-image image-type))
291     (set-buffer-modified-p nil)
292     (goto-char (point-min))
293     (sit-for (if (featurep 'lisp-float-type)
294                  (/ (float 5) (float 10))
295                1))
296     buffer))
297
298 (require 'product)
299 (product-provide (provide 'wl-demo) (require 'wl-version))
300
301 ;;; wl-demo.el ends here