Revert previous change.
[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 ;; 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 (defvar smiley-data-directory)
62
63 (defcustom smiley-style 'low-color
64   "Smiley style."
65   :type '(choice (const :tag "small, 3 colors" low-color)
66                  (const :tag "medium, ~10 colors" medium)
67                  (const :tag "dull, grayscale" grayscale))
68   :set (lambda (symbol value)
69          (set-default symbol value)
70          (setq smiley-data-directory (smiley-directory))
71          (smiley-update-cache))
72   :initialize 'custom-initialize-default
73   :version "23.0" ;; No Gnus
74   :group 'smiley)
75
76 ;; For compatibility, honor the variable `smiley-data-directory' if the user
77 ;; has set it.
78
79 (defun smiley-directory (&optional style)
80   "Return a the location of the smiley faces files.
81 STYLE specifies which style to use, see `smiley-style'.  If STYLE
82 is nil, use `smiley-style'."
83   (unless style (setq style smiley-style))
84   (nnheader-find-etc-directory
85    (concat "images/smilies"
86            (cond ((eq smiley-style 'low-color) "")
87                  ((eq smiley-style 'medium) "/medium")
88                  ((eq smiley-style 'grayscale) "/grayscale")))))
89
90 (defcustom smiley-data-directory (smiley-directory)
91   "*Location of the smiley faces files."
92   :set (lambda (symbol value)
93          (set-default symbol value)
94          (smiley-update-cache))
95   :initialize 'custom-initialize-default
96   :type 'directory
97   :group 'smiley)
98
99 ;; The XEmacs version has a baroque, if not rococo, set of these.
100 (defcustom smiley-regexp-alist
101   '(("\\(;-?)\\)\\W" 1 "blink")
102     ("\\(:-]\\)\\W" 1 "forced")
103     ("\\(8-)\\)\\W" 1 "braindamaged")
104     ("\\(:-|\\)\\W" 1 "indifferent")
105     ("\\(:-[/\\]\\)\\W" 1 "wry")
106     ("\\(:-(\\)\\W" 1 "sad")
107     ("\\(:-{\\)\\W" 1 "frown")
108     ("\\(>:-)\\)\\W" 1 "evil")
109     ("\\(;-(\\)\\W" 1 "cry")
110     ("\\(X-)\\)\\W" 1 "dead")
111     ("\\(:-D\\)\\W" 1 "grin")
112     ;; "smile" must be come after "evil"
113     ("\\(\\^?:-?)\\)\\W" 1 "smile"))
114   "*A list of regexps to map smilies to images.
115 The elements are (REGEXP MATCH IMAGE), where MATCH is the submatch in
116 regexp to replace with IMAGE.  IMAGE is the name of an image file in
117 `smiley-data-directory'."
118   :type '(repeat (list regexp
119                        (integer :tag "Regexp match number")
120                        (string :tag "Image name")))
121   :set (lambda (symbol value)
122          (set-default symbol value)
123          (smiley-update-cache))
124   :initialize 'custom-initialize-default
125   :group 'smiley)
126
127 (defcustom gnus-smiley-file-types
128   (let ((types (list "pbm")))
129     (when (gnus-image-type-available-p 'xpm)
130       (push "xpm" types))
131     types)
132   "*List of suffixes on smiley file names to try."
133   :version "22.1"
134   :type '(repeat string)
135   :group 'smiley)
136
137 (defvar smiley-cached-regexp-alist nil)
138
139 (defun smiley-update-cache ()
140   (setq smiley-cached-regexp-alist nil)
141   (dolist (elt (if (symbolp smiley-regexp-alist)
142                    (symbol-value smiley-regexp-alist)
143                  smiley-regexp-alist))
144     (let ((types gnus-smiley-file-types)
145           file type)
146       (while (and (not file)
147                   (setq type (pop types)))
148         (unless (file-exists-p
149                  (setq file (expand-file-name (concat (nth 2 elt) "." type)
150                                               smiley-data-directory)))
151           (setq file nil)))
152       (when type
153         (let ((image (gnus-create-image file (intern type) nil
154                                         :ascent 'center)))
155           (when image
156             (push (list (car elt) (cadr elt) image)
157                   smiley-cached-regexp-alist)))))))
158
159 ;; Not implemented:
160 ;; (defvar smiley-mouse-map
161 ;;   (let ((map (make-sparse-keymap)))
162 ;;     (define-key map [down-mouse-2] 'ignore) ; override widget
163 ;;     (define-key map [mouse-2]
164 ;;       'smiley-mouse-toggle-buffer)
165 ;;     map))
166
167 ;;;###autoload
168 (defun smiley-region (start end)
169   "Replace in the region `smiley-regexp-alist' matches with corresponding images.
170 A list of images is returned."
171   (interactive "r")
172   (when (gnus-graphic-display-p)
173     (unless smiley-cached-regexp-alist
174       (smiley-update-cache))
175     (save-excursion
176       (let ((beg (or start (point-min)))
177             group image images string)
178         (dolist (entry smiley-cached-regexp-alist)
179           (setq group (nth 1 entry)
180                 image (nth 2 entry))
181           (goto-char beg)
182           (while (re-search-forward (car entry) end t)
183             (setq string (match-string group))
184             (goto-char (match-end group))
185             (delete-region (match-beginning group) (match-end group))
186             (when image
187               (push image images)
188               (gnus-add-wash-type 'smiley)
189               (gnus-add-image 'smiley image)
190               (gnus-put-image image string 'smiley))))
191         images))))
192
193 ;;;###autoload
194 (defun smiley-buffer (&optional buffer)
195   "Run `smiley-region' at the buffer, specified in the argument or
196 interactively. If there's no argument, do it at the current buffer"
197   (interactive "bBuffer to run smiley-region: ")
198   (save-excursion
199     (if buffer
200         (set-buffer (get-buffer buffer)))
201     (smiley-region (point-min) (point-max))))
202
203 (defun smiley-toggle-buffer (&optional arg)
204   "Toggle displaying smiley faces in article buffer.
205 With arg, turn displaying on if and only if arg is positive."
206   (interactive "P")
207   (gnus-with-article-buffer
208     (if (if (numberp arg)
209             (> arg 0)
210           (not (memq 'smiley gnus-article-wash-types)))
211         (smiley-region (point-min) (point-max))
212       (gnus-delete-images 'smiley))))
213
214 (defun smiley-mouse-toggle-buffer (event)
215   "Toggle displaying smiley faces.
216 With arg, turn displaying on if and only if arg is positive."
217   (interactive "e")
218   (save-excursion
219     (save-window-excursion
220       (mouse-set-point event)
221       (smiley-toggle-buffer))))
222
223 (provide 'smiley)
224
225 ;;; arch-tag: 5beb161b-4321-40af-8ac9-876afb8ee818
226 ;;; smiley.el ends here