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