*** empty log message ***
[emacs.git] / lisp / gnus / smiley-ems.el
blob6ded76685f7d701fc256af4a7e1f30c98fd2daef
1 ;;; smiley-ems.el --- displaying smiley faces
3 ;; Copyright (C) 2000 Free Software Foundation, Inc.
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Keywords: news mail multimedia
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
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
34 ;;; Test smileys: :-) :-\ :-( :-/
36 ;;; Code:
38 (require 'nnheader)
40 (defgroup smiley nil
41 "Turn :-)'s into real images."
42 :group 'gnus-visual)
44 ;; Maybe this should go.
45 (defcustom smiley-data-directory (nnheader-find-etc-directory "smilies")
46 "*If non-nil, a directory to search for the smiley image files.
47 This is in addition to the normal image search path."
48 :type '(choice directory
49 (const nil))
50 :group 'smiley)
52 ;; The XEmacs version has a baroque, if not rococo, set of these.
53 (defcustom smiley-regexp-alist
54 ;; Perhaps :-) should be distinct -- it does appear in the Jargon File.
55 '(("\\([:;]-?)\\)\\(\\W\\|\\'\\)" 1 "smile.pbm")
56 ("\\(:-[/\\]\\)\\(\\W\\|\\'\\)" 1 "wry.pbm")
57 ("\\(:-[({]\\)\\(\\W\\|\\'\\)" 1 "frown.pbm"))
58 "*A list of regexps to map smilies to images.
59 The elements are (REGEXP MATCH FILE), where MATCH is the submatch in
60 rgexp to replace with IMAGE. IMAGE is the name of a PBM file in
61 `smiley-data-directory' or the normal image search path."
62 :type '(repeat (list regexp
63 (integer :tag "Regexp match number")
64 (string :tag "Image name")))
65 :set (lambda (symbol value)
66 (set-default symbol value)
67 (smiley-update-cache))
68 :initialize 'custom-initialize-default
69 :group 'smiley)
71 (defvar smiley-cached-regexp-alist nil)
73 (defun smiley-update-cache ()
74 (dolist (elt smiley-regexp-alist)
75 (let* ((data-directory smiley-data-directory)
76 (image (find-image (list (list :type 'pbm
77 :file (nth 2 elt)
78 :ascent 'center)))))
79 (if image
80 (push (list (car elt) (cadr elt) image)
81 smiley-cached-regexp-alist)))))
83 (defvar smiley-active nil
84 "Non-nil means smilies in the buffer will be displayed.")
85 (make-variable-buffer-local 'smiley-active)
87 (defvar smiley-mouse-map
88 (let ((map (make-sparse-keymap)))
89 (define-key map [down-mouse-2] 'ignore) ; override widget
90 (define-key map [mouse-2]
91 'smiley-mouse-toggle-buffer)
92 map))
94 ;;;###autoload
95 (defun smiley-region (start end)
96 "Display textual smileys as images.
97 START and END specify the region; interactively, use the values
98 of point and mark. The value of `smiley-regexp-alist' determines
99 which smileys to operate on and which images to use for them."
100 (interactive "r")
101 (when (and (fboundp 'display-graphic-p)
102 (display-graphic-p))
103 (mapc (lambda (o)
104 (if (eq 'smiley (overlay-get o 'smiley))
105 (delete-overlay o)))
106 (overlays-in start end))
107 (unless smiley-cached-regexp-alist
108 (smiley-update-cache))
109 (save-excursion
110 (let ((beg (or start (point-min)))
111 (inhibit-point-motion-hooks t)
112 group overlay image)
113 (dolist (entry smiley-cached-regexp-alist)
114 (setq group (nth 1 entry)
115 image (nth 2 entry))
116 (goto-char beg)
117 (while (re-search-forward (car entry) end t)
118 (when image
119 (setq overlay (make-overlay (match-beginning group)
120 (match-end group)))
121 (overlay-put overlay
122 'display `(when smiley-active ,@image))
123 (overlay-put overlay 'mouse-face 'highlight)
124 (overlay-put overlay 'smiley t)
125 (overlay-put overlay
126 'help-echo "mouse-2: toggle smilies in buffer")
127 (overlay-put overlay 'keymap smiley-mouse-map)
128 (goto-char (match-end group)))))))
129 (setq smiley-active t)))
131 (defun smiley-toggle-buffer (&optional arg)
132 "Toggle displaying smiley faces.
133 With arg, turn displaying on if and only if arg is positive."
134 (interactive "P")
135 (if (numberp arg)
136 (setq smiley-active (> arg 0))
137 (setq smiley-active (not smiley-active))))
139 (defun smiley-mouse-toggle-buffer (event)
140 "Toggle displaying smiley faces.
141 With arg, turn displaying on if and only if arg is positive."
142 (interactive "e")
143 (save-excursion
144 (save-window-excursion
145 (mouse-set-point event)
146 (smiley-toggle-buffer))))
148 (eval-when-compile (defvar gnus-article-buffer))
150 (defun gnus-smiley-display (&optional arg)
151 "Display textual emoticons (\"smilies\") as small graphical icons.
152 With arg, turn displaying on if and only if arg is positive."
153 (interactive "P")
154 (save-excursion
155 (set-buffer gnus-article-buffer)
156 (save-restriction
157 (widen)
158 (article-goto-body)
159 (smiley-region (point-min) (point-max))
160 (if (and (numberp arg) (<= arg 0))
161 (smiley-toggle-buffer arg)))))
163 (provide 'smiley)
165 ;;; smiley-ems.el ends here