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 "FaceHappy.xpm")
67 ("\\(:-*[/\\\"]\\)[^/]\\W" 1 "FaceIronic.xpm")
68 ("\\([8|]-*[|Oo%]\\)\\W" 1 "FaceKOed.xpm")
69 ("\\([:|]-*#+\\)\\W" 1 "FaceNyah.xpm")
70 ("\\(:-*[({]+\\)\\W" 1 "FaceSad.xpm")
71 ("\\(=[({]+\\)\\W" 1 "FaceSad.xpm")
72 ("\\(:-*[Oo\*]\\)\\W" 1 "FaceStartled.xpm")
73 ("\\(:-*|\\)\\W" 1 "FaceStraight.xpm")
74 ("\\(:-*p\\)\\W" 1 "FaceTalking.xpm")
75 ("\\(:-*d\\)\\W" 1 "FaceTasty.xpm")
76 ("\\(;-*[>)}»]+\\)\\W" 1 "FaceWinking.xpm")
77 ("\\(:-*[Vvµ]\\)\\W" 1 "FaceWry.xpm")
78 ("\\([:|]-*P\\)\\W" 1 "FaceYukky.xpm"))
79 "Normal and deformed faces for smilies."
80 :type '(repeat (list regexp
81 (integer :tag "Match")
82 (string :tag "Image")))
85 (defcustom smiley-nosey-regexp-alist
86 '(("\\(:-+[<«]+\\)\\W" 1 "FaceAngry.xpm")
87 ("\\(:-+\\]+\\)\\W" 1 "FaceGoofy.xpm")
88 ("\\(:-+D\\)\\W" 1 "FaceGrinning.xpm")
89 ("\\(:-+[}»]+\\)\\W" 1 "FaceHappy.xpm")
90 ("\\(:-*)+\\)\\W" 1 "FaceHappy.xpm")
91 ("\\(=[)>]+\\)\\W" 1 "FaceHappy.xpm")
92 ("\\(:-+[/\\\"]+\\)\\W" 1 "FaceIronic.xpm")
93 ("\\([8|]-+[|Oo%]\\)\\W" 1 "FaceKOed.xpm")
94 ("\\([:|]-+#+\\)\\W" 1 "FaceNyah.xpm")
95 ("\\(:-+[({]+\\)\\W" 1 "FaceSad.xpm")
96 ("\\(=[({]+\\)\\W" 1 "FaceSad.xpm")
97 ("\\(:-+[Oo\*]\\)\\W" 1 "FaceStartled.xpm")
98 ("\\(:-+|\\)\\W" 1 "FaceStraight.xpm")
99 ("\\(:-+p\\)\\W" 1 "FaceTalking.xpm")
100 ("\\(:-+d\\)\\W" 1 "FaceTasty.xpm")
101 ("\\(;-+[>)}»]+\\)\\W" 1 "FaceWinking.xpm")
102 ("\\(:-+[Vvµ]\\)\\W" 1 "FaceWry.xpm")
103 ("\\(][:8B]-[)>]\\)\\W" 1 "FaceDevilish.xpm")
104 ("\\([:|]-+P\\)\\W" 1 "FaceYukky.xpm"))
105 "Smileys with noses. These get less false matches."
106 :type '(repeat (list regexp
107 (integer :tag "Match")
108 (string :tag "Image")))
111 (defcustom smiley-regexp-alist smiley-deformed-regexp-alist
112 "A list of regexps to map smilies to real images.
113 Defaults to the contents of `smiley-deformed-regexp-alist'.
114 An alternative is `smiley-nosey-regexp-alist' that matches less
116 If this is a symbol, take its value."
117 :type '(radio (variable-item smiley-deformed-regexp-alist)
118 (variable-item smiley-nosey-regexp-alist)
121 (integer :tag "Match")
122 (string :tag "Image"))))
125 (defcustom smiley-flesh-color "yellow"
130 (defcustom smiley-features-color "black"
135 (defcustom smiley-tongue-color "red"
140 (defcustom smiley-circle-color "black"
145 (defcustom smiley-mouse-face 'highlight
146 "Face used for mouse highlighting in the smiley buffer.
148 Smiley buttons will be displayed in this face when the cursor is
154 (defvar smiley-glyph-cache nil)
155 (defvar smiley-running-xemacs (string-match "XEmacs" emacs-version))
157 (defvar smiley-map (make-sparse-keymap "smiley-keys")
158 "Keymap to toggle smiley states.")
160 (define-key smiley-map [(button2)] 'smiley-toggle-extent)
162 (defun smiley-create-glyph (smiley pixmap)
164 smiley-running-xemacs
166 (cdr-safe (assoc pixmap smiley-glyph-cache))
167 (let* ((xpm-color-symbols
169 (append `(("flesh" ,smiley-flesh-color)
170 ("features" ,smiley-features-color)
171 ("tongue" ,smiley-tongue-color))
175 (cons 'x (expand-file-name pixmap smiley-data-directory))
176 (cons 'tty smiley)))))
177 (setq smiley-glyph-cache (cons (cons pixmap glyph) smiley-glyph-cache))
178 (set-glyph-face glyph 'default)
182 (defun smiley-region (beg end)
183 "Smilify the region between point and mark."
185 (smiley-buffer (current-buffer) beg end))
187 (defun smiley-toggle-extent (event)
188 "Toggle smiley at given point"
190 (let* ((ant (event-glyph-extent event))
191 (pt (event-closest-point event))
193 (if (annotationp ant)
194 (when (extentp (setq ext (extent-property ant 'smiley-extent)))
195 (set-extent-property ext 'invisible nil)
196 (hide-annotation ant))
198 (while (setq ext (extent-at pt (event-buffer event) nil ext 'at))
199 (when (annotationp (setq ant
200 (extent-property ext 'smiley-annotation)))
201 (reveal-annotation ant)
202 (set-extent-property ext 'invisible t)))))))
205 (defun smiley-buffer (&optional buffer st nd)
211 (let ((buffer-read-only nil)
212 (alist (if (symbolp smiley-regexp-alist)
213 (symbol-value smiley-regexp-alist)
214 smiley-regexp-alist))
215 (case-fold-search nil)
216 entry regexp beg group file)
217 (goto-char (or st (point-min)))
219 ;; loop through alist
220 (while (setq entry (pop alist))
221 (setq regexp (car entry)
225 (while (re-search-forward regexp nd t)
226 (let* ((start (match-beginning group))
227 (end (match-end group))
228 (glyph (smiley-create-glyph (buffer-substring start end)
231 (mapcar 'delete-annotation (annotations-at end))
232 (let ((ext (make-extent start end))
233 (ant (make-annotation glyph end 'text)))
234 ;; set text extent params
235 (set-extent-property ext 'end-open t)
236 (set-extent-property ext 'start-open t)
237 (set-extent-property ext 'invisible t)
238 (set-extent-property ext 'keymap smiley-map)
239 (set-extent-property ext 'mouse-face smiley-mouse-face)
240 (set-extent-property ext 'intangible t)
241 ;; set annotation params
242 (set-extent-property ant 'mouse-face smiley-mouse-face)
243 (set-extent-property ant 'keymap smiley-map)
244 ;; remember each other
245 (set-extent-property ant 'smiley-extent ext)
246 (set-extent-property ext 'smiley-annotation ant))
247 (when (smiley-end-paren-p start end)
248 (make-annotation ")" end 'text))
249 (goto-char end)))))))))
251 (defun smiley-end-paren-p (start end)
252 "Try to guess whether the current smiley is an end-paren smiley."
255 (when (and (re-search-backward "[()]" nil t)
256 (= (following-char) ?\()
258 (or (not (re-search-forward "[()]" nil t))
259 (= (char-after (1- (point))) ?\()))
262 (defvar gnus-article-buffer)
264 (defun gnus-smiley-display ()
265 "Display \"smileys\" as small graphical icons."
268 (set-buffer gnus-article-buffer)
269 (goto-char (point-min))
270 ;; We skip the headers.
271 (unless (search-forward "\n\n" nil t)
272 (goto-char (point-max)))
273 (smiley-buffer (current-buffer) (point))))
277 ;;; smiley.el ends here