*** empty log message ***
[gnus] / lisp / smiley.el
1 ;;; smiley.el --- displaying smiley faces
2 ;; Copyright (C) 1996,97 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 <jreiss@vt.edu>. 
37
38 (require 'annotations)
39 (require 'messagexmas)
40 (require 'cl)
41 (require 'custom)
42
43 (defgroup smiley nil
44   "Turn :-)'s into real images (XEmacs)."
45   :group 'gnus-visual)
46
47 (defcustom smiley-data-directory (message-xmas-find-glyph-directory "smilies")
48   "Location of the smiley faces files."
49   :type 'directory
50   :group 'smiley)
51
52 ;; Notice the subtle differences in the regular expressions in the two alists below
53
54 (defcustom smiley-deformed-regexp-alist
55   '(("\\(:-*[<«]+\\)\\W" 1 "FaceAngry.xpm")
56     ("\\(:-+\\]+\\)\\W" 1 "FaceGoofy.xpm")
57     ("\\(:-*D\\)\\W" 1 "FaceGrinning.xpm")
58     ("\\(:-*[)>}»]+\\)\\W" 1 "FaceHappy.xpm")
59     ("\\(:-*[/\\\"]\\)[^/]" 1 "FaceIronic.xpm")
60     ("\\([8|]-*[|Oo%]\\)\\W" 1 "FaceKOed.xpm")
61     ("\\([:|]-*#+\\)\\W" 1 "FaceNyah.xpm")
62     ("\\(:-*[({]+\\)\\W" 1 "FaceSad.xpm")
63     ("\\(:-*[Oo\*]\\)\\W" 1 "FaceStartled.xpm")
64     ("\\(:-*|\\)\\W" 1 "FaceStraight.xpm")
65     ("\\(:-*p\\)\\W" 1 "FaceTalking.xpm")
66     ("\\(:-*d\\)\\W" 1 "FaceTasty.xpm")
67     ("\\(;-*[>)}»]+\\)\\W" 1 "FaceWinking.xpm")
68     ("\\(:-*[Vvµ]\\)\\W" 1 "FaceWry.xpm")  
69     ("\\([:|]-*P\\)\\W" 1 "FaceYukky.xpm"))
70   "Normal and deformed faces for smilies."
71   :type '(repeat (list regexp 
72                        (integer :tag "Match")
73                        (string :tag "Image")))
74   :group 'smiley)
75
76 (defcustom smiley-nosey-regexp-alist
77   '(("\\(:-+[<«]+\\)\\W" 1 "FaceAngry.xpm")
78     ("\\(:-+\\]+\\)\\W" 1 "FaceGoofy.xpm")
79     ("\\(:-+D\\)\\W" 1 "FaceGrinning.xpm")
80     ("\\(:-+[}»]+\\)\\W" 1 "FaceHappy.xpm")
81     ("\\(:-*)+\\)\\W" 1 "FaceHappy.xpm")
82     ("\\(:-+[/\\\"]+\\)\\W" 1 "FaceIronic.xpm")
83     ("\\([8|]-+[|Oo%]\\)\\W" 1 "FaceKOed.xpm")
84     ("\\([:|]-+#+\\)\\W" 1 "FaceNyah.xpm")
85     ("\\(:-+[({]+\\)\\W" 1 "FaceSad.xpm")
86     ("\\(:-+[Oo\*]\\)\\W" 1 "FaceStartled.xpm")
87     ("\\(:-+|\\)\\W" 1 "FaceStraight.xpm")
88     ("\\(:-+p\\)\\W" 1 "FaceTalking.xpm")
89     ("\\(:-+d\\)\\W" 1 "FaceTasty.xpm")
90     ("\\(;-+[>)}»]+\\)\\W" 1 "FaceWinking.xpm")
91     ("\\(:-+[Vvµ]\\)\\W" 1 "FaceWry.xpm")
92     ("\\(][:8B]-[)>]\\)\\W" 1 "FaceDevilish.xpm")
93     ("\\([:|]-+P\\)\\W" 1 "FaceYukky.xpm"))
94   "Smileys with noses.  These get less false matches."
95   :type '(repeat (list regexp 
96                        (integer :tag "Match")
97                        (string :tag "Image")))
98   :group 'smiley)
99
100 (defcustom smiley-regexp-alist smiley-deformed-regexp-alist
101   "A list of regexps to map smilies to real images.
102 Defaults to the content of smiley-deformed-regexp-alist.
103 An alternative smiley-nosey-regexp-alist that
104 matches less aggressively is available.
105 If this is a symbol, take its value."
106   :type '(radio (variable-item smiley-deformed-regexp-alist)
107                 (variable-item smiley-nosey-regexp-alist)
108                 symbol 
109                 (repeat (list regexp 
110                               (integer :tag "Match")
111                               (string :tag "Image"))))
112   :group 'smiley)
113
114 (defcustom smiley-flesh-color "yellow"
115   "Flesh color."
116   :type 'string
117   :group 'smiley)
118
119 (defcustom smiley-features-color "black"
120   "Features color."
121   :type 'string
122   :group 'smiley)
123
124 (defcustom smiley-tongue-color "red"
125   "Tongue color."
126   :type 'string
127   :group 'smiley)
128
129 (defcustom smiley-circle-color "black"
130   "Circle color."
131   :type 'string
132   :group 'smiley)
133
134 (defvar smiley-glyph-cache nil)
135 (defvar smiley-running-xemacs (string-match "XEmacs" emacs-version))
136
137 (defvar smiley-map (make-sparse-keymap "smiley-keys")
138  "keymap to toggle smiley states")
139
140 (define-key smiley-map [(button2)] 'smiley-toggle-extent)
141
142 (defun smiley-create-glyph (smiley pixmap)
143   (and
144    smiley-running-xemacs
145    (or
146     (cdr-safe (assoc pixmap smiley-glyph-cache))
147     (let* ((xpm-color-symbols 
148             (and (featurep 'xpm)
149                  (append `(("flesh" ,smiley-flesh-color)
150                            ("features" ,smiley-features-color)
151                            ("tongue" ,smiley-tongue-color))
152                          xpm-color-symbols)))
153            (glyph (make-glyph
154                    (list
155                     (cons 'x (expand-file-name pixmap smiley-data-directory))
156                     (cons 'tty smiley)))))
157       (setq smiley-glyph-cache (cons (cons pixmap glyph) smiley-glyph-cache))
158       (set-glyph-face glyph 'default)
159       glyph))))
160
161 ;;;###autoload
162 (defun smiley-region (beg end)
163   "Smilify the region between point and mark."
164   (interactive "r")
165   (smiley-buffer (current-buffer) beg end))
166
167 (defun smiley-toggle-extent (event)
168   "Toggle smiley at given point"
169   (interactive "e")
170   (let* ((ant (event-glyph-extent event))
171          (pt (event-closest-point event))
172          ext)
173     (if (annotationp ant)
174         (when (extentp (setq ext (extent-property ant 'smiley-extent)))
175           (set-extent-property ext 'invisible nil)
176           (hide-annotation ant))
177       (when pt
178         (while (setq ext (extent-at pt (event-buffer event) nil ext 'at))
179           (when (annotationp (setq ant 
180                                    (extent-property ext 'smiley-annotation)))
181             (reveal-annotation ant)
182             (set-extent-property ext 'invisible t)))))))
183
184 ;;;###autoload
185 (defun smiley-buffer (&optional buffer st nd)
186   (interactive)
187   (when (featurep 'x)
188     (save-excursion
189       (when buffer
190         (set-buffer buffer))
191       (let ((buffer-read-only nil)
192             (alist (if (symbolp smiley-regexp-alist)
193                        (symbol-value smiley-regexp-alist)
194                      smiley-regexp-alist))
195             entry regexp beg group file)
196         (goto-char (or st (point-min)))
197         (setq beg (point))
198         ;; loop through alist
199         (while (setq entry (pop alist))
200           (setq regexp (car entry)
201                 group (cadr entry)
202                 file (caddr entry))
203           (goto-char beg)
204           (while (re-search-forward regexp nd t)
205             (let* ((start (match-beginning group))
206                    (end (match-end group))
207                    (glyph (smiley-create-glyph (buffer-substring start end)
208                                                file)))
209               (when glyph
210                 (mapcar 'delete-annotation (annotations-at end))
211                 (let ((ext (make-extent start end))
212                       (ant (make-annotation glyph end 'text)))
213                   ;; set text extent params
214                   (set-extent-property ext 'end-open t)
215                   (set-extent-property ext 'start-open t)
216                   (set-extent-property ext 'invisible t)
217                   (set-extent-property ext 'keymap smiley-map)
218                   (set-extent-property ext 'mouse-face gnus-article-mouse-face)
219                   (set-extent-property ext 'intangible t)
220                   ;; set annotation params
221                   (set-extent-property ant 'mouse-face gnus-article-mouse-face)
222                   (set-extent-property ant 'keymap smiley-map)
223                   ;; remember each other
224                   (set-extent-property ant 'smiley-extent ext)
225                   (set-extent-property ext 'smiley-annotation ant))
226                 (when (smiley-end-paren-p start end)
227                   (make-annotation ")" end 'text))
228                 (goto-char end)))))))))
229
230 (defun smiley-end-paren-p (start end)
231   "Try to guess whether the current smiley is an end-paren smiley."
232   (save-excursion
233     (goto-char start)
234     (when (and (re-search-backward "[()]" nil t)
235                (= (following-char) ?\()
236                (goto-char end)
237                (or (not (re-search-forward "[()]" nil t))
238                    (= (char-after (1- (point))) ?\()))
239       t)))
240
241 ;;;###autoload    
242 (defun gnus-smiley-display ()
243   (interactive)
244   (save-excursion
245     (set-buffer gnus-article-buffer)
246     (goto-char (point-min))
247     ;; We skip the headers.
248     (unless (search-forward "\n\n" nil t)
249       (goto-char (point-max)))
250     (smiley-buffer (current-buffer) (point))))
251
252 (provide 'smiley)
253
254 ;;; smiley.el ends here