*** 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
90 (defvar smiley-flesh-color "yellow"
91   "Flesh color.")
92
93 (defvar smiley-features-color "black"
94   "Features color.")
95
96 (defvar smiley-tongue-color "red"
97   "Tongue color.")
98
99 (defvar smiley-circle-color "black"
100   "Circle color.")
101
102 (defvar smiley-glyph-cache nil)
103 (defvar smiley-running-xemacs (string-match "XEmacs" emacs-version))
104
105 (defun smiley-create-glyph (smiley pixmap)
106   (and
107    smiley-running-xemacs
108    (or
109     (cdr-safe (assoc pixmap smiley-glyph-cache))
110     (let* ((xpm-color-symbols 
111             (and (featurep 'xpm)
112                  (append `(("flesh" ,smiley-flesh-color)
113                            ("features" ,smiley-features-color)
114                            ("tongue" ,smiley-tongue-color))
115                          xpm-color-symbols)))
116            (glyph (make-glyph
117                    (list
118                     (cons 'x (expand-file-name pixmap smiley-data-directory))
119                     (cons 'tty smiley)))))
120       (setq smiley-glyph-cache (cons (cons pixmap glyph) smiley-glyph-cache))
121       (set-glyph-face glyph 'default)
122       glyph))))
123
124 ;;;###autoload
125 (defun smiley-region (beg end)
126   "Smilify the region between point and mark."
127   (interactive "r")
128   (smiley-buffer (current-buffer) beg end))
129
130 ;;;###autoload
131 (defun smiley-buffer (&optional buffer st nd)
132   (interactive)
133   (when (featurep 'x)
134     (save-excursion
135       (when buffer
136         (set-buffer buffer))
137       (let ((buffer-read-only nil)
138             (alist smiley-regexp-alist)
139             entry regexp beg group file)
140         (goto-char (or st (point-min)))
141         (setq beg (point))
142         ;; loop through alist
143         (while (setq entry (pop alist))
144           (setq regexp (car entry)
145                 group (cadr entry)
146                 file (caddr entry))
147           (goto-char beg)
148           (while (re-search-forward regexp nd t)
149             (let* ((start (match-beginning group))
150                    (end (match-end group))
151                    (glyph (smiley-create-glyph (buffer-substring start end)
152                                                file)))
153               (when glyph
154                 (mapcar 'delete-annotation (annotations-at end))
155                 (let ((ext (make-extent start end)))
156                   (set-extent-property ext 'invisible t)
157                   (set-extent-property ext 'end-open t)
158                   (set-extent-property ext 'intangible t))
159                 (make-annotation glyph end 'text)
160                 (when (smiley-end-paren-p start end)
161                   (make-annotation ")" end 'text))
162                 (goto-char end)))))))))
163
164 (defun smiley-end-paren-p (start end)
165   "Try to guess whether the current smiley is an end-paren smiley."
166   (save-excursion
167     (goto-char start)
168     (when (and (re-search-backward "[()]" nil t)
169                (= (following-char) ?\()
170                (goto-char end)
171                (or (not (re-search-forward "[()]" nil t))
172                    (= (char-after (1- (point))) ?\()))
173       t)))
174
175 ;;;###autoload    
176 (defun gnus-smiley-display ()
177   (interactive)
178   (save-excursion
179     (set-buffer gnus-article-buffer)
180     (goto-char (point-min))
181     ;; We skip the headers.
182     (unless (search-forward "\n\n" nil t)
183       (goto-char (point-max)))
184     (smiley-buffer (current-buffer) (point))))
185
186 (provide 'smiley)
187
188 ;;; smiley.el ends here