Importing Oort Gnus v0.04.
[elisp/gnus.git-] / lisp / smiley-ems.el
1 ;;; smiley-ems.el --- displaying smiley faces
2
3 ;; Copyright (C) 2000, 2001 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 (eval-when-compile (require 'cl))
39 (require 'nnheader)
40
41 (defgroup smiley nil
42   "Turn :-)'s into real images."
43   :group 'gnus-visual)
44
45 ;; Maybe this should go.
46 (defcustom smiley-data-directory (nnheader-find-etc-directory "smilies")
47   "*Location of the smiley faces files."
48   :type 'directory
49   :group 'smiley)
50
51 ;; The XEmacs version has a baroque, if not rococo, set of these.
52 (defcustom smiley-regexp-alist
53   '(("\\(:-?)\\)\\W" 1 "smile")
54     ("\\(;-?)\\)\\W" 1 "blink")
55     ("\\(:-]\\)\\W" 1 "forced")
56     ("\\(8-)\\)\\W" 1 "braindamaged")
57     ("\\(:-|\\)\\W" 1 "indifferent")
58     ("\\(:-[/\\]\\)\\W" 1 "wry")
59     ("\\(:-(\\)\\W" 1 "sad")
60     ("\\(:-{\\)\\W" 1 "frown"))
61   "*A list of regexps to map smilies to images.
62 The elements are (REGEXP MATCH FILE), where MATCH is the submatch in
63 regexp to replace with IMAGE.  IMAGE is the name of a PBM file in
64 `smiley-data-directory'."
65   :type '(repeat (list regexp
66                        (integer :tag "Regexp match number")
67                        (string :tag "Image name")))
68   :set (lambda (symbol value)
69          (set-default symbol value)
70          (smiley-update-cache))
71   :initialize 'custom-initialize-default
72   :group 'smiley)
73
74 (defcustom gnus-smiley-file-types
75   (let ((types (list "pbm")))
76     (when (gnus-image-type-available-p 'xpm)
77       (push "xpm" types))
78     types)
79   "*List of suffixes on picon file names to try."
80   :type '(repeat string)
81   :group 'smiley)
82
83 (defvar smiley-cached-regexp-alist nil)
84
85 (defun smiley-update-cache ()
86   (dolist (elt (if (symbolp smiley-regexp-alist)
87                    (symbol-value smiley-regexp-alist)
88                  smiley-regexp-alist))
89     (let ((types gnus-smiley-file-types)
90           file type)
91       (while (and (not file)
92                   (setq type (pop types)))
93         (unless (file-exists-p
94                  (setq file (expand-file-name (concat (nth 2 elt) "." type)
95                                               smiley-data-directory)))
96           (setq file nil)))
97       (when type
98         (let ((image (find-image (list (list :type (intern type) 
99                                              :file file
100                                              :ascent 'center)))))
101           (when image
102             (push (list (car elt) (cadr elt) image)
103                   smiley-cached-regexp-alist)))))))
104
105 (defvar smiley-active nil
106   "Non-nil means smilies in the buffer will be displayed.")
107 (make-variable-buffer-local 'smiley-active)
108
109 (defvar smiley-mouse-map
110   (let ((map (make-sparse-keymap)))
111     (define-key map [down-mouse-2] 'ignore) ; override widget
112     (define-key map [mouse-2]
113       'smiley-mouse-toggle-buffer)
114     map))
115
116 ;;;###autoload
117 (defun smiley-region (start end)
118   "Replace in the region `smiley-regexp-alist' matches with corresponding images.
119 A list of images is returned."
120   (interactive "r")
121   (when (and (fboundp 'display-graphic-p)
122              (display-graphic-p))
123     (mapcar (lambda (o)
124               (if (eq 'smiley (overlay-get o 'smiley))
125                   (delete-overlay o)))
126             (overlays-in start end))
127     (unless smiley-cached-regexp-alist
128       (smiley-update-cache))
129     (setq smiley-active t)
130     (save-excursion
131       (let ((beg (or start (point-min)))
132             group overlay image images)
133         (dolist (entry smiley-cached-regexp-alist)
134           (setq group (nth 1 entry)
135                 image (nth 2 entry))
136           (goto-char beg)
137           (while (re-search-forward (car entry) end t)
138             (when image
139               (push image images)
140               (add-text-properties
141                (match-beginning group) (match-end group)
142                `(display ,image
143                          mouse-face highlight
144                          smiley t
145                          help-echo "mouse-2: toggle smilies in buffer"
146                          keymap smiley-mouse-map)))))
147         images))))
148
149 (defun smiley-toggle-buffer (&optional arg)
150   "Toggle displaying smiley faces.
151 With arg, turn displaying on if and only if arg is positive."
152   (interactive "P")
153   (if (numberp arg)
154       (setq smiley-active (> arg 0))
155     (setq smiley-active (not smiley-active))))
156
157 (defun smiley-mouse-toggle-buffer (event)
158   "Toggle displaying smiley faces.
159 With arg, turn displaying on if and only if arg is positive."
160   (interactive "e")
161   (save-excursion
162     (save-window-excursion
163       (mouse-set-point event)
164       (smiley-toggle-buffer))))
165
166 (eval-when-compile (defvar gnus-article-buffer))
167
168 (defun gnus-smiley-display (&optional arg)
169   "Display textual emoticaons (\"smilies\") as small graphical icons.
170 With arg, turn displaying on if and only if arg is positive."
171   (interactive "P")
172   (gnus-with-article-buffer
173     (if (memq 'smiley gnus-article-wash-types)
174         (gnus-delete-images 'smiley)
175       (article-goto-body)
176       (let ((images (smiley-region (point) (point-max))))
177         (when images
178           (gnus-add-wash-type 'smiley)
179           (dolist (image images)
180             (gnus-add-image 'smiley image))))
181       (when (and (numberp arg)
182                  (<= arg 0))
183         (smiley-toggle-buffer arg)))))
184
185 (provide 'smiley)
186
187 ;;; smiley-ems.el ends here