1 ;;; smiley-ems.el --- displaying smiley faces
2 ;; Copyright (C) 2000 Free Software Foundation, Inc.
4 ;; Author: Dave Love <fx@gnu.org>
5 ;; Keywords: news mail multimedia
7 ;; This file is part of GNU Emacs.
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)
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.
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.
26 ;; A re-written, simplified version of Wes Hardaker's XEmacs smiley.el
27 ;; which might be merged back to smiley.el if we get an assignment for
28 ;; that. We don't have assignments for the images smiley.el uses, but
29 ;; I'm not sure we need that degree of rococoness and they shouldn't
30 ;; have a yellow background by default. Also, using XBM means we can
31 ;; display the images more generally. -- fx
33 ;;; Test smileys: :-) :-\ :-( :-/
40 "Turn :-)'s into real images."
43 ;; Maybe this should go.
44 (defcustom smiley-data-directory (nnheader-find-etc-directory "smilies")
45 "*Location of the smiley faces files."
49 ;; The XEmacs version has a baroque, if not rococo, set of these.
50 (defcustom smiley-regexp-alist
51 ;; Perhaps :-) should be distinct -- it does appear in the Jargon File.
52 '(("\\([:;]-?)\\)\\W" 1 "smile.xbm")
53 ("\\(:-[/\\]\\)\\W" 1 "wry.xbm")
54 ("\\(:-[({]\\)\\W" 1 "frown.xbm"))
55 "*A list of regexps to map smilies to images.
56 The elements are (REGEXP MATCH FILE), where MATCH is the submatch in
57 rgexp to replace with IMAGE. IMAGE is the name of an XBM file in
58 `smiley-data-directory'."
59 :type '(repeat (list regexp
60 (integer :tag "Regexp match number")
61 (string :tag "Image name")))
62 :set (lambda (symbol value)
63 (set-default symbol value)
64 (smiley-update-cache))
65 :initialize 'custom-initialize-default
68 (defvar smiley-cached-regexp-alist nil)
70 (defun smiley-update-cache ()
71 (dolist (elt smiley-regexp-alist)
72 (let* ((data-directory smiley-data-directory)
73 (image (find-image (list (list :type 'xbm
77 (push (list (car elt) (cadr elt) image)
78 smiley-cached-regexp-alist)))))
80 (defvar smiley-active nil
81 "Non-nil means smilies in the buffer will be displayed.")
82 (make-variable-buffer-local 'smiley-active)
84 (defvar smiley-mouse-map
85 (let ((map (make-sparse-keymap)))
86 (define-key map [down-mouse-2] 'ignore) ; override widget
87 (define-key map [mouse-2]
88 'smiley-mouse-toggle-buffer)
92 (defun smiley-region (start end)
93 "Replace in the region `smiley-regexp-alist' matches with corresponding images."
95 (when (and (fboundp 'display-graphic-p)
98 (if (eq 'smiley (overlay-get o 'smiley))
100 (overlays-in start end))
101 (unless smiley-cached-regexp-alist
102 (smiley-update-cache))
104 (let ((beg (or start (point-min)))
106 (dolist (entry smiley-cached-regexp-alist)
107 (setq group (nth 1 entry)
110 (while (re-search-forward (car entry) end t)
112 (setq overlay (make-overlay (match-beginning group)
115 'display `(when smiley-active ,@image))
116 (overlay-put overlay 'mouse-face 'highlight)
117 (overlay-put overlay 'smiley t)
119 'help-echo "mouse-2: toggle smilies in buffer")
120 (overlay-put overlay 'keymap smiley-mouse-map))))))
121 (setq smiley-active t)))
123 (defun smiley-toggle-buffer (&optional arg)
124 "Toggle displaying smiley faces.
125 With arg, turn displaying on if and only if arg is positive."
128 (setq smiley-active (> arg 0))
129 (setq smiley-active (not smiley-active))))
131 (defun smiley-mouse-toggle-buffer (event)
132 "Toggle displaying smiley faces.
133 With arg, turn displaying on if and only if arg is positive."
136 (save-window-excursion
137 (mouse-set-point event)
138 (smiley-toggle-buffer))))
140 (eval-when-compile (defvar gnus-article-buffer))
142 (defun gnus-smiley-display (&optional arg)
143 "Display textual emoticaons (\"smilies\") as small graphical icons.
144 With arg, turn displaying on if and only if arg is positive."
147 (set-buffer gnus-article-buffer)
151 (smiley-region (point-min) (point-max))
152 (if (and (numberp arg) (<= arg 0))
153 (smiley-toggle-buffer arg)))))
157 ;;; smiley-ems.el ends here