Fix the most recent change.
[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       (let ((xpm
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             (xbm
109              (when (or (featurep 'xemacs)
110                        (and wl-on-emacs21
111                             (display-images-p)
112                             (image-type-available-p 'xbm))
113                        (eq t wl-demo-bitmap-mule-available-p)
114                        (and (eq 'unknown wl-demo-bitmap-mule-available-p)
115                             (module-installed-p 'bitmap)
116                             (setq wl-demo-bitmap-mule-available-p t)))
117                '("xbm" . xbm)))
118             (bitmap
119              (when (and (not (featurep 'xemacs))
120                         (or (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                '("bitmap" . bitmap))))
125         (if (and wl-on-emacs21
126                  (image-type-available-p 'xbm))
127             ;; Prefer xbm rather than bitmap on Emacs 21.
128             (delq nil (list xpm xbm bitmap '("ascii")))
129           (delq nil (list xpm bitmap xbm '("ascii")))))
130     '(("ascii"))))
131
132 (defun wl-demo-insert-image (image-type)
133   "Insert a logo image at the point and position it to be centered.
134 IMAGE-TYPE specifies what a type of image should be displayed.
135 Return a number of lines that an image occupies in the buffer."
136   (let ((file (cond ((eq 'xpm image-type)
137                      (concat wl-demo-icon-name ".xpm"))
138                     ((eq 'bitmap image-type)
139                      (concat wl-demo-icon-name ".img"))
140                     ((eq 'xbm image-type)
141                      (concat wl-demo-icon-name ".xbm"))))
142         image width height)
143     (when (featurep 'xemacs)
144       (when (boundp 'default-gutter-visible-p)
145         (set-specifier (symbol-value 'default-gutter-visible-p)
146                        nil (current-buffer)))
147       (set-specifier (symbol-value 'scrollbar-height) 0 (current-buffer))
148       (set-specifier (symbol-value 'scrollbar-width) 0 (current-buffer)))
149     (if (and file
150              (if (and wl-icon-directory
151                       (file-directory-p wl-icon-directory))
152                  (setq file (expand-file-name file wl-icon-directory))
153                (message "You have to specify the value of `wl-icon-directory'")
154                nil)
155              (if (file-exists-p file)
156                  (if (file-readable-p file)
157                      t
158                    (message "Permission denied: %s" file)
159                    nil)
160                (message "File not found: %s" file)
161                nil))
162         (progn
163           (cond ((featurep 'xemacs)
164                  (setq width (window-pixel-width)
165                        height (window-pixel-height)
166                        image (make-glyph (vector image-type ':file file)))
167                  (when (eq 'xbm image-type)
168                    (set-glyph-face image 'wl-highlight-logo-face))
169                  (insert-char ?\  (max 0 (/ (+ (* (- width (glyph-width image))
170                                                   (window-width)) width)
171                                             (* 2 width))))
172                  (set-extent-end-glyph (make-extent (point) (point)) image)
173                  (insert "\n")
174                  (/ (+ (* 2 (glyph-height image) (window-height)) height)
175                     (* 2 height)))
176                 ((and wl-on-emacs21
177                       (or (eq 'xpm image-type)
178                           (and (eq 'xbm image-type)
179                                (image-type-available-p 'xbm))))
180                  ;; Use the new redisplay engine on Emacs 21.
181                  (setq image (create-image file image-type)
182                        width (image-size image)
183                        height (cdr width)
184                        width (car width))
185                  (when (eq 'xbm image-type)
186                    (let ((bg (face-background 'wl-highlight-logo-face))
187                          (fg (face-foreground 'wl-highlight-logo-face)))
188                      (when (stringp bg)
189                        (plist-put (cdr image) ':background bg))
190                      (when (stringp fg)
191                        (plist-put (cdr image) ':foreground fg))))
192                  (insert (propertize " " 'display
193                                      (list 'space ':align-to
194                                            (max 0 (round (- (window-width)
195                                                             width)
196                                                          2)))))
197                  (insert-image image)
198                  (insert "\n")
199                  (round height))
200                 ((eq 'bitmap image-type)
201                  ;; Use ready-composed bitmap image.
202                  (require 'bitmap)
203                  (if wl-on-emacs21
204                      (progn
205                        ;; Decode bitmap data line by line.
206                        (let ((coding-system-for-read 'raw-text))
207                          (insert-file-contents file))
208                        (while (not (eobp))
209                          (decode-coding-region (point) (line-end-position)
210                                                'iso-2022-7bit)
211                          (forward-line 1))
212                        (goto-char (point-min)))
213                    (let ((coding-system-for-read 'iso-2022-7bit)
214                          (input-coding-system '*iso-2022-jp*))
215                      (insert-file-contents file)))
216                  (end-of-line)
217                  (indent-rigidly (point-min) (point-max)
218                                  (max 0 (/ (1+ (- (window-width)
219                                                   (current-column)))
220                                            2)))
221                  (put-text-property (point-min) (point-max)
222                                     'face 'wl-highlight-logo-face)
223                  (count-lines (point-min) (goto-char (point-max))))
224                 ((eq 'xbm image-type)
225                  (message "Composing a bitmap image...")
226                  (require 'bitmap)
227                  (bitmap-insert-xbm-file file)
228                  (backward-char)
229                  (indent-rigidly (point-min) (point-max)
230                                  (max 0 (/ (1+ (- (window-width)
231                                                   (current-column)))
232                                            2)))
233                  (put-text-property (point-min) (point-max)
234                                     'face 'wl-highlight-logo-face)
235                  (message "Composing a bitmap image...done")
236                  (count-lines (point-min) (goto-char (point-max))))))
237       (insert wl-logo-ascii)
238       (put-text-property (point-min) (point) 'face 'wl-highlight-logo-face)
239       (unless (bolp)
240         (insert "\n"))
241       (setq width 0)
242       (while (progn
243                (end-of-line 0)
244                (not (bobp)))
245         (setq width (max width (current-column))))
246       (indent-rigidly (point-min) (point-max)
247                       (max 0 (/ (1+ (- (window-width) width)) 2)))
248       (count-lines (point-min) (goto-char (point-max))))))
249
250 (defun wl-demo-insert-text (height)
251   "Insert a version and the copyright message after a logo image.  HEIGHT
252 should be a number of lines that an image occupies in the buffer."
253   (let* ((height (- (window-height) height 1))
254          (text (format (cond ((<= height 2)
255                               "version %s - \"%s\"\n%s")
256                              ((eq height 3)
257                               "version %s - \"%s\"\n\n%s")
258                              (t
259                               "\nversion %s - \"%s\"\n\n%s"))
260                        (product-version-string (product-find 'wl-version))
261                        (product-code-name (product-find 'wl-version))
262                        wl-demo-copyright-notice))
263          start)
264     (goto-char (point-min))
265     (insert-char ?\n (max 0 (/ (- height 4) 2)))
266     (setq start (goto-char (point-max)))
267     (if wl-on-emacs21
268         (let ((bg (face-background 'wl-highlight-demo-face))
269               (fg (face-foreground 'wl-highlight-demo-face)))
270           (insert (propertize text
271                               'face (nconc '(variable-pitch :slant oblique)
272                                            (when (stringp bg)
273                                              (list ':background bg))
274                                            (when (stringp fg)
275                                              (list ':foreground fg))))))
276       (insert text)
277       (put-text-property start (point) 'face 'wl-highlight-demo-face))
278     (let ((fill-column (window-width)))
279       (center-region start (point)))))
280
281 (defun wl-demo (&optional image-type)
282   "Demo on the startup screen.  IMAGE-TYPE should be a symbol which
283 overrides the variable `wl-demo-display-logo'.  It will prompt user
284 for the type of image when it is called interactively with a prefix
285 argument."
286   (interactive "P")
287   (let ((selection (wl-demo-image-type-alist))
288         type)
289     (if (and image-type (interactive-p))
290         (setq type (completing-read "Image type: " selection nil t)
291               image-type (when (assoc type selection)
292                            (cdr (assoc type selection))))
293       (if (setq type (assoc (format "%s" (or image-type wl-demo-display-logo))
294                             selection))
295           (setq image-type (cdr type))
296         (setq image-type (when wl-demo-display-logo
297                            (cdr (car selection)))))))
298   (let ((buffer (let ((default-enable-multibyte-characters t)
299                       (default-mc-flag t)
300                       (default-line-spacing 0))
301                   (get-buffer-create "*WL Demo*"))))
302     (switch-to-buffer buffer)
303     (setq buffer-read-only nil)
304     (erase-buffer)
305     (setq truncate-lines t
306           tab-width 8)
307     (set (make-local-variable 'tab-stop-list)
308          '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120))
309     (wl-demo-insert-text (wl-demo-insert-image image-type))
310     (set-buffer-modified-p nil)
311     (goto-char (point-min))
312     (sit-for (if (featurep 'lisp-float-type)
313                  (/ (float 5) (float 10))
314                1))
315     buffer))
316
317 (require 'product)
318 (product-provide (provide 'wl-demo) (require 'wl-version))
319
320 ;;; wl-demo.el ends here