1 ;;; smiley.el --- displaying smiley faces
2 ;; Copyright (C) 1996,97 Free Software Foundation, Inc.
4 ;; Author: Wes Hardaker <hardaker@ece.ucdavis.edu>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
30 ;;; Test smileys: :-] :-o :-) ;-) :-\ :-| :-d :-P 8-| :-(
34 ;; (add-hook 'gnus-article-display-hook 'gnus-smiley-display t)
36 ;; The smilies were drawn by Joe Reiss <jreiss@vt.edu>.
38 (require 'annotations)
39 (require 'messagexmas)
44 "Turn :-)'s into real images (XEmacs)."
47 (defcustom smiley-data-directory (message-xmas-find-glyph-directory "smilies")
48 "Location of the smiley faces files."
52 ;; Notice the subtle differences in the regular expressions in the
55 (defcustom smiley-deformed-regexp-alist
56 '(("\\(:-*[<«]+\\)\\W" 1 "FaceAngry.xpm")
57 ("\\(:-+\\]+\\)\\W" 1 "FaceGoofy.xpm")
58 ("\\(:-*D\\)\\W" 1 "FaceGrinning.xpm")
59 ("\\(:-*[)>}»]+\\)\\W" 1 "FaceHappy.xpm")
60 ("\\(:-*[/\\\"]\\)[^/]\\W" 1 "FaceIronic.xpm")
61 ("\\([8|]-*[|Oo%]\\)\\W" 1 "FaceKOed.xpm")
62 ("\\([:|]-*#+\\)\\W" 1 "FaceNyah.xpm")
63 ("\\(:-*[({]+\\)\\W" 1 "FaceSad.xpm")
64 ("\\(:-*[Oo\*]\\)\\W" 1 "FaceStartled.xpm")
65 ("\\(:-*|\\)\\W" 1 "FaceStraight.xpm")
66 ("\\(:-*p\\)\\W" 1 "FaceTalking.xpm")
67 ("\\(:-*d\\)\\W" 1 "FaceTasty.xpm")
68 ("\\(;-*[>)}»]+\\)\\W" 1 "FaceWinking.xpm")
69 ("\\(:-*[Vvµ]\\)\\W" 1 "FaceWry.xpm")
70 ("\\([:|]-*P\\)\\W" 1 "FaceYukky.xpm"))
71 "Normal and deformed faces for smilies."
72 :type '(repeat (list regexp
73 (integer :tag "Match")
74 (string :tag "Image")))
77 (defcustom smiley-nosey-regexp-alist
78 '(("\\(:-+[<«]+\\)\\W" 1 "FaceAngry.xpm")
79 ("\\(:-+\\]+\\)\\W" 1 "FaceGoofy.xpm")
80 ("\\(:-+D\\)\\W" 1 "FaceGrinning.xpm")
81 ("\\(:-+[}»]+\\)\\W" 1 "FaceHappy.xpm")
82 ("\\(:-*)+\\)\\W" 1 "FaceHappy.xpm")
83 ("\\(:-+[/\\\"]+\\)\\W" 1 "FaceIronic.xpm")
84 ("\\([8|]-+[|Oo%]\\)\\W" 1 "FaceKOed.xpm")
85 ("\\([:|]-+#+\\)\\W" 1 "FaceNyah.xpm")
86 ("\\(:-+[({]+\\)\\W" 1 "FaceSad.xpm")
87 ("\\(:-+[Oo\*]\\)\\W" 1 "FaceStartled.xpm")
88 ("\\(:-+|\\)\\W" 1 "FaceStraight.xpm")
89 ("\\(:-+p\\)\\W" 1 "FaceTalking.xpm")
90 ("\\(:-+d\\)\\W" 1 "FaceTasty.xpm")
91 ("\\(;-+[>)}»]+\\)\\W" 1 "FaceWinking.xpm")
92 ("\\(:-+[Vvµ]\\)\\W" 1 "FaceWry.xpm")
93 ("\\(][:8B]-[)>]\\)\\W" 1 "FaceDevilish.xpm")
94 ("\\([:|]-+P\\)\\W" 1 "FaceYukky.xpm"))
95 "Smileys with noses. These get less false matches."
96 :type '(repeat (list regexp
97 (integer :tag "Match")
98 (string :tag "Image")))
101 (defcustom smiley-regexp-alist smiley-deformed-regexp-alist
102 "A list of regexps to map smilies to real images.
103 Defaults to the contents of `smiley-deformed-regexp-alist'.
104 An alternative is `smiley-nosey-regexp-alist' that matches less
106 If this is a symbol, take its value."
107 :type '(radio (variable-item smiley-deformed-regexp-alist)
108 (variable-item smiley-nosey-regexp-alist)
111 (integer :tag "Match")
112 (string :tag "Image"))))
115 (defcustom smiley-flesh-color "yellow"
120 (defcustom smiley-features-color "black"
125 (defcustom smiley-tongue-color "red"
130 (defcustom smiley-circle-color "black"
135 (defcustom smiley-mouse-face 'highlight
136 "Face used for mouse highlighting in the smiley buffer.
138 Smiley buttons will be displayed in this face when the cursor is
144 (defvar smiley-glyph-cache nil)
145 (defvar smiley-running-xemacs (string-match "XEmacs" emacs-version))
147 (defvar smiley-map (make-sparse-keymap "smiley-keys")
148 "Keymap to toggle smiley states.")
150 (define-key smiley-map [(button2)] 'smiley-toggle-extent)
152 (defun smiley-create-glyph (smiley pixmap)
154 smiley-running-xemacs
156 (cdr-safe (assoc pixmap smiley-glyph-cache))
157 (let* ((xpm-color-symbols
159 (append `(("flesh" ,smiley-flesh-color)
160 ("features" ,smiley-features-color)
161 ("tongue" ,smiley-tongue-color))
165 (cons 'x (expand-file-name pixmap smiley-data-directory))
166 (cons 'tty smiley)))))
167 (setq smiley-glyph-cache (cons (cons pixmap glyph) smiley-glyph-cache))
168 (set-glyph-face glyph 'default)
172 (defun smiley-region (beg end)
173 "Smilify the region between point and mark."
175 (smiley-buffer (current-buffer) beg end))
177 (defun smiley-toggle-extent (event)
178 "Toggle smiley at given point"
180 (let* ((ant (event-glyph-extent event))
181 (pt (event-closest-point event))
183 (if (annotationp ant)
184 (when (extentp (setq ext (extent-property ant 'smiley-extent)))
185 (set-extent-property ext 'invisible nil)
186 (hide-annotation ant))
188 (while (setq ext (extent-at pt (event-buffer event) nil ext 'at))
189 (when (annotationp (setq ant
190 (extent-property ext 'smiley-annotation)))
191 (reveal-annotation ant)
192 (set-extent-property ext 'invisible t)))))))
195 (defun smiley-buffer (&optional buffer st nd)
201 (let ((buffer-read-only nil)
202 (alist (if (symbolp smiley-regexp-alist)
203 (symbol-value smiley-regexp-alist)
204 smiley-regexp-alist))
205 entry regexp beg group file)
206 (goto-char (or st (point-min)))
208 ;; loop through alist
209 (while (setq entry (pop alist))
210 (setq regexp (car entry)
214 (while (re-search-forward regexp nd t)
215 (let* ((start (match-beginning group))
216 (end (match-end group))
217 (glyph (smiley-create-glyph (buffer-substring start end)
220 (mapcar 'delete-annotation (annotations-at end))
221 (let ((ext (make-extent start end))
222 (ant (make-annotation glyph end 'text)))
223 ;; set text extent params
224 (set-extent-property ext 'end-open t)
225 (set-extent-property ext 'start-open t)
226 (set-extent-property ext 'invisible t)
227 (set-extent-property ext 'keymap smiley-map)
228 (set-extent-property ext 'mouse-face smiley-mouse-face)
229 (set-extent-property ext 'intangible t)
230 ;; set annotation params
231 (set-extent-property ant 'mouse-face smiley-mouse-face)
232 (set-extent-property ant 'keymap smiley-map)
233 ;; remember each other
234 (set-extent-property ant 'smiley-extent ext)
235 (set-extent-property ext 'smiley-annotation ant))
236 (when (smiley-end-paren-p start end)
237 (make-annotation ")" end 'text))
238 (goto-char end)))))))))
240 (defun smiley-end-paren-p (start end)
241 "Try to guess whether the current smiley is an end-paren smiley."
244 (when (and (re-search-backward "[()]" nil t)
245 (= (following-char) ?\()
247 (or (not (re-search-forward "[()]" nil t))
248 (= (char-after (1- (point))) ?\()))
251 (defvar gnus-article-buffer)
253 (defun gnus-smiley-display ()
256 (set-buffer gnus-article-buffer)
257 (goto-char (point-min))
258 ;; We skip the headers.
259 (unless (search-forward "\n\n" nil t)
260 (goto-char (point-max)))
261 (smiley-buffer (current-buffer) (point))))
265 ;;; smiley.el ends here