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