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 two alists below
54 (defcustom smiley-deformed-regexp-alist
55 '(("\\(:-*[<«]+\\)\\W" 1 "FaceAngry.xpm")
56 ("\\(:-+\\]+\\)\\W" 1 "FaceGoofy.xpm")
57 ("\\(:-*D\\)\\W" 1 "FaceGrinning.xpm")
58 ("\\(:-*[)>}»]+\\)\\W" 1 "FaceHappy.xpm")
59 ("\\(:-*[/\\\"]\\)[^/]" 1 "FaceIronic.xpm")
60 ("\\([8|]-*[|Oo%]\\)\\W" 1 "FaceKOed.xpm")
61 ("\\([:|]-*#+\\)\\W" 1 "FaceNyah.xpm")
62 ("\\(:-*[({]+\\)\\W" 1 "FaceSad.xpm")
63 ("\\(:-*[Oo\*]\\)\\W" 1 "FaceStartled.xpm")
64 ("\\(:-*|\\)\\W" 1 "FaceStraight.xpm")
65 ("\\(:-*p\\)\\W" 1 "FaceTalking.xpm")
66 ("\\(:-*d\\)\\W" 1 "FaceTasty.xpm")
67 ("\\(;-*[>)}»]+\\)\\W" 1 "FaceWinking.xpm")
68 ("\\(:-*[Vvµ]\\)\\W" 1 "FaceWry.xpm")
69 ("\\([:|]-*P\\)\\W" 1 "FaceYukky.xpm"))
70 "Normal and deformed faces for smilies."
71 :type '(repeat (list regexp
72 (integer :tag "Match")
73 (string :tag "Image")))
76 (defcustom smiley-nosey-regexp-alist
77 '(("\\(:-+[<«]+\\)\\W" 1 "FaceAngry.xpm")
78 ("\\(:-+\\]+\\)\\W" 1 "FaceGoofy.xpm")
79 ("\\(:-+D\\)\\W" 1 "FaceGrinning.xpm")
80 ("\\(:-+[}»]+\\)\\W" 1 "FaceHappy.xpm")
81 ("\\(:-*)+\\)\\W" 1 "FaceHappy.xpm")
82 ("\\(:-+[/\\\"]+\\)\\W" 1 "FaceIronic.xpm")
83 ("\\([8|]-+[|Oo%]\\)\\W" 1 "FaceKOed.xpm")
84 ("\\([:|]-+#+\\)\\W" 1 "FaceNyah.xpm")
85 ("\\(:-+[({]+\\)\\W" 1 "FaceSad.xpm")
86 ("\\(:-+[Oo\*]\\)\\W" 1 "FaceStartled.xpm")
87 ("\\(:-+|\\)\\W" 1 "FaceStraight.xpm")
88 ("\\(:-+p\\)\\W" 1 "FaceTalking.xpm")
89 ("\\(:-+d\\)\\W" 1 "FaceTasty.xpm")
90 ("\\(;-+[>)}»]+\\)\\W" 1 "FaceWinking.xpm")
91 ("\\(:-+[Vvµ]\\)\\W" 1 "FaceWry.xpm")
92 ("\\(][:8B]-[)>]\\)\\W" 1 "FaceDevilish.xpm")
93 ("\\([:|]-+P\\)\\W" 1 "FaceYukky.xpm"))
94 "Smileys with noses. These get less false matches."
95 :type '(repeat (list regexp
96 (integer :tag "Match")
97 (string :tag "Image")))
100 (defcustom smiley-regexp-alist smiley-deformed-regexp-alist
101 "A list of regexps to map smilies to real images.
102 Defaults to the content of smiley-deformed-regexp-alist.
103 An alternative smiley-nosey-regexp-alist that
104 matches less aggressively is available.
105 If this is a symbol, take its value."
106 :type '(radio (variable-item smiley-deformed-regexp-alist)
107 (variable-item smiley-nosey-regexp-alist)
110 (integer :tag "Match")
111 (string :tag "Image"))))
114 (defcustom smiley-flesh-color "yellow"
119 (defcustom smiley-features-color "black"
124 (defcustom smiley-tongue-color "red"
129 (defcustom smiley-circle-color "black"
134 (defcustom smiley-mouse-face 'highlight
135 "Face used for mouse highlighting in the smiley buffer.
137 Smiley buttons will be displayed in this face when the cursor is
143 (defvar smiley-glyph-cache nil)
144 (defvar smiley-running-xemacs (string-match "XEmacs" emacs-version))
146 (defvar smiley-map (make-sparse-keymap "smiley-keys")
147 "keymap to toggle smiley states")
149 (define-key smiley-map [(button2)] 'smiley-toggle-extent)
151 (defun smiley-create-glyph (smiley pixmap)
153 smiley-running-xemacs
155 (cdr-safe (assoc pixmap smiley-glyph-cache))
156 (let* ((xpm-color-symbols
158 (append `(("flesh" ,smiley-flesh-color)
159 ("features" ,smiley-features-color)
160 ("tongue" ,smiley-tongue-color))
164 (cons 'x (expand-file-name pixmap smiley-data-directory))
165 (cons 'tty smiley)))))
166 (setq smiley-glyph-cache (cons (cons pixmap glyph) smiley-glyph-cache))
167 (set-glyph-face glyph 'default)
171 (defun smiley-region (beg end)
172 "Smilify the region between point and mark."
174 (smiley-buffer (current-buffer) beg end))
176 (defun smiley-toggle-extent (event)
177 "Toggle smiley at given point"
179 (let* ((ant (event-glyph-extent event))
180 (pt (event-closest-point event))
182 (if (annotationp ant)
183 (when (extentp (setq ext (extent-property ant 'smiley-extent)))
184 (set-extent-property ext 'invisible nil)
185 (hide-annotation ant))
187 (while (setq ext (extent-at pt (event-buffer event) nil ext 'at))
188 (when (annotationp (setq ant
189 (extent-property ext 'smiley-annotation)))
190 (reveal-annotation ant)
191 (set-extent-property ext 'invisible t)))))))
194 (defun smiley-buffer (&optional buffer st nd)
200 (let ((buffer-read-only nil)
201 (alist (if (symbolp smiley-regexp-alist)
202 (symbol-value smiley-regexp-alist)
203 smiley-regexp-alist))
204 entry regexp beg group file)
205 (goto-char (or st (point-min)))
207 ;; loop through alist
208 (while (setq entry (pop alist))
209 (setq regexp (car entry)
213 (while (re-search-forward regexp nd t)
214 (let* ((start (match-beginning group))
215 (end (match-end group))
216 (glyph (smiley-create-glyph (buffer-substring start end)
219 (mapcar 'delete-annotation (annotations-at end))
220 (let ((ext (make-extent start end))
221 (ant (make-annotation glyph end 'text)))
222 ;; set text extent params
223 (set-extent-property ext 'end-open t)
224 (set-extent-property ext 'start-open t)
225 (set-extent-property ext 'invisible t)
226 (set-extent-property ext 'keymap smiley-map)
227 (set-extent-property ext 'mouse-face smiley-mouse-face)
228 (set-extent-property ext 'intangible t)
229 ;; set annotation params
230 (set-extent-property ant 'mouse-face smiley-mouse-face)
231 (set-extent-property ant 'keymap smiley-map)
232 ;; remember each other
233 (set-extent-property ant 'smiley-extent ext)
234 (set-extent-property ext 'smiley-annotation ant))
235 (when (smiley-end-paren-p start end)
236 (make-annotation ")" end 'text))
237 (goto-char end)))))))))
239 (defun smiley-end-paren-p (start end)
240 "Try to guess whether the current smiley is an end-paren smiley."
243 (when (and (re-search-backward "[()]" nil t)
244 (= (following-char) ?\()
246 (or (not (re-search-forward "[()]" nil t))
247 (= (char-after (1- (point))) ?\()))
251 (defun gnus-smiley-display ()
254 (set-buffer gnus-article-buffer)
255 (goto-char (point-min))
256 ;; We skip the headers.
257 (unless (search-forward "\n\n" nil t)
258 (goto-char (point-max)))
259 (smiley-buffer (current-buffer) (point))))
263 ;;; smiley.el ends here