Fix my last change.
[gnus] / lisp / smiley-ems.el
1 ;;; smiley-ems.el --- displaying smiley faces
2
3 ;; Copyright (C) 2000 Free Software Foundation, Inc.
4
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Keywords: news mail multimedia
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; A re-written, simplified version of Wes Hardaker's XEmacs smiley.el
28 ;; which might be merged back to smiley.el if we get an assignment for
29 ;; that.  We don't have assignments for the images smiley.el uses, but
30 ;; I'm not sure we need that degree of rococoness and defaults like a
31 ;; yellow background.  Also, using PBM means we can display the images
32 ;; more generally.  -- fx
33
34 ;;; Test smileys:  :-) :-\ :-( :-/
35
36 ;;; Code:
37
38 (require 'nnheader)
39
40 (defgroup smiley nil
41   "Turn :-)'s into real images."
42   :group 'gnus-visual)
43
44 ;; Maybe this should go.
45 (defcustom smiley-data-directory (nnheader-find-etc-directory "smilies")
46   "*Location of the smiley faces files."
47   :type 'directory
48   :group 'smiley)
49
50 ;; The XEmacs version has a baroque, if not rococo, set of these.
51 (defcustom smiley-regexp-alist
52   ;; Perhaps :-) should be distinct -- it does appear in the Jargon File.
53   '(("\\([:;]-?)\\)\\W" 1 "smile.pbm")
54     ("\\(:-[/\\]\\)\\W" 1 "wry.pbm")
55     ("\\(:-[({]\\)\\W" 1 "frown.pbm"))
56   "*A list of regexps to map smilies to images.
57 The elements are (REGEXP MATCH FILE), where MATCH is the submatch in
58 rgexp to replace with IMAGE.  IMAGE is the name of a PBM file in
59 `smiley-data-directory'."
60   :type '(repeat (list regexp
61                        (integer :tag "Regexp match number")
62                        (string :tag "Image name")))
63   :set (lambda (symbol value)
64          (set-default symbol value)
65          (smiley-update-cache))
66   :initialize 'custom-initialize-default
67   :group 'smiley)
68
69 (defvar smiley-cached-regexp-alist nil)
70
71 (defun smiley-update-cache ()
72   (dolist (elt smiley-regexp-alist)
73     (let* ((data-directory smiley-data-directory)
74            (image (find-image (list (list :type 'pbm
75                                           :file (nth 2 elt)
76                                           :ascent 'center)))))
77       (if image
78           (push (list (car elt) (cadr elt) image)
79                 smiley-cached-regexp-alist)))))
80
81 (defvar smiley-active nil
82   "Non-nil means smilies in the buffer will be displayed.")
83 (make-variable-buffer-local 'smiley-active)
84
85 (defvar smiley-mouse-map
86   (let ((map (make-sparse-keymap)))
87     (define-key map [down-mouse-2] 'ignore) ; override widget
88     (define-key map [mouse-2]
89       'smiley-mouse-toggle-buffer)
90     map))
91
92 ;;;###autoload
93 (defun smiley-region (start end)
94   "Replace in the region `smiley-regexp-alist' matches with corresponding images."
95   (interactive "r")
96   (when (and (fboundp 'display-graphic-p)
97              (display-graphic-p))
98     (mapc (lambda (o)
99             (if (eq 'smiley (overlay-get o 'smiley))
100                 (delete-overlay o)))
101           (overlays-in start end))
102     (unless smiley-cached-regexp-alist
103       (smiley-update-cache))
104     (save-excursion
105       (let ((beg (or start (point-min)))
106             group overlay image)
107         (dolist (entry smiley-cached-regexp-alist)
108           (setq group (nth 1 entry)
109                 image (nth 2 entry))
110           (goto-char beg)
111           (while (re-search-forward (car entry) end t)
112             (when image
113               (setq overlay (make-overlay (match-beginning group)
114                                           (match-end group)))
115               (overlay-put overlay
116                            'display `(when smiley-active ,@image))
117               (overlay-put overlay 'mouse-face 'highlight)
118               (overlay-put overlay 'smiley t)
119               (overlay-put overlay
120                            'help-echo "mouse-2: toggle smilies in buffer")
121               (overlay-put overlay 'keymap smiley-mouse-map))))))
122         (setq smiley-active t)))
123
124 (defun smiley-toggle-buffer (&optional arg)
125   "Toggle displaying smiley faces.
126 With arg, turn displaying on if and only if arg is positive."
127   (interactive "P")
128   (if (numberp arg)
129       (setq smiley-active (> arg 0))
130     (setq smiley-active (not smiley-active))))
131
132 (defun smiley-mouse-toggle-buffer (event)
133   "Toggle displaying smiley faces.
134 With arg, turn displaying on if and only if arg is positive."
135   (interactive "e")
136   (save-excursion
137     (save-window-excursion
138       (mouse-set-point event)
139       (smiley-toggle-buffer))))
140
141 (eval-when-compile (defvar gnus-article-buffer))
142
143 (defun gnus-smiley-display (&optional arg)
144   "Display textual emoticaons (\"smilies\") as small graphical icons.
145 With arg, turn displaying on if and only if arg is positive."
146   (interactive "P")
147   (save-excursion
148     (set-buffer gnus-article-buffer)
149     (save-restriction
150       (widen)
151       (article-goto-body)
152       (smiley-region (point-min) (point-max))
153       (if (and (numberp arg) (<= arg 0))
154           (smiley-toggle-buffer arg)))))
155
156 (provide 'smiley)
157
158 ;;; smiley-ems.el ends here