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 "WideFaceAse3.xbm")
57 ("\\(\\^_?\\^;;\\)\\W" 1 "WideFaceAse2.xbm")
58 ("\\(\\^_?\\^;\\)\\W" 1 "WideFaceAse1.xbm")
59 ("\\(\\^_?\\^\\)\\W" 1 "WideFaceSmile.xbm")
60 ("\\(\\;_;\\)\\W" 1 "WideFaceWeep.xbm")
61 ("\\(\\T_T\\)\\W" 1 "WideFaceWeep.xbm")
62 ("\\(:-*[<«]+\\)\\W" 1 "FaceAngry.xpm")
63 ("\\(:-+\\]+\\)\\W" 1 "FaceGoofy.xpm")
64 ("\\(:-*D\\)\\W" 1 "FaceGrinning.xpm")
65 ("\\(:-*[)>}»]+\\)\\W" 1 "FaceHappy.xpm")
66 ("\\(:-*[/\\\"]\\)[^/]\\W" 1 "FaceIronic.xpm")
67 ("\\([8|]-*[|Oo%]\\)\\W" 1 "FaceKOed.xpm")
68 ("\\([:|]-*#+\\)\\W" 1 "FaceNyah.xpm")
69 ("\\(:-*[({]+\\)\\W" 1 "FaceSad.xpm")
70 ("\\(:-*[Oo\*]\\)\\W" 1 "FaceStartled.xpm")
71 ("\\(:-*|\\)\\W" 1 "FaceStraight.xpm")
72 ("\\(:-*p\\)\\W" 1 "FaceTalking.xpm")
73 ("\\(:-*d\\)\\W" 1 "FaceTasty.xpm")
74 ("\\(;-*[>)}»]+\\)\\W" 1 "FaceWinking.xpm")
75 ("\\(:-*[Vvµ]\\)\\W" 1 "FaceWry.xpm")
76 ("\\([:|]-*P\\)\\W" 1 "FaceYukky.xpm"))
77 "Normal and deformed faces for smilies."
78 :type '(repeat (list regexp
79 (integer :tag "Match")
80 (string :tag "Image")))
83 (defcustom smiley-nosey-regexp-alist
84 '(("\\(:-+[<«]+\\)\\W" 1 "FaceAngry.xpm")
85 ("\\(:-+\\]+\\)\\W" 1 "FaceGoofy.xpm")
86 ("\\(:-+D\\)\\W" 1 "FaceGrinning.xpm")
87 ("\\(:-+[}»]+\\)\\W" 1 "FaceHappy.xpm")
88 ("\\(:-*)+\\)\\W" 1 "FaceHappy.xpm")
89 ("\\(:-+[/\\\"]+\\)\\W" 1 "FaceIronic.xpm")
90 ("\\([8|]-+[|Oo%]\\)\\W" 1 "FaceKOed.xpm")
91 ("\\([:|]-+#+\\)\\W" 1 "FaceNyah.xpm")
92 ("\\(:-+[({]+\\)\\W" 1 "FaceSad.xpm")
93 ("\\(:-+[Oo\*]\\)\\W" 1 "FaceStartled.xpm")
94 ("\\(:-+|\\)\\W" 1 "FaceStraight.xpm")
95 ("\\(:-+p\\)\\W" 1 "FaceTalking.xpm")
96 ("\\(:-+d\\)\\W" 1 "FaceTasty.xpm")
97 ("\\(;-+[>)}»]+\\)\\W" 1 "FaceWinking.xpm")
98 ("\\(:-+[Vvµ]\\)\\W" 1 "FaceWry.xpm")
99 ("\\(][:8B]-[)>]\\)\\W" 1 "FaceDevilish.xpm")
100 ("\\([:|]-+P\\)\\W" 1 "FaceYukky.xpm"))
101 "Smileys with noses. These get less false matches."
102 :type '(repeat (list regexp
103 (integer :tag "Match")
104 (string :tag "Image")))
107 (defcustom smiley-regexp-alist smiley-deformed-regexp-alist
108 "A list of regexps to map smilies to real images.
109 Defaults to the contents of `smiley-deformed-regexp-alist'.
110 An alternative is `smiley-nosey-regexp-alist' that matches less
112 If this is a symbol, take its value."
113 :type '(radio (variable-item smiley-deformed-regexp-alist)
114 (variable-item smiley-nosey-regexp-alist)
117 (integer :tag "Match")
118 (string :tag "Image"))))
121 (defcustom smiley-flesh-color "yellow"
126 (defcustom smiley-features-color "black"
131 (defcustom smiley-tongue-color "red"
136 (defcustom smiley-circle-color "black"
141 (defcustom smiley-mouse-face 'highlight
142 "Face used for mouse highlighting in the smiley buffer.
144 Smiley buttons will be displayed in this face when the cursor is
150 (defvar smiley-glyph-cache nil)
151 (defvar smiley-running-xemacs (string-match "XEmacs" emacs-version))
153 (defvar smiley-map (make-sparse-keymap "smiley-keys")
154 "Keymap to toggle smiley states.")
156 (define-key smiley-map [(button2)] 'smiley-toggle-extent)
158 (defun smiley-create-glyph (smiley pixmap)
160 smiley-running-xemacs
162 (cdr-safe (assoc pixmap smiley-glyph-cache))
163 (let* ((xpm-color-symbols
165 (append `(("flesh" ,smiley-flesh-color)
166 ("features" ,smiley-features-color)
167 ("tongue" ,smiley-tongue-color))
171 (cons 'x (expand-file-name pixmap smiley-data-directory))
172 (cons 'tty smiley)))))
173 (setq smiley-glyph-cache (cons (cons pixmap glyph) smiley-glyph-cache))
174 (set-glyph-face glyph 'default)
178 (defun smiley-region (beg end)
179 "Smilify the region between point and mark."
181 (smiley-buffer (current-buffer) beg end))
183 (defun smiley-toggle-extent (event)
184 "Toggle smiley at given point"
186 (let* ((ant (event-glyph-extent event))
187 (pt (event-closest-point event))
189 (if (annotationp ant)
190 (when (extentp (setq ext (extent-property ant 'smiley-extent)))
191 (set-extent-property ext 'invisible nil)
192 (hide-annotation ant))
194 (while (setq ext (extent-at pt (event-buffer event) nil ext 'at))
195 (when (annotationp (setq ant
196 (extent-property ext 'smiley-annotation)))
197 (reveal-annotation ant)
198 (set-extent-property ext 'invisible t)))))))
201 (defun smiley-buffer (&optional buffer st nd)
207 (let ((buffer-read-only nil)
208 (alist (if (symbolp smiley-regexp-alist)
209 (symbol-value smiley-regexp-alist)
210 smiley-regexp-alist))
211 entry regexp beg group file)
212 (goto-char (or st (point-min)))
214 ;; loop through alist
215 (while (setq entry (pop alist))
216 (setq regexp (car entry)
220 (while (re-search-forward regexp nd t)
221 (let* ((start (match-beginning group))
222 (end (match-end group))
223 (glyph (smiley-create-glyph (buffer-substring start end)
226 (mapcar 'delete-annotation (annotations-at end))
227 (let ((ext (make-extent start end))
228 (ant (make-annotation glyph end 'text)))
229 ;; set text extent params
230 (set-extent-property ext 'end-open t)
231 (set-extent-property ext 'start-open t)
232 (set-extent-property ext 'invisible t)
233 (set-extent-property ext 'keymap smiley-map)
234 (set-extent-property ext 'mouse-face smiley-mouse-face)
235 (set-extent-property ext 'intangible t)
236 ;; set annotation params
237 (set-extent-property ant 'mouse-face smiley-mouse-face)
238 (set-extent-property ant 'keymap smiley-map)
239 ;; remember each other
240 (set-extent-property ant 'smiley-extent ext)
241 (set-extent-property ext 'smiley-annotation ant))
242 (when (smiley-end-paren-p start end)
243 (make-annotation ")" end 'text))
244 (goto-char end)))))))))
246 (defun smiley-end-paren-p (start end)
247 "Try to guess whether the current smiley is an end-paren smiley."
250 (when (and (re-search-backward "[()]" nil t)
251 (= (following-char) ?\()
253 (or (not (re-search-forward "[()]" nil t))
254 (= (char-after (1- (point))) ?\()))
257 (defvar gnus-article-buffer)
259 (defun gnus-smiley-display ()
260 "Display \"smileys\" as small graphical icons."
263 (set-buffer gnus-article-buffer)
264 (goto-char (point-min))
265 ;; We skip the headers.
266 (unless (search-forward "\n\n" nil t)
267 (goto-char (point-max)))
268 (smiley-buffer (current-buffer) (point))))
272 ;;; smiley.el ends here