*** 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 (eval-when-compile (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 expessions 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") ;; 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.")
83
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.")
90
91 (defvar smiley-flesh-color "yellow"
92   "Flesh color.")
93
94 (defvar smiley-features-color "black"
95   "Features color.")
96
97 (defvar smiley-tongue-color "red"
98   "Tongue color.")
99
100 (defvar smiley-circle-color "black"
101   "Circle color.")
102
103 (defvar smiley-glyph-cache nil)
104 (defvar smiley-running-xemacs (string-match "XEmacs" emacs-version))
105
106 (defvar smiley-map (make-sparse-keymap "smiley-keys")
107  "keymap to toggle smiley states")
108
109 (define-key smiley-map [(button2)] 'smiley-toggle-extent)
110
111 (defun smiley-create-glyph (smiley pixmap)
112   (and
113    smiley-running-xemacs
114    (or
115     (cdr-safe (assoc pixmap smiley-glyph-cache))
116     (let* ((xpm-color-symbols 
117             (and (featurep 'xpm)
118                  (append `(("flesh" ,smiley-flesh-color)
119                            ("features" ,smiley-features-color)
120                            ("tongue" ,smiley-tongue-color))
121                          xpm-color-symbols)))
122            (glyph (make-glyph
123                    (list
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)
128       glyph))))
129
130 ;;;###autoload
131 (defun smiley-region (beg end)
132   "Smilify the region between point and mark."
133   (interactive "r")
134   (smiley-buffer (current-buffer) beg end))
135
136 (defun smiley-toggle-extent (event)
137   "Toggle smiley at given point"
138   (interactive "e")
139   (let* ((ant (event-glyph-extent event))
140          (pt (event-closest-point event))
141          ext)
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))
146       (if pt
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)))))))
152
153 ;;;###autoload
154 (defun smiley-buffer (&optional buffer st nd)
155   (interactive)
156   (when (featurep 'x)
157     (save-excursion
158       (when buffer
159         (set-buffer buffer))
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)))
166         (setq beg (point))
167         ;; loop through alist
168         (while (setq entry (pop alist))
169           (setq regexp (car entry)
170                 group (cadr entry)
171                 file (caddr entry))
172           (goto-char beg)
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)
177                                                file)))
178               (when glyph
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 'invisible t)
184                   (set-extent-property ext 'end-open t)
185                   (set-extent-property ext 'keymap smiley-map)
186                   (set-extent-property ext 'mouse-face gnus-article-mouse-face)
187 ;                 (set-extent-property ext 'intangible t)
188                   ;; set annotation params
189                   (set-extent-property ant 'mouse-face gnus-article-mouse-face)
190                   (set-extent-property ant 'keymap smiley-map)
191                   ;; remember each other
192                   (set-extent-property ant 'smiley-extent ext)
193                   (set-extent-property ext 'smiley-annotation ant))
194                 (when (smiley-end-paren-p start end)
195                   (make-annotation ")" end 'text))
196                 (goto-char end)))))))))
197
198 (defun smiley-end-paren-p (start end)
199   "Try to guess whether the current smiley is an end-paren smiley."
200   (save-excursion
201     (goto-char start)
202     (when (and (re-search-backward "[()]" nil t)
203                (= (following-char) ?\()
204                (goto-char end)
205                (or (not (re-search-forward "[()]" nil t))
206                    (= (char-after (1- (point))) ?\()))
207       t)))
208
209 ;;;###autoload    
210 (defun gnus-smiley-display ()
211   (interactive)
212   (save-excursion
213     (set-buffer gnus-article-buffer)
214     (goto-char (point-min))
215     ;; We skip the headers.
216     (unless (search-forward "\n\n" nil t)
217       (goto-char (point-max)))
218     (smiley-buffer (current-buffer) (point))))
219
220 (provide 'smiley)
221
222 ;;; smiley.el ends here