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