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