1 ;;; smiley.el --- displaying smiley faces
2 ;; Copyright (C) 1996 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 <joe@jreiss.async.vt.edu>.
38 (require 'annotations)
39 (require 'messagexmas)
42 (defvar smiley-data-directory (message-xmas-find-glyph-directory "smilies")
43 "Location of the smiley faces files.")
45 ;; Notice the subtle differences in the regular expessions in the two alists below
47 (defvar smiley-deformed-regexp-alist
48 '(("\\(:-*[<«]+\\)\\W" 1 "FaceAngry.xpm")
49 ("\\(:-+\\]+\\)\\W" 1 "FaceGoofy.xpm")
50 ("\\(:-*D\\)\\W" 1 "FaceGrinning.xpm")
51 ("\\(:-*[)>}»]+\\)\\W" 1 "FaceHappy.xpm")
52 ("\\(:-*[/\\\"]\\)[^/]" 1 "FaceIronic.xpm")
53 ("\\([8|]-*[|Oo%]\\)\\W" 1 "FaceKOed.xpm")
54 ("\\([:|]-*#+\\)\\W" 1 "FaceNyah.xpm")
55 ("\\(:-*[({]+\\)\\W" 1 "FaceSad.xpm")
56 ("\\(:-*[Oo\*]\\)\\W" 1 "FaceStartled.xpm")
57 ("\\(:-*|\\)\\W" 1 "FaceStraight.xpm")
58 ("\\(:-*p\\)\\W" 1 "FaceTalking.xpm")
59 ("\\(:-*d\\)\\W" 1 "FaceTasty.xpm")
60 ("\\(;-*[>)}»]+\\)\\W" 1 "FaceWinking.xpm")
61 ("\\(:-*[Vvµ]\\)\\W" 1 "FaceWry.xpm")
62 ("\\([:|]-*P\\)\\W" 1 "FaceYukky.xpm"))
63 "Normal and deformed faces for smilies.")
65 (defvar smiley-nosey-regexp-alist
66 '(("\\(:-+[<«]+\\)\\W" 1 "FaceAngry.xpm")
67 ("\\(:-+\\]+\\)\\W" 1 "FaceGoofy.xpm")
68 ("\\(:-+D\\)\\W" 1 "FaceGrinning.xpm")
69 ("\\(:-+[}»]+\\)\\W" 1 "FaceHappy.xpm")
70 ("\\(:-*)+\\)\\W" 1 "FaceHappy.xpm") ;; The exception that confirms the rule
71 ("\\(:-+[/\\\"]+\\)\\W" 1 "FaceIronic.xpm")
72 ("\\([8|]-+[|Oo%]\\)\\W" 1 "FaceKOed.xpm")
73 ("\\([:|]-+#+\\)\\W" 1 "FaceNyah.xpm")
74 ("\\(:-+[({]+\\)\\W" 1 "FaceSad.xpm")
75 ("\\(:-+[Oo\*]\\)\\W" 1 "FaceStartled.xpm")
76 ("\\(:-+|\\)\\W" 1 "FaceStraight.xpm")
77 ("\\(:-+p\\)\\W" 1 "FaceTalking.xpm")
78 ("\\(:-+d\\)\\W" 1 "FaceTasty.xpm")
79 ("\\(;-+[>)}»]+\\)\\W" 1 "FaceWinking.xpm")
80 ("\\(:-+[Vvµ]\\)\\W" 1 "FaceWry.xpm")
81 ("\\([:|]-+P\\)\\W" 1 "FaceYukky.xpm"))
82 "Smileys with noses. These get less false matches.")
84 (defvar smiley-regexp-alist smiley-deformed-regexp-alist
85 "A list of regexps to map smilies to real images.
86 Defaults to the content of smiley-deformed-regexp-alist.
87 An alternative smiley-nose-regexp-alist that
88 matches less aggresively is available.
89 If this is a symbol, take its value.")
91 (defvar smiley-flesh-color "yellow"
94 (defvar smiley-features-color "black"
97 (defvar smiley-tongue-color "red"
100 (defvar smiley-circle-color "black"
103 (defvar smiley-glyph-cache nil)
104 (defvar smiley-running-xemacs (string-match "XEmacs" emacs-version))
106 (defvar smiley-map (make-sparse-keymap "smiley-keys")
107 "keymap to toggle smiley states")
109 (define-key smiley-map [(button2)] 'smiley-toggle-extent)
111 (defun smiley-create-glyph (smiley pixmap)
113 smiley-running-xemacs
115 (cdr-safe (assoc pixmap smiley-glyph-cache))
116 (let* ((xpm-color-symbols
118 (append `(("flesh" ,smiley-flesh-color)
119 ("features" ,smiley-features-color)
120 ("tongue" ,smiley-tongue-color))
124 (cons 'x (expand-file-name pixmap smiley-data-directory))
125 (cons 'tty smiley)))))
126 (setq smiley-glyph-cache (cons (cons pixmap glyph) smiley-glyph-cache))
127 (set-glyph-face glyph 'default)
131 (defun smiley-region (beg end)
132 "Smilify the region between point and mark."
134 (smiley-buffer (current-buffer) beg end))
136 (defun smiley-toggle-extent (event)
137 "Toggle smiley at given point"
139 (let* ((ant (event-glyph-extent event))
140 (pt (event-closest-point event))
142 (if (annotationp ant)
143 (when (extentp (setq ext (extent-property ant 'smiley-extent)))
144 (set-extent-property ext 'invisible nil)
145 (hide-annotation ant))
147 (while (setq ext (extent-at pt (event-buffer event) nil ext 'at))
148 (when (annotationp (setq ant
149 (extent-property ext 'smiley-annotation)))
150 (reveal-annotation ant)
151 (set-extent-property ext 'invisible t)))))))
154 (defun smiley-buffer (&optional buffer st nd)
160 (let ((buffer-read-only nil)
161 (alist (if (symbolp smiley-regexp-alist)
162 (symbol-value smiley-regexp-alist)
163 smiley-regexp-alist))
164 entry regexp beg group file)
165 (goto-char (or st (point-min)))
167 ;; loop through alist
168 (while (setq entry (pop alist))
169 (setq regexp (car entry)
173 (while (re-search-forward regexp nd t)
174 (let* ((start (match-beginning group))
175 (end (match-end group))
176 (glyph (smiley-create-glyph (buffer-substring start end)
179 (mapcar 'delete-annotation (annotations-at end))
180 (let ((ext (make-extent start end))
181 (ant (make-annotation glyph end 'text)))
182 ;; set text extent params
183 (set-extent-property ext 'end-open t)
184 (set-extent-property ext 'start-open t)
185 (set-extent-property ext 'invisible t)
186 (set-extent-property ext 'keymap smiley-map)
187 (set-extent-property ext 'mouse-face gnus-article-mouse-face)
188 (set-extent-property ext 'intangible t)
189 ;; set annotation params
190 (set-extent-property ant 'mouse-face gnus-article-mouse-face)
191 (set-extent-property ant 'keymap smiley-map)
192 ;; remember each other
193 (set-extent-property ant 'smiley-extent ext)
194 (set-extent-property ext 'smiley-annotation ant))
195 (when (smiley-end-paren-p start end)
196 (make-annotation ")" end 'text))
197 (goto-char end)))))))))
199 (defun smiley-end-paren-p (start end)
200 "Try to guess whether the current smiley is an end-paren smiley."
203 (when (and (re-search-backward "[()]" nil t)
204 (= (following-char) ?\()
206 (or (not (re-search-forward "[()]" nil t))
207 (= (char-after (1- (point))) ?\()))
211 (defun gnus-smiley-display ()
214 (set-buffer gnus-article-buffer)
215 (goto-char (point-min))
216 ;; We skip the headers.
217 (unless (search-forward "\n\n" nil t)
218 (goto-char (point-max)))
219 (smiley-buffer (current-buffer) (point))))
223 ;;; smiley.el ends here