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