*** empty log message ***
[gnus] / lisp / smiley.el
1 ;;; smiley.el --- displaying smiley faces
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4 ;; Author: Wes Hardaker <hardaker@ece.ucdavis.edu>
5 ;; Keywords: fun
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;;
27 ;; comments go here.
28 ;;
29
30 ;;; Test smileys:  :-] :-o :-) ;-) :-\ :-| :-d :-P 8-| :-(
31
32 ;; To use:
33 ;; (require 'smiley)
34 ;; (add-hook 'gnus-article-display-hook 'gnus-smiley-display t)
35
36 ;; The smilies were drawn by Joe Reiss <joe@jreiss.async.vt.edu>. 
37
38 (require 'annotations)
39 (require 'messagexmas)
40 (require 'cl)
41
42 (defvar smiley-data-directory (message-xmas-find-glyph-directory "smilies")
43   "Location of the smiley faces files.")
44
45 ;; Notice the subtle differences in the regular expressions in the two alists below
46
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.")
64
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")
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     ("\\(][:8B]-[)>]\\)\\W" 1 "FaceDevilish.xpm")
82     ("\\([:|]-+P\\)\\W" 1 "FaceYukky.xpm"))
83   "Smileys with noses.  These get less false matches.")
84
85 (defvar smiley-regexp-alist smiley-deformed-regexp-alist
86   "A list of regexps to map smilies to real images.
87 Defaults to the content of smiley-deformed-regexp-alist.
88 An alternative smiley-nosey-regexp-alist that
89 matches less aggressively is available.
90 If this is a symbol, take its value.")
91
92 (defvar smiley-flesh-color "yellow"
93   "Flesh color.")
94
95 (defvar smiley-features-color "black"
96   "Features color.")
97
98 (defvar smiley-tongue-color "red"
99   "Tongue color.")
100
101 (defvar smiley-circle-color "black"
102   "Circle color.")
103
104 (defvar smiley-glyph-cache nil)
105 (defvar smiley-running-xemacs (string-match "XEmacs" emacs-version))
106
107 (defvar smiley-map (make-sparse-keymap "smiley-keys")
108  "keymap to toggle smiley states")
109
110 (define-key smiley-map [(button2)] 'smiley-toggle-extent)
111
112 (defun smiley-create-glyph (smiley pixmap)
113   (and
114    smiley-running-xemacs
115    (or
116     (cdr-safe (assoc pixmap smiley-glyph-cache))
117     (let* ((xpm-color-symbols 
118             (and (featurep 'xpm)
119                  (append `(("flesh" ,smiley-flesh-color)
120                            ("features" ,smiley-features-color)
121                            ("tongue" ,smiley-tongue-color))
122                          xpm-color-symbols)))
123            (glyph (make-glyph
124                    (list
125                     (cons 'x (expand-file-name pixmap smiley-data-directory))
126                     (cons 'tty smiley)))))
127       (setq smiley-glyph-cache (cons (cons pixmap glyph) smiley-glyph-cache))
128       (set-glyph-face glyph 'default)
129       glyph))))
130
131 ;;;###autoload
132 (defun smiley-region (beg end)
133   "Smilify the region between point and mark."
134   (interactive "r")
135   (smiley-buffer (current-buffer) beg end))
136
137 (defun smiley-toggle-extent (event)
138   "Toggle smiley at given point"
139   (interactive "e")
140   (let* ((ant (event-glyph-extent event))
141          (pt (event-closest-point event))
142          ext)
143     (if (annotationp ant)
144         (when (extentp (setq ext (extent-property ant 'smiley-extent)))
145           (set-extent-property ext 'invisible nil)
146           (hide-annotation ant))
147       (when pt
148         (while (setq ext (extent-at pt (event-buffer event) nil ext 'at))
149           (when (annotationp (setq ant 
150                                    (extent-property ext 'smiley-annotation)))
151             (reveal-annotation ant)
152             (set-extent-property ext 'invisible t)))))))
153
154 ;;;###autoload
155 (defun smiley-buffer (&optional buffer st nd)
156   (interactive)
157   (when (featurep 'x)
158     (save-excursion
159       (when buffer
160         (set-buffer buffer))
161       (let ((buffer-read-only nil)
162             (alist (if (symbolp smiley-regexp-alist)
163                        (symbol-value smiley-regexp-alist)
164                      smiley-regexp-alist))
165             entry regexp beg group file)
166         (goto-char (or st (point-min)))
167         (setq beg (point))
168         ;; loop through alist
169         (while (setq entry (pop alist))
170           (setq regexp (car entry)
171                 group (cadr entry)
172                 file (caddr entry))
173           (goto-char beg)
174           (while (re-search-forward regexp nd t)
175             (let* ((start (match-beginning group))
176                    (end (match-end group))
177                    (glyph (smiley-create-glyph (buffer-substring start end)
178                                                file)))
179               (when glyph
180                 (mapcar 'delete-annotation (annotations-at end))
181                 (let ((ext (make-extent start end))
182                       (ant (make-annotation glyph end 'text)))
183                   ;; set text extent params
184                   (set-extent-property ext 'end-open t)
185                   (set-extent-property ext 'start-open t)
186                   (set-extent-property ext 'invisible t)
187                   (set-extent-property ext 'keymap smiley-map)
188                   (set-extent-property ext 'mouse-face gnus-article-mouse-face)
189                   (set-extent-property ext 'intangible t)
190                   ;; set annotation params
191                   (set-extent-property ant 'mouse-face gnus-article-mouse-face)
192                   (set-extent-property ant 'keymap smiley-map)
193                   ;; remember each other
194                   (set-extent-property ant 'smiley-extent ext)
195                   (set-extent-property ext 'smiley-annotation ant))
196                 (when (smiley-end-paren-p start end)
197                   (make-annotation ")" end 'text))
198                 (goto-char end)))))))))
199
200 (defun smiley-end-paren-p (start end)
201   "Try to guess whether the current smiley is an end-paren smiley."
202   (save-excursion
203     (goto-char start)
204     (when (and (re-search-backward "[()]" nil t)
205                (= (following-char) ?\()
206                (goto-char end)
207                (or (not (re-search-forward "[()]" nil t))
208                    (= (char-after (1- (point))) ?\()))
209       t)))
210
211 ;;;###autoload    
212 (defun gnus-smiley-display ()
213   (interactive)
214   (save-excursion
215     (set-buffer gnus-article-buffer)
216     (goto-char (point-min))
217     ;; We skip the headers.
218     (unless (search-forward "\n\n" nil t)
219       (goto-char (point-max)))
220     (smiley-buffer (current-buffer) (point))))
221
222 (provide 'smiley)
223
224 ;;; smiley.el ends here