Add more test smileys.
[gnus] / 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 ;; evil              >:-)
46 ;; cry               ;-(
47 ;; dead              X-)
48 ;; grin              :-D
49
50 ;;; Code:
51
52 (eval-when-compile (require 'cl))
53 (require 'nnheader)
54 (require 'gnus-art)
55
56 (defgroup smiley nil
57   "Turn :-)'s into real images."
58   :group 'gnus-visual)
59
60 ;; Maybe this should go.
61 (defcustom smiley-data-directory
62   (nnheader-find-etc-directory "images/smilies")
63   "Location of the smiley faces files."
64   :type 'directory
65   :group 'smiley)
66
67 ;; The XEmacs version has a baroque, if not rococo, set of these.
68 (defcustom smiley-regexp-alist
69   '(("\\(:-?)\\)\\W" 1 "smile")
70     ("\\(;-?)\\)\\W" 1 "blink")
71     ("\\(:-]\\)\\W" 1 "forced")
72     ("\\(8-)\\)\\W" 1 "braindamaged")
73     ("\\(:-|\\)\\W" 1 "indifferent")
74     ("\\(:-[/\\]\\)\\W" 1 "wry")
75     ("\\(:-(\\)\\W" 1 "sad")
76     ("\\(:-{\\)\\W" 1 "frown"))
77   "*A list of regexps to map smilies to images.
78 The elements are (REGEXP MATCH IMAGE), where MATCH is the submatch in
79 regexp to replace with IMAGE.  IMAGE is the name of an image file in
80 `smiley-data-directory'."
81   :type '(repeat (list regexp
82                        (integer :tag "Regexp match number")
83                        (string :tag "Image name")))
84   :set (lambda (symbol value)
85          (set-default symbol value)
86          (smiley-update-cache))
87   :initialize 'custom-initialize-default
88   :group 'smiley)
89
90 (defcustom gnus-smiley-file-types
91   (let ((types (list "pbm")))
92     (when (gnus-image-type-available-p 'xpm)
93       (push "xpm" types))
94     types)
95   "*List of suffixes on smiley file names to try."
96   :version "22.1"
97   :type '(repeat string)
98   :group 'smiley)
99
100 (defvar smiley-cached-regexp-alist nil)
101
102 (defun smiley-update-cache ()
103   (setq smiley-cached-regexp-alist nil)
104   (dolist (elt (if (symbolp smiley-regexp-alist)
105                    (symbol-value smiley-regexp-alist)
106                  smiley-regexp-alist))
107     (let ((types gnus-smiley-file-types)
108           file type)
109       (while (and (not file)
110                   (setq type (pop types)))
111         (unless (file-exists-p
112                  (setq file (expand-file-name (concat (nth 2 elt) "." type)
113                                               smiley-data-directory)))
114           (setq file nil)))
115       (when type
116         (let ((image (gnus-create-image file (intern type) nil
117                                         :ascent 'center)))
118           (when image
119             (push (list (car elt) (cadr elt) image)
120                   smiley-cached-regexp-alist)))))))
121
122 ;; Not implemented:
123 ;; (defvar smiley-mouse-map
124 ;;   (let ((map (make-sparse-keymap)))
125 ;;     (define-key map [down-mouse-2] 'ignore) ; override widget
126 ;;     (define-key map [mouse-2]
127 ;;       'smiley-mouse-toggle-buffer)
128 ;;     map))
129
130 ;;;###autoload
131 (defun smiley-region (start end)
132   "Replace in the region `smiley-regexp-alist' matches with corresponding images.
133 A list of images is returned."
134   (interactive "r")
135   (when (gnus-graphic-display-p)
136     (unless smiley-cached-regexp-alist
137       (smiley-update-cache))
138     (save-excursion
139       (let ((beg (or start (point-min)))
140             group image images string)
141         (dolist (entry smiley-cached-regexp-alist)
142           (setq group (nth 1 entry)
143                 image (nth 2 entry))
144           (goto-char beg)
145           (while (re-search-forward (car entry) end t)
146             (setq string (match-string group))
147             (goto-char (match-end group))
148             (delete-region (match-beginning group) (match-end group))
149             (when image
150               (push image images)
151               (gnus-add-wash-type 'smiley)
152               (gnus-add-image 'smiley image)
153               (gnus-put-image image string 'smiley))))
154         images))))
155
156 ;;;###autoload
157 (defun smiley-buffer (&optional buffer)
158   "Run `smiley-region' at the buffer, specified in the argument or
159 interactively. If there's no argument, do it at the current buffer"
160   (interactive "bBuffer to run smiley-region: ")
161   (save-excursion
162     (if buffer
163         (set-buffer (get-buffer buffer)))
164     (smiley-region (point-min) (point-max))))
165
166 (defun smiley-toggle-buffer (&optional arg)
167   "Toggle displaying smiley faces in article buffer.
168 With arg, turn displaying on if and only if arg is positive."
169   (interactive "P")
170   (gnus-with-article-buffer
171     (if (if (numberp arg)
172             (> arg 0)
173           (not (memq 'smiley gnus-article-wash-types)))
174         (smiley-region (point-min) (point-max))
175       (gnus-delete-images 'smiley))))
176
177 (defun smiley-mouse-toggle-buffer (event)
178   "Toggle displaying smiley faces.
179 With arg, turn displaying on if and only if arg is positive."
180   (interactive "e")
181   (save-excursion
182     (save-window-excursion
183       (mouse-set-point event)
184       (smiley-toggle-buffer))))
185
186 (provide 'smiley)
187
188 ;;; arch-tag: 5beb161b-4321-40af-8ac9-876afb8ee818
189 ;;; smiley.el ends here