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