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