*** 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 ;; 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
75 (defvar earcon-button-marker-list nil)
76 (make-variable-buffer-local 'earcon-button-marker-list)
77
78
79
80 ;;; FIXME!! clone of code from gnus-vis.el FIXME!!
81 (defun earcon-article-push-button (event)
82   "Check text under the mouse pointer for a callback function.
83 If the text under the mouse pointer has a `earcon-callback' property,
84 call it with the value of the `earcon-data' text property."
85   (interactive "e")
86   (set-buffer (window-buffer (posn-window (event-start event))))
87   (let* ((pos (posn-point (event-start event)))
88          (data (get-text-property pos 'earcon-data))
89          (fun (get-text-property pos 'earcon-callback)))
90     (if fun (funcall fun data))))
91
92 (defun earcon-article-press-button ()
93   "Check text at point for a callback function.
94 If the text at point has a `earcon-callback' property,
95 call it with the value of the `earcon-data' text property."
96   (interactive)
97   (let* ((data (get-text-property (point) 'earcon-data))
98          (fun (get-text-property (point) 'earcon-callback)))
99     (if fun (funcall fun data))))
100
101 (defun earcon-article-prev-button (n)
102   "Move point to N buttons backward.
103 If N is negative, move forward instead."
104   (interactive "p")
105   (earcon-article-next-button (- n)))
106
107 (defun earcon-article-next-button (n)
108   "Move point to N buttons forward.
109 If N is negative, move backward instead."
110   (interactive "p")
111   (let ((function (if (< n 0) 'previous-single-property-change
112                     'next-single-property-change))
113         (inhibit-point-motion-hooks t)
114         (backward (< n 0))
115         (limit (if (< n 0) (point-min) (point-max))))
116     (setq n (abs n))
117     (while (and (not (= limit (point)))
118                 (> n 0))
119       ;; Skip past the current button.
120       (when (get-text-property (point) 'earcon-callback)
121         (goto-char (funcall function (point) 'earcon-callback nil limit)))
122       ;; Go to the next (or previous) button.
123       (gnus-goto-char (funcall function (point) 'earcon-callback nil limit))
124       ;; Put point at the start of the button.
125       (when (and backward (not (get-text-property (point) 'earcon-callback)))
126         (goto-char (funcall function (point) 'earcon-callback nil limit)))
127       ;; Skip past intangible buttons.
128       (when (get-text-property (point) 'intangible)
129         (incf n))
130       (decf n))
131     (unless (zerop n)
132       (gnus-message 5 "No more buttons"))
133     n))
134
135 (defun earcon-article-add-button (from to fun &optional data)
136   "Create a button between FROM and TO with callback FUN and data DATA."
137   (and (boundp gnus-article-button-face)
138        gnus-article-button-face
139        (gnus-overlay-put (gnus-make-overlay from to)
140                          'face gnus-article-button-face))
141   (gnus-add-text-properties
142    from to
143    (nconc (and gnus-article-mouse-face
144                (list gnus-mouse-face-prop gnus-article-mouse-face))
145           (list 'gnus-callback fun)
146           (and data (list 'gnus-data data)))))
147
148 (defun earcon-button-entry ()
149   ;; Return the first entry in `gnus-button-alist' matching this place.
150   (let ((alist earcon-regexp-alist)
151         (case-fold-search t)
152         (entry nil))
153     (while alist
154       (setq entry (pop alist))
155       (if (looking-at (car entry))
156           (setq alist nil)
157         (setq entry nil)))
158     entry))
159
160
161 (defun earcon-button-push (marker)
162   ;; Push button starting at MARKER.
163   (save-excursion
164     (set-buffer gnus-article-buffer)
165     (goto-char marker)
166     (let* ((entry (earcon-button-entry))
167            (inhibit-point-motion-hooks t)
168            (fun 'gnus-audio-play)
169            (args (list (nth 2 entry))))
170       (cond
171        ((fboundp fun)
172         (apply fun args))
173        ((and (boundp fun)
174              (fboundp (symbol-value fun)))
175         (apply (symbol-value fun) args))
176        (t
177         (gnus-message 1 "You must define `%S' to use this button"
178                       (cons fun args)))))))
179
180 ;;; FIXME!! clone of code from gnus-vis.el FIXME!!
181
182 ;;;###interactive
183 (defun earcon-region (beg end)
184   "Play Sounds in the region between point and mark."
185   (interactive "r")
186   (earcon-buffer (current-buffer) beg end))
187
188 ;;;###interactive
189 (defun earcon-buffer (&optional buffer st nd)
190   (interactive)
191   (save-excursion
192     ;; clear old markers.
193     (if (boundp 'earcon-button-marker-list)
194         (while earcon-button-marker-list
195           (set-marker (pop earcon-button-marker-list) nil))
196       (setq earcon-button-marker-list nil))
197     (and buffer (set-buffer buffer))
198     (let ((buffer-read-only nil)
199           (inhibit-point-motion-hooks t)
200           (case-fold-search t)
201           (alist earcon-regexp-alist)
202           beg entry regexp)
203       (goto-char (point-min))
204       (setq beg (point))
205       (while (setq entry (pop alist))
206         (setq regexp (concat (regexp-quote earcon-prefix)
207                              ".*\\("
208                              (car entry)
209                              "\\).*"
210                              (regexp-quote earcon-suffix)))
211         (goto-char beg)
212         (while (re-search-forward regexp nil t)
213           (let* ((start (and entry (match-beginning 1)))
214                  (end (and entry (match-end 1)))
215                  (from (match-beginning 1)))
216             (earcon-article-add-button
217              start end 'earcon-button-push
218              (car (push (set-marker (make-marker) from)
219                         earcon-button-marker-list)))
220             (gnus-audio-play (caddr entry))))))))
221
222 ;;;###autoload
223 (defun gnus-earcon-display ()
224   "Play sounds in message buffers."
225   (interactive)
226   (save-excursion
227     (set-buffer gnus-article-buffer)
228     (goto-char (point-min))
229     ;; Skip headers
230     (unless (search-forward "\n\n" nil t)
231       (goto-char (point-max)))
232     (sit-for 0)
233     (earcon-buffer (current-buffer) (point))))
234
235 ;;;***
236
237 (provide 'earcon)
238
239 (run-hooks 'earcon-load-hook)
240
241 ;;; earcon.el ends here