]> code.delx.au - gnu-emacs/blob - lisp/gnus/smiley-ems.el
(smiley-update-cache): Use `:ascent center'.
[gnu-emacs] / lisp / gnus / smiley-ems.el
1 ;;; smiley-ems.el --- displaying smiley faces
2 ;; Copyright (C) 2000 Free Software Foundation, Inc.
3
4 ;; Author: Dave Love <fx@gnu.org>
5 ;; Keywords: news mail multimedia
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 ;; 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
32
33 ;;; Test smileys: :-) :-\ :-( :-/
34
35 ;;; Code:
36
37 (require 'nnheader)
38
39 (defgroup smiley nil
40 "Turn :-)'s into real images."
41 :group 'gnus-visual)
42
43 ;; Maybe this should go.
44 (defcustom smiley-data-directory (nnheader-find-etc-directory "smilies")
45 "*Location of the smiley faces files."
46 :type 'directory
47 :group 'smiley)
48
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
66 :group 'smiley)
67
68 (defvar smiley-cached-regexp-alist nil)
69
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
74 :file (nth 2 elt)
75 :ascent 'center)))))
76 (if image
77 (push (list (car elt) (cadr elt) image)
78 smiley-cached-regexp-alist)))))
79
80 (defvar smiley-active nil
81 "Non-nil means smilies in the buffer will be displayed.")
82 (make-variable-buffer-local 'smiley-active)
83
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)
89 map))
90
91 ;;;###autoload
92 (defun smiley-region (start end)
93 "Replace in the region `smiley-regexp-alist' matches with corresponding images."
94 (interactive "r")
95 (when (and (fboundp 'display-graphic-p)
96 (display-graphic-p))
97 (mapc (lambda (o)
98 (if (eq 'smiley (overlay-get o 'smiley))
99 (delete-overlay o)))
100 (overlays-in start end))
101 (unless smiley-cached-regexp-alist
102 (smiley-update-cache))
103 (save-excursion
104 (let ((beg (or start (point-min)))
105 group overlay image)
106 (dolist (entry smiley-cached-regexp-alist)
107 (setq group (nth 1 entry)
108 image (nth 2 entry))
109 (goto-char beg)
110 (while (re-search-forward (car entry) end t)
111 (when image
112 (setq overlay (make-overlay (match-beginning group)
113 (match-end group)))
114 (overlay-put overlay
115 'display `(when smiley-active ,@image))
116 (overlay-put overlay 'mouse-face 'highlight)
117 (overlay-put overlay 'smiley t)
118 (overlay-put overlay
119 'help-echo "mouse-2: toggle smilies in buffer")
120 (overlay-put overlay 'keymap smiley-mouse-map))))))
121 (setq smiley-active t)))
122
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."
126 (interactive "P")
127 (if (numberp arg)
128 (setq smiley-active (> arg 0))
129 (setq smiley-active (not smiley-active))))
130
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."
134 (interactive "e")
135 (save-excursion
136 (save-window-excursion
137 (mouse-set-point event)
138 (smiley-toggle-buffer))))
139
140 (eval-when-compile (defvar gnus-article-buffer))
141
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."
145 (interactive "P")
146 (save-excursion
147 (set-buffer gnus-article-buffer)
148 (save-restriction
149 (widen)
150 (article-goto-body)
151 (smiley-region (point-min) (point-max))
152 (if (and (numberp arg) (<= arg 0))
153 (smiley-toggle-buffer arg)))))
154
155 (provide 'smiley)
156
157 ;;; smiley-ems.el ends here