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