*** empty log message ***
[gnus] / lisp / 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 ;; Keywords: news fun sound
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 (if (null (boundp 'running-xemacs))
30     (defvar running-xemacs (string-match "XEmacs\\|Lucid" emacs-version)))
31
32 (require 'gnus)
33 (require 'gnus-audio)
34 (require 'gnus-art)
35 (eval-when-compile (require 'cl))
36
37 (defvar earcon-auto-play nil
38   "When True, automatically play sounds as well as buttonize them.")
39
40 (defvar earcon-prefix "**"
41   "The start of an earcon")
42
43 (defvar earcon-suffix "**"
44   "The end of an earcon")
45
46 (defvar earcon-regexp-alist
47   '(("boring" 1 "Boring.au")
48     ("evil[ \t]+laugh" 1 "Evil_Laugh.au")
49     ("gag\\|puke" 1 "Puke.au")
50     ("snicker" 1 "Snicker.au")
51     ("meow" 1 "catmeow.au")
52     ("sob\\|boohoo" 1 "cry.wav")
53     ("drum[ \t]*roll" 1 "drumroll.au")
54     ("blast" 1 "explosion.au")
55     ("flush\\|plonk!*" 1 "flush.au")
56     ("kiss" 1 "kiss.wav")
57     ("tee[ \t]*hee" 1 "laugh.au")
58     ("shoot" 1 "shotgun.wav")
59     ("yawn" 1 "snore.wav")
60     ("cackle" 1 "witch.au")
61     ("yell\\|roar" 1 "yell2.au")
62     ("whoop-de-doo" 1 "whistle.au"))
63   "A list of regexps to map earcons to real sounds.")
64
65 (defvar earcon-button-marker-list nil)
66 (make-variable-buffer-local 'earcon-button-marker-list)
67
68
69
70 ;;; FIXME!! clone of code from gnus-vis.el FIXME!!
71 (defun earcon-article-push-button (event)
72   "Check text under the mouse pointer for a callback function.
73 If the text under the mouse pointer has a `earcon-callback' property,
74 call it with the value of the `earcon-data' text property."
75   (interactive "e")
76   (set-buffer (window-buffer (posn-window (event-start event))))
77   (let* ((pos (posn-point (event-start event)))
78          (data (get-text-property pos 'earcon-data))
79          (fun (get-text-property pos 'earcon-callback)))
80     (if fun (funcall fun data))))
81
82 (defun earcon-article-press-button ()
83   "Check text at point for a callback function.
84 If the text at point has a `earcon-callback' property,
85 call it with the value of the `earcon-data' text property."
86   (interactive)
87   (let* ((data (get-text-property (point) 'earcon-data))
88          (fun (get-text-property (point) 'earcon-callback)))
89     (if fun (funcall fun data))))
90
91 (defun earcon-article-prev-button (n)
92   "Move point to N buttons backward.
93 If N is negative, move forward instead."
94   (interactive "p")
95   (earcon-article-next-button (- n)))
96
97 (defun earcon-article-next-button (n)
98   "Move point to N buttons forward.
99 If N is negative, move backward instead."
100   (interactive "p")
101   (let ((function (if (< n 0) 'previous-single-property-change
102                     'next-single-property-change))
103         (inhibit-point-motion-hooks t)
104         (backward (< n 0))
105         (limit (if (< n 0) (point-min) (point-max))))
106     (setq n (abs n))
107     (while (and (not (= limit (point)))
108                 (> n 0))
109       ;; Skip past the current button.
110       (when (get-text-property (point) 'earcon-callback)
111         (goto-char (funcall function (point) 'earcon-callback nil limit)))
112       ;; Go to the next (or previous) button.
113       (gnus-goto-char (funcall function (point) 'earcon-callback nil limit))
114       ;; Put point at the start of the button.
115       (when (and backward (not (get-text-property (point) 'earcon-callback)))
116         (goto-char (funcall function (point) 'earcon-callback nil limit)))
117       ;; Skip past intangible buttons.
118       (when (get-text-property (point) 'intangible)
119         (incf n))
120       (decf n))
121     (unless (zerop n)
122       (gnus-message 5 "No more buttons"))
123     n))
124
125 (defun earcon-article-add-button (from to fun &optional data)
126   "Create a button between FROM and TO with callback FUN and data DATA."
127   (and (boundp gnus-article-button-face)
128        gnus-article-button-face
129        (gnus-overlay-put (gnus-make-overlay from to)
130                          'face gnus-article-button-face))
131   (gnus-add-text-properties 
132    from to
133    (nconc (and gnus-article-mouse-face
134                (list gnus-mouse-face-prop gnus-article-mouse-face))
135           (list 'gnus-callback fun)
136           (and data (list 'gnus-data data)))))
137
138 (defun earcon-button-entry ()
139   ;; Return the first entry in `gnus-button-alist' matching this place.
140   (let ((alist earcon-regexp-alist)
141         (case-fold-search t)
142         (entry nil))
143     (while alist
144       (setq entry (pop alist))
145       (if (looking-at (car entry))
146           (setq alist nil)
147         (setq entry nil)))
148     entry))
149
150
151 (defun earcon-button-push (marker)
152   ;; Push button starting at MARKER.
153   (save-excursion
154     (set-buffer gnus-article-buffer)
155     (goto-char marker)
156     (let* ((entry (earcon-button-entry))
157            (inhibit-point-motion-hooks t)
158            (fun 'gnus-audio-play)
159            (args (list (nth 2 entry))))
160       (cond
161        ((fboundp fun)
162         (apply fun args))
163        ((and (boundp fun)
164              (fboundp (symbol-value fun)))
165         (apply (symbol-value fun) args))
166        (t
167         (gnus-message 1 "You must define `%S' to use this button"
168                       (cons fun args)))))))
169
170 ;;; FIXME!! clone of code from gnus-vis.el FIXME!!
171
172 ;;;###interactive
173 (defun earcon-region (beg end)
174   "Play Sounds in the region between point and mark."
175   (interactive "r")
176   (earcon-buffer (current-buffer) beg end))
177
178 ;;;###interactive
179 (defun earcon-buffer (&optional buffer st nd)
180   (interactive)
181   (save-excursion
182     ;; clear old markers.
183     (if (boundp 'earcon-button-marker-list)
184         (while earcon-button-marker-list
185           (set-marker (pop earcon-button-marker-list) nil))
186       (setq earcon-button-marker-list nil))
187     (and buffer (set-buffer buffer))
188     (let ((buffer-read-only nil)
189           (inhibit-point-motion-hooks t)
190           (case-fold-search t)
191           (alist earcon-regexp-alist)
192           beg entry regexp)
193       (goto-char (point-min))
194       (setq beg (point))
195       (while (setq entry (pop alist))
196         (setq regexp (concat (regexp-quote earcon-prefix)
197                              ".*\\("
198                              (car entry)
199                              "\\).*"
200                              (regexp-quote earcon-suffix)))
201         (goto-char beg)
202         (while (re-search-forward regexp nil t)
203           (let* ((start (and entry (match-beginning 1)))
204                  (end (and entry (match-end 1)))
205                  (from (match-beginning 1)))
206             (earcon-article-add-button
207              start end 'earcon-button-push
208              (car (push (set-marker (make-marker) from)
209                         earcon-button-marker-list)))
210             (gnus-audio-play (caddr entry))))))))
211
212 ;;;###autoload
213 (defun gnus-earcon-display ()
214   "Play sounds in message buffers."
215   (interactive)
216   (save-excursion
217     (set-buffer gnus-article-buffer)
218     (goto-char (point-min))
219     ;; Skip headers
220     (unless (search-forward "\n\n" nil t)
221       (goto-char (point-max)))
222     (sit-for 0)
223     (earcon-buffer (current-buffer) (point))))
224
225 ;;;***
226
227 (provide 'earcon)
228
229 (run-hooks 'earcon-load-hook)
230
231 ;;; earcon.el ends here