download hooks.rb
Language: Ruby
LOC: 282
Project Info
Snd
Server: SourceForge
Type: cvs
SourceForge\s\snd\snd\cvs‑snd\
   _new_snd-test.rb
   _sndlib.h
   aclocal.m4
   analog-filter.scm
   audinfo.c
   audio.c
   autosave.scm
   bess.rb
   bess.scm
   bess1.rb
   bess1.scm
   bird.rb
   bird.scm
   clm-ins.rb
   clm-ins.scm
   clm-strings.h
   clm.c
   clm.h
   clm23.scm
   clm2xen.c
   clm2xen.h
   cmn-glyphs.lisp
   config.guess
   config.h.in
   config.rpath
   config.sub
   configure.ac
   debug.scm
   dlocsig.rb
   draw.rb
   draw.scm
   dsp.rb
   dsp.scm
   edit-menu.scm
   edit123.scm
   effects-utils.scm
   effects.rb
   env.rb
   env.scm
   enved.scm
   eval-c.scm
   event.scm
   examp.rb
   examp.scm
   extensions.rb
   extensions.scm
   fade.scm
   fft-menu.scm
   fix-optargs.scm
   fmv.scm
   freeverb.rb
   freeverb.scm
   gettext.h
   gl.c
   grani.scm
   gtk-effects.scm
   gtk-popup.scm
   gui.scm
   headers.c
   help-snd-fm.pd
   help-snd.pd
   HISTORY.Snd
   hooks.rb
   hooks.scm
   index.rb
   index.scm
   inf-snd.el
   io.c
   jcrev.scm
   ladspa-help.scm
   ladspa.scm
   makefile.in
   makefile.no-gettext
   maraca.rb
   maraca.scm
   marks-menu.scm
   marks.rb
   marks.scm
   maxf.rb
   maxf.scm
   midi.c
   misc.scm
   mix-menu.scm
   mix.rb
   mix.scm
   mixer.scm
   moog.scm
   musglyphs.rb
   musglyphs.scm
   nb.rb
   nb.scm
   new-backgrounds.scm
   new-buttons.scm
   new-effects.scm
   new-icons.scm
   noise.rb
   noise.scm
   old-mac-audio.c
   old-snd.spec
   oo.scm
   oscope.scm
   panic.scm
   pd-add.scm
   pd-any.scm
   pd-fm.scm
   pd-global.scm
   pd-inout.scm
   pd-local.scm
   pd-mozilla.scm
   pd-send_receive.scm
   peak-env.scm
   piano.rb
   piano.scm
   play.rb
   play.scm
   poly.rb
   poly.scm
   popup.rb
   popup.scm
   prc95.rb
   prc95.scm
   pvoc.rb
   pvoc.scm
   README.Snd
   rgb.rb
   rgb.scm
   rmsgain.scm
   rt-compiler.scm
   rt-engine.scm
   rt-examples.scm
   rt.tex
   rtio.rb
   rtio.scm
   rubber.rb
   rubber.scm
   saw.c
   singer.rb
   singer.scm
   snd-0.h
   snd-1.h
   snd-axis.c
   snd-chn.c
   snd-completion.c
   snd-dac.c
   snd-data.c
   snd-draw.c
   snd-edits.c
   snd-env.c
   snd-error.c
   snd-fft.c
   snd-file.c
   snd-file.h
   snd-find.c
   snd-g0.h
   snd-g1.h
   snd-gchn.c
   snd-gdraw.c
   snd-gdrop.c
   snd-genv.c
   snd-gfft.c
   snd-gfile.c
   snd-gfind.c
   snd-ghelp.c
   snd-gl.scm
   snd-glistener.c
   snd-gmain.c
   snd-gmenu.c
   snd-gmix.c
   snd-gprefs.c
   snd-gprint.c
   snd-grec.c
   snd-gregion.c
   snd-gsnd.c
   snd-gtk.scm
   snd-gutils.c
   snd-gxbitmaps.c
   snd-gxcolormaps.c
   snd-gxen.c
   snd-gxutils.c
   snd-help.c
   snd-hobbit.scm
   snd-io.c
   snd-kbd.c
   snd-ladspa.c
   snd-listener.c
   snd-main.c
   snd-marks.c
   snd-menu.c
   snd-menu.h
   snd-mix.c
   snd-motif.scm
   snd-nogui.c
   snd-nogui0.h
   snd-nogui1.h
   snd-prefs.c
   snd-print.c
   snd-rec.c
   snd-rec.h
   snd-region.c
   snd-run.c
   snd-select.c
   snd-sig.c
   snd-snd.c
   snd-strings.h
   snd-trans.c
   snd-utils.c
   snd-x0.h
   snd-x1.h
   snd-xchn.c
   snd-xdraw.c
   snd-xdrop.c
   snd-xen.c
   snd-xenv.c
   snd-xfft.c
   snd-xfile.c
   snd-xfind.c
   snd-xhelp.c
   snd-xlistener.c
   snd-xm.rb
   snd-xmain.c
   snd-xmenu.c
   snd-xmix.c
   snd-xprefs.c
   snd-xprint.c
   snd-xrec.c
   snd-xref.c
   snd-xregion.c
   snd-xsnd.c
   snd-xutils.c
   snd-xxen.c
   snd.1
   Snd.ad
   snd.c
   Snd.gtkrc
   snd.h
   snd.spec
   snd_conffile.scm
   snd_pd_external.c
   snd_pd_external.h
   snd4.scm
   snd5.scm
   snd6.scm
   snd7.scm
   sndctrl.c
   sndinfo.c
   sndlib-strings.h
   sndlib.h.in
   sndlib2xen.c
   sndlib2xen.h
   sndplay.c
   sndrecord.c
   sndwarp.scm
   sound.c
   special-menu.scm
   spectr.rb
   spectr.scm
   strad.rb
   strad.scm
   TODO.Snd
   track-colors.scm
   v.rb
   v.scm
   vct.c
   vct.h
   ws.rb
   ws.scm
   xen.c
   xen.h
   xg-x11.h
   xm-enved.rb
   xm-enved.scm
   zip.rb
   zip.scm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# hooks.rb -- hook-related functions

