2001-11-01 07:00:00 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / smiley-ems.el
1 ;;; smiley-ems.el --- displaying smiley faces
2
3 ;; Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Keywords: news mail multimedia
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; A re-written, simplified version of Wes Hardaker's XEmacs smiley.el
28 ;; which might be merged back to smiley.el if we get an assignment for
29 ;; that.  We don't have assignments for the images smiley.el uses, but
30 ;; I'm not sure we need that degree of rococoness and defaults like a
31 ;; yellow background.  Also, using PBM means we can display the images
32 ;; more generally.  -- fx
33
34 ;;; Test smileys:  :-) :-\ :-( :-/
35
36 ;;; Code:
37
38 (eval-when-compile (require 'cl))
39 (require 'nnheader)
40
41 (defgroup smiley nil
42   "Turn :-)'s into real images."
43   :group 'gnus-visual)
44
45 ;; Maybe this should go.
46 (defcustom smiley-data-directory (nnheader-find-etc-directory "smilies")
47   "*Location of the smiley faces files."
48   :type 'directory
49   :group 'smiley)
50
51 ;; The XEmacs version has a baroque, if not rococo, set of these.
52 (defcustom smiley-regexp-alist
53   '(("\\(:-?)\\)\\W" 1 "smile.pbm")
54     ("\\(;-?)\\)\\W" 1 "blink.pbm")
55     ("\\(:-[/\\]\\)\\W" 1 "wry.pbm")
56     ("\\(:-(\\)\\W" 1 "sad.pbm")
57     ("\\(:-{\\)\\W" 1 "frown.pbm"))
58   "*A list of regexps to map smilies to images.
59 The elements are (REGEXP MATCH FILE), where MATCH is the submatch in
60 regexp to replace with IMAGE.  IMAGE is the name of a PBM file in
61 `smiley-data-directory'."
62   :type '(repeat (list regexp
63                        (integer :tag "Regexp match number")
64                        (string :tag "Image name")))
65   :set (lambda (symbol value)
66          (set-default symbol value)
67          (smiley-update-cache))
68   :initialize 'custom-initialize-default
69   :group 'smiley)
70
71 (defvar smiley-cached-regexp-alist nil)
72
73 (defun smiley-update-cache ()
74   (dolist (elt (if (symbolp smiley-regexp-alist)
75                    (symbol-value smiley-regexp-alist)
76                  smiley-regexp-alist))
77     (let* ((data-directory smiley-data-directory)
78            (image (find-image (list (list :type 
79                                           (image-type-from-file-header 
80                                            (nth 2 elt)) 
81                                           :file (nth 2 elt)
82                                           :ascent 'center)))))
83       (if image
84           (push (list (car elt) (cadr elt) image)
85                 smiley-cached-regexp-alist)))))
86
87 (defvar smiley-active nil
88   "Non-nil means smilies in the buffer will be displayed.")
89 (make-variable-buffer-local 'smiley-active)
90
91 (defvar smiley-mouse-map
92   (let ((map (make-sparse-keymap)))
93     (define-key map [down-mouse-2] 'ignore) ; override widget
94     (define-key map [mouse-2]
95       'smiley-mouse-toggle-buffer)
96     map))
97
98 ;;;###autoload
99 (defun smiley-region (start end)
100   "Replace in the region `smiley-regexp-alist' matches with corresponding images."
101   (interactive "r")
102   (when (and (fboundp 'display-graphic-p)
103              (display-graphic-p))
104     (mapcar (lambda (o)
105               (if (eq 'smiley (overlay-get o 'smiley))
106                   (delete-overlay o)))
107             (overlays-in start end))
108     (unless smiley-cached-regexp-alist
109       (smiley-update-cache))
110     (save-excursion
111       (let ((beg (or start (point-min)))
112             group overlay image)
113         (dolist (entry smiley-cached-regexp-alist)
114           (setq group (nth 1 entry)
115                 image (nth 2 entry))
116           (goto-char beg)
117           (while (re-search-forward (car entry) end t)
118             (when image
119               (setq overlay (make-overlay (match-beginning group)
120                                           (match-end group)))
121               (overlay-put overlay
122                            'display `(when smiley-active ,@image))
123               (overlay-put overlay 'mouse-face 'highlight)
124               (overlay-put overlay 'smiley t)
125               (overlay-put overlay
126                            'help-echo "mouse-2: toggle smilies in buffer")
127               (overlay-put overlay 'keymap smiley-mouse-map))))))
128     (setq smiley-active t)))
129
130 (defun smiley-toggle-buffer (&optional arg)
131   "Toggle displaying smiley faces.
132 With arg, turn displaying on if and only if arg is positive."
133   (interactive "P")
134   (if (numberp arg)
135       (setq smiley-active (> arg 0))
136     (setq smiley-active (not smiley-active))))
137
138 (defun smiley-mouse-toggle-buffer (event)
139   "Toggle displaying smiley faces.
140 With arg, turn displaying on if and only if arg is positive."
141   (interactive "e")
142   (save-excursion
143     (save-window-excursion
144       (mouse-set-point event)
145       (smiley-toggle-buffer))))
146
147 (eval-when-compile (defvar gnus-article-buffer))
148
149 (defun gnus-smiley-display (&optional arg)
150   "Display textual emoticaons (\"smilies\") as small graphical icons.
151 With arg, turn displaying on if and only if arg is positive."
152   (interactive "P")
153   (save-excursion
154     (article-goto-body)
155     (smiley-region (point) (point-max))
156     (if (and (numberp arg) (<= arg 0))
157         (smiley-toggle-buffer arg))))
158
159 (provide 'smiley)
160
161 ;;; smiley-ems.el ends here