* nnmail.el (nnmail-split-it): Revoke the change of 1999-08-19.
[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., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, 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 "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   :type '(repeat string)
141   :group 'smiley)
142
143 (defvar smiley-cached-regexp-alist nil)
144
145 (defun smiley-update-cache ()
146   (dolist (elt (if (symbolp smiley-regexp-alist)
147                    (symbol-value smiley-regexp-alist)
148                  smiley-regexp-alist))
149     (let ((types gnus-smiley-file-types)
150           file type)
151       (while (and (not file)
152                   (setq type (pop types)))
153         (unless (file-exists-p
154                  (setq file (expand-file-name (concat (nth 2 elt) "." type)
155                                               smiley-data-directory)))
156           (setq file nil)))
157       (when type
158         (let ((image (gnus-create-image file (intern type) nil
159                                         :ascent 'center)))
160           (when image
161             (push (list (car elt) (cadr elt) image)
162                   smiley-cached-regexp-alist)))))))
163
164 (defvar smiley-mouse-map
165   (let ((map (make-sparse-keymap)))
166     (define-key map [down-mouse-2] 'ignore) ; override widget
167     (define-key map [mouse-2]
168       'smiley-mouse-toggle-buffer)
169     map))
170
171 ;;;###autoload
172 (defun smiley-region (start end)
173   "Replace in the region `smiley-regexp-alist' matches with corresponding images.
174 A list of images is returned."
175   (interactive "r")
176   (when (gnus-graphic-display-p)
177     (unless smiley-cached-regexp-alist
178       (smiley-update-cache))
179     (save-excursion
180       (let ((beg (or start (point-min)))
181             group image images string)
182         (dolist (entry smiley-cached-regexp-alist)
183           (setq group (nth 1 entry)
184                 image (nth 2 entry))
185           (goto-char beg)
186           (while (re-search-forward (car entry) end t)
187             (goto-char (match-end group))
188             (unless (text-property-any (match-beginning group) (point)
189                                        'smilified t)
190               (setq string (match-string 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                 (put-text-property (point)
197                                    (progn
198                                      (gnus-put-image image string)
199                                      (point))
200                                    'smilified t)))))
201         (put-text-property beg (or end (point-max)) 'smilified nil)
202         images))))
203
204 ;;;###autoload
205 (defun smiley-buffer (&optional buffer)
206   "Run `smiley-region' at the buffer, specified in the argument or
207 interactively. If there's no argument, do it at the current buffer"
208   (interactive "bBuffer to run smiley-region: ")
209   (save-excursion
210     (if buffer
211         (set-buffer (get-buffer buffer)))
212     (smiley-region (point-min) (point-max))))
213
214 (defun smiley-toggle-buffer (&optional arg)
215   "Toggle displaying smiley faces in article buffer.
216 With arg, turn displaying on if and only if arg is positive."
217   (interactive "P")
218   (gnus-with-article-buffer
219     (if (if (numberp arg)
220             (> arg 0)
221           (not (memq 'smiley gnus-article-wash-types)))
222         (smiley-region (point-min) (point-max))
223       (gnus-delete-images 'smiley))))
224
225 (defun smiley-mouse-toggle-buffer (event)
226   "Toggle displaying smiley faces.
227 With arg, turn displaying on if and only if arg is positive."
228   (interactive "e")
229   (save-excursion
230     (save-window-excursion
231       (mouse-set-point event)
232       (smiley-toggle-buffer))))
233
234 (provide 'smiley)
235
236 ;;; smiley.el ends here