Merge branch 'master' into comment-cache
[emacs.git] / lisp / gnus / smiley.el
blob763a1cd5be724b4f3321aae35dd9b9a1cbcd71ff
1 ;;; smiley.el --- displaying smiley faces
3 ;; Copyright (C) 2000-2017 Free Software Foundation, Inc.
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Keywords: news mail multimedia
8 ;; This file is part of GNU Emacs.
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 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; A re-written, simplified version of Wes Hardaker's XEmacs smiley.el
26 ;; which might be merged back to smiley.el if we get an assignment for
27 ;; that. We don't have assignments for the images smiley.el uses, but
28 ;; I'm not sure we need that degree of rococoness and defaults like a
29 ;; yellow background. Also, using PBM means we can display the images
30 ;; more generally. -- fx
31 ;; `smiley.el' was replaced by `smiley-ems.el' on 2002-01-26 (after fx'
32 ;; comment).
34 ;; Test smileys:
35 ;; smile ^:-) ^:)
36 ;; blink ;-) ;)
37 ;; forced :-]
38 ;; braindamaged 8-)
39 ;; indifferent :-|
40 ;; wry :-/ :-\
41 ;; sad :-(
42 ;; frown :-{
43 ;; evil >:-)
44 ;; cry ;-(
45 ;; dead X-)
46 ;; grin :-D
48 ;;; Code:
50 (eval-when-compile (require 'cl))
51 (require 'nnheader)
52 (require 'gnus-art)
54 (defgroup smiley nil
55 "Turn :-)'s into real images."
56 :group 'gnus-visual)
58 (defvar smiley-data-directory)
60 (defcustom smiley-style
61 (if (and (fboundp 'face-attribute)
62 ;; In batch mode, attributes can be unspecified.
63 (condition-case nil
64 (>= (face-attribute 'default :height) 160)
65 (error nil)))
66 'medium
67 'low-color)
68 "Smiley style."
69 :type '(choice (const :tag "small, 3 colors" low-color) ;; 13x14
70 (const :tag "medium, ~10 colors" medium) ;; 16x16
71 (const :tag "dull, grayscale" grayscale)) ;; 14x14
72 :set (lambda (symbol value)
73 (set-default symbol value)
74 (setq smiley-data-directory (smiley-directory))
75 (smiley-update-cache))
76 :initialize 'custom-initialize-default
77 :version "23.1" ;; No Gnus
78 :group 'smiley)
80 ;; For compatibility, honor the variable `smiley-data-directory' if the user
81 ;; has set it.
83 (defun smiley-directory (&optional style)
84 "Return a the location of the smiley faces files.
85 STYLE specifies which style to use, see `smiley-style'. If STYLE
86 is nil, use `smiley-style'."
87 (unless style (setq style smiley-style))
88 (nnheader-find-etc-directory
89 (concat "images/smilies"
90 (cond ((eq smiley-style 'low-color) "")
91 ((eq smiley-style 'medium) "/medium")
92 ((eq smiley-style 'grayscale) "/grayscale")))))
94 (defcustom smiley-data-directory (smiley-directory)
95 "Location of the smiley faces files."
96 :set (lambda (symbol value)
97 (set-default symbol value)
98 (smiley-update-cache))
99 :initialize 'custom-initialize-default
100 :type 'directory
101 :group 'smiley)
103 ;; The XEmacs version has a baroque, if not rococo, set of these.
104 (defcustom smiley-regexp-alist
105 '(("\\(;-)\\)\\W" 1 "blink")
106 ("[^;]\\(;)\\)\\W" 1 "blink")
107 ("\\(:-]\\)\\W" 1 "forced")
108 ("\\(8-)\\)\\W" 1 "braindamaged")
109 ("\\(:-|\\)\\W" 1 "indifferent")
110 ("\\(:-[/\\]\\)\\W" 1 "wry")
111 ("\\(:-(\\)\\W" 1 "sad")
112 ("\\(X-)\\)\\W" 1 "dead")
113 ("\\(:-{\\)\\W" 1 "frown")
114 ("\\(>:-)\\)\\W" 1 "evil")
115 ("\\(;-(\\)\\W" 1 "cry")
116 ("\\(:-D\\)\\W" 1 "grin")
117 ;; "smile" must be come after "evil"
118 ("\\(\\^?:-?)\\)\\W" 1 "smile"))
119 "A list of regexps to map smilies to images.
120 The elements are (REGEXP MATCH IMAGE), where MATCH is the submatch in
121 regexp to replace with IMAGE. IMAGE is the name of an image file in
122 `smiley-data-directory'."
123 :version "24.1"
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)
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 'gif)
138 (push "gif" types))
139 types)
140 "List of suffixes on smiley file names to try."
141 :version "24.1"
142 :type '(repeat string)
143 :group 'smiley)
145 (defvar smiley-cached-regexp-alist nil)
147 (defun smiley-update-cache ()
148 (setq smiley-cached-regexp-alist nil)
149 (dolist (elt (if (symbolp smiley-regexp-alist)
150 (symbol-value smiley-regexp-alist)
151 smiley-regexp-alist))
152 (let ((types gnus-smiley-file-types)
153 file type)
154 (while (and (not file)
155 (setq type (pop types)))
156 (unless (file-exists-p
157 (setq file (expand-file-name (concat (nth 2 elt) "." type)
158 smiley-data-directory)))
159 (setq file nil)))
160 (when type
161 (let ((image (gnus-create-image file (intern type) nil
162 :ascent 'center)))
163 (when image
164 (push (list (car elt) (cadr elt) image)
165 smiley-cached-regexp-alist)))))))
167 ;; Not implemented:
168 ;; (defvar smiley-mouse-map
169 ;; (let ((map (make-sparse-keymap)))
170 ;; (define-key map [down-mouse-2] 'ignore) ; override widget
171 ;; (define-key map [mouse-2]
172 ;; 'smiley-mouse-toggle-buffer)
173 ;; map))
175 ;;;###autoload
176 (defun smiley-region (start end)
177 "Replace in the region `smiley-regexp-alist' matches with corresponding images.
178 A list of images is returned."
179 (interactive "r")
180 (when (display-graphic-p)
181 (unless smiley-cached-regexp-alist
182 (smiley-update-cache))
183 (save-excursion
184 (let ((beg (or start (point-min)))
185 group image images string)
186 (dolist (entry smiley-cached-regexp-alist)
187 (setq group (nth 1 entry)
188 image (nth 2 entry))
189 (goto-char beg)
190 (while (re-search-forward (car entry) end t)
191 (setq string (match-string group))
192 (goto-char (match-end 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 (gnus-put-image image string 'smiley))))
199 images))))
201 ;;;###autoload
202 (defun smiley-buffer (&optional buffer)
203 "Run `smiley-region' at the BUFFER, specified in the argument or
204 interactively. If there's no argument, do it at the current buffer."
205 (interactive "bBuffer to run smiley-region: ")
206 (save-excursion
207 (if buffer
208 (set-buffer (get-buffer buffer)))
209 (smiley-region (point-min) (point-max))))
211 (defun smiley-toggle-buffer (&optional arg)
212 "Toggle displaying smiley faces in article buffer.
213 With arg, turn displaying on if and only if arg is positive."
214 (interactive "P")
215 (gnus-with-article-buffer
216 (if (if (numberp arg)
217 (> arg 0)
218 (not (memq 'smiley gnus-article-wash-types)))
219 (smiley-region (point-min) (point-max))
220 (gnus-delete-images 'smiley))))
222 (defun smiley-mouse-toggle-buffer (event)
223 "Toggle displaying smiley faces.
224 With arg, turn displaying on if and only if arg is positive."
225 (interactive "e")
226 (save-excursion
227 (save-window-excursion
228 (mouse-set-point event)
229 (smiley-toggle-buffer))))
231 (provide 'smiley)
233 ;;; smiley.el ends here