* wl-message.el (wl-message-buffer-prefetch-move-spec-plugged-alist):
[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-2002 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-5]\\)"
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                  (let ((coding-system-for-read 'iso-2022-7bit)
204                        (input-coding-system '*iso-2022-jp*))
205                    (insert-file-contents file))
206                  (goto-char (point-max))
207                  (unless (bolp)
208                    (insert "\n"))
209                  (setq width 0)
210                  (while (progn
211                           (end-of-line 0)
212                           (not (bobp)))
213                    (setq width (max width (current-column))))
214                  ;; Emacs 21.1 would fail to decode composite chars
215                  ;; if it has been built without fixing coding.c.
216                  (when (and wl-on-emacs21
217                             (>= width 80))
218                    (erase-buffer)
219                    (let ((coding-system-for-read 'raw-text))
220                      (insert-file-contents file))
221                    (goto-char (point-max))
222                    (unless (bolp)
223                      (insert "\n"))
224                    (setq width 0)
225                    (while (progn
226                             (end-of-line 0)
227                             (not (bobp)))
228                      ;; Decode bitmap data line by line.
229                      (decode-coding-region (line-beginning-position)
230                                            (point)
231                                            'iso-2022-7bit)
232                      (setq width (max width (current-column)))))
233                  (indent-rigidly (point-min) (point-max)
234                                  (max 0 (/ (1+ (- (window-width) width)) 2)))
235                  (put-text-property (point-min) (point-max)
236                                     'face 'wl-highlight-logo-face)
237                  (count-lines (point-min) (goto-char (point-max))))
238                 ((eq 'xbm image-type)
239                  (message "Composing a bitmap image...")
240                  (require 'bitmap)
241                  (bitmap-insert-xbm-file file)
242                  (backward-char)
243                  (indent-rigidly (point-min) (point-max)
244                                  (max 0 (/ (1+ (- (window-width)
245                                                   (current-column)))
246                                            2)))
247                  (put-text-property (point-min) (point-max)
248                                     'face 'wl-highlight-logo-face)
249                  (message "Composing a bitmap image...done")
250                  (count-lines (point-min) (goto-char (point-max))))))
251       (insert wl-logo-ascii)
252       (put-text-property (point-min) (point) 'face 'wl-highlight-logo-face)
253       (unless (bolp)
254         (insert "\n"))
255       (setq width 0)
256       (while (progn
257                (end-of-line 0)
258                (not (bobp)))
259         (setq width (max width (current-column))))
260       (indent-rigidly (point-min) (point-max)
261                       (max 0 (/ (1+ (- (window-width) width)) 2)))
262       (count-lines (point-min) (goto-char (point-max))))))
263
264 (defun wl-demo-insert-text (height)
265   "Insert a version and the copyright message after a logo image.  HEIGHT
266 should be a number of lines that an image occupies in the buffer."
267   (let* ((height (- (window-height) height 1))
268          (text (format (cond ((<= height 2)
269                               "version %s - \"%s\"\n%s")
270                              ((eq height 3)
271                               "version %s - \"%s\"\n\n%s")
272                              (t
273                               "\nversion %s - \"%s\"\n\n%s"))
274                        (product-version-string (product-find 'wl-version))
275                        (product-code-name (product-find 'wl-version))
276                        wl-demo-copyright-notice))
277          start)
278     (goto-char (point-min))
279     (insert-char ?\n (max 0 (/ (- height 4) 2)))
280     (setq start (goto-char (point-max)))
281     (if wl-on-emacs21
282         (let ((bg (face-background 'wl-highlight-demo-face))
283               (fg (face-foreground 'wl-highlight-demo-face)))
284           (insert (propertize text
285                               'face (nconc '(variable-pitch :slant oblique)
286                                            (when (stringp bg)
287                                              (list ':background bg))
288                                            (when (stringp fg)
289                                              (list ':foreground fg))))))
290       (insert text)
291       (put-text-property start (point) 'face 'wl-highlight-demo-face))
292     (let ((fill-column (window-width)))
293       (center-region start (point)))))
294
295 (defun wl-demo (&optional image-type)
296   "Demo on the startup screen.  IMAGE-TYPE should be a symbol which
297 overrides the variable `wl-demo-display-logo'.  It will prompt user
298 for the type of image when it is called interactively with a prefix
299 argument."
300   (interactive "P")
301   (let ((selection (wl-demo-image-type-alist))
302         type)
303     (if (and image-type (interactive-p))
304         (setq type (completing-read "Image type: " selection nil t)
305               image-type (when (assoc type selection)
306                            (cdr (assoc type selection))))
307       (if (setq type (assoc (format "%s" (or image-type wl-demo-display-logo))
308                             selection))
309           (setq image-type (cdr type))
310         (setq image-type (when wl-demo-display-logo
311                            (cdr (car selection)))))))
312   (let ((buffer (let ((default-enable-multibyte-characters t)
313                       (default-mc-flag t)
314                       (default-line-spacing 0))
315                   (get-buffer-create "*WL Demo*"))))
316     (switch-to-buffer buffer)
317     (setq buffer-read-only nil)
318     (buffer-disable-undo)
319     (erase-buffer)
320     (setq truncate-lines t
321           tab-width 8)
322     (set (make-local-variable 'tab-stop-list)
323          '(8 16 24 32 40 48 56 64 72 80 88 96 104 112 120))
324     (wl-demo-insert-text (wl-demo-insert-image image-type))
325     (set-buffer-modified-p nil)
326     (goto-char (point-min))
327     (sit-for (if (featurep 'lisp-float-type)
328                  (/ (float 5) (float 10))
329                1))
330     buffer))
331
332 (require 'product)
333 (product-provide (provide 'wl-demo) (require 'wl-version))
334
335 ;;; wl-demo.el ends here