comparison lisp/gnus/smiley-ems.el @ 31789:ff5eb810214a

*** empty log message ***
author Dave Love <fx@gnu.org>
date Wed, 20 Sep 2000 17:08:54 +0000
parents
children 1e15c63f0711
comparison
equal deleted inserted replaced
31788:1142ff68054d 31789:ff5eb810214a
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 '(("\\([:;]-?)\\)\\W" 1 "smile.xbm")
52 ("\\(:-[/\\]\\)\\W" 1 "wry.xbm")
53 ("\\(:-[({]\\)\\W" 1 "frown.xbm"))
54 "*A list of regexps to map smilies to images.
55 The elements are (REGEXP MATCH FILE), where MATCH is the submatch in
56 rgexp to replace with IMAGE. IMAGE is the name of an XBM file in
57 `smiley-data-directory'."
58 :type '(repeat (list regexp
59 (integer :tag "Regexp match number")
60 (string :tag "Image name")))
61 :set (lambda (symbol value)
62 (set-default symbol value)
63 (smiley-update-cache))
64 :initialize 'custom-initialize-default
65 :group 'smiley)
66
67 (defvar smiley-cached-regexp-alist nil)
68
69 (defun smiley-update-cache ()
70 (dolist (elt smiley-regexp-alist)
71 (let* ((data-directory smiley-data-directory)
72 (image (find-image (list (list :type 'xbm
73 :file (nth 2 elt)
74 :ascent 100)))))
75 (if image
76 (push (list (car elt) (cadr elt) image)
77 smiley-cached-regexp-alist)))))
78
79 (defvar smiley-active nil
80 "Non-nil means smilies in the buffer will be displayed.")
81 (make-variable-buffer-local 'smiley-active)
82
83 (defvar smiley-mouse-map
84 (let ((map (make-sparse-keymap)))
85 (define-key map [down-mouse-2] 'ignore) ; override widget
86 (define-key map [mouse-2]
87 'smiley-mouse-toggle-buffer)
88 map))
89
90 ;;;###autoload
91 (defun smiley-region (start end)
92 "Replace in the region `smiley-regexp-alist' matches with corresponding images."
93 (interactive "r")
94 (when (display-graphic-p)
95 (mapc (lambda (o)
96 (if (eq 'smiley (overlay-get o 'smiley))
97 (delete-overlay o)))
98 (overlays-in start end))
99 (unless smiley-cached-regexp-alist
100 (smiley-update-cache))
101 (save-excursion
102 (let ((beg (or start (point-min)))
103 buffer-read-only entry beg group overlay image)
104 (dolist (entry smiley-cached-regexp-alist)
105 (setq group (nth 1 entry))
106 (goto-char beg)
107 (while (re-search-forward (car entry) end t)
108 (when image
109 (setq overlay (make-overlay (match-beginning group)
110 (match-end group)))
111 (overlay-put overlay
112 'display `(when smiley-active ,@(nth 2 entry)))
113 (overlay-put overlay 'mouse-face 'highlight)
114 (overlay-put overlay 'smiley t)
115 (overlay-put overlay
116 'help-echo "mouse-2: toggle smilies in buffer")
117 (overlay-put overlay 'keymap smiley-mouse-map))))))
118 (setq smiley-active t)))
119
120 (defun smiley-toggle-buffer (&optional arg)
121 "Toggle displaying smiley faces.
122 With arg, turn displaying on if and only if arg is positive."
123 (interactive "P")
124 (if (numberp arg)
125 (setq smiley-active (> arg 0))
126 (setq smiley-active (not smiley-active))))
127
128 (defun smiley-mouse-toggle-buffer (event)
129 "Toggle displaying smiley faces.
130 With arg, turn displaying on if and only if arg is positive."
131 (interactive "e")
132 (save-excursion
133 (save-window-excursion
134 (mouse-set-point event)
135 (smiley-toggle-buffer))))
136
137 (eval-when-compile (defvar gnus-article-buffer))
138
139 (defun gnus-smiley-display (&optional arg)
140 "Display textual emoticaons (\"smilies\") as small graphical icons.
141 With arg, turn displaying on if and only if arg is positive."
142 (interactive "P")
143 (save-excursion
144 (set-buffer gnus-article-buffer)
145 (save-restriction
146 (widen)
147 (article-goto-body)
148 (smiley-region (point-min) (point-max))
149 (if (and (numberp arg) (<= arg 0))
150 (smiley-toggle-buffer arg)))))
151
152 (provide 'smiley)
153
154 ;;; smiley-ems.el ends here