Import No Gnus v0.4.
[elisp/gnus.git-] / lisp / smiley.el
1 ;;; smiley.el --- displaying smiley faces
2
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Dave Love <fx@gnu.org>
7 ;; Keywords: news mail multimedia
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; A re-written, simplified version of Wes Hardaker's XEmacs smiley.el
29 ;; which might be merged back to smiley.el if we get an assignment for
30 ;; that.  We don't have assignments for the images smiley.el uses, but
31 ;; I'm not sure we need that degree of rococoness and defaults like a
32 ;; yellow background.  Also, using PBM means we can display the images
33 ;; more generally.  -- fx
34 ;; `smiley.el' was replaced by `smiley-ems.el' on 2002-01-26 (after fx'
35 ;; comment).
36
37 ;; Test smileys:
38 ;; smile             ^:-) ^:)
39 ;; blink             ;-)  ;)
40 ;; forced            :-]
41 ;; braindamaged      8-)
42 ;; indifferent       :-|
43 ;; wry               :-/  :-\
44 ;; sad               :-(
45 ;; frown             :-{
46 ;; evil              >:-)
47 ;; cry               ;-(
48 ;; dead              X-)
49 ;; grin              :-D
50
51 ;;; Code:
52
53 (eval-when-compile (require 'cl))
54 (require 'nnheader)
55 (require 'gnus-art)
56
57 (defgroup smiley nil
58   "Turn :-)'s into real images."
59   :group 'gnus-visual)
60
61 ;; Maybe this should go.
62 (defcustom smiley-data-directory
63   (nnheader-find-etc-directory "images/smilies")
64   "Location of the smiley faces files."
65   :type 'directory
66   :group 'smiley)
67
68 ;; The XEmacs version has a baroque, if not rococo, set of these.
69 (defcustom smiley-regexp-alist
70   '(("\\(:-?)\\)\\W" 1 "smile")
71     ("\\(;-?)\\)\\W" 1 "blink")
72     ("\\(:-]\\)\\W" 1 "forced")
73     ("\\(8-)\\)\\W" 1 "braindamaged")
74     ("\\(:-|\\)\\W" 1 "indifferent")
75     ("\\(:-[/\\]\\)\\W" 1 "wry")
76     ("\\(:-(\\)\\W" 1 "sad")
77     ("\\(:-{\\)\\W" 1 "frown"))
78   "*A list of regexps to map smilies to images.
79 The elements are (REGEXP MATCH IMAGE), where MATCH is the submatch in
80 regexp to replace with IMAGE.  IMAGE is the name of an image file in
81 `smiley-data-directory'."
82   :type '(repeat (list regexp
83                        (integer :tag "Regexp match number")
84                        (string :tag "Image name")))
85   :set (lambda (symbol value)
86          (set-default symbol value)
87          (smiley-update-cache))
88   :initialize 'custom-initialize-default
89   :group 'smiley)
90
91 (defcustom gnus-smiley-file-types
92   (let ((types (list "pbm")))
93     (when (gnus-image-type-available-p 'xpm)
94       (push "xpm" types))
95     types)
96   "*List of suffixes on smiley file names to try."
97   :version "22.1"
98   :type '(repeat string)
99   :group 'smiley)
100
101 (defvar smiley-cached-regexp-alist nil)
102
103 (defun smiley-update-cache ()
104   (setq smiley-cached-regexp-alist nil)
105   (dolist (elt (if (symbolp smiley-regexp-alist)
106                    (symbol-value smiley-regexp-alist)
107                  smiley-regexp-alist))
108     (let ((types gnus-smiley-file-types)
109           file type)
110       (while (and (not file)
111                   (setq type (pop types)))
112         (unless (file-exists-p
113                  (setq file (expand-file-name (concat (nth 2 elt) "." type)
114                                               smiley-data-directory)))
115           (setq file nil)))
116       (when type
117         (let ((image (gnus-create-image file (intern type) nil
118                                         :ascent 'center)))
119           (when image
120             (push (list (car elt) (cadr elt) image)
121                   smiley-cached-regexp-alist)))))))
122
123 ;; Not implemented:
124 ;; (defvar smiley-mouse-map
125 ;;   (let ((map (make-sparse-keymap)))
126 ;;     (define-key map [down-mouse-2] 'ignore) ; override widget
127 ;;     (define-key map [mouse-2]
128 ;;       'smiley-mouse-toggle-buffer)
129 ;;     map))
130
131 ;;;###autoload
132 (defun smiley-region (start end)
133   "Replace in the region `smiley-regexp-alist' matches with corresponding images.
134 A list of images is returned."
135   (interactive "r")
136   (when (gnus-graphic-display-p)
137     (unless smiley-cached-regexp-alist
138       (smiley-update-cache))
139     (save-excursion
140       (let ((beg (or start (point-min)))
141             group image images string)
142         (dolist (entry smiley-cached-regexp-alist)
143           (setq group (nth 1 entry)
144                 image (nth 2 entry))
145           (goto-char beg)
146           (while (re-search-forward (car entry) end t)
147             (setq string (match-string group))
148             (goto-char (match-end group))
149             (delete-region (match-beginning group) (match-end group))
150             (when image
151               (push image images)
152               (gnus-add-wash-type 'smiley)
153               (gnus-add-image 'smiley image)
154               (gnus-put-image image string 'smiley))))
155         images))))
156
157 ;;;###autoload
158 (defun smiley-buffer (&optional buffer)
159   "Run `smiley-region' at the buffer, specified in the argument or
160 interactively. If there's no argument, do it at the current buffer"
161   (interactive "bBuffer to run smiley-region: ")
162   (save-excursion
163     (if buffer
164         (set-buffer (get-buffer buffer)))
165     (smiley-region (point-min) (point-max))))
166
167 (defun smiley-toggle-buffer (&optional arg)
168   "Toggle displaying smiley faces in article buffer.
169 With arg, turn displaying on if and only if arg is positive."
170   (interactive "P")
171   (gnus-with-article-buffer
172     (if (if (numberp arg)
173             (> arg 0)
174           (not (memq 'smiley gnus-article-wash-types)))
175         (smiley-region (point-min) (point-max))
176       (gnus-delete-images 'smiley))))
177
178 (defun smiley-mouse-toggle-buffer (event)
179   "Toggle displaying smiley faces.
180 With arg, turn displaying on if and only if arg is positive."
181   (interactive "e")
182   (save-excursion
183     (save-window-excursion
184       (mouse-set-point event)
185       (smiley-toggle-buffer))))
186
187 (provide 'smiley)
188
189 ;;; arch-tag: 5beb161b-4321-40af-8ac9-876afb8ee818
190 ;;; smiley.el ends here