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