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