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