# Author: Michael Scholz <scholz-micha@gmx.de>
# Created: Sun Dec 21 13:48:01 CET 2003
# Last: Mon Aug 15 00:18:25 CEST 2005

# Commentary:
#
# If class Hook isn't compiled in, here is the corresponding Ruby
# class and the initialization of all global hooks.

=begin
# example in extsnd.html, "Channel-specific hooks"

def protect(snd = false, chn = false)
  edit_pos = edit_position(snd, chn)
  hook = edit_hook(snd, chn)
  hook.reset_hook!
  hook.add_hook!("protect") do | |
    report_in_minibuffer("protected") if val = edit_position(snd, chn) < edit_pos
    val
  end
end

def unprotect(snd = false, chn = false)
  edit_hook(snd, chn).reset_hook!
end
=end

# Contents:
#
# Snd_hooks             an array containing all global hook variables
# 
# $var_hook.member?("name of hook")
# $var_hook.show or
# describe_hook(hook)   prints code of hook procedures if file exists
# $var.run_hook_by_name(name, *args)
#
# with_local_hook(hook, *procs, &thunk)
# reset_all_hooks()     clears all hook procedures

# Code:

require "examp"

unless defined?(Hook)
  class Hook
    include Enumerable

    def initialize(name, arity = 0, help = "")
      @name = name
      @arity = arity
      @procs = []
      if string?(help) and (not help.empty?) then add_help(name, help) end
    end
    attr_reader :name, :arity
    
    def add_hook!(name, &body)
      @procs.push([name, body])
    end
    
    def remove_hook!(name)
      @procs.delete(@procs.assoc(name))
    end

    def reset_hook!
      @procs.clear
    end
    
    def run_hook(&body)
      self.to_a.each(&body)
    end
    alias each run_hook

    def call(*args)
      ret = nil
      self.run_hook do |prc| ret = prc.call(*args) end
      ret
    end
    
    def to_a
      @procs.map do |ary| ary.last end
    end
    
    def length
      @procs.length
    end
    alias size length

    def empty?
      @procs.empty?
    end

    def describe
      get_help(@name)
    end
    alias documentation describe
    
    def names
      @procs.map do |ary| ary.first end
    end
    
    def inspect
      format("#<%s name: %s, arity: %d, procs[%d]: %s>",
             self.class, @name.inspect, @arity, self.length, self.names.inspect)
    end
  end

  def make_hook(name, arity = 0, help = "", hook_name = nil, &body)
    error_str = "make_hook(name, arity = 0, help = "", hook_name = nil, &body): \
need a String or Symbol, not %s"
    var_sym = case name
              when Symbol
                name
              when String
                name.intern
              else
                raise format(error_str, name.inspect)
              end
    if var_sym.to_s.split(//).first != "$"
      var_sym = format("$%s", var_sym.to_s).intern
    end
    unless (var = Hook.instance_eval("#{var_sym} if defined?(#{var_sym})"))
      var = Hook.new(var_sym.to_s, arity, help)
    end
    if block_given?
      name = format("%s hook", var_sym.to_s) if name.empty?
      var.add_hook!(name, &body)
    end
    Hook.instance_eval("#{var_sym} = var")
  end
  
  def hook?(obj)
    obj.kind_of?(Hook)
  end
  
  $after_graph_hook             = Hook.new("$after_graph_hook", 2)
  $lisp_graph_hook              = Hook.new("$lisp_graph_hook", 2)
  $before_transform_hook        = Hook.new("$before_transform_hook", 2)
  $mix_release_hook             = Hook.new("$mix_release_hook", 2)
  $save_hook                    = Hook.new("$save_hook", 2)
  $mus_error_hook               = Hook.new("$mus_error_hook", 2)
  $mouse_enter_graph_hook       = Hook.new("$mouse_enter_graph_hook", 2)
  $mouse_leave_graph_hook       = Hook.new("$mouse_leave_graph_hook", 2)
  $open_raw_sound_hook          = Hook.new("$open_raw_sound_hook", 2)
  $select_channel_hook          = Hook.new("$select_channel_hook", 2)
  $after_open_hook              = Hook.new("$after_open_hook", 1)
  $close_hook                   = Hook.new("$close_hook", 1)
  $drop_hook                    = Hook.new("$drop_hook", 1)
  $update_hook                  = Hook.new("$update_hook", 1)
  $mark_click_hook              = Hook.new("$mark_click_hook", 1)
  $mark_drag_hook               = Hook.new("$mark_drag_hook", 1)
  $name_click_hook              = Hook.new("$name_click_hook", 1)
  $open_hook                    = Hook.new("$open_hook", 1)
  $help_hook                    = Hook.new("$help_hook", 2)
  $output_comment_hook          = Hook.new("$output_comment_hook", 1)
  $play_hook                    = Hook.new("$play_hook", 1)
  $snd_error_hook               = Hook.new("$snd_error_hook", 1)
  $snd_warning_hook             = Hook.new("$snd_warning_hook", 1)
  $start_hook                   = Hook.new("$start_hook", 1)
  $start_playing_hook           = Hook.new("$start_playing_hook", 1)
  $stop_playing_hook            = Hook.new("$stop_playing_hook", 1)
  $mouse_enter_listener_hook    = Hook.new("$mouse_enter_listener_hook", 1)
  $mouse_leave_listener_hook    = Hook.new("$mouse_leave_listener_hook", 1)
  $window_property_changed_hook = Hook.new("$window_property_changed_hook", 1)
  $select_sound_hook            = Hook.new("$select_sound_hook", 1)
  $print_hook                   = Hook.new("$print_hook", 1)
  $exit_hook                    = Hook.new("$exit_hook", 0)
  $output_name_hook             = Hook.new("$output_name_hook", 1)
  $during_open_hook             = Hook.new("$during_open_hook", 3)
  $after_transform_hook         = Hook.new("$after_transform_hook", 3)
  $mouse_enter_label_hook       = Hook.new("$mouse_enter_label_hook", 3)
  $mouse_leave_label_hook       = Hook.new("$mouse_leave_label_hook", 3)
  $initial_graph_hook           = Hook.new("$initial_graph_hook", 3)
  $graph_hook                   = Hook.new("$graph_hook", 4)
  $key_press_hook               = Hook.new("$key_press_hook", 4)
  $mouse_drag_hook              = Hook.new("$mouse_drag_hook", 6)
  $mouse_press_hook             = Hook.new("$mouse_press_hook", 6)
  $enved_hook                   = Hook.new("$enved_hook", 5)
  $read_hook                    = Hook.new("$read_hook", 1)
  $mouse_click_hook             = Hook.new("$mouse_click_hook", 7)
  $new_widget_hook              = Hook.new("$new_widget_hook", 1)
  $mark_hook                    = Hook.new("$mark_hook", 4)
  $view_files_select_hook       = Hook.new("$view_files_select_hook", 2)
  $dac_hook                     = Hook.new("$dac_hook", 1)
  $stop_dac_hook                = Hook.new("$stop_dac_hook", 0)
  $stop_playing_selection_hook  = Hook.new("$stop_playing_selection_hook", 0)
  $after_apply_hook             = Hook.new("$after_apply_hook", 1)
  $draw_mark_hook               = Hook.new("$draw_mark_hook", 1)
  $bad_header_hook              = Hook.new("$bad_header_hook", 1)
  $save_state_hook              = Hook.new("$save_state_hook", 1)
  $new_sound_hook               = Hook.new("$new_sound_hook", 1)
  $color_hook                   = Hook.new("$color_hook", 0)
  $orientation_hook             = Hook.new("$orientation_hook", 0)
  $listener_click_hook          = Hook.new("$listener_click_hook", 1)
  $mix_click_hook               = Hook.new("$mix_click_hook", 1)
  $before_save_state_hook       = Hook.new("$before_save_state_hook", 1)
  $after_save_state_hook        = Hook.new("$after_save_state_hook", 1)
  $mouse_enter_text_hook        = Hook.new("$mouse_enter_text_hook", 1)
  $mouse_leave_text_hook        = Hook.new("$mouse_leave_text_hook", 1)
  $optimization_hook            = Hook.new("$optimization_hook", 1)
  $mix_drag_hook                = Hook.new("$mix_drag_hook", 1)
  $mark_drag_triangle_hook      = Hook.new("$mark_drag_triangle_hook", 4)
  $start_playing_selection_hook = Hook.new("$start_playing_selection_hook", 0)
  $selection_changed_hook       = Hook.new("$selection_changed_hook", 0)
  $before_exit_hook             = Hook.new("$before_exit_hook", 0)
  $before_close_hook            = Hook.new("$before_close_hook", 1)
  $after_save_as_hook           = Hook.new("$after_save_as_hook", 3)
  $before_save_as_hook          = Hook.new("$before_save_as_hook", 7)
  # unless --with-no-gui
  $recorder_file_hook           = Hook.new("$recorder_file_hook", 1)
  # if --with-gtk
  $gtk_popup_hook               = Hook.new("$gtk_popup_hook", 5)
end

class Hook
  def to_names
    @procs.map do |ary| ary.first end
  end

  def member?(name)
    to_names.member?(name)
  end
  alias included? member?

  # This works only with newer ruby versions (I assume >= 1.8.x).
  # Proc#to_s must return #<Proc:0x80c96a0@xxx:x> not only
  # #<Proc:0x80c96a0>!
  def to_str
    self.each do |prc|
      # cover printf's %x
      Snd.display(prc.to_str.gsub(/%/, "%%"))
    end
    nil
  end
  alias show to_str

  def run_hook_by_name(name, *args)
    if prc = @procs.assoc(name) then prc.last.call(*args) end
  end
end

def describe_hook(hook)
  hook.show
end

add_help(:with_local_hook,
         "with_local_hook(hook, *procs, &thunk)  \
evaluates thunk with hook set to procs, then restores hook to its previous state")
def with_local_hook(hook, *procs, &thunk)
  old_procs = []
  hook.to_names.each do |name| old_procs.push(hook.remove_hook!(name)) end
  hook.reset_hook!
  procs.each do |prc| hook.add_hook!(prc.object_id.to_s, &prc) end
  thunk.call
rescue Interrupt, ScriptError, StandardError
  Snd.display(verbose_message_string(true, nil, get_func_name))
ensure
  hook.reset_hook!
  old_procs.each do |name, prc| hook.add_hook!(name, &prc) end
end

if defined? $after_graph_hook
  Snd_hooks = [
    $after_graph_hook,
    $lisp_graph_hook,
    $before_transform_hook,
    $mix_release_hook,
    $save_hook,
    $mus_error_hook,
    $mouse_enter_graph_hook,
    $mouse_leave_graph_hook,
    $open_raw_sound_hook,
    $select_channel_hook,
    $after_open_hook,
    $close_hook,
    $drop_hook,
    $update_hook,
    $mark_click_hook,
    $mark_drag_hook,
    $name_click_hook,
    $open_hook,
    $help_hook,
    $output_comment_hook,
    $play_hook,
    $snd_error_hook,
    $snd_warning_hook,
    $start_hook,
    $start_playing_hook,
    $stop_playing_hook,
    $mouse_enter_listener_hook,
    $mouse_leave_listener_hook,
    $window_property_changed_hook,
    $select_sound_hook,
    $print_hook,
    $exit_hook,
    $output_name_hook,
    $during_open_hook,
    $after_transform_hook,
    $mouse_enter_label_hook,
    $mouse_leave_label_hook,
    $initial_graph_hook,
    $graph_hook,
    $key_press_hook,
    $mouse_drag_hook,
    $mouse_press_hook,
    $enved_hook,
    $read_hook,
    $mouse_click_hook,
    $new_widget_hook,
    $mark_hook,
    $view_files_select_hook,
    $dac_hook,
    $stop_dac_hook,
    $stop_playing_selection_hook,
    $after_apply_hook,
    $draw_mark_hook,
    $bad_header_hook,
    $save_state_hook,
    $new_sound_hook,
    $color_hook,
    $orientation_hook,
    $listener_click_hook,
    $mix_click_hook,
    $before_save_state_hook,
    $after_save_state_hook,
    $mouse_enter_text_hook,
    $mouse_leave_text_hook,
    $optimization_hook,
    $mix_drag_hook,
    $mark_drag_triangle_hook,
    $start_playing_selection_hook,
    $selection_changed_hook,
    $after_save_as_hook,
    $before_save_as_hook,
    $before_exit_hook,
    $before_close_hook]

  unless provided? :snd_nogui
    Snd_hooks.push($recorder_file_hook)
  end
  if provided? :snd_gtk
    Snd_hooks.push $gtk_popup_hook
  end
  
  def reset_all_hooks
    Snd_hooks.each do |h| h.kind_of?(Hook) and h.reset_hook! end
    Snd.sounds.each do |snd|
      channels(snd).times do |chn|
        edit_hook(snd, chn).reset_hook! if hook?(edit_hook(snd, chn))
        after_edit_hook(snd, chn).reset_hook! if hook?(after_edit_hook(snd, chn))
        undo_hook(snd, chn).reset_hook! if hook?(undo_hook(snd, chn))
      end
    end
  end
end

# hooks.rb ends here

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us