Synch to No Gnus 200601131944.
[elisp/gnus.git-] / lisp / smiley.el
1 ;;; smiley.el --- displaying smiley faces
2
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;;   2005 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
35 ;;; Test smileys:  :-) :-\ :-( :-/
36
37 ;;; Code:
38
39 (eval-when-compile (require 'cl))
40 (require 'nnheader)
41 (require 'gnus-art)
42
43 (defgroup smiley nil
44   "Turn :-)'s into real images."
45   :group 'gnus-visual)
46
47 ;; Maybe this should go.
48 (defcustom smiley-data-directory (nnheader-find-etc-directory "images/smilies")
49   "*Location of the smiley faces files."
50   :type 'directory
51   :group 'smiley)
52
53 ;; The XEmacs version has a baroque, if not rococo, set of these.
54 (defcustom smiley-regexp-alist
55   (if (file-exists-p (expand-file-name "WideFaceSmile.xbm"
56                                        smiley-data-directory))
57       ;; Use faces in ftp://ftp.gnus.org/pub/gnus/etc-0.27.tar.gz
58       '(;; ^_^ ^^
59         ("\\(\\^_?\\^\\)\\W" 1 "WideFaceSmile")
60         ;; ;-> ;-) ;-} ;> ;) :}
61         ("\\(;-?[>)}]+\\)\\W" 1 "FaceWinking")
62         ;; ^_^; ^^;
63         ("\\(\\^_?\\^;\\)\\W" 1 "WideFaceAse1")
64         ;; ^_^;; ^^;;
65         ("\\(\\^_?\\^;;\\)\\W" 1 "WideFaceAse2")
66         ;; ^_^;;; ^^;;;
67         ("\\(\\^_?\\^;;;\\)\\W" 1 "WideFaceAse3")
68         ;; ;_;
69         ("\\(;_;\\)\\W" 1 "WideFaceWeep")
70         ;; T_T
71         ("\\(T_T\\)\\W" 1 "WideFaceWeep")
72         ;; >_<
73         ("\\(>_<\\)\\W" 1 "WideFaceWeep")
74         ;; :-< :<
75         ("\\(:-?<\\)\\W" 1 "FaceAngry")
76         ;; :-] :]
77         ("\\(:-?\\]+\\)\\W" 1 "FaceGoofy")
78         ;; :-D :D
79         ("\\(:-?D\\)\\W" 1 "FaceGrinning")
80         ;; :-) :-> :-} :) :> :}
81         ("\\(:-?[)>}]+\\)\\W" 1 "FaceHappy")
82         ;; =)
83         ("\\(=)\\)\\W" 1 "FaceHappy")
84         ;; :-/ :-\ :/ :\  excludes urls etc.
85         ("\\(:-[/\\]\\)\\W" 1 "FaceIronic")
86         ("\\(:/\\)\\([\t\n ]\\|[^/]\\W\\)" 1 "FaceIronic")
87         ("\\(:\\\\\\)\\([\t\n ]\\|[^\\]\\W\\)" 1 "FaceIronic")
88         ;; 8-| 8-O 8-%
89         ;; excludes just numbers
90         ("[^.0-9]\\(8-[|O%]\\)\\W" 1 "FaceKOed")
91         ;; :-# :#
92         ("\\(:-?#\\)\\W" 1 "FaceNyah")
93         ;; :-( :-{ :( :{
94         ("\\(:-?[({]+\\)\\W" 1 "FaceSad")
95         ;; =( ={
96         ("\\(=[({]+\\)\\W" 1 "FaceSad")
97         ;; :-O :-o :O :o
98         ("\\(:-?[Oo]\\)\\W" 1 "FaceStartled")
99         ;; :-| :|
100         ("\\(:-?|\\)\\W" 1 "FaceStraight")
101         ;; :-p :p
102         ("\\(:-?p\\)\\W" 1 "FaceTalking")
103         ;; :-d
104         ("\\(:-d\\)\\W" 1 "FaceTasty")
105         ;; :-V :-v :V :v
106         ("\\(:-?[Vv]\\)\\W" 1 "FaceWry")
107         ;; :-P :P
108         ("\\(:-?P\\)\\W" 1 "FaceYukky")
109         ;; ]:-) ]:-> ]:-} ]8-) ]8-> ]8-} ]B-) ]B-> ]B-}
110         ;; ]:) ]:> ]:} ]8) ]8> ]8} ]B) ]B> ]B}
111         ("\\(\\][:8B]-?[)>}]\\)\\W" 1 "FaceDevilish"))
112     '(("\\(:-?)\\)\\W" 1 "smile")
113       ("\\(;-?)\\)\\W" 1 "blink")
114       ("\\(:-]\\)\\W" 1 "forced")
115       ("\\(8-)\\)\\W" 1 "braindamaged")
116       ("\\(:-|\\)\\W" 1 "indifferent")
117       ("\\(:-[/\\]\\)\\W" 1 "wry")
118       ("\\(:-(\\)\\W" 1 "sad")
119       ("\\(:-{\\)\\W" 1 "frown")))
120   "*A list of regexps to map smilies to images.
121 The elements are (REGEXP MATCH FILE), where MATCH is the submatch in
122 regexp to replace with IMAGE.  IMAGE is the name of a PBM 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     (when (gnus-image-type-available-p 'xbm)
138       (push "xbm" types))
139     types)
140   "*List of suffixes on picon file names to try."
141   :version "22.1"
142   :type '(repeat string)
143   :group 'smiley)
144
145 (defvar smiley-cached-regexp-alist nil)
146
147 (defun smiley-update-cache ()
148   (dolist (elt (if (symbolp smiley-regexp-alist)
149                    (symbol-value smiley-regexp-alist)
150                  smiley-regexp-alist))
151     (let ((types gnus-smiley-file-types)
152           file type)
153       (while (and (not file)
154                   (setq type (pop types)))
155         (unless (file-exists-p
156                  (setq file (expand-file-name (concat (nth 2 elt) "." type)
157                                               smiley-data-directory)))
158           (setq file nil)))
159       (when type
160         (let ((image (gnus-create-image file (intern type) nil
161                                         :ascent 'center)))
162           (when image
163             (push (list (car elt) (cadr elt) image)
164                   smiley-cached-regexp-alist)))))))
165
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             (goto-char (match-end group))
190             (unless (text-property-any (match-beginning group) (point)
191                                        'smilified t)
192               (setq string (match-string group))
193               (delete-region (match-beginning group) (match-end group))
194               (when image
195                 (push image images)
196                 (gnus-add-wash-type 'smiley)
197                 (gnus-add-image 'smiley image)
198                 (put-text-property (point)
199                                    (progn
200                                      (gnus-put-image image string 'smiley)
201                                      (point))
202                                    'smilified t)))))
203         (put-text-property beg (or end (point-max)) 'smilified nil)
204         images))))
205
206 ;;;###autoload
207 (defun smiley-buffer (&optional buffer)
208   "Run `smiley-region' at the buffer, specified in the argument or
209 interactively. If there's no argument, do it at the current buffer"
210   (interactive "bBuffer to run smiley-region: ")
211   (save-excursion
212     (if buffer
213         (set-buffer (get-buffer buffer)))
214     (smiley-region (point-min) (point-max))))
215
216 (defun smiley-toggle-buffer (&optional arg)
217   "Toggle displaying smiley faces in article buffer.
218 With arg, turn displaying on if and only if arg is positive."
219   (interactive "P")
220   (gnus-with-article-buffer
221     (if (if (numberp arg)
222             (> arg 0)
223           (not (memq 'smiley gnus-article-wash-types)))
224         (smiley-region (point-min) (point-max))
225       (gnus-delete-images 'smiley))))
226
227 (defun smiley-mouse-toggle-buffer (event)
228   "Toggle displaying smiley faces.
229 With arg, turn displaying on if and only if arg is positive."
230   (interactive "e")
231   (save-excursion
232     (save-window-excursion
233       (mouse-set-point event)
234       (smiley-toggle-buffer))))
235
236 (provide 'smiley)
237
238 ;;; smiley.el ends here