*** 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 (defcustom smiley-mouse-face 'highlight
135   "Face used for mouse highlighting in the smiley buffer.
136
137 Smiley buttons will be displayed in this face when the cursor is
138 above them."
139   :type 'face
140   :group 'smiley)
141
142
143 (defvar smiley-glyph-cache nil)
144 (defvar smiley-running-xemacs (string-match "XEmacs" emacs-version))
145
146 (defvar smiley-map (make-sparse-keymap "smiley-keys")
147  "keymap to toggle smiley states")
148
149 (define-key smiley-map [(button2)] 'smiley-toggle-extent)
150
151 (defun smiley-create-glyph (smiley pixmap)
152   (and
153    smiley-running-xemacs
154    (or
155     (cdr-safe (assoc pixmap smiley-glyph-cache))
156     (let* ((xpm-color-symbols 
157             (and (featurep 'xpm)
158                  (append `(("flesh" ,smiley-flesh-color)
159                            ("features" ,smiley-features-color)
160                            ("tongue" ,smiley-tongue-color))
161                          xpm-color-symbols)))
162            (glyph (make-glyph
163                    (list
164                     (cons 'x (expand-file-name pixmap smiley-data-directory))
165                     (cons 'tty smiley)))))
166       (setq smiley-glyph-cache (cons (cons pixmap glyph) smiley-glyph-cache))
167       (set-glyph-face glyph 'default)
168       glyph))))
169
170 ;;;###autoload
171 (defun smiley-region (beg end)
172   "Smilify the region between point and mark."
173   (interactive "r")
174   (smiley-buffer (current-buffer) beg end))
175
176 (defun smiley-toggle-extent (event)
177   "Toggle smiley at given point"
178   (interactive "e")
179   (let* ((ant (event-glyph-extent event))
180          (pt (event-closest-point event))
181          ext)
182     (if (annotationp ant)
183         (when (extentp (setq ext (extent-property ant 'smiley-extent)))
184           (set-extent-property ext 'invisible nil)
185           (hide-annotation ant))
186       (when pt
187         (while (setq ext (extent-at pt (event-buffer event) nil ext 'at))
188           (when (annotationp (setq ant 
189                                    (extent-property ext 'smiley-annotation)))
190             (reveal-annotation ant)
191             (set-extent-property ext 'invisible t)))))))
192
193 ;;;###autoload
194 (defun smiley-buffer (&optional buffer st nd)
195   (interactive)
196   (when (featurep 'x)
197     (save-excursion
198       (when buffer
199         (set-buffer buffer))
200       (let ((buffer-read-only nil)
201             (alist (if (symbolp smiley-regexp-alist)
202                        (symbol-value smiley-regexp-alist)
203                      smiley-regexp-alist))
204             entry regexp beg group file)
205         (goto-char (or st (point-min)))
206         (setq beg (point))
207         ;; loop through alist
208         (while (setq entry (pop alist))
209           (setq regexp (car entry)
210                 group (cadr entry)
211                 file (caddr entry))
212           (goto-char beg)
213           (while (re-search-forward regexp nd t)
214             (let* ((start (match-beginning group))
215                    (end (match-end group))
216                    (glyph (smiley-create-glyph (buffer-substring start end)
217                                                file)))
218               (when glyph
219                 (mapcar 'delete-annotation (annotations-at end))
220                 (let ((ext (make-extent start end))
221                       (ant (make-annotation glyph end 'text)))
222                   ;; set text extent params
223                   (set-extent-property ext 'end-open t)
224                   (set-extent-property ext 'start-open t)
225                   (set-extent-property ext 'invisible t)
226                   (set-extent-property ext 'keymap smiley-map)
227                   (set-extent-property ext 'mouse-face 'smiley-mouse-face)
228                   (set-extent-property ext 'intangible t)
229                   ;; set annotation params
230                   (set-extent-property ant 'mouse-face 'smiley-mouse-face)
231                   (set-extent-property ant 'keymap smiley-map)
232                   ;; remember each other
233                   (set-extent-property ant 'smiley-extent ext)
234                   (set-extent-property ext 'smiley-annotation ant))
235                 (when (smiley-end-paren-p start end)
236                   (make-annotation ")" end 'text))
237                 (goto-char end)))))))))
238
239 (defun smiley-end-paren-p (start end)
240   "Try to guess whether the current smiley is an end-paren smiley."
241   (save-excursion
242     (goto-char start)
243     (when (and (re-search-backward "[()]" nil t)
244                (= (following-char) ?\()
245                (goto-char end)
246                (or (not (re-search-forward "[()]" nil t))
247                    (= (char-after (1- (point))) ?\()))
248       t)))
249
250 ;;;###autoload    
251 (defun gnus-smiley-display ()
252   (interactive)
253   (save-excursion
254     (set-buffer gnus-article-buffer)
255     (goto-char (point-min))
256     ;; We skip the headers.
257     (unless (search-forward "\n\n" nil t)
258       (goto-char (point-max)))
259     (smiley-buffer (current-buffer) (point))))
260
261 (provide 'smiley)
262
263 ;;; smiley.el ends here