(gnus-uu-default-view-rules): Don't use `xv'.
[bpt/emacs.git] / lisp / gnus / earcon.el
1 ;;; earcon.el --- Sound effects for messages
2 ;; Copyright (C) 1996 Free Software Foundation
3
4 ;; Author: Steven L. Baur <steve@miranova.com>
5 ;; GNU Emacs is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
8 ;; any later version.
9
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
14
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with GNU Emacs; see the file COPYING. If not, write to the
17 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
19
20 ;;; Commentary:
21 ;; This file provides access to sound effects in Gnus.
22
23 ;;; Code:
24
25 (if (null (boundp 'running-xemacs))
26 (defvar running-xemacs (string-match "XEmacs\\|Lucid" emacs-version)))
27
28 (eval-when-compile (require 'cl))
29 (require 'gnus)
30 (require 'gnus-audio)
31 (require 'gnus-art)
32
33 (defgroup earcon nil
34 "Turn ** sounds ** into noise."
35 :group 'gnus-visual)
36
37 (defcustom earcon-auto-play nil
38 "*When True, automatically play sounds as well as buttonize them."
39 :type 'boolean
40 :group 'earcon)
41
42 (defcustom earcon-prefix "**"
43 "*String denoting the start of an earcon."
44 :type 'string
45 :group 'earcon)
46
47 (defcustom earcon-suffix "**"
48 "String denoting the end of an earcon."
49 :type 'string
50 :group 'earcon)
51
52 (defcustom earcon-regexp-alist
53 '(("boring" 1 "Boring.au")
54 ("evil[ \t]+laugh" 1 "Evil_Laugh.au")
55 ("gag\\|puke" 1 "Puke.au")
56 ("snicker" 1 "Snicker.au")
57 ("meow" 1 "catmeow.au")
58 ("sob\\|boohoo" 1 "cry.wav")
59 ("drum[ \t]*roll" 1 "drumroll.au")
60 ("blast" 1 "explosion.au")
61 ("flush\\|plonk!*" 1 "flush.au")
62 ("kiss" 1 "kiss.wav")
63 ("tee[ \t]*hee" 1 "laugh.au")
64 ("shoot" 1 "shotgun.wav")
65 ("yawn" 1 "snore.wav")
66 ("cackle" 1 "witch.au")
67 ("yell\\|roar" 1 "yell2.au")
68 ("whoop-de-doo" 1 "whistle.au"))
69 "*A list of regexps to map earcons to real sounds."
70 :type '(repeat (list regexp
71 (integer :tag "Match")
72 (string :tag "Sound")))
73 :group 'earcon)
74 (defvar earcon-button-marker-list nil)
75 (make-variable-buffer-local 'earcon-button-marker-list)
76
77
78
79 ;;; FIXME!! clone of code from gnus-vis.el FIXME!!
80 (defun earcon-article-push-button (event)
81 "Check text under the mouse pointer for a callback function.
82 If the text under the mouse pointer has a `earcon-callback' property,
83 call it with the value of the `earcon-data' text property."
84 (interactive "e")
85 (set-buffer (window-buffer (posn-window (event-start event))))
86 (let* ((pos (posn-point (event-start event)))
87 (data (get-text-property pos 'earcon-data))
88 (fun (get-text-property pos 'earcon-callback)))
89 (if fun (funcall fun data))))
90
91 (defun earcon-article-press-button ()
92 "Check text at point for a callback function.
93 If the text at point has a `earcon-callback' property,
94 call it with the value of the `earcon-data' text property."
95 (interactive)
96 (let* ((data (get-text-property (point) 'earcon-data))
97 (fun (get-text-property (point) 'earcon-callback)))
98 (if fun (funcall fun data))))
99
100 (defun earcon-article-prev-button (n)
101 "Move point to N buttons backward.
102 If N is negative, move forward instead."
103 (interactive "p")
104 (earcon-article-next-button (- n)))
105
106 (defun earcon-article-next-button (n)
107 "Move point to N buttons forward.
108 If N is negative, move backward instead."
109 (interactive "p")
110 (let ((function (if (< n 0) 'previous-single-property-change
111 'next-single-property-change))
112 (inhibit-point-motion-hooks t)
113 (backward (< n 0))
114 (limit (if (< n 0) (point-min) (point-max))))
115 (setq n (abs n))
116 (while (and (not (= limit (point)))
117 (> n 0))
118 ;; Skip past the current button.
119 (when (get-text-property (point) 'earcon-callback)
120 (goto-char (funcall function (point) 'earcon-callback nil limit)))
121 ;; Go to the next (or previous) button.
122 (gnus-goto-char (funcall function (point) 'earcon-callback nil limit))
123 ;; Put point at the start of the button.
124 (when (and backward (not (get-text-property (point) 'earcon-callback)))
125 (goto-char (funcall function (point) 'earcon-callback nil limit)))
126 ;; Skip past intangible buttons.
127 (when (get-text-property (point) 'intangible)
128 (incf n))
129 (decf n))
130 (unless (zerop n)
131 (gnus-message 5 "No more buttons"))
132 n))
133
134 (defun earcon-article-add-button (from to fun &optional data)
135 "Create a button between FROM and TO with callback FUN and data DATA."
136 (and (boundp gnus-article-button-face)
137 gnus-article-button-face
138 (gnus-overlay-put (gnus-make-overlay from to)
139 'face gnus-article-button-face))
140 (gnus-add-text-properties
141 from to
142 (nconc (and gnus-article-mouse-face
143 (list gnus-mouse-face-prop gnus-article-mouse-face))
144 (list 'gnus-callback fun)
145 (and data (list 'gnus-data data)))))
146
147 (defun earcon-button-entry ()
148 ;; Return the first entry in `gnus-button-alist' matching this place.
149 (let ((alist earcon-regexp-alist)
150 (case-fold-search t)
151 (entry nil))
152 (while alist
153 (setq entry (pop alist))
154 (if (looking-at (car entry))
155 (setq alist nil)
156 (setq entry nil)))
157 entry))
158
159
160 (defun earcon-button-push (marker)
161 ;; Push button starting at MARKER.
162 (save-excursion
163 (set-buffer gnus-article-buffer)
164 (goto-char marker)
165 (let* ((entry (earcon-button-entry))
166 (inhibit-point-motion-hooks t)
167 (fun 'gnus-audio-play)
168 (args (list (nth 2 entry))))
169 (cond
170 ((fboundp fun)
171 (apply fun args))
172 ((and (boundp fun)
173 (fboundp (symbol-value fun)))
174 (apply (symbol-value fun) args))
175 (t
176 (gnus-message 1 "You must define `%S' to use this button"
177 (cons fun args)))))))
178
179 ;;; FIXME!! clone of code from gnus-vis.el FIXME!!
180
181 ;;;###interactive
182 (defun earcon-region (beg end)
183 "Play Sounds in the region between point and mark."
184 (interactive "r")
185 (earcon-buffer (current-buffer) beg end))
186
187 ;;;###interactive
188 (defun earcon-buffer (&optional buffer st nd)
189 (interactive)
190 (save-excursion
191 ;; clear old markers.
192 (if (boundp 'earcon-button-marker-list)
193 (while earcon-button-marker-list
194 (set-marker (pop earcon-button-marker-list) nil))
195 (setq earcon-button-marker-list nil))
196 (and buffer (set-buffer buffer))
197 (let ((buffer-read-only nil)
198 (inhibit-point-motion-hooks t)
199 (case-fold-search t)
200 (alist earcon-regexp-alist)
201 beg entry regexp)
202 (goto-char (point-min))
203 (setq beg (point))
204 (while (setq entry (pop alist))
205 (setq regexp (concat (regexp-quote earcon-prefix)
206 ".*\\("
207 (car entry)
208 "\\).*"
209 (regexp-quote earcon-suffix)))
210 (goto-char beg)
211 (while (re-search-forward regexp nil t)
212 (let* ((start (and entry (match-beginning 1)))
213 (end (and entry (match-end 1)))
214 (from (match-beginning 1)))
215 (earcon-article-add-button
216 start end 'earcon-button-push
217 (car (push (set-marker (make-marker) from)
218 earcon-button-marker-list)))
219 (gnus-audio-play (caddr entry))))))))
220
221 ;;;###autoload
222 (defun gnus-earcon-display ()
223 "Play sounds in message buffers."
224 (interactive)
225 (save-excursion
226 (set-buffer gnus-article-buffer)
227 (goto-char (point-min))
228 ;; Skip headers
229 (unless (search-forward "\n\n" nil t)
230 (goto-char (point-max)))
231 (sit-for 0)
232 (earcon-buffer (current-buffer) (point))))
233
234 ;;;***
235
236 (provide 'earcon)
237
238 (run-hooks 'earcon-load-hook)
239
240 ;;; earcon.el ends here