*** 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 (defvar smiley-regexp-alist
46   '(("\\s-\\(:-*\\]\\)\\W" 1 "FaceGrinning.xpm")
47     ("\\s-\\(:-*[oO]\\)\\W" 1 "FaceStartled.xpm")
48     ("\\s-\\(:-*[)>]\\)" 1 "FaceHappy.xpm")
49     ("\\s-\\(;-*[>)]\\)" 1 "FaceWinking.xpm")
50     ("\\s-\\(:-[/\\]\\)" 1 "FaceIronic.xpm")
51     ("\\s-\\(:-*|\\)" 1 "FaceStraight.xpm")
52     ("\\s-\\(:-*<\\)" 1 "FaceAngry.xpm")
53     ("\\s-\\(:-*d\\)\\W" 1 "FaceTasty.xpm")
54     ("\\s-\\(:-*[pP]\\)\\W" 1 "FaceYukky.xpm")
55     ("\\s-\\(8-*|\\)" 1 "FaceKOed.xpm")
56     ("\\s-\\(:-*(\\)" 1 "FaceAngry.xpm"))
57   "A list of regexps to map smilies to real images.")
58
59 (defvar smiley-flesh-color "yellow"
60   "Flesh color.")
61
62 (defvar smiley-features-color "black"
63   "Features color.")
64
65 (defvar smiley-tongue-color "red"
66   "Tongue color.")
67
68 (defvar smiley-circle-color "black"
69   "Circle color.")
70
71 (defvar smiley-glyph-cache nil)
72 (defvar smiley-running-xemacs (string-match "XEmacs" emacs-version))
73
74 (defun smiley-create-glyph (smiley pixmap)
75   (and
76    smiley-running-xemacs
77    (or
78     (cdr-safe (assoc pixmap smiley-glyph-cache))
79     (let* ((xpm-color-symbols 
80             (and (featurep 'xpm)
81                  (append `(("flesh" ,smiley-flesh-color)
82                            ("features" ,smiley-features-color)
83                            ("tongue" ,smiley-tongue-color))
84                          xpm-color-symbols)))
85            (glyph (make-glyph
86                    (list
87                     (cons 'x (expand-file-name pixmap smiley-data-directory))
88                     (cons 'tty smiley)))))
89       (setq smiley-glyph-cache (cons (cons pixmap glyph) smiley-glyph-cache))
90       (set-glyph-face glyph 'default)
91       glyph))))
92
93 ;;;###autoload
94 (defun smiley-region (beg end)
95   "Smilify the region between point and mark."
96   (interactive "r")
97   (smiley-buffer (current-buffer) beg end))
98
99 ;;;###autoload
100 (defun smiley-buffer (&optional buffer st nd)
101   (interactive)
102   (save-excursion
103     (and buffer (set-buffer buffer))
104     (let ((buffer-read-only nil)
105           (alist smiley-regexp-alist)
106           entry regexp beg group file)
107       (goto-char (or st (point-min)))
108       (setq beg (point))
109       ;; loop through alist
110       (while (setq entry (pop alist))
111         (setq regexp (car entry)
112               group (cadr entry)
113               file (caddr entry))
114         (goto-char beg)
115         (while (re-search-forward regexp nd t)
116           (let* ((start (match-beginning group))
117                  (end (match-end group))
118                  (glyph (smiley-create-glyph (buffer-substring start end)
119                                              file)))
120             (when glyph
121               (mapcar 'delete-annotation (annotations-at end))
122               (let ((ext (make-extent start end)))
123                 (set-extent-property ext 'invisible t)
124                 (set-extent-property ext 'end-open t)
125                 (set-extent-property ext 'intangible t))
126               (make-annotation glyph end 'text)
127               (when (smiley-end-paren-p start end)
128                 (make-annotation ")" end 'text))
129               (goto-char end))))))))
130
131 (defun smiley-end-paren-p (start end)
132   "Try to guess whether the current smiley is an end-paren smiley."
133   (save-excursion
134     (goto-char start)
135     (when (and (re-search-backward "[()]" nil t)
136                (= (following-char) ?\()
137                (goto-char end)
138                (or (not (re-search-forward "[()]" nil t))
139                    (= (char-after (1- (point))) ?\()))
140       t)))
141
142 ;;;###autoload    
143 (defun gnus-smiley-display ()
144   (interactive)
145   (save-excursion
146     (set-buffer gnus-article-buffer)
147     (goto-char (point-min))
148     ;; We skip the headers.
149     (unless (search-forward "\n\n" nil t)
150       (goto-char (point-max)))
151     (smiley-buffer (current-buffer) (point))))
152
153 (provide 'smiley)
154
155 ;;; smiley.el ends here