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