Synch to No Gnus 200507290604.
[elisp/gnus.git-] / lisp / smiley.el
1 ;;; smiley.el --- displaying smiley faces
2
3 ;; Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Keywords: news mail multimedia
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; A re-written, simplified version of Wes Hardaker's XEmacs smiley.el
28 ;; which might be merged back to smiley.el if we get an assignment for
29 ;; that.  We don't have assignments for the images smiley.el uses, but
30 ;; I'm not sure we need that degree of rococoness and defaults like a
31 ;; yellow background.  Also, using PBM means we can display the images
32 ;; more generally.  -- fx
33
34 ;;; Test smileys:  :-) :-\ :-( :-/
35
36 ;;; Code:
37
38 (eval-when-compile (require 'cl))
39 (require 'nnheader)
40 (require 'gnus-art)
41
42 (defgroup smiley nil
43   "Turn :-)'s into real images."
44   :group 'gnus-visual)
45
46 ;; Maybe this should go.
47 (defcustom smiley-data-directory (nnheader-find-etc-directory "images/smilies")
48   "*Location of the smiley faces files."
49   :type 'directory
50   :group 'smiley)
51
52 ;; The XEmacs version has a baroque, if not rococo, set of these.
53 (defcustom smiley-regexp-alist
54   (if (file-exists-p (expand-file-name "WideFaceSmile.xbm"
55                                        smiley-data-directory))
56       ;; Use faces in ftp://ftp.gnus.org/pub/gnus/etc-0.27.tar.gz
57       '(;; ^_^ ^^
58         ("\\(\\^_?\\^\\)\\W" 1 "WideFaceSmile")
59         ;; ;-> ;-) ;-} ;> ;) :}
60         ("\\(;-?[>)}]+\\)\\W" 1 "FaceWinking")
61         ;; ^_^; ^^;
62         ("\\(\\^_?\\^;\\)\\W" 1 "WideFaceAse1")
63         ;; ^_^;; ^^;;
64         ("\\(\\^_?\\^;;\\)\\W" 1 "WideFaceAse2")
65         ;; ^_^;;; ^^;;;
66         ("\\(\\^_?\\^;;;\\)\\W" 1 "WideFaceAse3")
67         ;; ;_;
68         ("\\(;_;\\)\\W" 1 "WideFaceWeep")
69         ;; T_T
70         ("\\(T_T\\)\\W" 1 "WideFaceWeep")
71         ;; >_<
72         ("\\(>_<\\)\\W" 1 "WideFaceWeep")
73         ;; :-< :<
74         ("\\(:-?<\\)\\W" 1 "FaceAngry")
75         ;; :-] :]
76         ("\\(:-?\\]+\\)\\W" 1 "FaceGoofy")
77         ;; :-D :D
78         ("\\(:-?D\\)\\W" 1 "FaceGrinning")
79         ;; :-) :-> :-} :) :> :}
80         ("\\(:-?[)>}]+\\)\\W" 1 "FaceHappy")
81         ;; =)
82         ("\\(=)\\)\\W" 1 "FaceHappy")
83         ;; :-/ :-\ :/ :\  excludes urls etc.
84         ("\\(:-[/\\]\\)\\W" 1 "FaceIronic")
85         ("\\(:/\\)\\([\t\n ]\\|[^/]\\W\\)" 1 "FaceIronic")
86         ("\\(:\\\\\\)\\([\t\n ]\\|[^\\]\\W\\)" 1 "FaceIronic")
87         ;; 8-| 8-O 8-%
88         ;; excludes just numbers
89         ("[^.0-9]\\(8-[|O%]\\)\\W" 1 "FaceKOed")
90         ;; :-# :#
91         ("\\(:-?#\\)\\W" 1 "FaceNyah")
92         ;; :-( :-{ :( :{
93         ("\\(:-?[({]+\\)\\W" 1 "FaceSad")
94         ;; =( ={
95         ("\\(=[({]+\\)\\W" 1 "FaceSad")
96         ;; :-O :-o :O :o
97         ("\\(:-?[Oo]\\)\\W" 1 "FaceStartled")
98         ;; :-| :|
99         ("\\(:-?|\\)\\W" 1 "FaceStraight")
100         ;; :-p :p
101         ("\\(:-?p\\)\\W" 1 "FaceTalking")
102         ;; :-d
103         ("\\(:-d\\)\\W" 1 "FaceTasty")
104         ;; :-V :-v :V :v
105         ("\\(:-?[Vv]\\)\\W" 1 "FaceWry")
106         ;; :-P :P
107         ("\\(:-?P\\)\\W" 1 "FaceYukky")
108         ;; ]:-) ]:-> ]:-} ]8-) ]8-> ]8-} ]B-) ]B-> ]B-}
109         ;; ]:) ]:> ]:} ]8) ]8> ]8} ]B) ]B> ]B}
110         ("\\(\\][:8B]-?[)>}]\\)\\W" 1 "FaceDevilish"))
111     '(("\\(:-?)\\)\\W" 1 "smile")
112       ("\\(;-?)\\)\\W" 1 "blink")
113       ("\\(:-]\\)\\W" 1 "forced")
114       ("\\(8-)\\)\\W" 1 "braindamaged")
115       ("\\(:-|\\)\\W" 1 "indifferent")
116       ("\\(:-[/\\]\\)\\W" 1 "wry")
117       ("\\(:-(\\)\\W" 1 "sad")
118       ("\\(:-{\\)\\W" 1 "frown")))
119   "*A list of regexps to map smilies to images.
120 The elements are (REGEXP MATCH FILE), where MATCH is the submatch in
121 regexp to replace with IMAGE.  IMAGE is the name of a PBM file in
122 `smiley-data-directory'."
123   :type '(repeat (list regexp
124                        (integer :tag "Regexp match number")
125                        (string :tag "Image name")))
126   :set (lambda (symbol value)
127          (set-default symbol value)
128          (smiley-update-cache))
129   :initialize 'custom-initialize-default
130   :group 'smiley)
131
132 (defcustom gnus-smiley-file-types
133   (let ((types (list "pbm")))
134     (when (gnus-image-type-available-p 'xpm)
135       (push "xpm" types))
136     (when (gnus-image-type-available-p 'xbm)
137       (push "xbm" types))
138     types)
139   "*List of suffixes on picon file names to try."
140   :version "22.1"
141   :type '(repeat string)
142   :group 'smiley)
143
144 (defvar smiley-cached-regexp-alist nil)
145
146 (defun smiley-update-cache ()
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 (defvar smiley-mouse-map
166   (let ((map (make-sparse-keymap)))
167     (define-key map [down-mouse-2] 'ignore) ; override widget
168     (define-key map [mouse-2]
169       'smiley-mouse-toggle-buffer)
170     map))
171
172 ;;;###autoload
173 (defun smiley-region (start end)
174   "Replace in the region `smiley-regexp-alist' matches with corresponding images.
175 A list of images is returned."
176   (interactive "r")
177   (when (gnus-graphic-display-p)
178     (unless smiley-cached-regexp-alist
179       (smiley-update-cache))
180     (save-excursion
181       (let ((beg (or start (point-min)))
182             group image images string)
183         (dolist (entry smiley-cached-regexp-alist)
184           (setq group (nth 1 entry)
185                 image (nth 2 entry))
186           (goto-char beg)
187           (while (re-search-forward (car entry) end t)
188             (goto-char (match-end group))
189             (unless (text-property-any (match-beginning group) (point)
190                                        'smilified t)
191               (setq string (match-string group))
192               (delete-region (match-beginning group) (match-end group))
193               (when image
194                 (push image images)
195                 (gnus-add-wash-type 'smiley)
196                 (gnus-add-image 'smiley image)
197                 (put-text-property (point)
198                                    (progn
199                                      (gnus-put-image image string 'smiley)
200                                      (point))
201                                    'smilified t)))))
202         (put-text-property beg (or end (point-max)) 'smilified nil)
203         images))))
204
205 ;;;###autoload
206 (defun smiley-buffer (&optional buffer)
207   "Run `smiley-region' at the buffer, specified in the argument or
208 interactively. If there's no argument, do it at the current buffer"
209   (interactive "bBuffer to run smiley-region: ")
210   (save-excursion
211     (if buffer
212         (set-buffer (get-buffer buffer)))
213     (smiley-region (point-min) (point-max))))
214
215 (defun smiley-toggle-buffer (&optional arg)
216   "Toggle displaying smiley faces in article buffer.
217 With arg, turn displaying on if and only if arg is positive."
218   (interactive "P")
219   (gnus-with-article-buffer
220     (if (if (numberp arg)
221             (> arg 0)
222           (not (memq 'smiley gnus-article-wash-types)))
223         (smiley-region (point-min) (point-max))
224       (gnus-delete-images 'smiley))))
225
226 (defun smiley-mouse-toggle-buffer (event)
227   "Toggle displaying smiley faces.
228 With arg, turn displaying on if and only if arg is positive."
229   (interactive "e")
230   (save-excursion
231     (save-window-excursion
232       (mouse-set-point event)
233       (smiley-toggle-buffer))))
234
235 (provide 'smiley)
236
237 ;;; smiley.el ends here