Small cleanups to port matrix context menu.
[ardour.git] / gtk2_ardour / editor_ops.cc
index e8d0763164f05816bda561462e3822bd9306d1da..0dfaecf4fd75b759eb8de02a3e55dedf8a9539c2 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2000-2004 Paul Davis 
+    Copyright (C) 2000-2004 Paul Davis
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
+/* Note: public Editor methods are documented in public_editor.h */
+
 #include <unistd.h>
 
 #include <cstdlib>
 #include <cmath>
 #include <string>
 #include <map>
+#include <set>
 
-#include <sndfile.h>
-
-#include <pbd/error.h>
-#include <pbd/basename.h>
-#include <pbd/pthread_utils.h>
+#include "pbd/error.h"
+#include "pbd/basename.h"
+#include "pbd/pthread_utils.h"
+#include "pbd/memento_command.h"
+#include "pbd/whitespace.h"
 
 #include <gtkmm2ext/utils.h>
 #include <gtkmm2ext/choice.h>
-
-#include <ardour/audioengine.h>
-#include <ardour/session.h>
-#include <ardour/audioplaylist.h>
-#include <ardour/audioregion.h>
-#include <ardour/diskstream.h>
-#include <ardour/filesource.h>
-#include <ardour/sndfilesource.h>
-#include <ardour/utils.h>
-#include <ardour/location.h>
-#include <ardour/named_selection.h>
-#include <ardour/audio_track.h>
-#include <ardour/audioplaylist.h>
-#include <ardour/region_factory.h>
-#include <ardour/reverse.h>
+#include <gtkmm2ext/popup.h>
+
+#include "ardour/audioengine.h"
+#include "ardour/session.h"
+#include "ardour/audioplaylist.h"
+#include "ardour/audioregion.h"
+#include "ardour/audio_diskstream.h"
+#include "ardour/utils.h"
+#include "ardour/location.h"
+#include "ardour/audio_track.h"
+#include "ardour/audioplaylist.h"
+#include "ardour/region_factory.h"
+#include "ardour/playlist_factory.h"
+#include "ardour/reverse.h"
+#include "ardour/transient_detector.h"
+#include "ardour/dB.h"
+#include "ardour/quantize.h"
+#include "ardour/strip_silence.h"
+#include "ardour/route_group.h"
 
 #include "ardour_ui.h"
 #include "editor.h"
 #include "time_axis_view.h"
+#include "route_time_axis.h"
 #include "audio_time_axis.h"
 #include "automation_time_axis.h"
 #include "streamview.h"
-#include "regionview.h"
+#include "audio_streamview.h"
+#include "audio_region_view.h"
+#include "midi_region_view.h"
 #include "rgb_macros.h"
 #include "selection_templates.h"
 #include "selection.h"
-#include "sfdb_ui.h"
 #include "editing.h"
 #include "gtk-custom-hruler.h"
+#include "gui_thread.h"
+#include "keyboard.h"
+#include "utils.h"
+#include "editor_drag.h"
+#include "strip_silence_dialog.h"
+#include "editor_routes.h"
+#include "editor_regions.h"
+#include "quantize_dialog.h"
 
 #include "i18n.h"
 
 using namespace std;
 using namespace ARDOUR;
+using namespace PBD;
 using namespace sigc;
 using namespace Gtk;
+using namespace Gtkmm2ext;
 using namespace Editing;
 
 /***********************************************************************
@@ -92,189 +110,132 @@ Editor::redo (uint32_t n)
 }
 
 void
-Editor::set_meter_hold (int32_t cnt)
+Editor::split_regions_at (nframes64_t where, RegionSelection& regions)
 {
-       if (session) {
-               session->set_meter_hold (cnt);
-       }
-}
+       list <boost::shared_ptr<Playlist > > used_playlists;
 
-void
-Editor::set_meter_falloff (float val)
-{
-       if (session) {
-               session->set_meter_falloff (val);
+       if (regions.empty()) {
+               return;
        }
-}
-
-
-int
-Editor::ensure_cursor (jack_nframes_t *pos)
-{
-       *pos = edit_cursor->current_frame;
-       return 0;
-}
 
-void
-Editor::split_region ()
-{
-       split_region_at (edit_cursor->current_frame);
-}
-
-void
-Editor::split_region_at (jack_nframes_t where)
-{
-       split_regions_at (where, selection->audio_regions);
-}
-
-void
-Editor::split_regions_at (jack_nframes_t where, AudioRegionSelection& regions)
-{
        begin_reversible_command (_("split"));
 
-       snap_to (where);
-       for (AudioRegionSelection::iterator a = regions.begin(); a != regions.end(); ) {
-
-               AudioRegionSelection::iterator tmp;
-               
-               tmp = a;
-               ++tmp;
+       // if splitting a single region, and snap-to is using
+       // region boundaries, don't pay attention to them
 
-               Playlist* pl = (*a)->region.playlist();
-
-               _new_regionviews_show_envelope = (*a)->envelope_visible();
-               
-               if (pl) {
-                       session->add_undo (pl->get_memento());
-                       pl->split_region ((*a)->region, where);
-                       session->add_redo_no_execute (pl->get_memento());
+       if (regions.size() == 1) {
+               switch (_snap_type) {
+               case SnapToRegionStart:
+               case SnapToRegionSync:
+               case SnapToRegionEnd:
+                       break;
+               default:
+                       snap_to (where);
                }
-
-               a = tmp;
-    }
-
-       commit_reversible_command ();
-       _new_regionviews_show_envelope = false;
-}
-
-void
-Editor::remove_clicked_region ()
-{
-       if (clicked_audio_trackview == 0 || clicked_regionview == 0) {
-               return;
+       } else {
+               snap_to (where);
        }
 
-       Playlist* playlist = clicked_audio_trackview->playlist();
-       
-       begin_reversible_command (_("remove region"));
-       session->add_undo (playlist->get_memento());
-       playlist->remove_region (&clicked_regionview->region);
-       session->add_redo_no_execute (playlist->get_memento());
-       commit_reversible_command ();
-}
+       for (RegionSelection::iterator a = regions.begin(); a != regions.end(); ) {
 
-void
-Editor::destroy_clicked_region ()
-{
-       int32_t selected = selection->audio_regions.size();
+               RegionSelection::iterator tmp;
 
-       if (!session || clicked_regionview == 0 && selected == 0) {
-               return;
-       }
+               /* XXX this test needs to be more complicated, to make sure we really
+                  have something to split.
+               */
 
-       vector<string> choices;
-       string prompt;
-       
-       prompt  = string_compose (_(" This is destructive, will possibly delete audio files\n\
-It cannot be undone\n\
-Do you really want to destroy %1 ?"),
-                          (selected > 1 ? 
-                           _("these regions") : _("this region")));
-
-       if (selected > 1) {
-               choices.push_back (_("Yes, destroy them."));
-       } else {
-               choices.push_back (_("Yes, destroy it."));
-       }
+               if (!(*a)->region()->covers (where)) {
+                       ++a;
+                       continue;
+               }
 
-       choices.push_back (_("No, do nothing."));
+               tmp = a;
+               ++tmp;
 
-       Gtkmm2ext::Choice prompter (prompt, choices);
+               boost::shared_ptr<Playlist> pl = (*a)->region()->playlist();
 
-       prompter.chosen.connect (ptr_fun (Main::quit));
-       prompter.show_all ();
+               if (! pl->frozen()) {
+                       /* we haven't seen this playlist before */
 
-       Main::run ();
-               
-       if (prompter.get_choice() != 0) {
-               return;
-       }
+                       /* remember used playlists so we can thaw them later */
+                       used_playlists.push_back(pl);
+                       pl->freeze();
+               }
 
-       if (selected > 0) {
-               list<Region*> r;
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*a);
+               if (arv) {
+                       _new_regionviews_show_envelope = arv->envelope_visible();
+               }
 
-               for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-                       r.push_back (&(*i)->region);
+               if (pl) {
+                       XMLNode &before = pl->get_state();
+                       pl->split_region ((*a)->region(), where);
+                       XMLNode &after = pl->get_state();
+                       session->add_command(new MementoCommand<Playlist>(*pl, &before, &after));
                }
 
-               session->destroy_regions (r);
+               a = tmp;
+       }
+
+       while (used_playlists.size() > 0) {
+               list <boost::shared_ptr<Playlist > >::iterator i = used_playlists.begin();
+               (*i)->thaw();
+               used_playlists.pop_front();
+       }
 
-       } else if (clicked_regionview) {
-               session->destroy_region (&clicked_regionview->region);
-       } 
+       commit_reversible_command ();
+       _new_regionviews_show_envelope = false;
 }
 
-AudioRegion *
-Editor::select_region_for_operation (int dir, TimeAxisView **tv)
+boost::shared_ptr<Region>
+Editor::select_region_for_operation (int /*dir*/, TimeAxisView **tv)
 {
-       AudioRegionView* rv;
-       AudioRegion *region;
-       jack_nframes_t start = 0;
+       RegionView* rv;
+       boost::shared_ptr<Region> region;
+       nframes64_t start = 0;
 
        if (selection->time.start () == selection->time.end_frame ()) {
-               
+
                /* no current selection-> is there a selected regionview? */
 
-               if (selection->audio_regions.empty()) {
-                       return 0;
+               if (selection->regions.empty()) {
+                       return region;
                }
 
-       } 
-
-       region = 0;
+       }
 
-       if (!selection->audio_regions.empty()) {
+       if (!selection->regions.empty()) {
 
-               rv = *(selection->audio_regions.begin());
+               rv = *(selection->regions.begin());
                (*tv) = &rv->get_time_axis_view();
-               region = &rv->region;
+               region = rv->region();
 
        } else if (!selection->tracks.empty()) {
 
                (*tv) = selection->tracks.front();
 
-               AudioTimeAxisView* atv;
+               RouteTimeAxisView* rtv;
 
-               if ((atv = dynamic_cast<AudioTimeAxisView*> (*tv)) != 0) {
-                       Playlist *pl;
-                       
-                       if ((pl = atv->playlist()) == 0) {
-                               return 0;
+               if ((rtv = dynamic_cast<RouteTimeAxisView*> (*tv)) != 0) {
+                       boost::shared_ptr<Playlist> pl;
+
+                       if ((pl = rtv->playlist()) == 0) {
+                               return region;
                        }
-                       
-                       region = dynamic_cast<AudioRegion*> (pl->top_region_at (start));
+
+                       region = pl->top_region_at (start);
                }
-       } 
-       
+       }
+
        return region;
 }
-       
+
 void
 Editor::extend_selection_to_end_of_region (bool next)
 {
        TimeAxisView *tv;
-       Region *region;
-       jack_nframes_t start;
+       boost::shared_ptr<Region> region;
+       nframes64_t start;
 
        if ((region = select_region_for_operation (next ? 1 : 0, &tv)) == 0) {
                return;
@@ -301,8 +262,8 @@ void
 Editor::extend_selection_to_start_of_region (bool previous)
 {
        TimeAxisView *tv;
-       Region *region;
-       jack_nframes_t end;
+       boost::shared_ptr<Region> region;
+       nframes64_t end;
 
        if ((region = select_region_for_operation (previous ? -1 : 0, &tv)) == 0) {
                return;
@@ -315,7 +276,7 @@ Editor::extend_selection_to_start_of_region (bool previous)
        }
 
        /* Try to leave the selection with the same route if possible */
-       
+
        if ((tv = selection->time.track) == 0) {
                return;
        }
@@ -325,31 +286,100 @@ Editor::extend_selection_to_start_of_region (bool previous)
        commit_reversible_command ();
 }
 
+bool
+Editor::nudge_forward_release (GdkEventButton* ev)
+{
+       if (ev->state & Keyboard::PrimaryModifier) {
+               nudge_forward (false, true);
+       } else {
+               nudge_forward (false, false);
+       }
+       return false;
+}
+
+bool
+Editor::nudge_backward_release (GdkEventButton* ev)
+{
+       if (ev->state & Keyboard::PrimaryModifier) {
+               nudge_backward (false, true);
+       } else {
+               nudge_backward (false, false);
+       }
+       return false;
+}
+
 
 void
-Editor::nudge_forward (bool next)
+Editor::nudge_forward (bool next, bool force_playhead)
 {
-       jack_nframes_t distance;
-       jack_nframes_t next_distance;
+       nframes64_t distance;
+       nframes64_t next_distance;
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
 
        if (!session) return;
-       
-       if (!selection->audio_regions.empty()) {
 
-               begin_reversible_command (_("nudge forward"));
+       if (!force_playhead && !rs.empty()) {
+
+               begin_reversible_command (_("nudge regions forward"));
+
+               for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+                       boost::shared_ptr<Region> r ((*i)->region());
 
-               for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-                       AudioRegion& r ((*i)->region);
-                       
-                       distance = get_nudge_distance (r.position(), next_distance);
+                       distance = get_nudge_distance (r->position(), next_distance);
 
                        if (next) {
                                distance = next_distance;
                        }
 
-                       session->add_undo (r.playlist()->get_memento());
-                       r.set_position (r.position() + distance, this);
-                       session->add_redo_no_execute (r.playlist()->get_memento());
+                        XMLNode &before = r->playlist()->get_state();
+                       r->set_position (r->position() + distance, this);
+                        XMLNode &after = r->playlist()->get_state();
+                       session->add_command (new MementoCommand<Playlist>(*(r->playlist()), &before, &after));
+               }
+
+               commit_reversible_command ();
+
+
+       } else if (!force_playhead && !selection->markers.empty()) {
+
+               bool is_start;
+
+               begin_reversible_command (_("nudge location forward"));
+
+               for (MarkerSelection::iterator i = selection->markers.begin(); i != selection->markers.end(); ++i) {
+
+                       Location* loc = find_location_from_marker ((*i), is_start);
+
+                       if (loc) {
+
+                               XMLNode& before (loc->get_state());
+
+                               if (is_start) {
+                                       distance = get_nudge_distance (loc->start(), next_distance);
+                                       if (next) {
+                                               distance = next_distance;
+                                       }
+                                       if (max_frames - distance > loc->start() + loc->length()) {
+                                               loc->set_start (loc->start() + distance);
+                                       } else {
+                                               loc->set_start (max_frames - loc->length());
+                                       }
+                               } else {
+                                       distance = get_nudge_distance (loc->end(), next_distance);
+                                       if (next) {
+                                               distance = next_distance;
+                                       }
+                                       if (max_frames - distance > loc->end()) {
+                                               loc->set_end (loc->end() + distance);
+                                       } else {
+                                               loc->set_end (max_frames);
+                                       }
+                               }
+                               XMLNode& after (loc->get_state());
+                               session->add_command (new MementoCommand<Location>(*loc, &before, &after));
+                       }
                }
 
                commit_reversible_command ();
@@ -359,36 +389,85 @@ Editor::nudge_forward (bool next)
                session->request_locate (playhead_cursor->current_frame + distance);
        }
 }
-               
+
 void
-Editor::nudge_backward (bool next)
+Editor::nudge_backward (bool next, bool force_playhead)
 {
-       jack_nframes_t distance;
-       jack_nframes_t next_distance;
+       nframes64_t distance;
+       nframes64_t next_distance;
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
 
        if (!session) return;
-       
-       if (!selection->audio_regions.empty()) {
 
-               begin_reversible_command (_("nudge forward"));
+       if (!force_playhead && !rs.empty()) {
+
+               begin_reversible_command (_("nudge regions backward"));
 
-               for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-                       AudioRegion& r ((*i)->region);
+               for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+                       boost::shared_ptr<Region> r ((*i)->region());
+
+                       distance = get_nudge_distance (r->position(), next_distance);
 
-                       distance = get_nudge_distance (r.position(), next_distance);
-                       
                        if (next) {
                                distance = next_distance;
                        }
 
-                       session->add_undo (r.playlist()->get_memento());
-                       
-                       if (r.position() > distance) {
-                               r.set_position (r.position() - distance, this);
+                        XMLNode &before = r->playlist()->get_state();
+
+                       if (r->position() > distance) {
+                               r->set_position (r->position() - distance, this);
                        } else {
-                               r.set_position (0, this);
+                               r->set_position (0, this);
+                       }
+                        XMLNode &after = r->playlist()->get_state();
+                       session->add_command(new MementoCommand<Playlist>(*(r->playlist()), &before, &after));
+               }
+
+               commit_reversible_command ();
+
+       } else if (!force_playhead && !selection->markers.empty()) {
+
+               bool is_start;
+
+               begin_reversible_command (_("nudge location forward"));
+
+               for (MarkerSelection::iterator i = selection->markers.begin(); i != selection->markers.end(); ++i) {
+
+                       Location* loc = find_location_from_marker ((*i), is_start);
+
+                       if (loc) {
+
+                               XMLNode& before (loc->get_state());
+
+                               if (is_start) {
+                                       distance = get_nudge_distance (loc->start(), next_distance);
+                                       if (next) {
+                                               distance = next_distance;
+                                       }
+                                       if (distance < loc->start()) {
+                                               loc->set_start (loc->start() - distance);
+                                       } else {
+                                               loc->set_start (0);
+                                       }
+                               } else {
+                                       distance = get_nudge_distance (loc->end(), next_distance);
+
+                                       if (next) {
+                                               distance = next_distance;
+                                       }
+
+                                       if (distance < loc->end() - loc->length()) {
+                                               loc->set_end (loc->end() - distance);
+                                       } else {
+                                               loc->set_end (loc->length());
+                                       }
+                               }
+
+                               XMLNode& after (loc->get_state());
+                               session->add_command (new MementoCommand<Location>(*loc, &before, &after));
                        }
-                       session->add_redo_no_execute (r.playlist()->get_memento());
                }
 
                commit_reversible_command ();
@@ -400,7 +479,7 @@ Editor::nudge_backward (bool next)
                if (playhead_cursor->current_frame > distance) {
                        session->request_locate (playhead_cursor->current_frame - distance);
                } else {
-                       session->request_locate (0);
+                       session->goto_start();
                }
        }
 }
@@ -408,53 +487,61 @@ Editor::nudge_backward (bool next)
 void
 Editor::nudge_forward_capture_offset ()
 {
-       jack_nframes_t distance;
+       nframes64_t distance;
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
 
        if (!session) return;
-       
-       if (!selection->audio_regions.empty()) {
+
+       if (!rs.empty()) {
 
                begin_reversible_command (_("nudge forward"));
 
                distance = session->worst_output_latency();
 
-               for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-                       AudioRegion& r ((*i)->region);
-                       
-                       session->add_undo (r.playlist()->get_memento());
-                       r.set_position (r.position() + distance, this);
-                       session->add_redo_no_execute (r.playlist()->get_memento());
+               for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+                       boost::shared_ptr<Region> r ((*i)->region());
+
+                       XMLNode &before = r->playlist()->get_state();
+                       r->set_position (r->position() + distance, this);
+                       XMLNode &after = r->playlist()->get_state();
+                       session->add_command(new MementoCommand<Playlist>(*(r->playlist()), &before, &after));
                }
 
                commit_reversible_command ();
 
-       } 
+       }
 }
-               
+
 void
 Editor::nudge_backward_capture_offset ()
 {
-       jack_nframes_t distance;
+       nframes64_t distance;
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
 
        if (!session) return;
-       
-       if (!selection->audio_regions.empty()) {
+
+       if (!rs.empty()) {
 
                begin_reversible_command (_("nudge forward"));
 
                distance = session->worst_output_latency();
 
-               for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-                       AudioRegion& r ((*i)->region);
+               for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+                       boost::shared_ptr<Region> r ((*i)->region());
+
+                        XMLNode &before = r->playlist()->get_state();
 
-                       session->add_undo (r.playlist()->get_memento());
-                       
-                       if (r.position() > distance) {
-                               r.set_position (r.position() - distance, this);
+                       if (r->position() > distance) {
+                               r->set_position (r->position() - distance, this);
                        } else {
-                               r.set_position (0, this);
+                               r->set_position (0, this);
                        }
-                       session->add_redo_no_execute (r.playlist()->get_memento());
+                        XMLNode &after = r->playlist()->get_state();
+                       session->add_command(new MementoCommand<Playlist>(*(r->playlist()), &before, &after));
                }
 
                commit_reversible_command ();
@@ -466,7 +553,7 @@ Editor::nudge_backward_capture_offset ()
 void
 Editor::move_to_start ()
 {
-       session->request_locate (0);
+       session->goto_start ();
 }
 
 void
@@ -479,125 +566,141 @@ Editor::move_to_end ()
 void
 Editor::build_region_boundary_cache ()
 {
-       jack_nframes_t pos = 0;
-       RegionPoint point;
-       Region *r;
+       nframes64_t pos = 0;
+       vector<RegionPoint> interesting_points;
+       boost::shared_ptr<Region> r;
        TrackViewList tracks;
+       bool at_end = false;
 
        region_boundary_cache.clear ();
 
        if (session == 0) {
                return;
        }
-       
-       switch (snap_type) {
+
+       switch (_snap_type) {
        case SnapToRegionStart:
-               point = Start;
+               interesting_points.push_back (Start);
                break;
        case SnapToRegionEnd:
-               point = End;
-               break;  
+               interesting_points.push_back (End);
+               break;
        case SnapToRegionSync:
-               point = SyncPoint;
-               break;  
+               interesting_points.push_back (SyncPoint);
+               break;
        case SnapToRegionBoundary:
-               point = Start;
-               break;  
+               interesting_points.push_back (Start);
+               interesting_points.push_back (End);
+               break;
        default:
-               fatal << string_compose (_("build_region_boundary_cache called with snap_type = %1"), snap_type) << endmsg;
+               fatal << string_compose (_("build_region_boundary_cache called with snap_type = %1"), _snap_type) << endmsg;
                /*NOTREACHED*/
                return;
        }
-       
+
        TimeAxisView *ontrack = 0;
+       TrackViewList tlist;
+
+       if (!selection->tracks.empty()) {
+               tlist = selection->tracks;
+       } else {
+               tlist = track_views;
+       }
 
-       while (pos < session->current_end_frame()) {
+       while (pos < session->current_end_frame() && !at_end) {
 
-               if (!selection->tracks.empty()) {
+               nframes64_t rpos;
+               nframes64_t lpos = max_frames;
 
-                       if ((r = find_next_region (pos, point, 1, selection->tracks, &ontrack)) == 0) {
-                               break;
-                       }
+               for (vector<RegionPoint>::iterator p = interesting_points.begin(); p != interesting_points.end(); ++p) {
 
-               } else if (clicked_trackview) {
+                       if ((r = find_next_region (pos, *p, 1, tlist, &ontrack)) == 0) {
+                               if (*p == interesting_points.back()) {
+                                       at_end = true;
+                               }
+                               /* move to next point type */
+                               continue;
+                       }
 
-                       TrackViewList t;
-                       t.push_back (clicked_trackview);
+                       switch (*p) {
+                       case Start:
+                               rpos = r->first_frame();
+                               break;
 
-                       if ((r = find_next_region (pos, point, 1, t, &ontrack)) == 0) {
+                       case End:
+                               rpos = r->last_frame();
                                break;
-                       }
 
-               } else {
+                       case SyncPoint:
+                               rpos = r->sync_position ();
+                               //r->adjust_to_sync (r->first_frame());
+                               break;
 
-                       if ((r = find_next_region (pos, point, 1, track_views, &ontrack)) == 0) {
+                       default:
                                break;
                        }
-               }
 
-               jack_nframes_t rpos;
-               
-               switch (snap_type) {
-               case SnapToRegionStart:
-                       rpos = r->first_frame();
-                       break;
-               case SnapToRegionEnd:
-                       rpos = r->last_frame();
-                       break;  
-               case SnapToRegionSync:
-                       rpos = r->adjust_to_sync (r->first_frame());
-                       break;
+                       float speed = 1.0f;
+                       RouteTimeAxisView *rtav;
 
-               case SnapToRegionBoundary:
-                       rpos = r->last_frame();
-                       break;  
-               default:
-                       break;
-               }
-               
-               float speed = 1.0f;
-               AudioTimeAxisView *atav;
+                       if (ontrack != 0 && (rtav = dynamic_cast<RouteTimeAxisView*>(ontrack)) != 0 ) {
+                               if (rtav->get_diskstream() != 0) {
+                                       speed = rtav->get_diskstream()->speed();
+                               }
+                       }
 
-               if ( ontrack != 0 && (atav = dynamic_cast<AudioTimeAxisView*>(ontrack)) != 0 ) {
-                       if (atav->get_diskstream() != 0) {
-                               speed = atav->get_diskstream()->speed();
+                       rpos = track_frame_to_session_frame (rpos, speed);
+
+                       if (rpos < lpos) {
+                               lpos = rpos;
                        }
-               }
 
-               rpos = track_frame_to_session_frame(rpos, speed);
+                       /* prevent duplicates, but we don't use set<> because we want to be able
+                          to sort later.
+                       */
+
+                       vector<nframes64_t>::iterator ri;
+
+                       for (ri = region_boundary_cache.begin(); ri != region_boundary_cache.end(); ++ri) {
+                               if (*ri == rpos) {
+                                       break;
+                               }
+                       }
 
-               if (region_boundary_cache.empty() || rpos != region_boundary_cache.back()) {
-                       if (snap_type == SnapToRegionBoundary) {
-                               region_boundary_cache.push_back (r->first_frame());
+                       if (ri == region_boundary_cache.end()) {
+                               region_boundary_cache.push_back (rpos);
                        }
-                       region_boundary_cache.push_back (rpos);
                }
 
-               pos = rpos + 1;
+               pos = lpos + 1;
        }
+
+       /* finally sort to be sure that the order is correct */
+
+       sort (region_boundary_cache.begin(), region_boundary_cache.end());
 }
 
-Region*
-Editor::find_next_region (jack_nframes_t frame, RegionPoint point, int32_t dir, TrackViewList& tracks, TimeAxisView **ontrack)
+boost::shared_ptr<Region>
+Editor::find_next_region (nframes64_t frame, RegionPoint point, int32_t dir, TrackViewList& tracks, TimeAxisView **ontrack)
 {
        TrackViewList::iterator i;
-       jack_nframes_t closest = max_frames;
-       Region* ret = 0;
-       jack_nframes_t rpos = 0;
+       nframes64_t closest = max_frames;
+       boost::shared_ptr<Region> ret;
+       nframes64_t rpos = 0;
 
        float track_speed;
-       jack_nframes_t track_frame;
-       AudioTimeAxisView *atav;
+       nframes64_t track_frame;
+       RouteTimeAxisView *rtav;
 
        for (i = tracks.begin(); i != tracks.end(); ++i) {
 
-               jack_nframes_t distance;
-               Region* r;
+               nframes64_t distance;
+               boost::shared_ptr<Region> r;
 
                track_speed = 1.0f;
-               if ( (atav = dynamic_cast<AudioTimeAxisView*>(*i)) != 0 ) {
-                       if (atav->get_diskstream()!=0)
-                               track_speed = atav->get_diskstream()->speed();
+               if ( (rtav = dynamic_cast<RouteTimeAxisView*>(*i)) != 0 ) {
+                       if (rtav->get_diskstream()!=0)
+                               track_speed = rtav->get_diskstream()->speed();
                }
 
                track_frame = session_frame_to_track_frame(frame, track_speed);
@@ -616,9 +719,11 @@ Editor::find_next_region (jack_nframes_t frame, RegionPoint point, int32_t dir,
                        break;
 
                case SyncPoint:
-                       rpos = r->adjust_to_sync (r->first_frame());
+                       rpos = r->sync_position ();
+                       // r->adjust_to_sync (r->first_frame());
                        break;
                }
+
                // rpos is a "track frame", converting it to "session frame"
                rpos = track_frame_to_session_frame(rpos, track_speed);
 
@@ -639,94 +744,201 @@ Editor::find_next_region (jack_nframes_t frame, RegionPoint point, int32_t dir,
        return ret;
 }
 
-void
-Editor::cursor_to_region_point (Cursor* cursor, RegionPoint point, int32_t dir)
+nframes64_t
+Editor::find_next_region_boundary (nframes64_t pos, int32_t dir, const TrackViewList& tracks)
 {
-       Region* r;
-       jack_nframes_t pos = cursor->current_frame;
+       nframes64_t distance = max_frames;
+       nframes64_t current_nearest = -1;
 
-       if (!session) {
-               return;
-       }
 
-       TimeAxisView *ontrack = 0;
+       for (TrackViewList::const_iterator i = tracks.begin(); i != tracks.end(); ++i) {
+               nframes64_t contender;
+               nframes64_t d;
 
-       // so we don't find the current region again..
-       if (dir>0 || pos>0)
-               pos+=dir;
+               RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
 
-       if (!selection->tracks.empty()) {
-               
-               r = find_next_region (pos, point, dir, selection->tracks, &ontrack);
-               
-       } else if (clicked_trackview) {
-               
-               TrackViewList t;
-               t.push_back (clicked_trackview);
-               
-               r = find_next_region (pos, point, dir, t, &ontrack);
-               
-       } else {
-               
-               r = find_next_region (pos, point, dir, track_views, &ontrack);
-       }
+               if (!rtv) {
+                       continue;
+               }
 
-       if (r == 0) {
-               return;
-       }
-       
-       switch (point){
-       case Start:
-               pos = r->first_frame ();
-               break;
+               if ((contender = rtv->find_next_region_boundary (pos, dir)) < 0) {
+                       continue;
+               }
 
-       case End:
-               pos = r->last_frame ();
-               break;
+               d = ::llabs (pos - contender);
 
-       case SyncPoint:
-               pos = r->adjust_to_sync (r->first_frame());
-               break;  
+               if (d < distance) {
+                       current_nearest = contender;
+                       distance = d;
+               }
        }
-       
-       float speed = 1.0f;
-       AudioTimeAxisView *atav;
 
-       if ( ontrack != 0 && (atav = dynamic_cast<AudioTimeAxisView*>(ontrack)) != 0 ) {
-               if (atav->get_diskstream() != 0) {
-                       speed = atav->get_diskstream()->speed();
+       return current_nearest;
+}
+
+nframes64_t
+Editor::get_region_boundary (nframes64_t pos, int32_t dir, bool with_selection, bool only_onscreen)
+{
+       nframes64_t target;
+       TrackViewList tvl;
+
+       if (with_selection && Config->get_region_boundaries_from_selected_tracks()) {
+
+               if (!selection->tracks.empty()) {
+
+                       target = find_next_region_boundary (pos, dir, selection->tracks);
+
+               } else {
+
+                       if (only_onscreen || Config->get_region_boundaries_from_onscreen_tracks()) {
+                               get_onscreen_tracks (tvl);
+                               target = find_next_region_boundary (pos, dir, tvl);
+                       } else {
+                               target = find_next_region_boundary (pos, dir, track_views);
+                       }
                }
-       }
 
-       pos = track_frame_to_session_frame(pos, speed);
-       
-       if (cursor == playhead_cursor) {
-               session->request_locate (pos);
        } else {
-               cursor->set_position (pos);
+
+               if (only_onscreen || Config->get_region_boundaries_from_onscreen_tracks()) {
+                       get_onscreen_tracks (tvl);
+                       target = find_next_region_boundary (pos, dir, tvl);
+               } else {
+                       target = find_next_region_boundary (pos, dir, track_views);
+               }
        }
-}
 
-void
-Editor::cursor_to_next_region_point (Cursor* cursor, RegionPoint point)
-{
-       cursor_to_region_point (cursor, point, 1);
+       return target;
 }
 
 void
-Editor::cursor_to_previous_region_point (Cursor* cursor, RegionPoint point)
+Editor::cursor_to_region_boundary (bool with_selection, int32_t dir)
 {
-       cursor_to_region_point (cursor, point, -1);
+       nframes64_t pos = playhead_cursor->current_frame;
+       nframes64_t target;
+
+       if (!session) {
+               return;
+       }
+
+       // so we don't find the current region again..
+       if (dir > 0 || pos > 0) {
+               pos += dir;
+       }
+
+       if ((target = get_region_boundary (pos, dir, with_selection, false)) < 0) {
+               return;
+       }
+
+
+       session->request_locate (target);
+}
+
+void
+Editor::cursor_to_next_region_boundary (bool with_selection)
+{
+       cursor_to_region_boundary (with_selection, 1);
+}
+
+void
+Editor::cursor_to_previous_region_boundary (bool with_selection)
+{
+       cursor_to_region_boundary (with_selection, -1);
+}
+
+void
+Editor::cursor_to_region_point (EditorCursor* cursor, RegionPoint point, int32_t dir)
+{
+       boost::shared_ptr<Region> r;
+       nframes64_t pos = cursor->current_frame;
+
+       if (!session) {
+               return;
+       }
+
+       TimeAxisView *ontrack = 0;
+
+       // so we don't find the current region again..
+       if (dir>0 || pos>0)
+               pos+=dir;
+
+       if (!selection->tracks.empty()) {
+
+               r = find_next_region (pos, point, dir, selection->tracks, &ontrack);
+
+       } else if (clicked_axisview) {
+
+               TrackViewList t;
+               t.push_back (clicked_axisview);
+
+               r = find_next_region (pos, point, dir, t, &ontrack);
+
+       } else {
+
+               r = find_next_region (pos, point, dir, track_views, &ontrack);
+       }
+
+       if (r == 0) {
+               return;
+       }
+
+       switch (point){
+       case Start:
+               pos = r->first_frame ();
+               break;
+
+       case End:
+               pos = r->last_frame ();
+               break;
+
+       case SyncPoint:
+               pos = r->sync_position ();
+               // r->adjust_to_sync (r->first_frame());
+               break;
+       }
+
+       float speed = 1.0f;
+       RouteTimeAxisView *rtav;
+
+       if ( ontrack != 0 && (rtav = dynamic_cast<RouteTimeAxisView*>(ontrack)) != 0 ) {
+               if (rtav->get_diskstream() != 0) {
+                       speed = rtav->get_diskstream()->speed();
+               }
+       }
+
+       pos = track_frame_to_session_frame(pos, speed);
+
+       if (cursor == playhead_cursor) {
+               session->request_locate (pos);
+       } else {
+               cursor->set_position (pos);
+       }
+}
+
+void
+Editor::cursor_to_next_region_point (EditorCursor* cursor, RegionPoint point)
+{
+       cursor_to_region_point (cursor, point, 1);
+}
+
+void
+Editor::cursor_to_previous_region_point (EditorCursor* cursor, RegionPoint point)
+{
+       cursor_to_region_point (cursor, point, -1);
 }
 
 void
-Editor::cursor_to_selection_start (Cursor *cursor)
+Editor::cursor_to_selection_start (EditorCursor *cursor)
 {
-       jack_nframes_t pos = 0;
+       nframes64_t pos = 0;
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
        switch (mouse_mode) {
        case MouseObject:
-               if (!selection->audio_regions.empty()) {
-                       pos = selection->audio_regions.start();
+               if (!rs.empty()) {
+                       pos = rs.start();
                }
                break;
 
@@ -748,14 +960,17 @@ Editor::cursor_to_selection_start (Cursor *cursor)
 }
 
 void
-Editor::cursor_to_selection_end (Cursor *cursor)
+Editor::cursor_to_selection_end (EditorCursor *cursor)
 {
-       jack_nframes_t pos = 0;
+       nframes64_t pos = 0;
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
 
        switch (mouse_mode) {
        case MouseObject:
-               if (!selection->audio_regions.empty()) {
-                       pos = selection->audio_regions.end_frame();
+               if (!rs.empty()) {
+                       pos = rs.end_frame();
                }
                break;
 
@@ -777,2946 +992,5530 @@ Editor::cursor_to_selection_end (Cursor *cursor)
 }
 
 void
-Editor::playhead_backward ()
+Editor::selected_marker_to_region_boundary (bool with_selection, int32_t dir)
 {
-       jack_nframes_t pos;
-       jack_nframes_t cnt;
-       float prefix;
-       bool was_floating;
+       nframes64_t target;
+       Location* loc;
+       bool ignored;
 
-       if (get_prefix (prefix, was_floating)) {
-               cnt = 1;
-       } else {
-               if (was_floating) {
-                       cnt = (jack_nframes_t) floor (prefix * session->frame_rate ());
-               } else {
-                       cnt = (jack_nframes_t) prefix;
-               }
+       if (!session) {
+               return;
        }
 
-       pos = playhead_cursor->current_frame;
+       if (selection->markers.empty()) {
+               nframes64_t mouse;
+               bool ignored;
 
-       if ((jack_nframes_t) pos < cnt) {
-               pos = 0;
-       } else {
-               pos -= cnt;
+               if (!mouse_frame (mouse, ignored)) {
+                       return;
+               }
+
+               add_location_mark (mouse);
        }
-       
-       /* XXX this is completely insane. with the current buffering
-          design, we'll force a complete track buffer flush and
-          reload, just to move 1 sample !!!
-       */
 
-       session->request_locate (pos);
-}
+       if ((loc = find_location_from_marker (selection->markers.front(), ignored)) == 0) {
+               return;
+       }
 
-void
-Editor::playhead_forward ()
-{
-       jack_nframes_t pos;
-       jack_nframes_t cnt;
-       bool was_floating;
-       float prefix;
+       nframes64_t pos = loc->start();
 
-       if (get_prefix (prefix, was_floating)) {
-               cnt = 1;
-       } else {
-               if (was_floating) {
-                       cnt = (jack_nframes_t) floor (prefix * session->frame_rate ());
-               } else {
-                       cnt = (jack_nframes_t) floor (prefix);
-               }
+       // so we don't find the current region again..
+       if (dir > 0 || pos > 0) {
+               pos += dir;
        }
 
-       pos = playhead_cursor->current_frame;
-       
-       /* XXX this is completely insane. with the current buffering
-          design, we'll force a complete track buffer flush and
-          reload, just to move 1 sample !!!
-       */
+       if ((target = get_region_boundary (pos, dir, with_selection, false)) < 0) {
+               return;
+       }
 
-       session->request_locate (pos+cnt);
+       loc->move_to (target);
 }
 
 void
-Editor::cursor_align (bool playhead_to_edit)
+Editor::selected_marker_to_next_region_boundary (bool with_selection)
 {
-       if (playhead_to_edit) {
-               if (session) {
-                       session->request_locate (edit_cursor->current_frame);
-               }
-       } else {
-               edit_cursor->set_position (playhead_cursor->current_frame);
-       }
+       selected_marker_to_region_boundary (with_selection, 1);
 }
 
 void
-Editor::edit_cursor_backward ()
+Editor::selected_marker_to_previous_region_boundary (bool with_selection)
 {
-       jack_nframes_t pos;
-       jack_nframes_t cnt;
-       float prefix;
-       bool was_floating;
-
-       if (get_prefix (prefix, was_floating)) {
-               cnt = 1;
-       } else {
-               if (was_floating) {
-                       cnt = (jack_nframes_t) floor (prefix * session->frame_rate ());
-               } else {
-                       cnt = (jack_nframes_t) prefix;
-               }
-       }
-
-       pos = edit_cursor->current_frame;
-
-       if ((jack_nframes_t) pos < cnt) {
-               pos = 0;
-       } else {
-               pos -= cnt;
-       }
-       
-       edit_cursor->set_position (pos);
+       selected_marker_to_region_boundary (with_selection, -1);
 }
 
 void
-Editor::edit_cursor_forward ()
+Editor::selected_marker_to_region_point (RegionPoint point, int32_t dir)
 {
-       jack_nframes_t pos;
-       jack_nframes_t cnt;
-       bool was_floating;
-       float prefix;
+       boost::shared_ptr<Region> r;
+       nframes64_t pos;
+       Location* loc;
+       bool ignored;
 
-       if (get_prefix (prefix, was_floating)) {
-               cnt = 1;
-       } else {
-               if (was_floating) {
-                       cnt = (jack_nframes_t) floor (prefix * session->frame_rate ());
-               } else {
-                       cnt = (jack_nframes_t) floor (prefix);
-               }
+       if (!session || selection->markers.empty()) {
+               return;
        }
 
-       pos = edit_cursor->current_frame;
-       edit_cursor->set_position (pos+cnt);
-}
-
-void
-Editor::goto_frame ()
-{
-       float prefix;
-       bool was_floating;
-       jack_nframes_t frame;
-
-       if (get_prefix (prefix, was_floating)) {
+       if ((loc = find_location_from_marker (selection->markers.front(), ignored)) == 0) {
                return;
        }
 
-       if (was_floating) {
-               frame = (jack_nframes_t) floor (prefix * session->frame_rate());
-       } else {
-               frame = (jack_nframes_t) floor (prefix);
-       }
+       TimeAxisView *ontrack = 0;
 
-       session->request_locate (frame);
-}
+       pos = loc->start();
+
+       // so we don't find the current region again..
+       if (dir>0 || pos>0)
+               pos+=dir;
+
+       if (!selection->tracks.empty()) {
+
+               r = find_next_region (pos, point, dir, selection->tracks, &ontrack);
 
-void
-Editor::scroll_backward (float pages)
-{
-       jack_nframes_t frame;
-       jack_nframes_t one_page = (jack_nframes_t) rint (canvas_width * frames_per_unit);
-       bool was_floating;
-       float prefix;
-       jack_nframes_t cnt;
-       
-       if (get_prefix (prefix, was_floating)) {
-               cnt = (jack_nframes_t) floor (pages * one_page);
        } else {
-               if (was_floating) {
-                       cnt = (jack_nframes_t) floor (prefix * session->frame_rate());
-               } else {
-                       cnt = (jack_nframes_t) floor (prefix * one_page);
-               }
+
+               r = find_next_region (pos, point, dir, track_views, &ontrack);
        }
 
-       if (leftmost_frame < cnt) {
-               frame = 0;
-       } else {
-               frame = leftmost_frame - cnt;
+       if (r == 0) {
+               return;
        }
 
-       reposition_x_origin (frame);
-}
+       switch (point){
+       case Start:
+               pos = r->first_frame ();
+               break;
 
-void
-Editor::scroll_forward (float pages)
-{
-       jack_nframes_t frame;
-       jack_nframes_t one_page = (jack_nframes_t) rint (canvas_width * frames_per_unit);
-       bool was_floating;
-       float prefix;
-       jack_nframes_t cnt;
-       
-       if (get_prefix (prefix, was_floating)) {
-               cnt = (jack_nframes_t) floor (pages * one_page);
-       } else {
-               if (was_floating) {
-                       cnt = (jack_nframes_t) floor (prefix * session->frame_rate());
-               } else {
-                       cnt = (jack_nframes_t) floor (prefix * one_page);
-               }
+       case End:
+               pos = r->last_frame ();
+               break;
+
+       case SyncPoint:
+               pos = r->adjust_to_sync (r->first_frame());
+               break;
        }
 
-       if (ULONG_MAX - cnt < leftmost_frame) {
-               frame = ULONG_MAX - cnt;
-       } else {
-               frame = leftmost_frame + cnt;
+       float speed = 1.0f;
+       RouteTimeAxisView *rtav;
+
+       if (ontrack != 0 && (rtav = dynamic_cast<RouteTimeAxisView*>(ontrack)) != 0) {
+               if (rtav->get_diskstream() != 0) {
+                       speed = rtav->get_diskstream()->speed();
+               }
        }
 
-       reposition_x_origin (frame);
+       pos = track_frame_to_session_frame(pos, speed);
+
+       loc->move_to (pos);
 }
 
 void
-Editor::scroll_tracks_down ()
+Editor::selected_marker_to_next_region_point (RegionPoint point)
 {
-       float prefix;
-       bool was_floating;
-       int cnt;
-
-       if (get_prefix (prefix, was_floating)) {
-               cnt = 1;
-       } else {
-               cnt = (int) floor (prefix);
-       }
+       selected_marker_to_region_point (point, 1);
+}
 
-       vertical_adjustment.set_value (vertical_adjustment.get_value() + (cnt * vertical_adjustment.get_page_size()));
+void
+Editor::selected_marker_to_previous_region_point (RegionPoint point)
+{
+       selected_marker_to_region_point (point, -1);
 }
 
 void
-Editor::scroll_tracks_up ()
+Editor::selected_marker_to_selection_start ()
 {
-       float prefix;
-       bool was_floating;
-       int cnt;
+       nframes64_t pos = 0;
+       Location* loc;
+       bool ignored;
 
-       if (get_prefix (prefix, was_floating)) {
-               cnt = 1;
-       } else {
-               cnt = (int) floor (prefix);
+       if (!session || selection->markers.empty()) {
+               return;
        }
 
-       vertical_adjustment.set_value (vertical_adjustment.get_value() - (cnt * vertical_adjustment.get_page_size()));
-}
+       if ((loc = find_location_from_marker (selection->markers.front(), ignored)) == 0) {
+               return;
+       }
 
-void
-Editor::scroll_tracks_down_line ()
-{
-        Gtk::Adjustment* adj = edit_vscrollbar.get_adjustment();
-       adj->set_value (adj->get_value() + 10);
-}
+       RegionSelection rs;
 
-void
-Editor::scroll_tracks_up_line ()
-{
-        Gtk::Adjustment* adj = edit_vscrollbar.get_adjustment();
-       adj->set_value (adj->get_value() - 10);
-}
+       get_regions_for_action (rs);
 
-/* ZOOM */
+       switch (mouse_mode) {
+       case MouseObject:
+               if (!rs.empty()) {
+                       pos = rs.start();
+               }
+               break;
 
-void
-Editor::temporal_zoom_step (bool coarser)
-{
-       double nfpu;
+       case MouseRange:
+               if (!selection->time.empty()) {
+                       pos = selection->time.start ();
+               }
+               break;
 
-       nfpu = frames_per_unit;
-       
-       if (coarser) { 
-               nfpu *= 2.0;
-       } else { 
-               nfpu = max(1.0,(nfpu/2.0));
+       default:
+               return;
        }
 
-       temporal_zoom (nfpu);
-}      
+       loc->move_to (pos);
+}
 
 void
-Editor::temporal_zoom (gdouble fpu)
+Editor::selected_marker_to_selection_end ()
 {
-       if (!session) return;
-       
-       jack_nframes_t current_page = current_page_frames();
-       jack_nframes_t current_leftmost = leftmost_frame;
-       jack_nframes_t current_rightmost;
-       jack_nframes_t current_center;
-       jack_nframes_t new_page;
-       jack_nframes_t leftmost_after_zoom = 0;
-       double nfpu;
+       nframes64_t pos = 0;
+       Location* loc;
+       bool ignored;
 
-       nfpu = fpu;
-       
-       new_page = (jack_nframes_t) floor (canvas_width * nfpu);
+       if (!session || selection->markers.empty()) {
+               return;
+       }
 
-       switch (zoom_focus) {
-       case ZoomFocusLeft:
-               leftmost_after_zoom = current_leftmost;
-               break;
-               
-       case ZoomFocusRight:
-               current_rightmost = leftmost_frame + current_page;
-               if (current_rightmost > new_page) {
-                       leftmost_after_zoom = current_rightmost - new_page;
-               } else {
-                       leftmost_after_zoom = 0;
+       if ((loc = find_location_from_marker (selection->markers.front(), ignored)) == 0) {
+               return;
+       }
+
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       switch (mouse_mode) {
+       case MouseObject:
+               if (!rs.empty()) {
+                       pos = rs.end_frame();
                }
                break;
-               
-       case ZoomFocusCenter:
-               current_center = current_leftmost + (current_page/2); 
-               if (current_center > (new_page/2)) {
-                       leftmost_after_zoom = current_center - (new_page / 2);
-               } else {
-                       leftmost_after_zoom = 0;
+
+       case MouseRange:
+               if (!selection->time.empty()) {
+                       pos = selection->time.end_frame ();
                }
                break;
-               
-       case ZoomFocusPlayhead:
-               /* try to keep the playhead in the center */
-               if (playhead_cursor->current_frame > new_page/2) {
-                       leftmost_after_zoom = playhead_cursor->current_frame - (new_page/2);
+
+       default:
+               return;
+       }
+
+       loc->move_to (pos);
+}
+
+void
+Editor::scroll_playhead (bool forward)
+{
+       nframes64_t pos = playhead_cursor->current_frame;
+       nframes64_t delta = (nframes64_t) floor (current_page_frames() / 0.8);
+
+       if (forward) {
+               if (pos == max_frames) {
+                       return;
+               }
+
+               if (pos < max_frames - delta) {
+                       pos += delta ;
                } else {
-                       leftmost_after_zoom = 0;
+                       pos = max_frames;
                }
-               break;
 
-       case ZoomFocusEdit:
-               /* try to keep the edit cursor in the center */
-               if (edit_cursor->current_frame > leftmost_frame + (new_page/2)) {
-                       leftmost_after_zoom = edit_cursor->current_frame - (new_page/2);
+       } else {
+
+               if (pos == 0) {
+                       return;
+               }
+
+               if (pos > delta) {
+                       pos -= delta;
                } else {
-                       leftmost_after_zoom = 0;
+                       pos = 0;
                }
-               break;
-               
        }
-       // leftmost_after_zoom = min (leftmost_after_zoom, session->current_end_frame());
-
-//     begin_reversible_command (_("zoom"));
-//     session->add_undo (bind (mem_fun(*this, &Editor::reposition_and_zoom), current_leftmost, frames_per_unit));
-//     session->add_redo (bind (mem_fun(*this, &Editor::reposition_and_zoom), leftmost_after_zoom, nfpu));
-//     commit_reversible_command ();
 
-       reposition_and_zoom (leftmost_after_zoom, nfpu);
-}      
+       session->request_locate (pos);
+}
 
 void
-Editor::temporal_zoom_selection ()
+Editor::playhead_backward ()
 {
-       if (!selection) return;
-       
-       if (selection->time.empty()) {
-               return;
+       nframes64_t pos;
+       nframes64_t cnt;
+       float prefix;
+       bool was_floating;
+
+       if (get_prefix (prefix, was_floating)) {
+               cnt = 1;
+       } else {
+               if (was_floating) {
+                       cnt = (nframes64_t) floor (prefix * session->frame_rate ());
+               } else {
+                       cnt = (nframes64_t) prefix;
+               }
        }
 
-       jack_nframes_t start = selection->time[clicked_selection].start;
-       jack_nframes_t end = selection->time[clicked_selection].end;
+       pos = playhead_cursor->current_frame;
 
-       temporal_zoom_by_frame (start, end, "zoom to selection");
+       if ((nframes64_t) pos < cnt) {
+               pos = 0;
+       } else {
+               pos -= cnt;
+       }
+
+       /* XXX this is completely insane. with the current buffering
+          design, we'll force a complete track buffer flush and
+          reload, just to move 1 sample !!!
+       */
+
+       session->request_locate (pos);
 }
 
 void
-Editor::temporal_zoom_session ()
+Editor::playhead_forward ()
 {
-       if (session) {
-               temporal_zoom_by_frame (0, session->current_end_frame(), "zoom to session");
+       nframes64_t pos;
+       nframes64_t cnt;
+       bool was_floating;
+       float prefix;
+
+       if (get_prefix (prefix, was_floating)) {
+               cnt = 1;
+       } else {
+               if (was_floating) {
+                       cnt = (nframes64_t) floor (prefix * session->frame_rate ());
+               } else {
+                       cnt = (nframes64_t) floor (prefix);
+               }
        }
+
+       pos = playhead_cursor->current_frame;
+
+       /* XXX this is completely insane. with the current buffering
+          design, we'll force a complete track buffer flush and
+          reload, just to move 1 sample !!!
+       */
+
+       session->request_locate (pos+cnt);
 }
 
 void
-Editor::temporal_zoom_by_frame (jack_nframes_t start, jack_nframes_t end, const string & op)
+Editor::cursor_align (bool playhead_to_edit)
 {
-       if (!session) return;
-
-       if ((start == 0 && end == 0) || end < start) {
+       if (!session) {
                return;
        }
 
-       jack_nframes_t range = end - start;
+       if (playhead_to_edit) {
+
+               if (selection->markers.empty()) {
+                       return;
+               }
 
-       double new_fpu = (double)range / (double)canvas_width;
-//     double p2 = 1.0;
+               session->request_locate (selection->markers.front()->position(), session->transport_rolling());
 
-//     while (p2 < new_fpu) {
-//             p2 *= 2.0;
-//     }
-//     new_fpu = p2;
-       
-       jack_nframes_t new_page = (jack_nframes_t) floor (canvas_width * new_fpu);
-       jack_nframes_t middle = (jack_nframes_t) floor( (double)start + ((double)range / 2.0f ));
-       jack_nframes_t new_leftmost = (jack_nframes_t) floor( (double)middle - ((double)new_page/2.0f));
+       } else {
+               /* move selected markers to playhead */
 
-       if (new_leftmost > middle) new_leftmost = 0;
+               for (MarkerSelection::iterator i = selection->markers.begin(); i != selection->markers.end(); ++i) {
+                       bool ignored;
 
-//     begin_reversible_command (op);
-//     session->add_undo (bind (mem_fun(*this, &Editor::reposition_and_zoom), leftmost_frame, frames_per_unit));
-//     session->add_redo (bind (mem_fun(*this, &Editor::reposition_and_zoom), new_leftmost, new_fpu));
-//     commit_reversible_command ();
+                       Location* loc = find_location_from_marker (*i, ignored);
 
-       reposition_and_zoom (new_leftmost, new_fpu);
+                       if (loc->is_mark()) {
+                               loc->set_start (playhead_cursor->current_frame);
+                       } else {
+                               loc->set (playhead_cursor->current_frame,
+                                         playhead_cursor->current_frame + loc->length());
+                       }
+               }
+       }
 }
 
-void 
-Editor::temporal_zoom_to_frame (bool coarser, jack_nframes_t frame)
+void
+Editor::edit_cursor_backward ()
 {
-       if (!session) return;
-       
-       jack_nframes_t range_before = frame - leftmost_frame;
-       double new_fpu;
-       
-       new_fpu = frames_per_unit;
-       
-       if (coarser) { 
-               new_fpu *= 2.0;
-               range_before *= 2;
-       } else { 
-               new_fpu = max(1.0,(new_fpu/2.0));
-               range_before /= 2;
+       nframes64_t pos;
+       nframes64_t cnt;
+       float prefix;
+       bool was_floating;
+
+       if (get_prefix (prefix, was_floating)) {
+               cnt = 1;
+       } else {
+               if (was_floating) {
+                       cnt = (nframes64_t) floor (prefix * session->frame_rate ());
+               } else {
+                       cnt = (nframes64_t) prefix;
+               }
        }
 
-       if (new_fpu == frames_per_unit) return;
+       if ((pos = get_preferred_edit_position()) < 0) {
+               return;
+       }
 
-       jack_nframes_t new_leftmost = frame - range_before;
+       if (pos < cnt) {
+               pos = 0;
+       } else {
+               pos -= cnt;
+       }
 
-       if (new_leftmost > frame) new_leftmost = 0;
+       // EDIT CURSOR edit_cursor->set_position (pos);
+}
 
-//     begin_reversible_command (_("zoom to frame"));
-//     session->add_undo (bind (mem_fun(*this, &Editor::reposition_and_zoom), leftmost_frame, frames_per_unit));
-//     session->add_redo (bind (mem_fun(*this, &Editor::reposition_and_zoom), new_leftmost, new_fpu));
-//     commit_reversible_command ();
+void
+Editor::edit_cursor_forward ()
+{
+       //nframes64_t pos;
+       nframes64_t cnt;
+       bool was_floating;
+       float prefix;
 
-       reposition_and_zoom (new_leftmost, new_fpu);
+       if (get_prefix (prefix, was_floating)) {
+               cnt = 1;
+       } else {
+               if (was_floating) {
+                       cnt = (nframes64_t) floor (prefix * session->frame_rate ());
+               } else {
+                       cnt = (nframes64_t) floor (prefix);
+               }
+       }
+
+       // pos = edit_cursor->current_frame;
+       // EDIT CURSOR edit_cursor->set_position (pos+cnt);
 }
 
 void
-Editor::select_all_in_track (bool add)
+Editor::goto_frame ()
 {
-       list<Selectable *> touched;
+       float prefix;
+       bool was_floating;
+       nframes64_t frame;
 
-       if (!clicked_trackview) {
+       if (get_prefix (prefix, was_floating)) {
                return;
        }
-       
-       clicked_trackview->get_selectables (0, max_frames, 0, DBL_MAX, touched);
 
-       if (add) {
-               selection->add (touched);
+       if (was_floating) {
+               frame = (nframes64_t) floor (prefix * session->frame_rate());
        } else {
-               selection->set (touched);
+               frame = (nframes64_t) floor (prefix);
        }
+
+       session->request_locate (frame);
 }
 
 void
-Editor::select_all (bool add)
+Editor::scroll_backward (float pages)
 {
-       list<Selectable *> touched;
-       
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
+       nframes64_t frame;
+       nframes64_t one_page = (nframes64_t) rint (_canvas_width * frames_per_unit);
+       bool was_floating;
+       float prefix;
+       nframes64_t cnt;
+
+       if (get_prefix (prefix, was_floating)) {
+               cnt = (nframes64_t) floor (pages * one_page);
+       } else {
+               if (was_floating) {
+                       cnt = (nframes64_t) floor (prefix * session->frame_rate());
+               } else {
+                       cnt = (nframes64_t) floor (prefix * one_page);
                }
-               (*iter)->get_selectables (0, max_frames, 0, DBL_MAX, touched);
        }
 
-       if (add) {
-               selection->add (touched);
+       if (leftmost_frame < cnt) {
+               frame = 0;
        } else {
-               selection->set (touched);
+               frame = leftmost_frame - cnt;
        }
 
+       reset_x_origin (frame);
 }
 
 void
-Editor::invert_selection_in_track ()
+Editor::scroll_forward (float pages)
 {
-       list<Selectable *> touched;
+       nframes64_t frame;
+       nframes64_t one_page = (nframes64_t) rint (_canvas_width * frames_per_unit);
+       bool was_floating;
+       float prefix;
+       nframes64_t cnt;
 
-       if (!clicked_trackview) {
-               return;
+       if (get_prefix (prefix, was_floating)) {
+               cnt = (nframes64_t) floor (pages * one_page);
+       } else {
+               if (was_floating) {
+                       cnt = (nframes64_t) floor (prefix * session->frame_rate());
+               } else {
+                       cnt = (nframes64_t) floor (prefix * one_page);
+               }
+       }
+
+       if (max_frames - cnt < leftmost_frame) {
+               frame = max_frames - cnt;
+       } else {
+               frame = leftmost_frame + cnt;
        }
-       
-       clicked_trackview->get_inverted_selectables (*selection, touched);
-       selection->set (touched);
+
+       reset_x_origin (frame);
 }
 
 void
-Editor::invert_selection ()
+Editor::scroll_tracks_down ()
 {
-       list<Selectable *> touched;
-       
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_inverted_selectables (*selection, touched);
+       float prefix;
+       bool was_floating;
+       int cnt;
+
+       if (get_prefix (prefix, was_floating)) {
+               cnt = 1;
+       } else {
+               cnt = (int) floor (prefix);
        }
 
-       selection->set (touched);
+       double vert_value = vertical_adjustment.get_value() + (cnt *
+               vertical_adjustment.get_page_size());
+       if (vert_value > vertical_adjustment.get_upper() - _canvas_height) {
+               vert_value = vertical_adjustment.get_upper() - _canvas_height;
+       }
+       vertical_adjustment.set_value (vert_value);
 }
 
-bool
-Editor::select_all_within (jack_nframes_t start, jack_nframes_t end, double top, double bot, bool add)
+void
+Editor::scroll_tracks_up ()
 {
-       list<Selectable *> touched;
-       
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_selectables (start, end, top, bot, touched);
-       }
-       begin_reversible_command (_("select all within"));
-       if (add) {
-               selection->add (touched);
-       } else {
-               selection->set (touched);
+       float prefix;
+       bool was_floating;
+       int cnt;
+
+       if (get_prefix (prefix, was_floating)) {
+               cnt = 1;
+       } else {
+               cnt = (int) floor (prefix);
+       }
+
+       vertical_adjustment.set_value (vertical_adjustment.get_value() - (cnt * vertical_adjustment.get_page_size()));
+}
+
+void
+Editor::scroll_tracks_down_line ()
+{
+
+        Gtk::Adjustment* adj = edit_vscrollbar.get_adjustment();
+       double vert_value = adj->get_value() + 60;
+
+       if (vert_value>adj->get_upper() - _canvas_height) {
+               vert_value = adj->get_upper() - _canvas_height;
+       }
+       adj->set_value (vert_value);
+}
+
+void
+Editor::scroll_tracks_up_line ()
+{
+        Gtk::Adjustment* adj = edit_vscrollbar.get_adjustment();
+       adj->set_value (adj->get_value() - 60);
+}
+
+/* ZOOM */
+
+void
+Editor::tav_zoom_step (bool coarser)
+{
+       ENSURE_GUI_THREAD (bind (mem_fun (*this, &Editor::temporal_zoom_step), coarser));
+
+       _routes->suspend_redisplay ();
+
+       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+               TimeAxisView *tv = (static_cast<TimeAxisView*>(*i));
+                       tv->step_height (coarser);
+       }
+
+       _routes->resume_redisplay ();
+}
+
+void
+Editor::temporal_zoom_step (bool coarser)
+{
+       ENSURE_GUI_THREAD (bind (mem_fun (*this, &Editor::temporal_zoom_step), coarser));
+
+       double nfpu;
+
+       nfpu = frames_per_unit;
+
+       if (coarser) {
+               nfpu *= 1.61803399;
+       } else {
+               nfpu = max(1.0,(nfpu/1.61803399));
+       }
+
+       temporal_zoom (nfpu);
+}
+
+void
+Editor::temporal_zoom (gdouble fpu)
+{
+       if (!session) return;
+
+       nframes64_t current_page = current_page_frames();
+       nframes64_t current_leftmost = leftmost_frame;
+       nframes64_t current_rightmost;
+       nframes64_t current_center;
+       nframes64_t new_page_size;
+       nframes64_t half_page_size;
+       nframes64_t leftmost_after_zoom = 0;
+       nframes64_t where;
+       bool in_track_canvas;
+       double nfpu;
+       double l;
+
+       /* XXX this limit is also in ::set_frames_per_unit() */
+
+       if (frames_per_unit <= 2.0 && fpu <= frames_per_unit) {
+               return;
+       }
+
+       nfpu = fpu;
+
+       new_page_size = (nframes64_t) floor (_canvas_width * nfpu);
+       half_page_size = new_page_size / 2;
+
+       switch (zoom_focus) {
+       case ZoomFocusLeft:
+               leftmost_after_zoom = current_leftmost;
+               break;
+
+       case ZoomFocusRight:
+               current_rightmost = leftmost_frame + current_page;
+               if (current_rightmost < new_page_size) {
+                       leftmost_after_zoom = 0;
+               } else {
+                       leftmost_after_zoom = current_rightmost - new_page_size;
+               }
+               break;
+
+       case ZoomFocusCenter:
+               current_center = current_leftmost + (current_page/2);
+               if (current_center < half_page_size) {
+                       leftmost_after_zoom = 0;
+               } else {
+                       leftmost_after_zoom = current_center - half_page_size;
+               }
+               break;
+
+       case ZoomFocusPlayhead:
+               /* try to keep the playhead in the same place */
+
+               where = playhead_cursor->current_frame;
+
+               l = - ((new_page_size * ((where - current_leftmost)/(double)current_page)) - where);
+
+               if (l < 0) {
+                       leftmost_after_zoom = 0;
+               } else if (l > max_frames) {
+                       leftmost_after_zoom = max_frames - new_page_size;
+               } else {
+                       leftmost_after_zoom = (nframes64_t) l;
+               }
+               break;
+
+       case ZoomFocusMouse:
+               /* try to keep the mouse over the same point in the display */
+
+               if (!mouse_frame (where, in_track_canvas)) {
+                       /* use playhead instead */
+                       where = playhead_cursor->current_frame;
+
+                       if (where < half_page_size) {
+                               leftmost_after_zoom = 0;
+                       } else {
+                               leftmost_after_zoom = where - half_page_size;
+                       }
+
+               } else {
+
+                       l = - ((new_page_size * ((where - current_leftmost)/(double)current_page)) - where);
+
+                       if (l < 0) {
+                               leftmost_after_zoom = 0;
+                       } else if (l > max_frames) {
+                               leftmost_after_zoom = max_frames - new_page_size;
+                       } else {
+                               leftmost_after_zoom = (nframes64_t) l;
+                       }
+               }
+
+               break;
+
+       case ZoomFocusEdit:
+               /* try to keep the edit point in the same place */
+               where = get_preferred_edit_position ();
+
+               if (where > 0) {
+
+                       double l = - ((new_page_size * ((where - current_leftmost)/(double)current_page)) - where);
+
+                       if (l < 0) {
+                               leftmost_after_zoom = 0;
+                       } else if (l > max_frames) {
+                               leftmost_after_zoom = max_frames - new_page_size;
+                       } else {
+                               leftmost_after_zoom = (nframes64_t) l;
+                       }
+
+               } else {
+                       /* edit point not defined */
+                       return;
+               }
+               break;
+
+       }
+
+       // leftmost_after_zoom = min (leftmost_after_zoom, session->current_end_frame());
+
+       reposition_and_zoom (leftmost_after_zoom, nfpu);
+}
+
+void
+Editor::temporal_zoom_region (bool both_axes)
+{
+
+       nframes64_t start = max_frames;
+       nframes64_t end = 0;
+       RegionSelection rs;
+       set<TimeAxisView*> tracks;
+
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
+               return;
+       }
+
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+
+               if ((*i)->region()->position() < start) {
+                       start = (*i)->region()->position();
+               }
+
+               if ((*i)->region()->last_frame() + 1 > end) {
+                       end = (*i)->region()->last_frame() + 1;
+               }
+
+               tracks.insert (&((*i)->get_time_axis_view()));
+       }
+
+       /* now comes an "interesting" hack ... make sure we leave a little space
+          at each end of the editor so that the zoom doesn't fit the region
+          precisely to the screen.
+       */
+
+       GdkScreen* screen = gdk_screen_get_default ();
+       gint pixwidth = gdk_screen_get_width (screen);
+       gint mmwidth = gdk_screen_get_width_mm (screen);
+       double pix_per_mm = (double) pixwidth/ (double) mmwidth;
+       double one_centimeter_in_pixels = pix_per_mm * 10.0;
+
+       if ((start == 0 && end == 0) || end < start) {
+               return;
+       }
+
+       nframes64_t range = end - start;
+       double new_fpu = (double)range / (double)_canvas_width;
+       nframes64_t extra_samples = (nframes64_t) floor (one_centimeter_in_pixels * new_fpu);
+
+       if (start > extra_samples) {
+               start -= extra_samples;
+       } else {
+               start = 0;
+       }
+
+       if (max_frames - extra_samples > end) {
+               end += extra_samples;
+       } else {
+               end = max_frames;
+       }
+
+       if (both_axes) {
+               /* save visual state with track states included, and prevent
+                  set_frames_per_unit() from doing it again.
+               */
+               undo_visual_stack.push_back (current_visual_state(true));
+               no_save_visual = true;
+       }
+
+       temporal_zoom_by_frame (start, end, "zoom to region");
+
+       if (both_axes) {
+               uint32_t per_track_height = (uint32_t) floor ((_canvas_height - canvas_timebars_vsize - 10.0) / tracks.size());
+
+               /* set visible track heights appropriately */
+
+               for (set<TimeAxisView*>::iterator t = tracks.begin(); t != tracks.end(); ++t) {
+                       (*t)->set_height (per_track_height);
+               }
+
+               /* hide irrelevant tracks */
+
+               _routes->suspend_redisplay ();
+
+               for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+                       if (find (tracks.begin(), tracks.end(), (*i)) == tracks.end()) {
+                               hide_track_in_display (**i, true);
+                       }
+               }
+
+               _routes->resume_redisplay ();
+
+               vertical_adjustment.set_value (0.0);
+               no_save_visual = false;
+       }
+
+       redo_visual_stack.push_back (current_visual_state());
+}
+
+void
+Editor::zoom_to_region (bool both_axes)
+{
+       temporal_zoom_region (both_axes);
+}
+
+void
+Editor::temporal_zoom_selection ()
+{
+       if (!selection) return;
+
+       if (selection->time.empty()) {
+               return;
+       }
+
+       nframes64_t start = selection->time[clicked_selection].start;
+       nframes64_t end = selection->time[clicked_selection].end;
+
+       temporal_zoom_by_frame (start, end, "zoom to selection");
+}
+
+void
+Editor::temporal_zoom_session ()
+{
+       ENSURE_GUI_THREAD (mem_fun (*this, &Editor::temporal_zoom_session));
+
+       if (session) {
+               temporal_zoom_by_frame (session->current_start_frame(), session->current_end_frame(), "zoom to session");
+       }
+}
+
+void
+Editor::temporal_zoom_by_frame (nframes64_t start, nframes64_t end, const string & /*op*/)
+{
+       if (!session) return;
+
+       if ((start == 0 && end == 0) || end < start) {
+               return;
+       }
+
+       nframes64_t range = end - start;
+
+       double new_fpu = (double)range / (double)_canvas_width;
+
+       nframes64_t new_page = (nframes64_t) floor (_canvas_width * new_fpu);
+       nframes64_t middle = (nframes64_t) floor( (double)start + ((double)range / 2.0f ));
+       nframes64_t new_leftmost = (nframes64_t) floor( (double)middle - ((double)new_page/2.0f));
+
+       if (new_leftmost > middle) {
+               new_leftmost = 0;
+       }
+
+       reposition_and_zoom (new_leftmost, new_fpu);
+}
+
+void
+Editor::temporal_zoom_to_frame (bool coarser, nframes64_t frame)
+{
+       if (!session) {
+               return;
+       }
+       double range_before = frame - leftmost_frame;
+       double new_fpu;
+
+       new_fpu = frames_per_unit;
+
+       if (coarser) {
+               new_fpu *= 1.61803399;
+               range_before *= 1.61803399;
+       } else {
+               new_fpu = max(1.0,(new_fpu/1.61803399));
+               range_before /= 1.61803399;
+       }
+
+       if (new_fpu == frames_per_unit)  {
+               return;
+       }
+
+       nframes64_t new_leftmost = frame - (nframes64_t)range_before;
+
+       if (new_leftmost > frame) {
+               new_leftmost = 0;
+       }
+//     begin_reversible_command (_("zoom to frame"));
+//     session->add_undo (bind (mem_fun(*this, &Editor::reposition_and_zoom), leftmost_frame, frames_per_unit));
+//     session->add_redo (bind (mem_fun(*this, &Editor::reposition_and_zoom), new_leftmost, new_fpu));
+//     commit_reversible_command ();
+
+       reposition_and_zoom (new_leftmost, new_fpu);
+}
+
+
+bool
+Editor::choose_new_marker_name(string &name) {
+
+       if (!Config->get_name_new_markers()) {
+               /* don't prompt user for a new name */
+               return true;
+       }
+
+       ArdourPrompter dialog (true);
+
+       dialog.set_prompt (_("New Name:"));
+
+       dialog.set_title (_("New Location Marker"));
+
+       dialog.set_name ("MarkNameWindow");
+       dialog.set_size_request (250, -1);
+       dialog.set_position (Gtk::WIN_POS_MOUSE);
+
+       dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
+       dialog.set_initial_text (name);
+
+       dialog.show ();
+
+       switch (dialog.run ()) {
+       case RESPONSE_ACCEPT:
+               break;
+       default:
+               return false;
+       }
+
+       dialog.get_result(name);
+       return true;
+
+}
+
+
+void
+Editor::add_location_from_selection ()
+{
+       string rangename;
+
+       if (selection->time.empty()) {
+               return;
+       }
+
+       if (session == 0 || clicked_axisview == 0) {
+               return;
+       }
+
+       nframes64_t start = selection->time[clicked_selection].start;
+       nframes64_t end = selection->time[clicked_selection].end;
+
+       session->locations()->next_available_name(rangename,"selection");
+       Location *location = new Location (start, end, rangename, Location::IsRangeMarker);
+
+       session->begin_reversible_command (_("add marker"));
+        XMLNode &before = session->locations()->get_state();
+       session->locations()->add (location, true);
+        XMLNode &after = session->locations()->get_state();
+       session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
+       session->commit_reversible_command ();
+}
+
+void
+Editor::add_location_mark (nframes64_t where)
+{
+       string markername;
+
+       select_new_marker = true;
+
+       session->locations()->next_available_name(markername,"mark");
+       if (!choose_new_marker_name(markername)) {
+               return;
+       }
+       Location *location = new Location (where, where, markername, Location::IsMark);
+       session->begin_reversible_command (_("add marker"));
+        XMLNode &before = session->locations()->get_state();
+       session->locations()->add (location, true);
+        XMLNode &after = session->locations()->get_state();
+       session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
+       session->commit_reversible_command ();
+}
+
+void
+Editor::add_location_from_playhead_cursor ()
+{
+       add_location_mark (session->audible_frame());
+}
+
+void
+Editor::add_locations_from_audio_region ()
+{
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
+               return;
+       }
+
+       session->begin_reversible_command (rs.size () > 1 ? _("add markers") : _("add marker"));
+       XMLNode &before = session->locations()->get_state();
+
+       cerr << "Add locations\n";
+
+       for (RegionSelection::iterator i = rs.begin (); i != rs.end (); ++i) {
+
+               boost::shared_ptr<Region> region = (*i)->region ();
+
+               Location *location = new Location (region->position(), region->last_frame(), region->name(), Location::IsRangeMarker);
+
+               session->locations()->add (location, true);
+       }
+
+       XMLNode &after = session->locations()->get_state();
+       session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
+       session->commit_reversible_command ();
+}
+
+void
+Editor::add_location_from_audio_region ()
+{
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
+               return;
+       }
+
+       session->begin_reversible_command (_("add marker"));
+       XMLNode &before = session->locations()->get_state();
+
+       string markername;
+
+       if (rs.size() > 1) {            // more than one region selected
+               session->locations()->next_available_name(markername, "regions");
+       } else {
+               RegionView* rv = *(rs.begin());
+               boost::shared_ptr<Region> region = rv->region();
+               markername = region->name();
+       }
+
+       if (!choose_new_marker_name(markername)) {
+               return;
+       }
+
+       cerr << "Add location\n";
+
+       // single range spanning all selected
+       Location *location = new Location (rs.start(), rs.end_frame(), markername, Location::IsRangeMarker);
+       session->locations()->add (location, true);
+
+       XMLNode &after = session->locations()->get_state();
+       session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
+       session->commit_reversible_command ();
+}
+
+void
+Editor::amplitude_zoom_step (bool in)
+{
+       gdouble zoom = 1.0;
+
+       if (in) {
+               zoom *= 2.0;
+       } else {
+               if (zoom > 2.0) {
+                       zoom /= 2.0;
+               } else {
+                       zoom = 1.0;
+               }
+       }
+
+#ifdef FIX_FOR_CANVAS
+       /* XXX DO SOMETHING */
+#endif
+}
+
+
+/* DELETION */
+
+
+void
+Editor::delete_sample_forward ()
+{
+}
+
+void
+Editor::delete_sample_backward ()
+{
+}
+
+void
+Editor::delete_screen ()
+{
+}
+
+/* SEARCH */
+
+void
+Editor::search_backwards ()
+{
+       /* what ? */
+}
+
+void
+Editor::search_forwards ()
+{
+       /* what ? */
+}
+
+/* MARKS */
+
+void
+Editor::jump_forward_to_mark ()
+{
+       if (!session) {
+               return;
+       }
+
+       Location *location = session->locations()->first_location_after (playhead_cursor->current_frame);
+
+       if (location) {
+               session->request_locate (location->start(), session->transport_rolling());
+       } else {
+               session->request_locate (session->current_end_frame());
+       }
+}
+
+void
+Editor::jump_backward_to_mark ()
+{
+       if (!session) {
+               return;
+       }
+
+       Location *location = session->locations()->first_location_before (playhead_cursor->current_frame);
+
+       if (location) {
+               session->request_locate (location->start(), session->transport_rolling());
+       } else {
+               session->goto_start ();
+       }
+}
+
+void
+Editor::set_mark ()
+{
+       nframes64_t pos;
+       float prefix;
+       bool was_floating;
+       string markername;
+
+       if (get_prefix (prefix, was_floating)) {
+               pos = session->audible_frame ();
+       } else {
+               if (was_floating) {
+                       pos = (nframes64_t) floor (prefix * session->frame_rate ());
+               } else {
+                       pos = (nframes64_t) floor (prefix);
+               }
+       }
+
+       session->locations()->next_available_name(markername,"mark");
+       if (!choose_new_marker_name(markername)) {
+               return;
+       }
+       session->locations()->add (new Location (pos, 0, markername, Location::IsMark), true);
+}
+
+void
+Editor::clear_markers ()
+{
+       if (session) {
+               session->begin_reversible_command (_("clear markers"));
+                XMLNode &before = session->locations()->get_state();
+               session->locations()->clear_markers ();
+                XMLNode &after = session->locations()->get_state();
+               session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
+               session->commit_reversible_command ();
+       }
+}
+
+void
+Editor::clear_ranges ()
+{
+       if (session) {
+               session->begin_reversible_command (_("clear ranges"));
+                XMLNode &before = session->locations()->get_state();
+
+               Location * looploc = session->locations()->auto_loop_location();
+               Location * punchloc = session->locations()->auto_punch_location();
+
+               session->locations()->clear_ranges ();
+               // re-add these
+               if (looploc) session->locations()->add (looploc);
+               if (punchloc) session->locations()->add (punchloc);
+
+                XMLNode &after = session->locations()->get_state();
+               session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
+               session->commit_reversible_command ();
+       }
+}
+
+void
+Editor::clear_locations ()
+{
+       session->begin_reversible_command (_("clear locations"));
+        XMLNode &before = session->locations()->get_state();
+       session->locations()->clear ();
+        XMLNode &after = session->locations()->get_state();
+       session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
+       session->commit_reversible_command ();
+       session->locations()->clear ();
+}
+
+void
+Editor::unhide_markers ()
+{
+       for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
+               Location *l = (*i).first;
+               if (l->is_hidden() && l->is_mark()) {
+                       l->set_hidden(false, this);
+               }
+       }
+}
+
+void
+Editor::unhide_ranges ()
+{
+       for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
+               Location *l = (*i).first;
+               if (l->is_hidden() && l->is_range_marker()) {
+                       l->set_hidden(false, this);
+               }
+       }
+}
+
+/* INSERT/REPLACE */
+
+void
+Editor::insert_region_list_drag (boost::shared_ptr<Region> region, int x, int y)
+{
+       double wx, wy;
+       double cx, cy;
+       nframes64_t where;
+       RouteTimeAxisView *rtv = 0;
+       boost::shared_ptr<Playlist> playlist;
+
+       track_canvas->window_to_world (x, y, wx, wy);
+       //wx += horizontal_adjustment.get_value();
+       //wy += vertical_adjustment.get_value();
+
+       GdkEvent event;
+       event.type = GDK_BUTTON_RELEASE;
+       event.button.x = wx;
+       event.button.y = wy;
+
+       where = event_frame (&event, &cx, &cy);
+
+       if (where < leftmost_frame || where > leftmost_frame + current_page_frames()) {
+               /* clearly outside canvas area */
+               return;
+       }
+
+       std::pair<TimeAxisView*, int> tv = trackview_by_y_position (cy);
+       if (tv.first == 0) {
+               return;
+       }
+
+       if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv.first)) == 0) {
+               return;
+       }
+
+       if ((playlist = rtv->playlist()) == 0) {
+               return;
+       }
+
+       snap_to (where);
+
+       begin_reversible_command (_("insert dragged region"));
+        XMLNode &before = playlist->get_state();
+       playlist->add_region (RegionFactory::create (region), where, 1.0);
+       session->add_command(new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
+       commit_reversible_command ();
+}
+
+void
+Editor::insert_route_list_drag (boost::shared_ptr<Route> route, int x, int y) {
+       double wx, wy;
+       double cx, cy;
+       nframes_t where;
+       RouteTimeAxisView *dest_rtv = 0;
+       RouteTimeAxisView *source_rtv = 0;
+
+       track_canvas->window_to_world (x, y, wx, wy);
+       wx += horizontal_adjustment.get_value();
+       wy += vertical_adjustment.get_value();
+
+       GdkEvent event;
+       event.type = GDK_BUTTON_RELEASE;
+       event.button.x = wx;
+       event.button.y = wy;
+
+       where = event_frame (&event, &cx, &cy);
+
+       std::pair<TimeAxisView*, int> const tv = trackview_by_y_position (cy);
+       if (tv.first == 0) {
+               return;
+       }
+
+       if ((dest_rtv = dynamic_cast<RouteTimeAxisView*> (tv.first)) == 0) {
+               return;
+       }
+
+       /* use this drag source to add underlay to a track. But we really don't care
+          about the Route, only the view of the route, so find it first */
+       for(TrackViewList::iterator it = track_views.begin(); it != track_views.end(); ++it) {
+               if((source_rtv = dynamic_cast<RouteTimeAxisView*>(*it)) == 0) {
+                       continue;
+               }
+
+               if(source_rtv->route() == route && source_rtv != dest_rtv) {
+                       dest_rtv->add_underlay(source_rtv->view());
+                       break;
+               }
+       }
+}
+
+void
+Editor::insert_region_list_selection (float times)
+{
+       RouteTimeAxisView *tv = 0;
+       boost::shared_ptr<Playlist> playlist;
+
+       if (clicked_routeview != 0) {
+               tv = clicked_routeview;
+       } else if (!selection->tracks.empty()) {
+               if ((tv = dynamic_cast<RouteTimeAxisView*>(selection->tracks.front())) == 0) {
+                       return;
+               }
+       } else if (entered_track != 0) {
+               if ((tv = dynamic_cast<RouteTimeAxisView*>(entered_track)) == 0) {
+                       return;
+               }
+       } else {
+               return;
+       }
+
+       if ((playlist = tv->playlist()) == 0) {
+               return;
+       }
+
+       boost::shared_ptr<Region> region = _regions->get_single_selection ();
+       if (region == 0) {
+               return;
+       }
+
+       begin_reversible_command (_("insert region"));
+       XMLNode &before = playlist->get_state();
+       playlist->add_region ((RegionFactory::create (region)), get_preferred_edit_position(), times);
+       session->add_command(new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
+       commit_reversible_command ();
+}
+
+/* BUILT-IN EFFECTS */
+
+void
+Editor::reverse_selection ()
+{
+
+}
+
+/* GAIN ENVELOPE EDITING */
+
+void
+Editor::edit_envelope ()
+{
+}
+
+/* PLAYBACK */
+
+void
+Editor::transition_to_rolling (bool fwd)
+{
+       if (!session) {
+               return;
+       }
+
+       if (session->config.get_external_sync()) {
+               switch (session->config.get_sync_source()) {
+               case JACK:
+                       break;
+               default:
+                       /* transport controlled by the master */
+                       return;
+               }
+       }
+
+       if (session->is_auditioning()) {
+               session->cancel_audition ();
+               return;
+       }
+
+       session->request_transport_speed (fwd ? 1.0f : -1.0f);
+}
+
+void
+Editor::play_from_start ()
+{
+       session->request_locate (session->current_start_frame(), true);
+}
+
+void
+Editor::play_from_edit_point ()
+{
+       session->request_locate (get_preferred_edit_position(), true);
+}
+
+void
+Editor::play_from_edit_point_and_return ()
+{
+       nframes64_t start_frame;
+       nframes64_t return_frame;
+
+       start_frame = get_preferred_edit_position (true);
+
+       if (session->transport_rolling()) {
+               session->request_locate (start_frame, false);
+               return;
+       }
+
+       /* don't reset the return frame if its already set */
+
+       if ((return_frame = session->requested_return_frame()) < 0) {
+               return_frame = session->audible_frame();
+       }
+
+       if (start_frame >= 0) {
+               session->request_roll_at_and_return (start_frame, return_frame);
+       }
+}
+
+void
+Editor::play_selection ()
+{
+       if (selection->time.empty()) {
+               return;
+       }
+
+       session->request_play_range (&selection->time, true);
+}
+
+void
+Editor::loop_selected_region ()
+{
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       if (!rs.empty()) {
+               RegionView *rv = *(rs.begin());
+               Location* tll;
+
+               if ((tll = transport_loop_location()) != 0)  {
+
+                       tll->set (rv->region()->position(), rv->region()->last_frame());
+
+                       // enable looping, reposition and start rolling
+
+                       session->request_play_loop (true);
+                       session->request_locate (tll->start(), false);
+                       session->request_transport_speed (1.0f);
+               }
+       }
+}
+
+void
+Editor::play_location (Location& location)
+{
+       if (location.start() <= location.end()) {
+               return;
+       }
+
+       session->request_bounded_roll (location.start(), location.end());
+}
+
+void
+Editor::loop_location (Location& location)
+{
+       if (location.start() <= location.end()) {
+               return;
+       }
+
+       Location* tll;
+
+       if ((tll = transport_loop_location()) != 0) {
+               tll->set (location.start(), location.end());
+
+               // enable looping, reposition and start rolling
+               session->request_play_loop (true);
+               session->request_locate (tll->start(), true);
+       }
+}
+
+void
+Editor::raise_region ()
+{
+       selection->foreach_region (&Region::raise);
+}
+
+void
+Editor::raise_region_to_top ()
+{
+       selection->foreach_region (&Region::raise_to_top);
+}
+
+void
+Editor::lower_region ()
+{
+       selection->foreach_region (&Region::lower);
+}
+
+void
+Editor::lower_region_to_bottom ()
+{
+       selection->foreach_region (&Region::lower_to_bottom);
+}
+
+/** Show the region editor for the selected regions */
+void
+Editor::edit_region ()
+{
+       selection->foreach_regionview (&RegionView::show_region_editor);
+}
+
+/** Show the midi list editor for the selected MIDI regions */
+void
+Editor::show_midi_list_editor ()
+{
+       selection->foreach_midi_regionview (&MidiRegionView::show_list_editor);
+}
+
+void
+Editor::rename_region()
+{
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
+               return;
+       }
+
+       ArdourDialog d (*this, _("Rename Region"), true, false);
+       Entry entry;
+       Label label (_("New name:"));
+       HBox hbox;
+
+       hbox.set_spacing (6);
+       hbox.pack_start (label, false, false);
+       hbox.pack_start (entry, true, true);
+
+       d.get_vbox()->set_border_width (12);
+       d.get_vbox()->pack_start (hbox, false, false);
+
+       d.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
+       d.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
+
+       d.set_size_request (300, -1);
+       d.set_position (Gtk::WIN_POS_MOUSE);
+
+       entry.set_text (rs.front()->region()->name());
+       entry.select_region (0, -1);
+
+       entry.signal_activate().connect (bind (mem_fun (d, &Dialog::response), RESPONSE_OK));
+
+       d.show_all ();
+
+       entry.grab_focus();
+
+       int ret = d.run();
+
+       d.hide ();
+
+       if (ret == RESPONSE_OK) {
+               std::string str = entry.get_text();
+               strip_whitespace_edges (str);
+               if (!str.empty()) {
+                       rs.front()->region()->set_name (str);
+                       _regions->redisplay ();
+               }
+       }
+}
+
+void
+Editor::audition_playlist_region_via_route (boost::shared_ptr<Region> region, Route& route)
+{
+       if (session->is_auditioning()) {
+               session->cancel_audition ();
+       }
+
+       // note: some potential for creativity here, because region doesn't
+       // have to belong to the playlist that Route is handling
+
+       // bool was_soloed = route.soloed();
+
+       route.set_solo (true, this);
+
+       session->request_bounded_roll (region->position(), region->position() + region->length());
+
+       /* XXX how to unset the solo state ? */
+}
+
+/** Start an audition of the first selected region */
+void
+Editor::play_edit_range ()
+{
+       nframes64_t start, end;
+
+       if (get_edit_op_range (start, end)) {
+               session->request_bounded_roll (start, end);
+       }
+}
+
+void
+Editor::play_selected_region ()
+{
+       nframes64_t start = max_frames;
+       nframes64_t end = 0;
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
+               return;
+       }
+
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               if ((*i)->region()->position() < start) {
+                       start = (*i)->region()->position();
+               }
+               if ((*i)->region()->last_frame() + 1 > end) {
+                       end = (*i)->region()->last_frame() + 1;
+               }
+       }
+
+       session->request_bounded_roll (start, end);
+}
+
+void
+Editor::audition_playlist_region_standalone (boost::shared_ptr<Region> region)
+{
+       session->audition_region (region);
+}
+
+void
+Editor::build_interthread_progress_window ()
+{
+       interthread_progress_window = new ArdourDialog (X_("interthread progress"), true);
+
+       interthread_progress_bar.set_orientation (Gtk::PROGRESS_LEFT_TO_RIGHT);
+
+       interthread_progress_window->set_border_width (12);
+       interthread_progress_window->get_vbox()->set_spacing (6);
+
+       interthread_progress_label.set_alignment (0.5, 0.5);
+
+       interthread_progress_window->get_vbox()->pack_start (interthread_progress_label, false, false);
+       interthread_progress_window->get_vbox()->pack_start (interthread_progress_bar,false, false);
+
+       // GTK2FIX: this button needs a modifiable label
+
+       Button* b = interthread_progress_window->add_button (Stock::CANCEL, RESPONSE_CANCEL);
+       b->signal_clicked().connect (mem_fun(*this, &Editor::interthread_cancel_clicked));
+
+       interthread_cancel_button.add (interthread_cancel_label);
+
+       interthread_progress_window->set_default_size (200, 100);
+}
+
+void
+Editor::interthread_cancel_clicked ()
+{
+       if (current_interthread_info) {
+               current_interthread_info->cancel = true;
+       }
+}
+
+void
+Editor::region_from_selection ()
+{
+       if (clicked_axisview == 0) {
+               return;
+       }
+
+       if (selection->time.empty()) {
+               return;
+       }
+
+       nframes64_t start = selection->time[clicked_selection].start;
+       nframes64_t end = selection->time[clicked_selection].end;
+
+       TrackSelection tracks = get_tracks_for_range_action ();
+
+       nframes64_t selection_cnt = end - start + 1;
+
+       for (TrackSelection::iterator i = tracks.begin(); i != tracks.end(); ++i) {
+               boost::shared_ptr<Region> current;
+               boost::shared_ptr<Playlist> pl;
+               nframes64_t internal_start;
+               string new_name;
+
+               if ((pl = (*i)->playlist()) == 0) {
+                       continue;
+               }
+
+               if ((current = pl->top_region_at (start)) == 0) {
+                       continue;
+               }
+
+               internal_start = start - current->position();
+               session->region_name (new_name, current->name(), true);
+               boost::shared_ptr<Region> region (RegionFactory::create (current,
+                               internal_start, selection_cnt, new_name));
+       }
+}
+
+void
+Editor::create_region_from_selection (vector<boost::shared_ptr<Region> >& new_regions)
+{
+       if (selection->time.empty() || selection->tracks.empty()) {
+               return;
+       }
+
+       nframes64_t start = selection->time[clicked_selection].start;
+       nframes64_t end = selection->time[clicked_selection].end;
+
+       sort_track_selection ();
+
+       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+               boost::shared_ptr<Region> current;
+               boost::shared_ptr<Playlist> playlist;
+               nframes64_t internal_start;
+               string new_name;
+
+               if ((playlist = (*i)->playlist()) == 0) {
+                       continue;
+               }
+
+               if ((current = playlist->top_region_at(start)) == 0) {
+                       continue;
+               }
+
+               internal_start = start - current->position();
+               session->region_name (new_name, current->name(), true);
+
+               new_regions.push_back (RegionFactory::create (current,
+                                       internal_start, end - start + 1, new_name));
+       }
+}
+
+void
+Editor::split_multichannel_region ()
+{
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
+               return;
+       }
+
+       vector< boost::shared_ptr<Region> > v;
+
+       for (list<RegionView*>::iterator x = rs.begin(); x != rs.end(); ++x) {
+               (*x)->region()->separate_by_channel (*session, v);
+       }
+}
+
+void
+Editor::new_region_from_selection ()
+{
+       region_from_selection ();
+       cancel_selection ();
+}
+
+static void
+add_if_covered (RegionView* rv, const AudioRange* ar, RegionSelection* rs)
+{
+       switch (rv->region()->coverage (ar->start, ar->end - 1)) {
+       case OverlapNone:
+               break;
+       default:
+               rs->push_back (rv);
+       }
+}
+
+/** Return either:
+ *    - selected tracks, or if there are none...
+ *    - tracks containing selected regions, or if there are none...
+ *    - all tracks
+ * @return tracks.
+ */
+TrackSelection
+Editor::get_tracks_for_range_action () const
+{
+       TrackSelection t;
+
+       if (selection->tracks.empty()) {
+
+               /* use tracks with selected regions */
+
+               RegionSelection rs = selection->regions;
+
+               for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+                       TimeAxisView* tv = &(*i)->get_time_axis_view();
+
+                       if (!t.contains (tv)) {
+                               t.push_back (tv);
+                       }
+               }
+
+               if (t.empty()) {
+                       /* no regions and no tracks: use all tracks */
+                       t = track_views;
+               }
+
+       } else {
+
+               t = selection->tracks;
+       }
+
+       return t;
+}
+
+void
+Editor::separate_regions_between (const TimeSelection& ts)
+{
+       bool in_command = false;
+       boost::shared_ptr<Playlist> playlist;
+       RegionSelection new_selection;
+
+       TrackSelection tmptracks = get_tracks_for_range_action ();
+       sort_track_selection (&tmptracks);
+
+       for (TrackSelection::iterator i = tmptracks.begin(); i != tmptracks.end(); ++i) {
+
+               RouteTimeAxisView* rtv;
+
+               if ((rtv = dynamic_cast<RouteTimeAxisView*> ((*i))) != 0) {
+
+                       if (rtv->is_track()) {
+
+                               /* no edits to destructive tracks */
+
+                               if (rtv->track()->diskstream()->destructive()) {
+                                       continue;
+                               }
+
+                               if ((playlist = rtv->playlist()) != 0) {
+
+                                       XMLNode *before;
+                                       bool got_some;
+
+                                       before = &(playlist->get_state());
+                                       got_some = false;
+
+                                       /* XXX need to consider musical time selections here at some point */
+
+                                       double speed = rtv->get_diskstream()->speed();
+
+
+                                       for (list<AudioRange>::const_iterator t = ts.begin(); t != ts.end(); ++t) {
+
+                                               sigc::connection c = rtv->view()->RegionViewAdded.connect (
+                                                               mem_fun(*this, &Editor::collect_new_region_view));
+                                               latest_regionviews.clear ();
+
+                                               playlist->partition ((nframes64_t)((*t).start * speed),
+                                                               (nframes64_t)((*t).end * speed), true);
+
+                                               c.disconnect ();
+
+                                               if (!latest_regionviews.empty()) {
+
+                                                       got_some = true;
+
+                                                       rtv->view()->foreach_regionview (bind (
+                                                                               sigc::ptr_fun (add_if_covered),
+                                                                               &(*t), &new_selection));
+
+                                                       if (!in_command) {
+                                                               begin_reversible_command (_("separate"));
+                                                               in_command = true;
+                                                       }
+
+                                                       session->add_command(new MementoCommand<Playlist>(
+                                                                       *playlist, before, &playlist->get_state()));
+                                               }
+                                       }
+
+                                       if (!got_some) {
+                                               delete before;
+                                       }
+                               }
+                       }
+               }
+       }
+
+       if (in_command) {
+               selection->set (new_selection);
+               set_mouse_mode (MouseObject);
+
+               commit_reversible_command ();
+       }
+}
+
+/** Take tracks from get_tracks_for_range_action and cut any regions
+ *  on those tracks so that the tracks are empty over the time
+ *  selection.
+ */
+void
+Editor::separate_region_from_selection ()
+{
+       /* preferentially use *all* ranges in the time selection if we're in range mode
+          to allow discontiguous operation, since get_edit_op_range() currently
+          returns a single range.
+       */
+
+       if (mouse_mode == MouseRange && !selection->time.empty()) {
+
+               separate_regions_between (selection->time);
+
+       } else {
+
+               nframes64_t start;
+               nframes64_t end;
+
+               if (get_edit_op_range (start, end)) {
+
+                       AudioRange ar (start, end, 1);
+                       TimeSelection ts;
+                       ts.push_back (ar);
+
+                       separate_regions_between (ts);
+               }
+       }
+}
+
+void
+Editor::separate_region_from_punch ()
+{
+       Location* loc  = session->locations()->auto_punch_location();
+       if (loc) {
+               separate_regions_using_location (*loc);
+       }
+}
+
+void
+Editor::separate_region_from_loop ()
+{
+       Location* loc  = session->locations()->auto_loop_location();
+       if (loc) {
+               separate_regions_using_location (*loc);
+       }
+}
+
+void
+Editor::separate_regions_using_location (Location& loc)
+{
+       if (loc.is_mark()) {
+               return;
+       }
+
+       AudioRange ar (loc.start(), loc.end(), 1);
+       TimeSelection ts;
+
+       ts.push_back (ar);
+
+       separate_regions_between (ts);
+}
+
+void
+Editor::crop_region_to_selection ()
+{
+       if (!selection->time.empty()) {
+
+               crop_region_to (selection->time.start(), selection->time.end_frame());
+
+       } else {
+
+               nframes64_t start;
+               nframes64_t end;
+
+               if (get_edit_op_range (start, end)) {
+                       crop_region_to (start, end);
+               }
+       }
+
+}
+
+void
+Editor::crop_region_to (nframes64_t start, nframes64_t end)
+{
+       vector<boost::shared_ptr<Playlist> > playlists;
+       boost::shared_ptr<Playlist> playlist;
+       TrackSelection* ts;
+
+       if (selection->tracks.empty()) {
+               ts = &track_views;
+       } else {
+               sort_track_selection ();
+               ts = &selection->tracks;
+       }
+
+       for (TrackSelection::iterator i = ts->begin(); i != ts->end(); ++i) {
+
+               RouteTimeAxisView* rtv;
+
+               if ((rtv = dynamic_cast<RouteTimeAxisView*> ((*i))) != 0) {
+
+                       boost::shared_ptr<Track> t = rtv->track();
+
+                       if (t != 0 && ! t->diskstream()->destructive()) {
+
+                               if ((playlist = rtv->playlist()) != 0) {
+                                       playlists.push_back (playlist);
+                               }
+                       }
+               }
+       }
+
+       if (playlists.empty()) {
+               return;
+       }
+
+       nframes64_t the_start;
+       nframes64_t the_end;
+       nframes64_t cnt;
+
+       begin_reversible_command (_("trim to selection"));
+
+       for (vector<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
+
+               boost::shared_ptr<Region> region;
+
+               the_start = start;
+
+               if ((region = (*i)->top_region_at(the_start)) == 0) {
+                       continue;
+               }
+
+               /* now adjust lengths to that we do the right thing
+                  if the selection extends beyond the region
+               */
+
+               the_start = max (the_start, (nframes64_t) region->position());
+               if (max_frames - the_start < region->length()) {
+                       the_end = the_start + region->length() - 1;
+               } else {
+                       the_end = max_frames;
+               }
+               the_end = min (end, the_end);
+               cnt = the_end - the_start + 1;
+
+               XMLNode &before = (*i)->get_state();
+               region->trim_to (the_start, cnt, this);
+               XMLNode &after = (*i)->get_state();
+               session->add_command (new MementoCommand<Playlist>(*(*i), &before, &after));
+       }
+
+       commit_reversible_command ();
+}
+
+void
+Editor::region_fill_track ()
+{
+       nframes64_t end;
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       if (!session || rs.empty()) {
+               return;
+       }
+
+       end = session->current_end_frame ();
+
+       begin_reversible_command (_("region fill"));
+
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+
+               boost::shared_ptr<Region> region ((*i)->region());
+
+               boost::shared_ptr<Playlist> pl = region->playlist();
+
+               if (end <= region->last_frame()) {
+                       return;
+               }
+
+               double times = (double) (end - region->last_frame()) / (double) region->length();
+
+               if (times == 0) {
+                       return;
+               }
+
+               XMLNode &before = pl->get_state();
+               pl->add_region (RegionFactory::create (region), region->last_frame(), times);
+               session->add_command (new MementoCommand<Playlist>(*pl, &before, &pl->get_state()));
+       }
+
+       commit_reversible_command ();
+}
+
+void
+Editor::region_fill_selection ()
+{
+       if (clicked_routeview == 0 || !clicked_routeview->is_audio_track()) {
+               return;
+       }
+
+       if (selection->time.empty()) {
+               return;
+       }
+
+       boost::shared_ptr<Region> region = _regions->get_single_selection ();
+       if (region == 0) {
+               return;
+       }
+
+       nframes64_t start = selection->time[clicked_selection].start;
+       nframes64_t end = selection->time[clicked_selection].end;
+
+       boost::shared_ptr<Playlist> playlist;
+
+       if (selection->tracks.empty()) {
+               return;
+       }
+
+       nframes64_t selection_length = end - start;
+       float times = (float)selection_length / region->length();
+
+       begin_reversible_command (_("fill selection"));
+
+       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+
+               if ((playlist = (*i)->playlist()) == 0) {
+                       continue;
+               }
+
+                XMLNode &before = playlist->get_state();
+               playlist->add_region (RegionFactory::create (region), start, times);
+               session->add_command (new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
+       }
+
+       commit_reversible_command ();
+}
+
+void
+Editor::set_region_sync_from_edit_point ()
+{
+       nframes64_t where = get_preferred_edit_position ();
+       RegionSelection rs;
+       get_regions_for_action (rs);
+       set_sync_point (where, rs);
+}
+
+void
+Editor::set_sync_point (nframes64_t where, const RegionSelection& rs)
+{
+       bool in_command = false;
+
+       for (RegionSelection::const_iterator r = rs.begin(); r != rs.end(); ++r) {
+
+               if (!(*r)->region()->covers (where)) {
+                       continue;
+               }
+
+               boost::shared_ptr<Region> region ((*r)->region());
+
+               if (!in_command) {
+                       begin_reversible_command (_("set sync point"));
+                       in_command = true;
+               }
+
+               XMLNode &before = region->playlist()->get_state();
+               region->set_sync_position (where);
+               XMLNode &after = region->playlist()->get_state();
+               session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
+       }
+
+       if (in_command) {
+               commit_reversible_command ();
+       }
+}
+
+/** Remove the sync positions of the selection */
+void
+Editor::remove_region_sync ()
+{
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
+               return;
+       }
+
+       begin_reversible_command (_("remove sync"));
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+
+                XMLNode &before = (*i)->region()->playlist()->get_state();
+               (*i)->region()->clear_sync_position ();
+                XMLNode &after = (*i)->region()->playlist()->get_state();
+               session->add_command(new MementoCommand<Playlist>(*((*i)->region()->playlist()), &before, &after));
+       }
+       commit_reversible_command ();
+}
+
+void
+Editor::naturalize ()
+{
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
+               return;
+       }
+
+       begin_reversible_command (_("naturalize"));
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+                XMLNode &before = (*i)->region()->get_state();
+               (*i)->region()->move_to_natural_position (this);
+                XMLNode &after = (*i)->region()->get_state();
+               session->add_command (new MementoCommand<Region>(*((*i)->region().get()), &before, &after));
+       }
+       commit_reversible_command ();
+}
+
+void
+Editor::align (RegionPoint what)
+{
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+       nframes64_t where = get_preferred_edit_position();
+
+       if (!rs.empty()) {
+               align_selection (what, where, rs);
+       } else {
+
+               RegionSelection rs;
+               get_regions_at (rs, where, selection->tracks);
+               align_selection (what, where, rs);
+       }
+}
+
+void
+Editor::align_relative (RegionPoint what)
+{
+       nframes64_t where = get_preferred_edit_position();
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       if (!rs.empty()) {
+               align_selection_relative (what, where, rs);
+       }
+}
+
+struct RegionSortByTime {
+    bool operator() (const RegionView* a, const RegionView* b) {
+           return a->region()->position() < b->region()->position();
+    }
+};
+
+void
+Editor::align_selection_relative (RegionPoint point, nframes64_t position, const RegionSelection& rs)
+{
+       if (rs.empty()) {
+               return;
+       }
+
+       nframes64_t distance = 0;
+       nframes64_t pos = 0;
+       int dir = 1;
+
+       list<RegionView*> sorted;
+       rs.by_position (sorted);
+
+       boost::shared_ptr<Region> r ((*sorted.begin())->region());
+
+       switch (point) {
+       case Start:
+               pos = position;
+               if (position > r->position()) {
+                       distance = position - r->position();
+               } else {
+                       distance = r->position() - position;
+                       dir = -1;
+               }
+               break;
+
+       case End:
+               if (position > r->last_frame()) {
+                       distance = position - r->last_frame();
+                       pos = r->position() + distance;
+               } else {
+                       distance = r->last_frame() - position;
+                       pos = r->position() - distance;
+                       dir = -1;
+               }
+               break;
+
+       case SyncPoint:
+               pos = r->adjust_to_sync (position);
+               if (pos > r->position()) {
+                       distance = pos - r->position();
+               } else {
+                       distance = r->position() - pos;
+                       dir = -1;
+               }
+               break;
+       }
+
+       if (pos == r->position()) {
+               return;
+       }
+
+       begin_reversible_command (_("align selection (relative)"));
+
+       /* move first one specially */
+
+       XMLNode &before = r->playlist()->get_state();
+       r->set_position (pos, this);
+       XMLNode &after = r->playlist()->get_state();
+       session->add_command(new MementoCommand<Playlist>(*(r->playlist()), &before, &after));
+
+       /* move rest by the same amount */
+
+       sorted.pop_front();
+
+       for (list<RegionView*>::iterator i = sorted.begin(); i != sorted.end(); ++i) {
+
+               boost::shared_ptr<Region> region ((*i)->region());
+
+                XMLNode &before = region->playlist()->get_state();
+
+               if (dir > 0) {
+                       region->set_position (region->position() + distance, this);
+               } else {
+                       region->set_position (region->position() - distance, this);
+               }
+
+                XMLNode &after = region->playlist()->get_state();
+               session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
+
+       }
+
+       commit_reversible_command ();
+}
+
+void
+Editor::align_selection (RegionPoint point, nframes64_t position, const RegionSelection& rs)
+{
+       if (rs.empty()) {
+               return;
+       }
+
+       begin_reversible_command (_("align selection"));
+
+       for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
+               align_region_internal ((*i)->region(), point, position);
+       }
+
+       commit_reversible_command ();
+}
+
+void
+Editor::align_region (boost::shared_ptr<Region> region, RegionPoint point, nframes64_t position)
+{
+       begin_reversible_command (_("align region"));
+       align_region_internal (region, point, position);
+       commit_reversible_command ();
+}
+
+void
+Editor::align_region_internal (boost::shared_ptr<Region> region, RegionPoint point, nframes64_t position)
+{
+       XMLNode &before = region->playlist()->get_state();
+
+       switch (point) {
+       case SyncPoint:
+               region->set_position (region->adjust_to_sync (position), this);
+               break;
+
+       case End:
+               if (position > region->length()) {
+                       region->set_position (position - region->length(), this);
+               }
+               break;
+
+       case Start:
+               region->set_position (position, this);
+               break;
+       }
+
+       XMLNode &after = region->playlist()->get_state();
+       session->add_command(new MementoCommand<Playlist>(*(region->playlist()), &before, &after));
+}
+
+void
+Editor::trim_region_front ()
+{
+       trim_region (true);
+}
+
+void
+Editor::trim_region_back ()
+{
+       trim_region (false);
+}
+
+void
+Editor::trim_region (bool front)
+{
+       nframes64_t where = get_preferred_edit_position();
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
+               return;
+       }
+
+       begin_reversible_command (front ? _("trim front") : _("trim back"));
+
+       for (list<RegionView*>::const_iterator i = rs.by_layer().begin(); i != rs.by_layer().end(); ++i) {
+               if (!(*i)->region()->locked()) {
+                       boost::shared_ptr<Playlist> pl = (*i)->region()->playlist();
+                       XMLNode &before = pl->get_state();
+                       if (front) {
+                               (*i)->region()->trim_front (where, this);
+                       } else {
+                               (*i)->region()->trim_end (where, this);
+                       }
+                       XMLNode &after = pl->get_state();
+                       session->add_command(new MementoCommand<Playlist>(*pl.get(), &before, &after));
+               }
+       }
+
+       commit_reversible_command ();
+}
+
+/** Trim the end of the selected regions to the position of the edit cursor */
+void
+Editor::trim_region_to_loop ()
+{
+       Location* loc = session->locations()->auto_loop_location();
+       if (!loc) {
+               return;
+       }
+       trim_region_to_location (*loc, _("trim to loop"));
+}
+
+void
+Editor::trim_region_to_punch ()
+{
+       Location* loc = session->locations()->auto_punch_location();
+       if (!loc) {
+               return;
+       }
+       trim_region_to_location (*loc, _("trim to punch"));
+}
+void
+Editor::trim_region_to_location (const Location& loc, const char* str)
+{
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       begin_reversible_command (str);
+
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+               RegionView* rv = (*x);
+
+               /* require region to span proposed trim */
+               switch (rv->region()->coverage (loc.start(), loc.end())) {
+               case OverlapInternal:
+                       break;
+               default:
+                       continue;
+               }
+
+               RouteTimeAxisView* tav = dynamic_cast<RouteTimeAxisView*> (&rv->get_time_axis_view());
+               if (!tav) {
+                       return;
+               }
+
+               float speed = 1.0;
+               nframes64_t start;
+               nframes64_t end;
+
+               if (tav->get_diskstream() != 0) {
+                       speed = tav->get_diskstream()->speed();
+               }
+
+               start = session_frame_to_track_frame (loc.start(), speed);
+               end = session_frame_to_track_frame (loc.end(), speed);
+
+               XMLNode &before = rv->region()->playlist()->get_state();
+               rv->region()->trim_to (start, (end - start), this);
+               XMLNode &after = rv->region()->playlist()->get_state();
+               session->add_command(new MementoCommand<Playlist>(
+                               *(rv->region()->playlist()), &before, &after));
+       }
+
+       commit_reversible_command ();
+}
+
+void
+Editor::trim_region_to_edit_point ()
+{
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       nframes64_t where = get_preferred_edit_position();
+
+       begin_reversible_command (_("trim region start to edit point"));
+
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+               RegionView* rv = (*x);
+
+               /* require region to cover trim */
+               if (!rv->region()->covers (where)) {
+                       continue;
+               }
+
+               RouteTimeAxisView* tav = dynamic_cast<RouteTimeAxisView*> (&rv->get_time_axis_view());
+               if (!tav) {
+                       return;
+               }
+
+               float speed = 1.0;
+
+               if (tav->get_diskstream() != 0) {
+                       speed = tav->get_diskstream()->speed();
+               }
+
+               XMLNode &before = rv->region()->playlist()->get_state();
+               rv->region()->trim_end( session_frame_to_track_frame(where, speed), this);
+               XMLNode &after = rv->region()->playlist()->get_state();
+               session->add_command(new MementoCommand<Playlist>(
+                               *(rv->region()->playlist()), &before, &after));
+       }
+
+       commit_reversible_command ();
+}
+
+void
+Editor::trim_region_from_edit_point ()
+{
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       nframes64_t where = get_preferred_edit_position();
+
+       begin_reversible_command (_("trim region end to edit point"));
+
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+               RegionView* rv = (*x);
+
+               /* require region to cover trim */
+               if (!rv->region()->covers (where)) {
+                       continue;
+               }
+
+               RouteTimeAxisView* tav = dynamic_cast<RouteTimeAxisView*> (&rv->get_time_axis_view());
+               if (!tav) {
+                       return;
+               }
+
+               float speed = 1.0;
+
+               if (tav->get_diskstream() != 0) {
+                       speed = tav->get_diskstream()->speed();
+               }
+
+               XMLNode &before = rv->region()->playlist()->get_state();
+               rv->region()->trim_front ( session_frame_to_track_frame(where, speed), this);
+               XMLNode &after = rv->region()->playlist()->get_state();
+               session->add_command(new MementoCommand<Playlist>(
+                               *(rv->region()->playlist()), &before, &after));
+       }
+
+       commit_reversible_command ();
+}
+
+void
+Editor::trim_region_to_previous_region_end ()
+{
+       return trim_to_region(false);
+}
+
+void
+Editor::trim_region_to_next_region_start ()
+{
+       return trim_to_region(true);
+}
+
+void
+Editor::trim_to_region(bool forward)
+{
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       begin_reversible_command (_("trim to region"));
+
+       boost::shared_ptr<Region> next_region;
+
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+
+               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*x);
+
+               if (!arv) {
+                       continue;
+               }
+
+               AudioTimeAxisView* atav = dynamic_cast<AudioTimeAxisView*> (&arv->get_time_axis_view());
+
+               if (!atav) {
+                       return;
+               }
+
+               float speed = 1.0;
+
+               if (atav->get_diskstream() != 0) {
+                       speed = atav->get_diskstream()->speed();
+               }
+
+
+               boost::shared_ptr<Region> region = arv->region();
+               boost::shared_ptr<Playlist> playlist (region->playlist());
+
+               XMLNode &before = playlist->get_state();
+
+               if(forward){
+
+                   next_region = playlist->find_next_region (region->first_frame(), Start, 1);
+
+                   if(!next_region){
+                       continue;
+                   }
+
+                   region->trim_end((nframes64_t) (next_region->first_frame() * speed), this);
+                   arv->region_changed (Change (LengthChanged));
+               }
+               else {
+
+                   next_region = playlist->find_next_region (region->first_frame(), Start, 0);
+
+                   if(!next_region){
+                       continue;
+                   }
+
+                   region->trim_front((nframes64_t) ((next_region->last_frame() + 1) * speed), this);
+                   arv->region_changed (Change (LengthChanged|PositionChanged|StartChanged));
+               }
+
+               XMLNode &after = playlist->get_state();
+               session->add_command(new MementoCommand<Playlist>(*playlist, &before, &after));
+       }
+
+       commit_reversible_command ();
+}
+
+void
+Editor::unfreeze_route ()
+{
+       if (clicked_routeview == 0 || !clicked_routeview->is_track()) {
+               return;
+       }
+
+       clicked_routeview->track()->unfreeze ();
+}
+
+void*
+Editor::_freeze_thread (void* arg)
+{
+       PBD::notify_gui_about_thread_creation (pthread_self(), X_("Freeze"));
+       return static_cast<Editor*>(arg)->freeze_thread ();
+}
+
+void*
+Editor::freeze_thread ()
+{
+       clicked_routeview->audio_track()->freeze (*current_interthread_info);
+       current_interthread_info->done = true;
+       return 0;
+}
+
+gint
+Editor::freeze_progress_timeout (void */*arg*/)
+{
+       interthread_progress_bar.set_fraction (current_interthread_info->progress);
+       return !(current_interthread_info->done || current_interthread_info->cancel);
+}
+
+void
+Editor::freeze_route ()
+{
+       if (clicked_routeview == 0 || !clicked_routeview->is_audio_track()) {
+               return;
+       }
+
+       InterThreadInfo itt;
+
+       if (interthread_progress_window == 0) {
+               build_interthread_progress_window ();
+       }
+
+       interthread_progress_window->set_title (_("Freeze"));
+       interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
+       interthread_progress_window->show_all ();
+       interthread_progress_bar.set_fraction (0.0f);
+       interthread_progress_label.set_text ("");
+       interthread_cancel_label.set_text (_("Cancel Freeze"));
+       current_interthread_info = &itt;
+
+       interthread_progress_connection =
+         Glib::signal_timeout().connect (bind (mem_fun(*this, &Editor::freeze_progress_timeout), (gpointer) 0), 100);
+
+       itt.done = false;
+       itt.cancel = false;
+       itt.progress = 0.0f;
+
+       pthread_attr_t attr;
+       pthread_attr_init(&attr);
+       pthread_attr_setstacksize(&attr, 500000);
+
+       pthread_create_and_store (X_("freezer"), &itt.thread, &attr, _freeze_thread, this);
+
+       pthread_attr_destroy(&attr);
+
+       track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
+
+       while (!itt.done && !itt.cancel) {
+               gtk_main_iteration ();
+       }
+
+       interthread_progress_connection.disconnect ();
+       interthread_progress_window->hide_all ();
+       current_interthread_info = 0;
+       track_canvas->get_window()->set_cursor (*current_canvas_cursor);
+}
+
+void
+Editor::bounce_range_selection (bool replace, bool enable_processing)
+{
+       if (selection->time.empty()) {
+               return;
+       }
+
+       TrackSelection views = selection->tracks;
+
+       nframes64_t start = selection->time[clicked_selection].start;
+       nframes64_t end = selection->time[clicked_selection].end;
+       nframes64_t cnt = end - start + 1;
+
+       begin_reversible_command (_("bounce range"));
+
+       for (TrackViewList::iterator i = views.begin(); i != views.end(); ++i) {
+
+               RouteTimeAxisView* rtv;
+
+               if ((rtv = dynamic_cast<RouteTimeAxisView*> (*i)) == 0) {
+                       continue;
+               }
+
+               boost::shared_ptr<Playlist> playlist;
+
+               if ((playlist = rtv->playlist()) == 0) {
+                       return;
+               }
+
+               InterThreadInfo itt;
+
+               itt.done = false;
+               itt.cancel = false;
+               itt.progress = false;
+
+                XMLNode &before = playlist->get_state();
+               boost::shared_ptr<Region> r = rtv->track()->bounce_range (start, start+cnt, itt, enable_processing);
+
+               if (replace) {
+                       list<AudioRange> ranges;
+                       ranges.push_back (AudioRange (start, start+cnt, 0));
+                       playlist->cut (ranges); // discard result
+                       playlist->add_region (r, start);
+               }
+
+                XMLNode &after = playlist->get_state();
+               session->add_command (new MementoCommand<Playlist> (*playlist, &before, &after));
+       }
+
+       commit_reversible_command ();
+}
+
+/** Cut selected regions, automation points or a time range */
+void
+Editor::cut ()
+{
+       cut_copy (Cut);
+}
+
+/** Copy selected regions, automation points or a time range */
+void
+Editor::copy ()
+{
+       cut_copy (Copy);
+}
+
+
+/** @return true if a Cut, Copy or Clear is possible */
+bool
+Editor::can_cut_copy () const
+{
+       switch (current_mouse_mode()) {
+
+       case MouseObject:
+               if (!selection->regions.empty() || !selection->points.empty()) {
+                       return true;
+               }
+               break;
+
+       case MouseRange:
+               if (!selection->time.empty()) {
+                       return true;
+               }
+               break;
+
+       default:
+               break;
+       }
+
+       return false;
+}
+
+
+/** Cut, copy or clear selected regions, automation points or a time range.
+ * @param op Operation (Cut, Copy or Clear)
+ */
+void
+Editor::cut_copy (CutCopyOp op)
+{
+       /* only cancel selection if cut/copy is successful.*/
+
+       string opname;
+
+       switch (op) {
+       case Cut:
+               opname = _("cut");
+               break;
+       case Copy:
+               opname = _("copy");
+               break;
+       case Clear:
+               opname = _("clear");
+               break;
+       }
+
+       /* if we're deleting something, and the mouse is still pressed,
+          the thing we started a drag for will be gone when we release
+          the mouse button(s). avoid this. see part 2 at the end of
+          this function.
+       */
+
+       if (op == Cut || op == Clear) {
+               if (_drag) {
+                       _drag->item()->ungrab (0);
+                       delete _drag;
+                       _drag = 0;
+               }
+       }
+
+       cut_buffer->clear ();
+
+       if (entered_marker) {
+
+               /* cut/delete op while pointing at a marker */
+
+               bool ignored;
+               Location* loc = find_location_from_marker (entered_marker, ignored);
+
+               if (session && loc) {
+                       Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::really_remove_marker), loc));
+               }
+
+               break_drag ();
+               delete _drag;
+               _drag = 0;
+
+               return;
+       }
+
+       if (internal_editing()) {
+
+               switch (current_mouse_mode()) {
+               case MouseObject:
+               case MouseRange:
+                       cut_copy_midi (op);
+                       break;
+               default:
+                       break;
+               }
+
+       } else {
+
+               RegionSelection rs;
+
+               /* we only want to cut regions if some are selected */
+
+               if (!selection->regions.empty()) {
+                       get_regions_for_action (rs, false, false);
+               }
+
+               switch (current_mouse_mode()) {
+               case MouseObject:
+                       if (!rs.empty() || !selection->points.empty()) {
+
+                               begin_reversible_command (opname + _(" objects"));
+
+                               if (!rs.empty()) {
+                                       cut_copy_regions (op, rs);
+
+                                       if (op == Cut) {
+                                               selection->clear_regions ();
+                                       }
+                               }
+
+                               if (!selection->points.empty()) {
+                                       cut_copy_points (op);
+
+                                       if (op == Cut) {
+                                               selection->clear_points ();
+                                       }
+                               }
+
+                               commit_reversible_command ();
+                               break; // terminate case statement here
+                       }
+                       if (!selection->time.empty()) {
+                               /* don't cause suprises */
+                               break;
+                       }
+                       // fall thru if there was nothing selected
+
+               case MouseRange:
+                       if (selection->time.empty()) {
+                               nframes64_t start, end;
+                               if (!get_edit_op_range (start, end)) {
+                                       return;
+                               }
+                               selection->set ((TimeAxisView*) 0, start, end);
+                       }
+
+                       begin_reversible_command (opname + _(" range"));
+                       cut_copy_ranges (op);
+                       commit_reversible_command ();
+
+                       if (op == Cut) {
+                               selection->clear_time ();
+                       }
+
+                       break;
+
+               default:
+                       break;
+               }
+       }
+
+       if (op == Cut || op == Clear) {
+               break_drag ();
+               delete _drag;
+               _drag = 0;
+       }
+}
+
+/** Cut, copy or clear selected automation points.
+ * @param op Operation (Cut, Copy or Clear)
+ */
+void
+Editor::cut_copy_points (CutCopyOp op)
+{
+       for (PointSelection::iterator i = selection->points.begin(); i != selection->points.end(); ++i) {
+
+               AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*>(&(*i).track);
+
+               if (atv) {
+                       atv->cut_copy_clear_objects (selection->points, op);
+               }
+       }
+}
+
+/** Cut, copy or clear selected automation points.
+ * @param op Operation (Cut, Copy or Clear)
+ */
+void
+Editor::cut_copy_midi (CutCopyOp op)
+{
+       for (MidiRegionSelection::iterator i = selection->midi_regions.begin(); i != selection->midi_regions.end(); ++i) {
+               MidiRegionView* mrv = *i;
+               mrv->cut_copy_clear (op);
+       }
+}
+
+struct PlaylistState {
+    boost::shared_ptr<Playlist> playlist;
+    XMLNode*  before;
+};
+
+struct lt_playlist {
+    bool operator () (const PlaylistState& a, const PlaylistState& b) {
+           return a.playlist < b.playlist;
+    }
+};
+
+struct PlaylistMapping {
+    TimeAxisView* tv;
+    boost::shared_ptr<Playlist> pl;
+
+    PlaylistMapping (TimeAxisView* tvp) : tv (tvp) {}
+};
+
+/** Remove `clicked_regionview' */
+void
+Editor::remove_clicked_region ()
+{
+       if (clicked_routeview == 0 || clicked_regionview == 0) {
+               return;
+       }
+
+       boost::shared_ptr<Playlist> playlist = clicked_routeview->playlist();
+
+       begin_reversible_command (_("remove region"));
+        XMLNode &before = playlist->get_state();
+       playlist->remove_region (clicked_regionview->region());
+        XMLNode &after = playlist->get_state();
+       session->add_command(new MementoCommand<Playlist>(*playlist, &before, &after));
+       commit_reversible_command ();
+}
+
+
+/** Remove the selected regions */
+void
+Editor::remove_selected_regions ()
+{
+       RegionSelection rs;
+       get_regions_for_action (rs);
+
+       if (!session) {
+               return;
+       }
+
+       if (rs.empty()) {
+               return;
+       }
+
+       begin_reversible_command (_("remove region"));
+
+       list<boost::shared_ptr<Region> > regions_to_remove;
+
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               // we can't just remove the region(s) in this loop because
+               // this removes them from the RegionSelection, and they thus
+               // disappear from underneath the iterator, and the ++i above
+               // SEGVs in a puzzling fashion.
+
+               // so, first iterate over the regions to be removed from rs and
+               // add them to the regions_to_remove list, and then
+               // iterate over the list to actually remove them.
+
+               regions_to_remove.push_back ((*i)->region());
+       }
+
+       vector<PlaylistState> playlists;
+
+       for (list<boost::shared_ptr<Region> >::iterator rl = regions_to_remove.begin(); rl != regions_to_remove.end(); ++rl) {
+
+               boost::shared_ptr<Playlist> playlist = (*rl)->playlist();
+
+               if (!playlist) {
+                       // is this check necessary?
+                       continue;
+               }
+
+               vector<PlaylistState>::iterator i;
+
+               //only take state if this is a new playlist.
+               for (i = playlists.begin(); i != playlists.end(); ++i) {
+                       if ((*i).playlist == playlist) {
+                               break;
+                       }
+               }
+
+               if (i == playlists.end()) {
+
+                       PlaylistState before;
+                       before.playlist = playlist;
+                       before.before = &playlist->get_state();
+
+                       playlist->freeze ();
+                       playlists.push_back(before);
+               }
+
+               playlist->remove_region (*rl);
+       }
+
+       vector<PlaylistState>::iterator pl;
+
+       for (pl = playlists.begin(); pl != playlists.end(); ++pl) {
+               (*pl).playlist->thaw ();
+               session->add_command(new MementoCommand<Playlist>(*(*pl).playlist, (*pl).before, &(*pl).playlist->get_state()));
+       }
+
+       commit_reversible_command ();
+}
+
+/** Cut, copy or clear selected regions.
+ * @param op Operation (Cut, Copy or Clear)
+ */
+void
+Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
+{
+       /* we can't use a std::map here because the ordering is important, and we can't trivially sort
+          a map when we want ordered access to both elements. i think.
+       */
+
+       vector<PlaylistMapping> pmap;
+
+       nframes64_t first_position = max_frames;
+
+       set<PlaylistState, lt_playlist> freezelist;
+       pair<set<PlaylistState, lt_playlist>::iterator,bool> insert_result;
+
+       /* get ordering correct before we cut/copy */
+
+       rs.sort_by_position_and_track ();
+
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+
+               first_position = min ((nframes64_t) (*x)->region()->position(), first_position);
+
+               if (op == Cut || op == Clear) {
+                       boost::shared_ptr<Playlist> pl = (*x)->region()->playlist();
+
+                       if (pl) {
+                               set<PlaylistState, lt_playlist>::iterator fl;
+
+                               //only take state if this is a new playlist.
+                               for (fl = freezelist.begin(); fl != freezelist.end(); ++fl) {
+                                       if ((*fl).playlist == pl) {
+                                               break;
+                                       }
+                               }
+
+                               if (fl == freezelist.end()) {
+                                       PlaylistState before;
+                                       before.playlist = pl;
+                                       before.before = &pl->get_state();
+                                       pl->freeze ();
+                                       insert_result = freezelist.insert (before);
+                               }
+                       }
+               }
+
+               TimeAxisView* tv = &(*x)->get_trackview();
+               vector<PlaylistMapping>::iterator z;
+
+               for (z = pmap.begin(); z != pmap.end(); ++z) {
+                       if ((*z).tv == tv) {
+                               break;
+                       }
+               }
+
+               if (z == pmap.end()) {
+                       pmap.push_back (PlaylistMapping (tv));
+               }
+       }
+
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ) {
+
+               boost::shared_ptr<Playlist> pl = (*x)->region()->playlist();
+
+               if (!pl) {
+                       /* impossible, but this handles it for the future */
+                       continue;
+               }
+
+               TimeAxisView& tv = (*x)->get_trackview();
+               boost::shared_ptr<Playlist> npl;
+               RegionSelection::iterator tmp;
+
+               tmp = x;
+               ++tmp;
+
+               vector<PlaylistMapping>::iterator z;
+
+               for (z = pmap.begin(); z != pmap.end(); ++z) {
+                       if ((*z).tv == &tv) {
+                               break;
+                       }
+               }
+
+               assert (z != pmap.end());
+
+               if (!(*z).pl) {
+                       npl = PlaylistFactory::create (pl->data_type(), *session, "cutlist", true);
+                       npl->freeze();
+                       (*z).pl = npl;
+               } else {
+                       npl = (*z).pl;
+               }
+
+               boost::shared_ptr<Region> r = (*x)->region();
+               boost::shared_ptr<Region> _xx;
+
+               assert (r != 0);
+
+               switch (op) {
+               case Cut:
+                       _xx = RegionFactory::create (r);
+                       npl->add_region (_xx, r->position() - first_position);
+                       pl->remove_region (r);
+                       break;
+
+               case Copy:
+                       /* copy region before adding, so we're not putting same object into two different playlists */
+                       npl->add_region (RegionFactory::create (r), r->position() - first_position);
+                       break;
+
+               case Clear:
+                       pl->remove_region (r);
+                       break;
+               }
+
+               x = tmp;
+       }
+
+       list<boost::shared_ptr<Playlist> > foo;
+
+       /* the pmap is in the same order as the tracks in which selected regions occured */
+
+       for (vector<PlaylistMapping>::iterator i = pmap.begin(); i != pmap.end(); ++i) {
+               (*i).pl->thaw();
+               foo.push_back ((*i).pl);
+       }
+
+
+       if (!foo.empty()) {
+               cut_buffer->set (foo);
+       }
+
+       for (set<PlaylistState, lt_playlist>::iterator pl = freezelist.begin(); pl != freezelist.end(); ++pl) {
+               (*pl).playlist->thaw ();
+               session->add_command (new MementoCommand<Playlist>(*(*pl).playlist, (*pl).before, &(*pl).playlist->get_state()));
+       }
+}
+
+void
+Editor::cut_copy_ranges (CutCopyOp op)
+{
+       TrackSelection* ts;
+       TrackSelection entered;
+
+       if (selection->tracks.empty()) {
+               if (!entered_track) {
+                       return;
+               }
+               entered.push_back (entered_track);
+               ts = &entered;
+       } else {
+               ts = &selection->tracks;
+       }
+
+       for (TrackSelection::iterator i = ts->begin(); i != ts->end(); ++i) {
+               (*i)->cut_copy_clear (*selection, op);
+       }
+}
+
+void
+Editor::paste (float times)
+{
+       paste_internal (get_preferred_edit_position(), times);
+}
+
+void
+Editor::mouse_paste ()
+{
+       nframes64_t where;
+       bool ignored;
+
+       if (!mouse_frame (where, ignored)) {
+               return;
+       }
+
+       snap_to (where);
+       paste_internal (where, 1);
+}
+
+void
+Editor::paste_internal (nframes64_t position, float times)
+{
+       bool commit = false;
+
+       if (internal_editing()) {
+               if (cut_buffer->midi_notes.empty()) {
+                       return;
+               }
+       } else {
+               if (cut_buffer->empty()) {
+                       return;
+               }
+       }
+
+       if (position == max_frames) {
+               position = get_preferred_edit_position();
+       }
+
+       begin_reversible_command (_("paste"));
+
+       TrackSelection ts;
+       TrackSelection::iterator i;
+       size_t nth;
+
+       /* get everything in the correct order */
+
+       if (!selection->tracks.empty()) {
+               sort_track_selection ();
+               ts = selection->tracks;
+       } else if (entered_track) {
+               ts.push_back (entered_track);
+       }
+
+       for (nth = 0, i = ts.begin(); i != ts.end(); ++i, ++nth) {
+
+               /* undo/redo is handled by individual tracks/regions */
+
+               if (internal_editing()) {
+
+                       RegionSelection rs;
+                       RegionSelection::iterator r;
+                       MidiNoteSelection::iterator cb;
+
+                       get_regions_at (rs, position, ts);
+
+                       for (cb = cut_buffer->midi_notes.begin(), r = rs.begin();
+                            cb != cut_buffer->midi_notes.end() && r != rs.end(); ++r) {
+                               MidiRegionView* mrv = dynamic_cast<MidiRegionView*> (*r);
+                               if (mrv) {
+                                       mrv->paste (position, times, **cb);
+                                       ++cb;
+                               }
+                       }
+
+               } else {
+
+                       if ((*i)->paste (position, times, *cut_buffer, nth)) {
+                               commit = true;
+                       }
+               }
+       }
+
+       if (commit) {
+               commit_reversible_command ();
        }
-       commit_reversible_command ();
-       return !touched.empty();
 }
 
 void
-Editor::set_selection_from_punch()
+Editor::duplicate_some_regions (RegionSelection& regions, float times)
 {
-       Location* location;
+       boost::shared_ptr<Playlist> playlist;
+       RegionSelection sel = regions; // clear (below) may  clear the argument list if its the current region selection
+       RegionSelection foo;
 
-       if ((location = session->locations()->auto_punch_location()) == 0)  {
-               return;
-       }
+       begin_reversible_command (_("duplicate region"));
 
-       set_selection_from_range (*location);
-}
+       selection->clear_regions ();
 
-void
-Editor::set_selection_from_loop()
-{
-       Location* location;
+       for (RegionSelection::iterator i = sel.begin(); i != sel.end(); ++i) {
 
-       if ((location = session->locations()->auto_loop_location()) == 0)  {
-               return;
-       }
-       set_selection_from_range (*location);
-}
+               boost::shared_ptr<Region> r ((*i)->region());
 
-void
-Editor::select_all_from_punch()
-{
-       Location* location;
-       list<Selectable *> touched;
-       if ((location = session->locations()->auto_punch_location()) == 0)  {
-               return;
-       }
+               TimeAxisView& tv = (*i)->get_time_axis_view();
+               RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (&tv);
+               latest_regionviews.clear ();
+               sigc::connection c = rtv->view()->RegionViewAdded.connect (mem_fun(*this, &Editor::collect_new_region_view));
 
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_selectables (location->start(), location->end(), 0, DBL_MAX, touched);
+               playlist = (*i)->region()->playlist();
+                XMLNode &before = playlist->get_state();
+               playlist->duplicate (r, r->last_frame(), times);
+               session->add_command(new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
+
+               c.disconnect ();
+
+               foo.insert (foo.end(), latest_regionviews.begin(), latest_regionviews.end());
        }
-       begin_reversible_command (_("select all from punch"));
-       selection->set (touched);
+
        commit_reversible_command ();
 
+       if (!foo.empty()) {
+               selection->set (foo);
+       }
 }
 
 void
-Editor::select_all_from_loop()
+Editor::duplicate_selection (float times)
 {
-       Location* location;
-       list<Selectable *> touched;
-
-       if ((location = session->locations()->auto_loop_location()) == 0)  {
+       if (selection->time.empty() || selection->tracks.empty()) {
                return;
        }
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
-                       continue;
-               }
-               (*iter)->get_selectables (location->start(), location->end(), 0, DBL_MAX, touched);
-       }
-       begin_reversible_command (_("select all from loop"));
-       selection->set (touched);
-       commit_reversible_command ();
 
-}
+       boost::shared_ptr<Playlist> playlist;
+       vector<boost::shared_ptr<Region> > new_regions;
+       vector<boost::shared_ptr<Region> >::iterator ri;
 
-void
-Editor::set_selection_from_range (Location& range)
-{
-       if (clicked_trackview == 0) {
+       create_region_from_selection (new_regions);
+
+       if (new_regions.empty()) {
                return;
        }
-       
-       begin_reversible_command (_("set selection from range"));
-       selection->set (0, range.start(), range.end());
-       commit_reversible_command ();
-}
 
-void
-Editor::select_all_after_cursor (Cursor *cursor, bool after)
-{
-        jack_nframes_t start;
-       jack_nframes_t end;
-       list<Selectable *> touched;
+       begin_reversible_command (_("duplicate selection"));
 
-       if (after) {
-         begin_reversible_command (_("select all after cursor"));
-         start = cursor->current_frame ;
-         end = session->current_end_frame();
-       } else {
-         begin_reversible_command (_("select all before cursor"));
-         start = 0;
-         end = cursor->current_frame ;
-       }
-       for (TrackViewList::iterator iter = track_views.begin(); iter != track_views.end(); ++iter) {
-               if ((*iter)->hidden()) {
+       ri = new_regions.begin();
+
+       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+               if ((playlist = (*i)->playlist()) == 0) {
                        continue;
                }
-               (*iter)->get_selectables (start, end, 0, DBL_MAX, touched);
+               XMLNode &before = playlist->get_state();
+               playlist->duplicate (*ri, selection->time[clicked_selection].end, times);
+               XMLNode &after = playlist->get_state();
+               session->add_command (new MementoCommand<Playlist>(*playlist, &before, &after));
+
+               ++ri;
+               if (ri == new_regions.end()) {
+                       --ri;
+               }
        }
-       selection->set (touched);
+
        commit_reversible_command ();
 }
 
 void
-Editor::amplitude_zoom_step (bool in)
+Editor::reset_point_selection ()
 {
-       gdouble zoom = 1.0;
-
-       if (in) {
-               zoom *= 2.0;
-       } else {
-               if (zoom > 2.0) {
-                       zoom /= 2.0;
-               } else {
-                       zoom = 1.0;
-               }
-       }
-
-#ifdef FIX_FOR_CANVAS
-       /* XXX DO SOMETHING */
-#endif
-}      
-
-
-/* DELETION */
+       /* reset all selected points to the relevant default value */
 
+       for (PointSelection::iterator i = selection->points.begin(); i != selection->points.end(); ++i) {
 
-void
-Editor::delete_sample_forward ()
-{
-}
+               AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*>(&(*i).track);
 
-void
-Editor::delete_sample_backward ()
-{
+               if (atv) {
+                       atv->reset_objects (selection->points);
+               }
+       }
 }
 
 void
-Editor::delete_screen ()
+Editor::center_playhead ()
 {
+       float page = _canvas_width * frames_per_unit;
+       center_screen_internal (playhead_cursor->current_frame, page);
 }
 
-/* SEARCH */
-
 void
-Editor::search_backwards ()
+Editor::center_edit_point ()
 {
-       /* what ? */
+       float page = _canvas_width * frames_per_unit;
+       center_screen_internal (get_preferred_edit_position(), page);
 }
 
 void
-Editor::search_forwards ()
+Editor::clear_playlist (boost::shared_ptr<Playlist> playlist)
 {
-       /* what ? */
+       begin_reversible_command (_("clear playlist"));
+        XMLNode &before = playlist->get_state();
+       playlist->clear ();
+        XMLNode &after = playlist->get_state();
+       session->add_command (new MementoCommand<Playlist>(*playlist.get(), &before, &after));
+       commit_reversible_command ();
 }
 
-/* MARKS */
-
 void
-Editor::jump_forward_to_mark ()
+Editor::nudge_track (bool use_edit, bool forwards)
 {
-       if (!session) {
-               return;
-       }
-       
-       Location *location = session->locations()->first_location_after (playhead_cursor->current_frame);
+       boost::shared_ptr<Playlist> playlist;
+       nframes64_t distance;
+       nframes64_t next_distance;
+       nframes64_t start;
 
-       if (location) {
-               session->request_locate (location->start(), session->transport_rolling());
+       if (use_edit) {
+               start = get_preferred_edit_position();
        } else {
-               session->request_locate (session->current_end_frame());
+               start = 0;
        }
-}
 
-void
-Editor::jump_backward_to_mark ()
-{
-       if (!session) {
+       if ((distance = get_nudge_distance (start, next_distance)) == 0) {
                return;
        }
 
-       Location *location = session->locations()->first_location_before (playhead_cursor->current_frame);
-       
-       if (location) {
-               session->request_locate (location->start(), session->transport_rolling());
-       } else {
-               session->request_locate (0);
+       if (selection->tracks.empty()) {
+               return;
        }
-}
 
-void
-Editor::set_mark ()
-{
-       jack_nframes_t pos;
-       float prefix;
-       bool was_floating;
+       begin_reversible_command (_("nudge track"));
 
-       if (get_prefix (prefix, was_floating)) {
-               pos = session->audible_frame ();
-       } else {
-               if (was_floating) {
-                       pos = (jack_nframes_t) floor (prefix * session->frame_rate ());
-               } else {
-                       pos = (jack_nframes_t) floor (prefix);
+       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+
+               if ((playlist = (*i)->playlist()) == 0) {
+                       continue;
                }
+
+                XMLNode &before = playlist->get_state();
+               playlist->nudge_after (start, distance, forwards);
+                XMLNode &after = playlist->get_state();
+               session->add_command (new MementoCommand<Playlist>(*playlist, &before, &after));
        }
 
-       session->locations()->add (new Location (pos, 0, "mark", Location::IsMark), true);
+       commit_reversible_command ();
 }
 
 void
-Editor::clear_markers ()
+Editor::remove_last_capture ()
 {
-       if (session) {
-               session->begin_reversible_command (_("clear markers"));
-               session->add_undo (session->locations()->get_memento());
-               session->locations()->clear_markers ();
-               session->add_redo_no_execute (session->locations()->get_memento());
-               session->commit_reversible_command ();
-       }
-}
+       vector<string> choices;
+       string prompt;
 
-void
-Editor::clear_ranges ()
-{
-       if (session) {
-               session->begin_reversible_command (_("clear ranges"));
-               session->add_undo (session->locations()->get_memento());
-               
-               Location * looploc = session->locations()->auto_loop_location();
-               Location * punchloc = session->locations()->auto_punch_location();
-               
-               session->locations()->clear_ranges ();
-               // re-add these
-               if (looploc) session->locations()->add (looploc);
-               if (punchloc) session->locations()->add (punchloc);
-               
-               session->add_redo_no_execute (session->locations()->get_memento());
-               session->commit_reversible_command ();
+       if (!session) {
+               return;
        }
-}
-
-void
-Editor::clear_locations ()
-{
-       session->begin_reversible_command (_("clear locations"));
-       session->add_undo (session->locations()->get_memento());
-       session->locations()->clear ();
-       session->add_redo_no_execute (session->locations()->get_memento());
-       session->commit_reversible_command ();
-       session->locations()->clear ();
-}
 
-/* INSERT/REPLACE */
+       if (Config->get_verify_remove_last_capture()) {
+               prompt  = _("Do you really want to destroy the last capture?"
+                           "\n(This is destructive and cannot be undone)");
 
-void
-Editor::insert_region_list_drag (AudioRegion& region, int x, int y)
-{
-       double wx, wy;
-       double cx, cy;
-       TimeAxisView *tv;
-       jack_nframes_t where;
-       AudioTimeAxisView *atv = 0;
-       Playlist *playlist;
-       
-       track_canvas.window_to_world (x, y, wx, wy);
-       wx += horizontal_adjustment.get_value();
-       wy += vertical_adjustment.get_value();
+               choices.push_back (_("No, do nothing."));
+               choices.push_back (_("Yes, destroy it."));
 
-       GdkEvent event;
-       event.type = GDK_BUTTON_RELEASE;
-       event.button.x = wx;
-       event.button.y = wy;
-       
-       where = event_frame (&event, &cx, &cy);
+               Gtkmm2ext::Choice prompter (prompt, choices);
 
-       if (where < leftmost_frame || where > leftmost_frame + current_page_frames()) {
-               /* clearly outside canvas area */
-               return;
-       }
-       
-       if ((tv = trackview_by_y_position (cy)) == 0) {
-               return;
-       }
-       
-       if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) == 0) {
-               return;
-       }
+               if (prompter.run () == 1) {
+                       session->remove_last_capture ();
+               }
 
-       if ((playlist = atv->playlist()) == 0) {
-               return;
+       } else {
+               session->remove_last_capture();
        }
-       
-       snap_to (where);
-       
-       begin_reversible_command (_("insert dragged region"));
-       session->add_undo (playlist->get_memento());
-       playlist->add_region (*(new AudioRegion (region)), where, 1.0);
-       session->add_redo_no_execute (playlist->get_memento());
-       commit_reversible_command ();
 }
 
 void
-Editor::insert_region_list_selection (float times)
+Editor::normalize_region ()
 {
-       AudioTimeAxisView *tv = 0;
-       Playlist *playlist;
-
-       if (clicked_audio_trackview != 0) {
-               tv = clicked_audio_trackview;
-       } else if (!selection->tracks.empty()) {
-               if ((tv = dynamic_cast<AudioTimeAxisView*>(selection->tracks.front())) == 0) {
-                       return;
-               }
-       } else {
+       if (!session) {
                return;
        }
 
-       if ((playlist = tv->playlist()) == 0) {
+       RegionSelection rs;
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
                return;
        }
-       
-       Glib::RefPtr<TreeSelection> selected = region_list_display.get_selection();
-       
-       if (selected->count_selected_rows() != 1) {
+
+       Dialog dialog (rs.size() > 1 ? _("Normalize regions") : _("Normalize region"));
+       HBox hbox;
+       hbox.pack_start (*manage (new Label (_("Normalize to:"))));
+       SpinButton spin (0.2, 2);
+       spin.set_range (-112, 0);
+       spin.set_increments (0.1, 1);
+       spin.set_value (0);
+       hbox.pack_start (spin);
+       spin.set_value (_last_normalization_value);
+       hbox.pack_start (*manage (new Label (_("dbFS"))));
+       hbox.show_all ();
+       dialog.get_vbox()->pack_start (hbox);
+       dialog.add_button (Stock::CANCEL, RESPONSE_CANCEL);
+       dialog.add_button (_("Normalize"), RESPONSE_ACCEPT);
+
+       if (dialog.run () == RESPONSE_CANCEL) {
                return;
        }
-       
-       TreeModel::iterator i = region_list_display.get_selection()->get_selected();
-       Region* region = (*i)[region_list_columns.region];
-
-       begin_reversible_command (_("insert region"));
-       session->add_undo (playlist->get_memento());
-       playlist->add_region (*(createRegion (*region)), edit_cursor->current_frame, times);
-       session->add_redo_no_execute (playlist->get_memento());
-       commit_reversible_command ();
-}
-
 
-/* BUILT-IN EFFECTS */
+       begin_reversible_command (_("normalize"));
 
-void
-Editor::reverse_selection ()
-{
+       track_canvas->get_window()->set_cursor (*wait_cursor);
+       gdk_flush ();
 
-}
+       for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*r);
+               if (!arv)
+                       continue;
+               XMLNode &before = arv->region()->get_state();
+               arv->audio_region()->normalize_to (spin.get_value());
+               session->add_command (new MementoCommand<Region>(*(arv->region().get()), &before, &arv->region()->get_state()));
+       }
 
-/* GAIN ENVELOPE EDITING */
+       commit_reversible_command ();
+       track_canvas->get_window()->set_cursor (*current_canvas_cursor);
 
-void
-Editor::edit_envelope ()
-{
+       _last_normalization_value = spin.get_value ();
 }
 
-/* PLAYBACK */
 
 void
-Editor::toggle_playback (bool with_abort)
+Editor::reset_region_scale_amplitude ()
 {
        if (!session) {
                return;
        }
 
-       switch (session->slave_source()) {
-       case Session::None:
-       case Session::JACK:
-               break;
-       default:
-               /* transport controlled by the master */
-               return;
-       }
+       RegionSelection rs;
 
-       if (session->is_auditioning()) {
-               session->cancel_audition ();
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
                return;
        }
-       
-       if (session->transport_rolling()) {
-               session->request_stop (with_abort);
-               if (session->get_auto_loop()) {
-                       session->request_auto_loop (false);
-               }
-       } else {
-               session->request_transport_speed (1.0f);
+
+       begin_reversible_command ("reset gain");
+
+       for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*r);
+               if (!arv)
+                       continue;
+               XMLNode &before = arv->region()->get_state();
+               arv->audio_region()->set_scale_amplitude (1.0f);
+               session->add_command (new MementoCommand<Region>(*(arv->region().get()), &before, &arv->region()->get_state()));
        }
-}
 
-void
-Editor::play_from_start ()
-{
-       session->request_locate (0, true);
+       commit_reversible_command ();
 }
 
 void
-Editor::play_selection ()
+Editor::adjust_region_scale_amplitude (bool up)
 {
-       if (selection->time.empty()) {
+       if (!session) {
                return;
        }
 
-       session->request_play_range (true);
-}
+       RegionSelection rs;
 
-void
-Editor::play_selected_region ()
-{
-       if (!selection->audio_regions.empty()) {
-               AudioRegionView *rv = *(selection->audio_regions.begin());
+       get_regions_for_action (rs);
 
-               session->request_bounded_roll (rv->region.position(), rv->region.last_frame()); 
+       if (rs.empty()) {
+               return;
        }
-}
 
-void
-Editor::loop_selected_region ()
-{
-       if (!selection->audio_regions.empty()) {
-               AudioRegionView *rv = *(selection->audio_regions.begin());
-               Location* tll;
+       begin_reversible_command ("denormalize");
 
-               if ((tll = transport_loop_location()) != 0)  {
+       for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*r);
+               if (!arv)
+                       continue;
+               XMLNode &before = arv->region()->get_state();
 
-                       tll->set (rv->region.position(), rv->region.last_frame());
-                       
-                       // enable looping, reposition and start rolling
+               double fraction = gain_to_slider_position (arv->audio_region()->scale_amplitude ());
 
-                       session->request_auto_loop (true);
-                       session->request_locate (tll->start(), false);
-                       session->request_transport_speed (1.0f);
+               if (up) {
+                       fraction += 0.05;
+                       fraction = min (fraction, 1.0);
+               } else {
+                       fraction -= 0.05;
+                       fraction = max (fraction, 0.0);
+               }
+
+               if (!up && fraction <= 0) {
+                       continue;
                }
+
+               fraction = slider_position_to_gain (fraction);
+
+               if (up && fraction >= 2.0) {
+                       continue;
+               }
+
+               arv->audio_region()->set_scale_amplitude (fraction);
+               session->add_command (new MementoCommand<Region>(*(arv->region().get()), &before, &arv->region()->get_state()));
        }
+
+       commit_reversible_command ();
 }
 
+
 void
-Editor::play_location (Location& location)
+Editor::reverse_region ()
 {
-       if (location.start() <= location.end()) {
+       if (!session) {
                return;
        }
 
-       session->request_bounded_roll (location.start(), location.end());
+       Reverse rev (*session);
+       apply_filter (rev, _("reverse regions"));
 }
 
 void
-Editor::loop_location (Location& location)
+Editor::strip_region_silence ()
 {
-       if (location.start() <= location.end()) {
+       if (!session) {
                return;
        }
 
-       Location* tll;
-
-       if ((tll = transport_loop_location()) != 0) {
-               tll->set (location.start(), location.end());
+       RegionSelection rs;
+       get_regions_for_action (rs);
 
-               // enable looping, reposition and start rolling
-               session->request_auto_loop (true);
-               session->request_locate (tll->start(), true);
+       if (rs.empty()) {
+               return;
        }
-}
 
-void 
-Editor::toggle_region_mute ()
-{
-       if (clicked_regionview) {
-               clicked_regionview->region.set_muted (!clicked_regionview->region.muted());
-       } else if (!selection->audio_regions.empty()) {
-               bool yn = ! (*selection->audio_regions.begin())->region.muted();
-               selection->foreach_audio_region (&AudioRegion::set_muted, yn);
-       }
-}
+       std::list<boost::shared_ptr<AudioRegion> > ar;
 
-void
-Editor::toggle_region_opaque ()
-{
-       if (clicked_regionview) {
-               clicked_regionview->region.set_opaque (!clicked_regionview->region.opaque());
-       } else if (!selection->audio_regions.empty()) {
-               bool yn = ! (*selection->audio_regions.begin())->region.opaque();
-               selection->foreach_audio_region (&Region::set_opaque, yn);
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*> (*i);
+               if (arv) {
+                       ar.push_back (arv->audio_region ());
+               }
        }
-}
 
-void
-Editor::raise_region ()
-{
-       selection->foreach_audio_region (&Region::raise);
-}
+       StripSilenceDialog d (ar);
+       int const r = d.run ();
 
-void
-Editor::raise_region_to_top ()
-{
-       selection->foreach_audio_region (&Region::raise_to_top);
+       if (r == Gtk::RESPONSE_OK) {
+               StripSilence s (*session, d.threshold (), d.minimum_length (), d.fade_length ());
+               apply_filter (s, _("strip silence"));
+       }
 }
 
-void
-Editor::lower_region ()
+Command*
+Editor::apply_midi_note_edit_op_to_region (MidiOperator& op, MidiRegionView& mrv)
 {
-       selection->foreach_audio_region (&Region::lower);
-}
+       Evoral::Sequence<Evoral::MusicalTime>::Notes selected;
+       mrv.selection_as_notelist (selected);
 
-void
-Editor::lower_region_to_bottom ()
-{
-       selection->foreach_audio_region (&Region::lower_to_bottom);
-}
+       vector<Evoral::Sequence<Evoral::MusicalTime>::Notes> v;
+       v.push_back (selected);
 
-void
-Editor::edit_region ()
-{
-       if (clicked_regionview == 0) {
-               return;
-       }
-       
-       clicked_regionview->show_region_editor ();
+       return op (mrv.midi_region()->model(), v);
 }
 
 void
-Editor::rename_region ()
+Editor::apply_midi_note_edit_op (MidiOperator& op)
 {
-       Dialog dialog;
-       Entry  entry;
-       Button ok_button (_("OK"));
-       Button cancel_button (_("Cancel"));
+       RegionSelection rs;
+       Command* cmd;
+
+       get_regions_for_action (rs);
 
-       if (selection->audio_regions.empty()) {
+       if (rs.empty()) {
                return;
        }
 
-       dialog.set_title (_("ardour: rename region"));
-       dialog.set_name ("RegionRenameWindow");
-       dialog.set_size_request (300, -1);
-       dialog.set_position (Gtk::WIN_POS_MOUSE);
-       dialog.set_modal (true);
-
-       dialog.get_vbox()->set_border_width (10);
-       dialog.get_vbox()->pack_start (entry);
-       dialog.get_action_area()->pack_start (ok_button);
-       dialog.get_action_area()->pack_start (cancel_button);
-
-       entry.set_name ("RegionNameDisplay");
-       ok_button.set_name ("EditorGTKButton");
-       cancel_button.set_name ("EditorGTKButton");
-
-       region_renamed = false;
+       begin_reversible_command (op.name ());
 
-       entry.signal_activate().connect (bind (mem_fun(*this, &Editor::rename_region_finished), true));
-       ok_button.signal_clicked().connect (bind (mem_fun(*this, &Editor::rename_region_finished), true));
-       cancel_button.signal_clicked().connect (bind (mem_fun(*this, &Editor::rename_region_finished), false));
+       for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ) {
+               RegionSelection::iterator tmp = r;
+               ++tmp;
 
-       /* recurse */
+               MidiRegionView* const mrv = dynamic_cast<MidiRegionView*> (*r);
 
-       dialog.show_all ();
-       Main::run ();
+               if (mrv) {
+                       cmd = apply_midi_note_edit_op_to_region (op, *mrv);
+                       if (cmd) {
+                               (*cmd)();
+                               session->add_command (cmd);
+                       }
+               }
 
-       if (region_renamed) {
-               (*selection->audio_regions.begin())->region.set_name (entry.get_text());
-               redisplay_regions ();
+               r = tmp;
        }
-}
-
-void
-Editor::rename_region_finished (bool status)
 
-{
-       region_renamed = status;
-       Main::quit ();
+       commit_reversible_command ();
+       rs.clear ();
 }
 
 void
-Editor::audition_playlist_region_via_route (AudioRegion& region, Route& route)
+Editor::quantize_region ()
 {
-       if (session->is_auditioning()) {
-               session->cancel_audition ();
-       } 
+       if (!session) {
+               return;
+       }
 
-       // note: some potential for creativity here, because region doesn't
-       // have to belong to the playlist that Route is handling
+       QuantizeDialog* qd = new QuantizeDialog (*this);
 
-       // bool was_soloed = route.soloed();
+       qd->present ();
+       const int r = qd->run ();
+       qd->hide ();
 
-       route.set_solo (true, this);
-       
-       session->request_bounded_roll (region.position(), region.position() + region.length());
-       
-       /* XXX how to unset the solo state ? */
-}
+       if (r == Gtk::RESPONSE_OK) {
+               Quantize quant (*session, Plain,
+                               qd->snap_start(), qd->snap_end(),
+                               qd->start_grid_size(), qd->end_grid_size(),
+                               qd->strength(), qd->swing(), qd->threshold());
 
-void
-Editor::audition_selected_region ()
-{
-       if (!selection->audio_regions.empty()) {
-               AudioRegionView* rv = *(selection->audio_regions.begin());
-               session->audition_region (rv->region);
+               apply_midi_note_edit_op (quant);
        }
 }
 
 void
-Editor::audition_playlist_region_standalone (AudioRegion& region)
+Editor::apply_filter (Filter& filter, string command)
 {
-       session->audition_region (region);
-}
+       RegionSelection rs;
 
-void
-Editor::build_interthread_progress_window ()
-{
-       interthread_progress_window = new ArdourDialog (X_("interthread progress"), true);
+       get_regions_for_action (rs);
 
-       interthread_progress_bar.set_orientation (Gtk::PROGRESS_LEFT_TO_RIGHT);
-       
-       interthread_progress_window->get_vbox()->pack_start (interthread_progress_label, false, false);
-       interthread_progress_window->get_vbox()->pack_start (interthread_progress_bar,false, false);
+       if (rs.empty()) {
+               return;
+       }
 
-       // GTK2FIX: this button needs a modifiable label
+       begin_reversible_command (command);
 
-       Button* b = interthread_progress_window->add_button (Stock::CANCEL, RESPONSE_CANCEL);
-       b->signal_clicked().connect (mem_fun(*this, &Editor::interthread_cancel_clicked));
+       track_canvas->get_window()->set_cursor (*wait_cursor);
+       gdk_flush ();
 
-       interthread_cancel_button.add (interthread_cancel_label);
+       for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ) {
+               RegionSelection::iterator tmp = r;
+               ++tmp;
 
-       interthread_progress_window->set_default_size (200, 100);
-}
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*r);
+               if (arv) {
+                       boost::shared_ptr<Playlist> playlist = arv->region()->playlist();
 
-void
-Editor::interthread_cancel_clicked ()
-{
-       if (current_interthread_info) {
-               current_interthread_info->cancel = true;
-       }
-}
+                       if (arv->audio_region()->apply (filter) == 0) {
 
-void *
-Editor::_import_thread (void *arg)
-{
-       PBD::ThreadCreated (pthread_self(), X_("Import"));
+                               XMLNode &before = playlist->get_state();
 
-       Editor *ed = (Editor *) arg;
-       return ed->import_thread ();
-}
+                               if (filter.results.empty ()) {
 
-void *
-Editor::import_thread ()
-{
-       session->import_audiofile (import_status);
-       return 0;
-}
+                                       /* no regions returned; remove the old one */
+                                       playlist->remove_region (arv->region ());
 
-gint
-Editor::import_progress_timeout (void *arg)
-{
-       interthread_progress_label.set_text (import_status.doing_what);
+                               } else {
 
-       if (import_status.freeze) {
-               interthread_cancel_button.set_sensitive(false);
-       } else {
-               interthread_cancel_button.set_sensitive(true);
-       }
+                                       std::vector<boost::shared_ptr<Region> >::iterator res = filter.results.begin ();
 
-       if (import_status.doing_what == "building peak files") {
-               interthread_progress_bar.pulse ();
-               return FALSE;
-       } else {
-               interthread_progress_bar.set_fraction (import_status.progress/100);
-       }
+                                       /* first region replaces the old one */
+                                       playlist->replace_region (arv->region(), *res, (*res)->position());
+                                       ++res;
 
-       return !(import_status.done || import_status.cancel);
-}
+                                       /* add the rest */
+                                       while (res != filter.results.end()) {
+                                               playlist->add_region (*res, (*res)->position());
+                                               ++res;
+                                       }
 
-void
-Editor::import_audio (bool as_tracks)
-{
-       if (session == 0) {
-               warning << _("You can't import an audiofile until you have a session loaded.") << endmsg;
-               return;
-       }
+                               }
 
-       string str;
+                               XMLNode &after = playlist->get_state();
+                               session->add_command(new MementoCommand<Playlist>(*playlist, &before, &after));
+                       } else {
+                               goto out;
+                       }
+               }
 
-       if (as_tracks) {
-               str =_("Import selected as tracks");
-       } else {
-               str = _("Import selected to region list");
+               r = tmp;
        }
 
-       SoundFileOmega sfdb (str);
-       sfdb.Imported.connect (bind (mem_fun (*this, &Editor::do_import), as_tracks));
+       commit_reversible_command ();
+       rs.clear ();
 
-       sfdb.run();
+  out:
+       track_canvas->get_window()->set_cursor (*current_canvas_cursor);
 }
 
 void
-Editor::catch_new_audio_region (AudioRegion* ar)
+Editor::region_selection_op (void (Region::*pmf)(void))
 {
-       last_audio_region = ar;
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               Region* region = (*i)->region().get();
+               (region->*pmf)();
+       }
 }
 
+
 void
-Editor::do_import (vector<string> paths, bool split, bool as_tracks)
+Editor::region_selection_op (void (Region::*pmf)(void*), void *arg)
 {
-       sigc::connection c;
-       
-       /* SFDB sets "multichan" to true to indicate "split channels"
-          so reverse the setting to match the way libardour
-          interprets it.
-       */
-       
-       import_status.multichan = !split;
-
-       if (interthread_progress_window == 0) {
-               build_interthread_progress_window ();
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               Region* region = (*i)->region().get();
+               (region->*pmf)(arg);
        }
-       
-       interthread_progress_window->set_title (_("ardour: audio import in progress"));
-       interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
-       interthread_progress_window->show_all ();
-       interthread_progress_bar.set_fraction (0.0f);
-       interthread_cancel_label.set_text (_("Cancel Import"));
-       current_interthread_info = &import_status;
-
-       c = session->AudioRegionAdded.connect (mem_fun(*this, &Editor::catch_new_audio_region));
-
-       for (vector<string>::iterator i = paths.begin(); i != paths.end(); ++i ) {
-
-               interthread_progress_window->set_title (string_compose (_("ardour: importing %1"), (*i)));
-       
-               import_status.pathname = (*i);
-               import_status.done = false;
-               import_status.cancel = false;
-               import_status.freeze = false;
-               import_status.done = 0.0;
-               
-               interthread_progress_connection = 
-                 Glib::signal_timeout().connect (bind (mem_fun(*this, &Editor::import_progress_timeout), (gpointer) 0), 100);
-               
-               last_audio_region = 0;
-               
-               pthread_create_and_store ("import", &import_status.thread, 0, _import_thread, this);
-               pthread_detach (import_status.thread);
-               
-               while (!(import_status.done || import_status.cancel)) {
-                       gtk_main_iteration ();
-               }
-               
-               import_status.done = true;
-               interthread_progress_connection.disconnect ();
-
-               if (as_tracks && last_audio_region != 0) {
-                       uint32_t channels = last_audio_region->n_channels();
-
-                       AudioTrack* at = session->new_audio_track (channels, channels);
-                       AudioRegion* copy = new AudioRegion (*last_audio_region);
-                       at->disk_stream().playlist()->add_region (*copy, 0);
-               }
-       }
-
-       c.disconnect ();
-       interthread_progress_window->hide_all ();
 }
 
-int
-Editor::reject_because_rate_differs (const string & path, SF_INFO& finfo, const string & action, bool multiple_pending)
+void
+Editor::region_selection_op (void (Region::*pmf)(bool), bool yn)
 {
-       if (!session) {
-               return 1;
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               Region* region = (*i)->region().get();
+               (region->*pmf)(yn);
        }
+}
 
-       if (finfo.samplerate != (int) session->frame_rate()) {
-               vector<string> choices;
-
-               choices.push_back (string_compose (_("%1 it anyway"), action));
-
-               if (multiple_pending) {
-                       /* XXX assumptions about sentence structure
-                          here for translators. Sorry.
-                       */
-                       choices.push_back (string_compose (_("Don't %1 it"), action));
-                       choices.push_back (string_compose (_("%1 all without questions"), action));
-                       choices.push_back (_("Cancel entire import"));
-               } else {
-                       choices.push_back (_("Cancel"));
-               }
+void
+Editor::external_edit_region ()
+{
+       /* more to come */
+}
 
-               Gtkmm2ext::Choice rate_choice (
-                       string_compose (_("%1\nThis audiofile's sample rate doesn't match the session sample rate!"), path),
-                       choices);
+void
+Editor::brush (nframes64_t pos)
+{
+       RegionSelection sel;
+       RegionSelection rs;
 
-               rate_choice.chosen.connect (ptr_fun (Main::quit));
-               rate_choice.show_all ();
+       get_regions_for_action (rs);
 
-               Main::run ();
+       snap_to (pos);
 
-               switch (rate_choice.get_choice()) {
-               case 0: /* do it anyway */
-                       return 0;
-               case 1: /* don't import this one */
-                       return 1;
-               case 2: /* do the rest without asking */
-                       return -1;
-               case 3: /* stop a multi-file import */
-               default:
-                       return -2;
-               }
+       if (rs.empty()) {
+               return;
        }
 
-       return 0;
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               mouse_brush_insert_region ((*i), pos);
+       }
 }
 
-void 
-Editor::embed_audio ()
+void
+Editor::reset_region_gain_envelopes ()
 {
-       if (session == 0) {
-               warning << _("You can't embed an audiofile until you have a session loaded.") << endmsg;
+       RegionSelection rs = get_equivalent_regions (selection->regions, RouteGroup::Edit);
+
+       if (!session || rs.empty()) {
                return;
        }
 
-       SoundFileOmega sfdb (_("Add to External Region list"));
-       sfdb.Embedded.connect (mem_fun (*this, &Editor::do_embed_sndfiles));
+       session->begin_reversible_command (_("reset region gain"));
+
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
+               if (arv) {
+                       boost::shared_ptr<AutomationList> alist (arv->audio_region()->envelope());
+                       XMLNode& before (alist->get_state());
+
+                       arv->audio_region()->set_default_envelope ();
+                       session->add_command (new MementoCommand<AutomationList>(*arv->audio_region()->envelope().get(), &before, &alist->get_state()));
+               }
+       }
 
-       sfdb.run ();
+       session->commit_reversible_command ();
 }
 
 void
-Editor::do_embed_sndfiles (vector<string> paths, bool split)
+Editor::toggle_gain_envelope_visibility ()
 {
-       bool multiple_files = paths.size() > 1;
-       bool check_sample_rate = true;
+       RegionSelection rs = get_equivalent_regions (selection->regions, RouteGroup::Edit);
+
+       if (!session || rs.empty()) {
+               return;
+       }
 
-       for (vector<string>::iterator i = paths.begin(); i != paths.end(); ++i) {
-               embed_sndfile (*i, split, multiple_files, check_sample_rate);
+       session->begin_reversible_command (_("region gain envelope visible"));
+
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
+               if (arv) {
+                       XMLNode &before = arv->region()->get_state ();
+                       arv->set_envelope_visible (!arv->envelope_visible());
+                       XMLNode &after = arv->region()->get_state ();
+                       session->add_command (new MementoCommand<Region> (*(arv->region().get()), &before, &after));
+               }
        }
 
-       session->save_state ("");
+       session->commit_reversible_command ();
 }
 
 void
-Editor::embed_sndfile (string path, bool split, bool multiple_files, bool& check_sample_rate)
+Editor::toggle_gain_envelope_active ()
 {
-       SndFileSource *source = 0; /* keep g++ quiet */
-       AudioRegion::SourceList sources;
-       string idspec;
-       string linked_path;
-       SNDFILE *sf;
-       SF_INFO finfo;
+       RegionSelection rs = get_equivalent_regions (selection->regions, RouteGroup::Edit);
 
-       /* lets see if we can link it into the session */
-       
-       linked_path = session->sound_dir();
-       linked_path += PBD::basename (path);
-
-       if (link (path.c_str(), linked_path.c_str()) == 0) {
+       if (!session || rs.empty()) {
+               return;
+       }
 
-               /* there are many reasons why link(2) might have failed.
-                  but if it succeeds, we now have a link in the
-                  session sound dir that will protect against
-                  unlinking of the original path. nice.
-               */
+       session->begin_reversible_command (_("region gain envelope active"));
 
-               path = linked_path;
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
+               if (arv) {
+                       XMLNode &before = arv->region()->get_state ();
+                       arv->audio_region()->set_envelope_active (!arv->audio_region()->envelope_active());
+                       XMLNode &after = arv->region()->get_state ();
+                       session->add_command (new MementoCommand<Region> (*(arv->region().get()), &before, &after));
+               }
        }
 
-       memset (&finfo, 0, sizeof(finfo));
+       session->commit_reversible_command ();
+}
+
+void
+Editor::toggle_region_lock ()
+{
+       RegionSelection rs = get_equivalent_regions (selection->regions, RouteGroup::Edit);
 
-       /* note that we temporarily truncated _id at the colon */
-       
-       if ((sf = sf_open (path.c_str(), SFM_READ, &finfo)) == 0) {
-               char errbuf[256];
-               sf_error_str (0, errbuf, sizeof (errbuf) - 1);
-               error << string_compose(_("Editor: cannot open file \"%1\" (%2)"), selection, errbuf) << endmsg;
+       if (!session || rs.empty()) {
                return;
        }
-       sf_close (sf);
-       sf = 0;
-       
-       if (check_sample_rate) {
-               switch (reject_because_rate_differs (path, finfo, "Embed", multiple_files)) {
-               case 0:
-                       break;
-               case 1:
-                       return;
-               case -1:
-                       check_sample_rate = false;
-                       break;
-                       
-               case -2:
-               default:
-                       return;
-               }
-       }
 
-       track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
-       ARDOUR_UI::instance()->flush_pending ();
+       session->begin_reversible_command (_("region lock"));
 
-       /* make the proper number of channels in the region */
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               XMLNode &before = (*i)->region()->get_state ();
+               (*i)->region()->set_locked (!(*i)->region()->locked());
+               XMLNode &after = (*i)->region()->get_state ();
+               session->add_command (new MementoCommand<Region> (*((*i)->region().get()), &before, &after));
+       }
 
-       for (int n=0; n < finfo.channels; ++n)
-       {
-               idspec = path;
-               idspec += string_compose(":%1", n);
-               
-               try {
-                       source = new SndFileSource (idspec.c_str());
-                       sources.push_back(source);
-               } 
+       session->commit_reversible_command ();
+}
 
-               catch (failed_constructor& err) {
-                       error << string_compose(_("could not open %1"), path) << endmsg;
-                       goto out;
-               }
+void
+Editor::set_region_lock_style (Region::PositionLockStyle ps)
+{
+       RegionSelection rs = get_equivalent_regions (selection->regions, RouteGroup::Edit);
 
-               ARDOUR_UI::instance()->flush_pending ();
+       if (!session || rs.empty()) {
+               return;
        }
 
-       if (sources.size() > 0) {
+       session->begin_reversible_command (_("region lock style"));
 
-               string region_name = PBD::basename_nosuffix (path);
-               region_name += "-0";
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               XMLNode &before = (*i)->region()->get_state ();
+               (*i)->region()->set_position_lock_style (ps);
+               XMLNode &after = (*i)->region()->get_state ();
+               session->add_command (new MementoCommand<Region> (*((*i)->region().get()), &before, &after));
+       }
 
-               /* The created region isn't dropped.  It emits a signal
-                  that is picked up by the session. 
-               */
+       session->commit_reversible_command ();
+}
 
-               new AudioRegion (sources, 0, sources[0]->length(), region_name, 0,
-                                Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External));
-               
-               /* make sure we can see it in the list */
 
-                /* its the second node, always */
+void
+Editor::toggle_region_mute ()
+{
+       RegionSelection rs = get_equivalent_regions (selection->regions, RouteGroup::Edit);
 
-               // GTK2FIX ?? is it still always the 2nd node
+       if (!session || rs.empty()) {
+               return;
+       }
 
-               TreeModel::Path path ("2");
-               region_list_display.expand_row (path, true);
+       session->begin_reversible_command (_("region mute"));
 
-               ARDOUR_UI::instance()->flush_pending ();
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               XMLNode &before = (*i)->region()->get_state ();
+               (*i)->region()->set_muted (!(*i)->region()->muted());
+               XMLNode &after = (*i)->region()->get_state ();
+               session->add_command (new MementoCommand<Region> (*((*i)->region().get()), &before, &after));
        }
 
-  out:
-       track_canvas.get_window()->set_cursor (*current_canvas_cursor);
+       session->commit_reversible_command ();
 }
 
 void
-Editor::insert_sndfile (bool as_tracks)
+Editor::toggle_region_opaque ()
 {
-//     SoundFileSelector& sfdb (ARDOUR_UI::instance()->get_sfdb_window());
-       sigc::connection c;
-       string str;
-
-       if (as_tracks) {
+       RegionSelection rs = get_equivalent_regions (selection->regions, RouteGroup::Edit);
 
-//             c = sfdb.Action.connect (mem_fun(*this, &Editor::insert_paths_as_new_tracks));
-               str = _("Insert selected as new tracks");
-
-       } else {
-
-               jack_nframes_t pos;
-
-               if (clicked_audio_trackview == 0) {
-                       return;
-               }
+       if (!session || rs.empty()) {
+               return;
+       }
 
-               if (ensure_cursor (&pos)) {
-                       return;
-               }
+       session->begin_reversible_command (_("region opacity"));
 
-//             c = sfdb.Action.connect (bind (mem_fun(*this, &Editor::do_insert_sndfile), pos));
-               str = _("Insert selected");
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               XMLNode &before = (*i)->region()->get_state ();
+               (*i)->region()->set_opaque (!(*i)->region()->opaque());
+               XMLNode &after = (*i)->region()->get_state ();
+               session->add_command (new MementoCommand<Region> (*((*i)->region().get()), &before, &after));
        }
 
-//     sfdb.run (str, false);
-//     c.disconnect ();
+       session->commit_reversible_command ();
 }
 
 void
-Editor::insert_paths_as_new_tracks (vector<string> paths, bool split)
+Editor::toggle_record_enable ()
 {
-       SNDFILE *sf;
-       SF_INFO finfo;
-       bool multiple_files;
-       bool check_sample_rate = true;
-
-       multiple_files = paths.size() > 1;      
-
-       for (vector<string>::iterator p = paths.begin(); p != paths.end(); ++p) {
-               
-               memset (&finfo, 0, sizeof(finfo));
-               
-               if ((sf = sf_open ((*p).c_str(), SFM_READ, &finfo)) == 0) {
-                       char errbuf[256];
-                       sf_error_str (0, errbuf, sizeof (errbuf) - 1);
-                       error << string_compose(_("Editor: cannot open file \"%1\" (%2)"), (*p), errbuf) << endmsg;
+       bool new_state = false;
+       bool first = true;
+       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+               RouteTimeAxisView *rtav = dynamic_cast<RouteTimeAxisView *>(*i);
+               if (!rtav)
+                       continue;
+               if (!rtav->is_track())
                        continue;
-               }
-               
-               sf_close (sf);
-               sf = 0;
-               
-               /* add a new track */
-               
-               if (check_sample_rate) {
-                       switch (reject_because_rate_differs (*p, finfo, "Insert", multiple_files)) {
-                       case 0:
-                               break;
-                       case 1:
-                               continue;
-                       case -1:
-                               check_sample_rate = false;
-                               break;
-                               
-                       case -2:
-                               return;
-                       }
-               }
-               
-               uint32_t input_chan = finfo.channels;
-               uint32_t output_chan;
-               
-               if (session->get_output_auto_connect() & Session::AutoConnectMaster) {
-                       output_chan = (session->master_out() ? session->master_out()->n_inputs() : input_chan);
-               } else {
-                       output_chan = input_chan;
-               }
-               
-               (void) session->new_audio_track (input_chan, output_chan);
-
 
-               /* get the last (most recently added) track view */
-       
-               AudioTimeAxisView* tv;
-       
-               if ((tv = dynamic_cast<AudioTimeAxisView*>(track_views.back())) == 0) {
-                       fatal << _("programming error: ")
-                             << X_("last trackview after new_audio_track is not an audio track!")
-                             << endmsg;
-                       /*NOTREACHED*/
+               if (first) {
+                       new_state = !rtav->track()->record_enabled();
+                       first = false;
                }
-               
-               jack_nframes_t pos = 0;
-               insert_sndfile_into (*p, true, tv, pos, false);
-       }
-}
 
-void
-Editor::do_insert_sndfile (vector<string> paths, bool split, jack_nframes_t pos)
-{
-       for (vector<string>::iterator x = paths.begin(); x != paths.end(); ++x) {
-               insert_sndfile_into (*x, !split, clicked_audio_trackview, pos);
+               rtav->track()->set_record_enable(new_state, this);
        }
 }
 
+
 void
-Editor::insert_sndfile_into (const string & path, bool multi, AudioTimeAxisView* tv, jack_nframes_t& pos, bool prompt)
+Editor::set_fade_length (bool in)
 {
-       SndFileSource *source = 0; /* keep g++ quiet */
-       AudioRegion::SourceList sources;
-       string idspec;
-       SNDFILE *sf;
-       SF_INFO finfo;
+       RegionSelection rs;
 
-       memset (&finfo, 0, sizeof(finfo));
+       get_regions_for_action (rs, true);
 
-       /* note that we temporarily truncated _id at the colon */
-       
-       if ((sf = sf_open (path.c_str(), SFM_READ, &finfo)) == 0) {
-               char errbuf[256];
-               sf_error_str (0, errbuf, sizeof (errbuf) - 1);
-               error << string_compose(_("Editor: cannot open file \"%1\" (%2)"), path, errbuf) << endmsg;
-               return;
-       }
-       sf_close (sf);
-       sf = 0;
-       
-       if (prompt && (reject_because_rate_differs (path, finfo, "Insert", false) != 0)) {
+       if (rs.empty()) {
                return;
        }
 
-       track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
-       ARDOUR_UI::instance()->flush_pending ();
+       /* we need a region to measure the offset from the start */
 
-       /* make the proper number of channels in the region */
+       RegionView* rv = rs.front ();
 
-       for (int n=0; n < finfo.channels; ++n)
-       {
-               idspec = path;
-               idspec += string_compose(":%1", n);
+       nframes64_t pos = get_preferred_edit_position();
+       nframes64_t len;
+       char* cmd;
 
-               try {
-                       source = new SndFileSource (idspec.c_str());
-                       sources.push_back(source);
-               } 
+       if (pos > rv->region()->last_frame() || pos < rv->region()->first_frame()) {
+               /* edit point is outside the relevant region */
+               return;
+       }
 
-               catch (failed_constructor& err) {
-                       error << string_compose(_("could not open %1"), path) << endmsg;
-                       goto out;
+       if (in) {
+               if (pos <= rv->region()->position()) {
+                       /* can't do it */
+                       return;
                }
-
-               ARDOUR_UI::instance()->flush_pending ();
+               len = pos - rv->region()->position();
+               cmd = _("set fade in length");
+       } else {
+               if (pos >= rv->region()->last_frame()) {
+                       /* can't do it */
+                       return;
+               }
+               len = rv->region()->last_frame() - pos;
+               cmd = _("set fade out length");
        }
 
-       if (sources.size() > 0) {
+       begin_reversible_command (cmd);
 
-               string region_name = region_name_from_path (PBD::basename (path));
-               
-               AudioRegion *region = new AudioRegion (sources, 0, sources[0]->length(), region_name, 
-                                                      0, /* irrelevant these days */
-                                                      Region::Flag (Region::DefaultFlags|Region::WholeFile|Region::External));
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+               AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
 
-               begin_reversible_command (_("insert sndfile"));
-               session->add_undo (tv->playlist()->get_memento());
-               tv->playlist()->add_region (*region, pos);
-               session->add_redo_no_execute (tv->playlist()->get_memento());
-               commit_reversible_command ();
-               
-               pos += sources[0]->length();
+               if (!tmp) {
+                       return;
+               }
+
+               boost::shared_ptr<AutomationList> alist;
+               if (in) {
+                       alist = tmp->audio_region()->fade_in();
+               } else {
+                       alist = tmp->audio_region()->fade_out();
+               }
+
+               XMLNode &before = alist->get_state();
+
+               if (in) {
+                       tmp->audio_region()->set_fade_in_length (len);
+                       tmp->audio_region()->set_fade_in_active (true);
+               } else {
+                       tmp->audio_region()->set_fade_out_length (len);
+                       tmp->audio_region()->set_fade_out_active (true);
+               }
 
-               ARDOUR_UI::instance()->flush_pending ();
+               XMLNode &after = alist->get_state();
+               session->add_command(new MementoCommand<AutomationList>(*alist, &before, &after));
        }
 
-  out:
-       track_canvas.get_window()->set_cursor (*current_canvas_cursor);
-       return;
+       commit_reversible_command ();
 }
 
 void
-Editor::region_from_selection ()
+Editor::toggle_fade_active (bool in)
 {
-       if (clicked_trackview == 0) {
-               return;
-       }
+       RegionSelection rs;
 
-       if (selection->time.empty()) {
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
                return;
        }
 
-       jack_nframes_t start = selection->time[clicked_selection].start;
-       jack_nframes_t end = selection->time[clicked_selection].end;
+       const char* cmd = (in ? _("toggle fade in active") : _("toggle fade out active"));
+       bool have_switch = false;
+       bool yn = false;
 
-       jack_nframes_t selection_cnt = end - start + 1;
-       
-       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
-
-               AudioRegion *region;
-               AudioRegion *current;
-               Region* current_r;
-               Playlist *pl;
+       begin_reversible_command (cmd);
 
-               jack_nframes_t internal_start;
-               string new_name;
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+               AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
 
-               if ((pl = (*i)->playlist()) == 0) {
-                       continue;
+               if (!tmp) {
+                       return;
                }
 
-               if ((current_r = pl->top_region_at (start)) == 0) {
-                       continue;
+               boost::shared_ptr<AudioRegion> region (tmp->audio_region());
+
+               /* make the behaviour consistent across all regions */
+
+               if (!have_switch) {
+                       if (in) {
+                               yn = region->fade_in_active();
+                       } else {
+                               yn = region->fade_out_active();
+                       }
+                       have_switch = true;
                }
 
-               if ((current = dynamic_cast<AudioRegion*> (current_r)) != 0) {
-                       internal_start = start - current->position();
-                       session->region_name (new_name, current->name(), true);
-                       region = new AudioRegion (*current, internal_start, selection_cnt, new_name);
+               XMLNode &before = region->get_state();
+               if (in) {
+                       region->set_fade_in_active (!yn);
+               } else {
+                       region->set_fade_out_active (!yn);
                }
+               XMLNode &after = region->get_state();
+               session->add_command(new MementoCommand<AudioRegion>(*region.get(), &before, &after));
        }
-}      
+
+       commit_reversible_command ();
+}
 
 void
-Editor::create_region_from_selection (vector<AudioRegion *>& new_regions)
+Editor::set_fade_in_shape (AudioRegion::FadeShape shape)
 {
-       if (selection->time.empty() || selection->tracks.empty()) {
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
                return;
        }
 
-       jack_nframes_t start = selection->time[clicked_selection].start;
-       jack_nframes_t end = selection->time[clicked_selection].end;
-       
-       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
-
-               AudioRegion* current;
-               Region* current_r;
-               Playlist* playlist;
-               jack_nframes_t internal_start;
-               string new_name;
+       begin_reversible_command (_("set fade in shape"));
 
-               if ((playlist = (*i)->playlist()) == 0) {
-                       continue;
-               }
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+               AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
 
-               if ((current_r = playlist->top_region_at(start)) == 0) {
-                       continue;
+               if (!tmp) {
+                       return;
                }
 
-               if ((current = dynamic_cast<AudioRegion*>(current_r)) == 0) {
-                       continue;
-               }
-       
-               internal_start = start - current->position();
-               session->region_name (new_name, current->name(), true);
-               
-               new_regions.push_back (new AudioRegion (*current, internal_start, end - start + 1, new_name));
-       }
-}
+               boost::shared_ptr<AutomationList> alist = tmp->audio_region()->fade_in();
+               XMLNode &before = alist->get_state();
 
-void
-Editor::split_multichannel_region ()
-{
-       vector<AudioRegion*> v;
+               tmp->audio_region()->set_fade_in_shape (shape);
 
-       if (!clicked_regionview || clicked_regionview->region.n_channels() < 2) {
-               return;
+               XMLNode &after = alist->get_state();
+               session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &after));
        }
 
-       clicked_regionview->region.separate_by_channel (*session, v);
+       commit_reversible_command ();
 
-       /* nothing else to do, really */
 }
 
 void
-Editor::new_region_from_selection ()
+Editor::set_fade_out_shape (AudioRegion::FadeShape shape)
 {
-       region_from_selection ();
-       cancel_selection ();
-}
+       RegionSelection rs;
 
-void
-Editor::separate_region_from_selection ()
-{
-       bool doing_undo = false;
+       get_regions_for_action (rs);
 
-       if (selection->time.empty()) {
+       if (rs.empty()) {
                return;
        }
 
-       Playlist *playlist;
-               
-       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
-
-               AudioTimeAxisView* atv;
+       begin_reversible_command (_("set fade out shape"));
 
-               if ((atv = dynamic_cast<AudioTimeAxisView*> ((*i))) != 0) {
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+               AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
 
-                       if (atv->is_audio_track()) {
-                                       
-                               if ((playlist = atv->playlist()) != 0) {
-                                       if (!doing_undo) {
-                                               begin_reversible_command (_("separate"));
-                                               doing_undo = true;
-                                       }
-                                       if (doing_undo) session->add_undo ((playlist)->get_memento());
-                       
-                                       /* XXX need to consider musical time selections here at some point */
+               if (!tmp) {
+                       return;
+               }
 
-                                       double speed = atv->get_diskstream()->speed();
+               boost::shared_ptr<AutomationList> alist = tmp->audio_region()->fade_out();
+               XMLNode &before = alist->get_state();
 
-                                       for (list<AudioRange>::iterator t = selection->time.begin(); t != selection->time.end(); ++t) {
-                                               playlist->partition ((jack_nframes_t)((*t).start * speed), (jack_nframes_t)((*t).end * speed), true);
-                                       }
+               tmp->audio_region()->set_fade_out_shape (shape);
 
-                                       if (doing_undo) session->add_redo_no_execute (playlist->get_memento());
-                               }
-                       }
-               }
+               XMLNode &after = alist->get_state();
+               session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &after));
        }
 
-       if (doing_undo) commit_reversible_command ();
+       commit_reversible_command ();
 }
 
 void
-Editor::crop_region_to_selection ()
+Editor::set_fade_in_active (bool yn)
 {
-       if (selection->time.empty()) {
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
                return;
        }
 
-       vector<Playlist*> playlists;
-       Playlist *playlist;
+       begin_reversible_command (_("set fade in active"));
 
-       if (clicked_trackview != 0) {
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+               AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
 
-               if ((playlist = clicked_trackview->playlist()) == 0) {
+               if (!tmp) {
                        return;
                }
 
-               playlists.push_back (playlist);
 
-       } else {
-               
-               for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+               boost::shared_ptr<AudioRegion> ar (tmp->audio_region());
 
-                       AudioTimeAxisView* atv;
+               XMLNode &before = ar->get_state();
 
-                       if ((atv = dynamic_cast<AudioTimeAxisView*> ((*i))) != 0) {
+               ar->set_fade_in_active (yn);
 
-                               if (atv->is_audio_track()) {
-                                       
-                                       if ((playlist = atv->playlist()) != 0) {
-                                               playlists.push_back (playlist);
-                                       }
-                               }
-                       }
-               }
+               XMLNode &after = ar->get_state();
+               session->add_command(new MementoCommand<AudioRegion>(*ar, &before, &after));
        }
 
-       if (!playlists.empty()) {
-
-               jack_nframes_t start;
-               jack_nframes_t end;
-               jack_nframes_t cnt;
-
-               begin_reversible_command (_("trim to selection"));
-
-               for (vector<Playlist*>::iterator i = playlists.begin(); i != playlists.end(); ++i) {
-                       
-                       Region *region;
-                       
-                       start = selection->time.start();
-
-                       if ((region = (*i)->top_region_at(start)) == 0) {
-                               continue;
-                       }
-                       
-                       /* now adjust lengths to that we do the right thing
-                          if the selection extends beyond the region
-                       */
-                       
-                       start = max (start, region->position());
-                       end = min (selection->time.end_frame(), start + region->length() - 1);
-                       cnt = end - start + 1;
-
-                       session->add_undo ((*i)->get_memento());
-                       region->trim_to (start, cnt, this);
-                       session->add_redo_no_execute ((*i)->get_memento());
-               }
-
-               commit_reversible_command ();
-       }
-}              
+       commit_reversible_command ();
+}
 
 void
-Editor::region_fill_track ()
+Editor::set_fade_out_active (bool yn)
 {
-       jack_nframes_t end;
+       RegionSelection rs;
 
-       if (!session || selection->audio_regions.empty()) {
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
                return;
        }
 
-       end = session->current_end_frame ();
-
-       begin_reversible_command (_("region fill"));
-
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
+       begin_reversible_command (_("set fade out active"));
 
-               AudioRegion& region ((*i)->region);
-               Playlist* pl = region.playlist();
+       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
+               AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
 
-               if (end <= region.last_frame()) {
+               if (!tmp) {
                        return;
                }
 
-               double times = (double) (end - region.last_frame()) / (double) region.length();
+               boost::shared_ptr<AudioRegion> ar (tmp->audio_region());
 
-               if (times == 0) {
-                       return;
-               }
+               XMLNode &before = ar->get_state();
 
-               session->add_undo (pl->get_memento());
-               pl->add_region (*(new AudioRegion (region)), region.last_frame(), times);
-               session->add_redo_no_execute (pl->get_memento());
+               ar->set_fade_out_active (yn);
+
+               XMLNode &after = ar->get_state();
+               session->add_command(new MementoCommand<AudioRegion>(*ar, &before, &after));
        }
 
        commit_reversible_command ();
 }
 
 void
-Editor::region_fill_selection ()
+Editor::toggle_selected_region_fades (int dir)
 {
-               if (clicked_audio_trackview == 0 || !clicked_audio_trackview->is_audio_track()) {
-               return;
-       }
+       RegionSelection rs;
+       RegionSelection::iterator i;
+       boost::shared_ptr<AudioRegion> ar;
+       bool yn;
 
-       if (selection->time.empty()) {
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
                return;
        }
 
-       Region *region;
-
-       Glib::RefPtr<TreeSelection> selected = region_list_display.get_selection();
+       for (i = rs.begin(); i != rs.end(); ++i) {
+               if ((ar = boost::dynamic_pointer_cast<AudioRegion>((*i)->region())) != 0) {
+                       if (dir == -1) {
+                               yn = ar->fade_out_active ();
+                       } else {
+                               yn = ar->fade_in_active ();
+                       }
+                       break;
+               }
+       }
 
-       if (selected->count_selected_rows() != 1) {
+       if (i == rs.end()) {
                return;
        }
 
-       TreeModel::iterator i = region_list_display.get_selection()->get_selected();
-       region = (*i)[region_list_columns.region];
+       /* XXX should this undo-able? */
 
-       jack_nframes_t start = selection->time[clicked_selection].start;
-       jack_nframes_t end = selection->time[clicked_selection].end;
-
-       Playlist *playlist; 
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               if ((ar = boost::dynamic_pointer_cast<AudioRegion>((*i)->region())) == 0) {
+                       continue;
+               }
+               if (dir == 1 || dir == 0) {
+                       ar->set_fade_in_active (!yn);
+               }
 
-       if (selection->tracks.empty()) {
-               return;
+               if (dir == -1 || dir == 0) {
+                       ar->set_fade_out_active (!yn);
+               }
        }
+}
 
-       jack_nframes_t selection_length = end - start;
-       float times = (float)selection_length / region->length();
-       
-       begin_reversible_command (_("fill selection"));
-       
-       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
 
-               if ((playlist = (*i)->playlist()) == 0) {
-                       continue;
-               }               
-               
-               session->add_undo (playlist->get_memento());
-               playlist->add_region (*(createRegion (*region)), start, times);
-               session->add_redo_no_execute (playlist->get_memento());
+/** Update region fade visibility after its configuration has been changed */
+void
+Editor::update_region_fade_visibility ()
+{
+       bool _fade_visibility = session->config.get_show_region_fades ();
+
+       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+               AudioTimeAxisView* v = dynamic_cast<AudioTimeAxisView*>(*i);
+               if (v) {
+                       if (_fade_visibility) {
+                               v->audio_view()->show_all_fades ();
+                       } else {
+                               v->audio_view()->hide_all_fades ();
+                       }
+               }
        }
-       
-       commit_reversible_command ();                   
 }
 
+/** Update crossfade visibility after its configuration has been changed */
 void
-Editor::set_a_regions_sync_position (Region& region, jack_nframes_t position)
+Editor::update_xfade_visibility ()
 {
+       _xfade_visibility = session->config.get_xfades_visible ();
 
-       if (!region.covers (position)) {
-         error << _("Programming error. that region doesn't cover that position") << __FILE__ << " +" << __LINE__ << endmsg;
-               return;
+       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+               AudioTimeAxisView* v = dynamic_cast<AudioTimeAxisView*>(*i);
+               if (v) {
+                       if (_xfade_visibility) {
+                               v->show_all_xfades ();
+                       } else {
+                               v->hide_all_xfades ();
+                       }
+               }
        }
-       begin_reversible_command (_("set region sync position"));
-       session->add_undo (region.playlist()->get_memento());
-       region.set_sync_position (position);
-       session->add_redo_no_execute (region.playlist()->get_memento());
-       commit_reversible_command ();
 }
 
 void
-Editor::set_region_sync_from_edit_cursor ()
+Editor::set_edit_point ()
 {
-       if (clicked_regionview == 0) {
-               return;
-       }
+       nframes64_t where;
+       bool ignored;
 
-       if (!clicked_regionview->region.covers (edit_cursor->current_frame)) {
-               error << _("Place the edit cursor at the desired sync point") << endmsg;
+       if (!mouse_frame (where, ignored)) {
                return;
        }
 
-       Region& region (clicked_regionview->region);
-       begin_reversible_command (_("set sync from edit cursor"));
-       session->add_undo (region.playlist()->get_memento());
-       region.set_sync_position (edit_cursor->current_frame);
-       session->add_redo_no_execute (region.playlist()->get_memento());
-       commit_reversible_command ();
+       snap_to (where);
+
+       if (selection->markers.empty()) {
+
+               mouse_add_new_marker (where);
+
+       } else {
+               bool ignored;
+
+               Location* loc = find_location_from_marker (selection->markers.front(), ignored);
+
+               if (loc) {
+                       loc->move_to (where);
+               }
+       }
 }
 
 void
-Editor::remove_region_sync ()
+Editor::set_playhead_cursor ()
 {
-       if (clicked_regionview) {
-               Region& region (clicked_regionview->region);
-               begin_reversible_command (_("remove sync"));
-               session->add_undo (region.playlist()->get_memento());
-               region.clear_sync_position ();
-               session->add_redo_no_execute (region.playlist()->get_memento());
-               commit_reversible_command ();
+       if (entered_marker) {
+               session->request_locate (entered_marker->position(), session->transport_rolling());
+       } else {
+               nframes64_t where;
+               bool ignored;
+
+               if (!mouse_frame (where, ignored)) {
+                       return;
+               }
+
+               snap_to (where);
+
+               if (session) {
+                       session->request_locate (where, session->transport_rolling());
+               }
        }
 }
 
 void
-Editor::naturalize ()
+Editor::split ()
 {
-       if (selection->audio_regions.empty()) {
+       RegionSelection rs;
+
+       get_regions_for_action (rs, true);
+
+       nframes64_t where = get_preferred_edit_position();
+
+       if (rs.empty()) {
                return;
        }
-       begin_reversible_command (_("naturalize"));
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-               session->add_undo ((*i)->region.get_memento());
-               (*i)->region.move_to_natural_position (this);
-               session->add_redo_no_execute ((*i)->region.get_memento());
-       }
-       commit_reversible_command ();
-}
 
-void
-Editor::align (RegionPoint what)
-{
-       align_selection (what, edit_cursor->current_frame);
+       split_regions_at (where, rs);
 }
 
 void
-Editor::align_relative (RegionPoint what)
+Editor::ensure_entered_track_selected (bool op_really_wants_one_track_if_none_are_selected)
 {
-       align_selection_relative (what, edit_cursor->current_frame);
+       if (entered_track && mouse_mode == MouseObject) {
+               if (!selection->tracks.empty()) {
+                       if (!selection->selected (entered_track)) {
+                               selection->add (entered_track);
+                       }
+               } else {
+                       /* there is no selection, but this operation requires/prefers selected objects */
+
+                       if (op_really_wants_one_track_if_none_are_selected) {
+                               selection->set (entered_track);
+                       }
+               }
+       }
 }
 
-struct RegionSortByTime {
-    bool operator() (const AudioRegionView* a, const AudioRegionView* b) {
-           return a->region.position() < b->region.position();
+struct EditorOrderRouteSorter {
+    bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
+           /* use of ">" forces the correct sort order */
+           return a->order_key ("editor") < b->order_key ("editor");
     }
 };
 
 void
-Editor::align_selection_relative (RegionPoint point, jack_nframes_t position)
+Editor::select_next_route()
 {
-       if (selection->audio_regions.empty()) {
+       if (selection->tracks.empty()) {
+               selection->set (track_views.front());
                return;
        }
 
-       jack_nframes_t distance;
-       jack_nframes_t pos = 0;
-       int dir;
-
-       list<AudioRegionView*> sorted;
-       selection->audio_regions.by_position (sorted);
-       Region& r ((*sorted.begin())->region);
+       TimeAxisView* current = selection->tracks.front();
 
-       switch (point) {
-       case Start:
-               pos = r.first_frame ();
-               break;
+       RouteUI *rui;
+       do {
+               for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+                       if (*i == current) {
+                               ++i;
+                               if (i != track_views.end()) {
+                                       current = (*i);
+                               } else {
+                                       current = (*(track_views.begin()));
+                                       //selection->set (*(track_views.begin()));
+                               }
+                               break;
+                       }
+               }
+               rui = dynamic_cast<RouteUI *>(current);
+       } while ( current->hidden() || (rui != NULL && !rui->route()->active()));
 
-       case End:
-               pos = r.last_frame();
-               break;
+       selection->set(current);
 
-       case SyncPoint:
-               pos = r.adjust_to_sync (r.first_frame());
-               break;  
-       }
+       ensure_track_visible(current);
+}
 
-       if (pos > position) {
-               distance = pos - position;
-               dir = -1;
-       } else {
-               distance = position - pos;
-               dir = 1;
+void
+Editor::select_prev_route()
+{
+       if (selection->tracks.empty()) {
+               selection->set (track_views.front());
+               return;
        }
 
-       begin_reversible_command (_("align selection (relative)"));
-
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-
-               Region& region ((*i)->region);
+       TimeAxisView* current = selection->tracks.front();
 
-               session->add_undo (region.playlist()->get_memento());
-               
-               if (dir > 0) {
-                       region.set_position (region.position() + distance, this);
-               } else {
-                       region.set_position (region.position() - distance, this);
+       RouteUI *rui;
+       do {
+               for (TrackViewList::reverse_iterator i = track_views.rbegin(); i != track_views.rend(); ++i) {
+                       if (*i == current) {
+                               ++i;
+                               if (i != track_views.rend()) {
+                                       current = (*i);
+                               } else {
+                                       current = *(track_views.rbegin());
+                               }
+                               break;
+                       }
                }
+               rui = dynamic_cast<RouteUI *>(current);
+       } while ( current->hidden() || (rui != NULL && !rui->route()->active()));
 
-               session->add_redo_no_execute (region.playlist()->get_memento());
-
-       }
+       selection->set (current);
 
-       commit_reversible_command ();
+       ensure_track_visible(current);
 }
 
 void
-Editor::align_selection (RegionPoint point, jack_nframes_t position)
+Editor::ensure_track_visible(TimeAxisView *track)
 {
-       if (selection->audio_regions.empty()) {
+       if (track->hidden())
                return;
-       }
 
-       begin_reversible_command (_("align selection"));
+       double const current_view_min_y = vertical_adjustment.get_value();
+       double const current_view_max_y = vertical_adjustment.get_value() + vertical_adjustment.get_page_size() - canvas_timebars_vsize;
 
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-               align_region_internal ((*i)->region, point, position);
+       double const track_min_y = track->y_position ();
+       double const track_max_y = track->y_position () + track->effective_height ();
+
+       if (track_min_y >= current_view_min_y &&
+            track_max_y <= current_view_max_y) {
+               return;
        }
 
-       commit_reversible_command ();
-}
+       double new_value;
 
-void
-Editor::align_region (Region& region, RegionPoint point, jack_nframes_t position)
-{
-       begin_reversible_command (_("align region"));
-       align_region_internal (region, point, position);
-       commit_reversible_command ();
+       if (track_min_y < current_view_min_y) {
+               // Track is above the current view
+               new_value = track_min_y;
+       } else {
+               // Track is below the current view
+               new_value = track->y_position () + track->effective_height() + canvas_timebars_vsize - vertical_adjustment.get_page_size();
+       }
+
+       vertical_adjustment.set_value(new_value);
 }
 
 void
-Editor::align_region_internal (Region& region, RegionPoint point, jack_nframes_t position)
+Editor::set_loop_from_selection (bool play)
 {
-       session->add_undo (region.playlist()->get_memento());
+       if (session == 0 || selection->time.empty()) {
+               return;
+       }
 
-       switch (point) {
-       case SyncPoint:
-               region.set_position (region.adjust_to_sync (position), this);
-               break;
+       nframes64_t start = selection->time[clicked_selection].start;
+       nframes64_t end = selection->time[clicked_selection].end;
 
-       case End:
-               if (position > region.length()) {
-                       region.set_position (position - region.length(), this);
-               }
-               break;
+       set_loop_range (start, end,  _("set loop range from selection"));
 
-       case Start:
-               region.set_position (position, this);
-               break;
+       if (play) {
+               session->request_play_loop (true);
+               session->request_locate (start, true);
        }
-
-       session->add_redo_no_execute (region.playlist()->get_memento());
-}      
+}
 
 void
-Editor::trim_region_to_edit_cursor ()
+Editor::set_loop_from_edit_range (bool play)
 {
-       if (clicked_regionview == 0) {
+       if (session == 0) {
                return;
        }
 
-       Region& region (clicked_regionview->region);
-
-       float speed = 1.0f;
-       AudioTimeAxisView *atav;
+       nframes64_t start;
+       nframes64_t end;
 
-       if ( clicked_trackview != 0 && (atav = dynamic_cast<AudioTimeAxisView*>(clicked_trackview)) != 0 ) {
-               if (atav->get_diskstream() != 0) {
-                       speed = atav->get_diskstream()->speed();
-               }
+       if (!get_edit_op_range (start, end)) {
+               return;
        }
 
-       begin_reversible_command (_("trim to edit"));
-       session->add_undo (region.playlist()->get_memento());
-       region.trim_end( session_frame_to_track_frame(edit_cursor->current_frame, speed), this);
-       session->add_redo_no_execute (region.playlist()->get_memento());
-       commit_reversible_command ();
+       set_loop_range (start, end,  _("set loop range from edit range"));
+
+       if (play) {
+               session->request_play_loop (true);
+               session->request_locate (start, true);
+       }
 }
 
 void
-Editor::trim_region_from_edit_cursor ()
+Editor::set_loop_from_region (bool play)
 {
-       if (clicked_regionview == 0) {
-               return;
-       }
+       nframes64_t start = max_frames;
+       nframes64_t end = 0;
 
-       Region& region (clicked_regionview->region);
+       RegionSelection rs;
 
-       float speed = 1.0f;
-       AudioTimeAxisView *atav;
+       get_regions_for_action (rs);
 
-       if ( clicked_trackview != 0 && (atav = dynamic_cast<AudioTimeAxisView*>(clicked_trackview)) != 0 ) {
-               if (atav->get_diskstream() != 0) {
-                       speed = atav->get_diskstream()->speed();
+       if (rs.empty()) {
+               return;
+       }
+
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               if ((*i)->region()->position() < start) {
+                       start = (*i)->region()->position();
+               }
+               if ((*i)->region()->last_frame() + 1 > end) {
+                       end = (*i)->region()->last_frame() + 1;
                }
        }
 
-       begin_reversible_command (_("trim to edit"));
-       session->add_undo (region.playlist()->get_memento());
-       region.trim_end( session_frame_to_track_frame(edit_cursor->current_frame, speed), this);
-       session->add_redo_no_execute (region.playlist()->get_memento());
-       commit_reversible_command ();
+       set_loop_range (start, end, _("set loop range from region"));
+
+       if (play) {
+               session->request_play_loop (true);
+               session->request_locate (start, true);
+       }
 }
 
 void
-Editor::unfreeze_route ()
+Editor::set_punch_from_selection ()
 {
-       if (clicked_audio_trackview == 0 || !clicked_audio_trackview->is_audio_track()) {
+       if (session == 0 || selection->time.empty()) {
                return;
        }
-       
-       clicked_audio_trackview->audio_track()->unfreeze ();
-}
 
-void*
-Editor::_freeze_thread (void* arg)
-{
-       PBD::ThreadCreated (pthread_self(), X_("Freeze"));
-       return static_cast<Editor*>(arg)->freeze_thread ();
-}
+       nframes64_t start = selection->time[clicked_selection].start;
+       nframes64_t end = selection->time[clicked_selection].end;
 
-void*
-Editor::freeze_thread ()
-{
-       clicked_audio_trackview->audio_track()->freeze (*current_interthread_info);
-       return 0;
+       set_punch_range (start, end,  _("set punch range from selection"));
 }
 
-gint
-Editor::freeze_progress_timeout (void *arg)
+void
+Editor::set_punch_from_edit_range ()
 {
-       interthread_progress_bar.set_fraction (current_interthread_info->progress/100);
-       return !(current_interthread_info->done || current_interthread_info->cancel);
+       if (session == 0) {
+               return;
+       }
+
+       nframes64_t start;
+       nframes64_t end;
+
+       if (!get_edit_op_range (start, end)) {
+               return;
+       }
+
+       set_punch_range (start, end,  _("set punch range from edit range"));
 }
 
 void
-Editor::freeze_route ()
+Editor::set_punch_from_region ()
 {
-       if (clicked_audio_trackview == 0 || !clicked_audio_trackview->is_audio_track()) {
+       nframes64_t start = max_frames;
+       nframes64_t end = 0;
+
+       RegionSelection rs;
+
+       get_regions_for_action (rs);
+
+       if (rs.empty()) {
                return;
        }
-       
-       InterThreadInfo itt;
 
-       if (interthread_progress_window == 0) {
-               build_interthread_progress_window ();
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               if ((*i)->region()->position() < start) {
+                       start = (*i)->region()->position();
+               }
+               if ((*i)->region()->last_frame() + 1 > end) {
+                       end = (*i)->region()->last_frame() + 1;
+               }
        }
-       
-       interthread_progress_window->set_title (_("ardour: freeze"));
-       interthread_progress_window->set_position (Gtk::WIN_POS_MOUSE);
-       interthread_progress_window->show_all ();
-       interthread_progress_bar.set_fraction (0.0f);
-       interthread_progress_label.set_text ("");
-       interthread_cancel_label.set_text (_("Cancel Freeze"));
-       current_interthread_info = &itt;
-
-       interthread_progress_connection = 
-         Glib::signal_timeout().connect (bind (mem_fun(*this, &Editor::freeze_progress_timeout), (gpointer) 0), 100);
 
-       itt.done = false;
-       itt.cancel = false;
-       itt.progress = 0.0f;
+       set_punch_range (start, end, _("set punch range from region"));
+}
 
-       pthread_create (&itt.thread, 0, _freeze_thread, this);
+void
+Editor::pitch_shift_regions ()
+{
+       RegionSelection rs;
 
-       track_canvas.get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
+       get_regions_for_action (rs);
 
-       while (!itt.done && !itt.cancel) {
-               gtk_main_iteration ();
+       if (rs.empty()) {
+               return;
        }
 
-       interthread_progress_connection.disconnect ();
-       interthread_progress_window->hide_all ();
-       current_interthread_info = 0;
-       track_canvas.get_window()->set_cursor (*current_canvas_cursor);
+       pitch_shift (rs, 1.2);
 }
 
 void
-Editor::bounce_range_selection ()
+Editor::use_region_as_bar ()
 {
-       if (selection->time.empty()) {
+       if (!session) {
                return;
        }
 
-       TrackViewList *views = get_valid_views (selection->time.track, selection->time.group);
-
-       jack_nframes_t start = selection->time[clicked_selection].start;
-       jack_nframes_t end = selection->time[clicked_selection].end;
-       jack_nframes_t cnt = end - start + 1;
-       
-       begin_reversible_command (_("bounce range"));
+       RegionSelection rs;
 
-       for (TrackViewList::iterator i = views->begin(); i != views->end(); ++i) {
+       get_regions_for_action (rs);
 
-               AudioTimeAxisView* atv;
+       if (rs.empty()) {
+               return;
+       }
 
-               if ((atv = dynamic_cast<AudioTimeAxisView*> (*i)) == 0) {
-                       continue;
-               }
-               
-               Playlist* playlist;
-               
-               if ((playlist = atv->playlist()) == 0) {
-                       return;
-               }
+       RegionView* rv = rs.front();
 
-               InterThreadInfo itt;
-               
-               itt.done = false;
-               itt.cancel = false;
-               itt.progress = false;
-               
-               session->add_undo (playlist->get_memento());
-               atv->audio_track()->bounce_range (start, cnt, itt);
-               session->add_redo_no_execute (playlist->get_memento());
-       }
-       
-       commit_reversible_command ();
-       
-       delete views;
+       define_one_bar (rv->region()->position(), rv->region()->last_frame() + 1);
 }
 
 void
-Editor::cut ()
+Editor::use_range_as_bar ()
 {
-       cut_copy (Cut);
+       nframes64_t start, end;
+       if (get_edit_op_range (start, end)) {
+               define_one_bar (start, end);
+       }
 }
 
 void
-Editor::copy ()
+Editor::define_one_bar (nframes64_t start, nframes64_t end)
 {
-       cut_copy (Copy);
-}
+       nframes64_t length = end - start;
 
-void 
-Editor::cut_copy (CutCopyOp op)
-{
-       /* only cancel selection if cut/copy is successful.*/
+       const Meter& m (session->tempo_map().meter_at (start));
 
-       string opname;
+       /* length = 1 bar */
 
-       switch (op) {
-       case Cut:
-               opname = _("cut");
-               break;
-       case Copy:
-               opname = _("copy");
-               break;
-       case Clear:
-               opname = _("clear");
-               break;
-       }
-       
-       cut_buffer->clear ();
+       /* now we want frames per beat.
+          we have frames per bar, and beats per bar, so ...
+       */
 
-       switch (current_mouse_mode()) {
-       case MouseObject: 
-               if (!selection->audio_regions.empty() || !selection->points.empty()) {
+       double frames_per_beat = length / m.beats_per_bar();
 
-                       begin_reversible_command (opname + _(" objects"));
+       /* beats per minute = */
 
-                       if (!selection->audio_regions.empty()) {
-                               
-                               cut_copy_regions (op);
-                               
-                               if (op == Cut) {
-                                       selection->clear_audio_regions ();
-                               }
-                       }
+       double beats_per_minute = (session->frame_rate() * 60.0) / frames_per_beat;
 
-                       if (!selection->points.empty()) {
-                               cut_copy_points (op);
+       /* now decide whether to:
 
-                               if (op == Cut) {
-                                       selection->clear_points ();
-                               }
-                       }
+           (a) set global tempo
+           (b) add a new tempo marker
 
-                       commit_reversible_command ();   
-               }
-               break;
-               
-       case MouseRange:
-               if (!selection->time.empty()) {
+       */
 
-                       begin_reversible_command (opname + _(" range"));
-                       cut_copy_ranges (op);
-                       commit_reversible_command ();
+       const TempoSection& t (session->tempo_map().tempo_section_at (start));
 
-                       if (op == Cut) {
-                               selection->clear_time ();
-                       }
-                       
-               }
-               break;
-               
-       default:
-               break;
-       }
-}
+       bool do_global = false;
 
-void
-Editor::cut_copy_points (CutCopyOp op)
-{
-       for (PointSelection::iterator i = selection->points.begin(); i != selection->points.end(); ++i) {
+       if ((session->tempo_map().n_tempos() == 1) && (session->tempo_map().n_meters() == 1)) {
 
-               AutomationTimeAxisView* atv = dynamic_cast<AutomationTimeAxisView*>(&(*i).track);
+               /* only 1 tempo & 1 meter: ask if the user wants to set the tempo
+                  at the start, or create a new marker
+               */
 
-               if (atv) {
-                       atv->cut_copy_clear_objects (selection->points, op);
-               } 
-       }
-}
+               vector<string> options;
+               options.push_back (_("Cancel"));
+               options.push_back (_("Add new marker"));
+               options.push_back (_("Set global tempo"));
+               Choice c (_("Do you want to set the global tempo or add new tempo marker?"),
+                         options);
+               c.set_default_response (2);
 
-void
-Editor::cut_copy_regions (CutCopyOp op)
-{
-        typedef std::map<AudioPlaylist*,AudioPlaylist*> PlaylistMapping;
-       PlaylistMapping pmap;
-       jack_nframes_t first_position = max_frames;
-       set<Playlist*> freezelist;
-       pair<set<Playlist*>::iterator,bool> insert_result;
+               switch (c.run()) {
+               case 0:
+                       return;
 
-       for (AudioRegionSelection::iterator x = selection->audio_regions.begin(); x != selection->audio_regions.end(); ++x) {
-               first_position = min ((*x)->region.position(), first_position);
+               case 2:
+                       do_global = true;
+                       break;
 
-               if (op == Cut || op == Clear) {
-                       AudioPlaylist *pl = dynamic_cast<AudioPlaylist*>((*x)->region.playlist());
-                       if (pl) {
-                               insert_result = freezelist.insert (pl);
-                               if (insert_result.second) {
-                                       pl->freeze ();
-                                       session->add_undo (pl->get_memento());
-                               }
-                       }
+               default:
+                       do_global = false;
                }
-       }
 
-       for (AudioRegionSelection::iterator x = selection->audio_regions.begin(); x != selection->audio_regions.end(); ) {
+       } else {
 
-               AudioPlaylist *pl = dynamic_cast<AudioPlaylist*>((*x)->region.playlist());
-               AudioPlaylist* npl;
-               AudioRegionSelection::iterator tmp;
-               
-               tmp = x;
-               ++tmp;
+               /* more than 1 tempo and/or meter section already, go ahead do the "usual":
+                  if the marker is at the region starter, change it, otherwise add
+                  a new tempo marker
+               */
+       }
 
-               if (pl) {
+       begin_reversible_command (_("set tempo from region"));
+       XMLNode& before (session->tempo_map().get_state());
 
-                       PlaylistMapping::iterator pi = pmap.find (pl);
-                       
-                       if (pi == pmap.end()) {
-                               npl = new AudioPlaylist (*session, "cutlist", true);
-                               npl->freeze();
-                               pmap[pl] = npl;
-                       } else {
-                               npl = pi->second;
-                       }
+       if (do_global) {
+               session->tempo_map().change_initial_tempo (beats_per_minute, t.note_type());
+       } else if (t.frame() == start) {
+               session->tempo_map().change_existing_tempo_at (start, beats_per_minute, t.note_type());
+       } else {
+               session->tempo_map().add_tempo (Tempo (beats_per_minute, t.note_type()), start);
+       }
 
-                       switch (op) {
-                       case Cut:
-                               npl->add_region (*(new AudioRegion ((*x)->region)), (*x)->region.position() - first_position);
-                               pl->remove_region (&((*x)->region));
-                               break;
+       XMLNode& after (session->tempo_map().get_state());
 
-                       case Copy:
-                               npl->add_region (*(new AudioRegion ((*x)->region)), (*x)->region.position() - first_position);
-                               break;
+       session->add_command (new MementoCommand<TempoMap>(session->tempo_map(), &before, &after));
+       commit_reversible_command ();
+}
 
-                       case Clear:
-                               pl->remove_region (&((*x)->region));
-                               break;
-                       }
-               }
+void
+Editor::split_region_at_transients ()
+{
+       AnalysisFeatureList positions;
 
-               x = tmp;
+       if (!session) {
+               return;
        }
 
-       list<Playlist*> foo;
+       RegionSelection rs;
 
-       for (PlaylistMapping::iterator i = pmap.begin(); i != pmap.end(); ++i) {
-               foo.push_back (i->second);
-       }
+       get_regions_for_action (rs);
 
-       if (!foo.empty()) {
-               cut_buffer->set (foo);
-       }
-       
-       for (set<Playlist*>::iterator pl = freezelist.begin(); pl != freezelist.end(); ++pl) {
-               (*pl)->thaw ();
-               session->add_redo_no_execute ((*pl)->get_memento());
+       if (rs.empty()) {
+               return;
        }
-}
 
-void
-Editor::cut_copy_ranges (CutCopyOp op)
-{
-       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
-               (*i)->cut_copy_clear (*selection, op);
-       }
-}
+       session->begin_reversible_command (_("split regions"));
 
-void
-Editor::paste (float times)
-{
-       paste_internal (edit_cursor->current_frame, times);
-}
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ) {
 
-void
-Editor::mouse_paste ()
-{
-       int x, y;
-       double wx, wy;
+               RegionSelection::iterator tmp;
 
-       track_canvas.get_pointer (x, y);
-       track_canvas.window_to_world (x, y, wx, wy);
-       wx += horizontal_adjustment.get_value();
-       wy += vertical_adjustment.get_value();
+               tmp = i;
+               ++tmp;
+
+               boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> ((*i)->region());
+
+               if (ar && (ar->get_transients (positions) == 0)) {
+                       split_region_at_points ((*i)->region(), positions, true);
+                       positions.clear ();
+               }
+
+               i = tmp;
+       }
+
+       session->commit_reversible_command ();
 
-       GdkEvent event;
-       event.type = GDK_BUTTON_RELEASE;
-       event.button.x = wx;
-       event.button.y = wy;
-       
-       jack_nframes_t where = event_frame (&event, 0, 0);
-       snap_to (where);
-       paste_internal (where, 1);
 }
 
 void
-Editor::paste_internal (jack_nframes_t position, float times)
+Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList& positions, bool can_ferret)
 {
-       bool commit = false;
+       bool use_rhythmic_rodent = false;
 
-       if (cut_buffer->empty() || selection->tracks.empty()) {
+       boost::shared_ptr<Playlist> pl = r->playlist();
+
+       if (!pl) {
                return;
        }
 
-       if (position == max_frames) {
-               position = edit_cursor->current_frame;
+       if (positions.empty()) {
+               return;
        }
 
-       begin_reversible_command (_("paste"));
 
-       TrackSelection::iterator i;
-       size_t nth;
+       if (positions.size() > 20) {
+               Glib::ustring msgstr = string_compose (_("You are about to split\n%1\ninto %2 pieces.\nThis could take a long time."), r->name(), positions.size() + 1);
+               MessageDialog msg (msgstr,
+                                  false,
+                                  Gtk::MESSAGE_INFO,
+                                  Gtk::BUTTONS_OK_CANCEL);
 
-       for (nth = 0, i = selection->tracks.begin(); i != selection->tracks.end(); ++i, ++nth) {
-               
-               /* undo/redo is handled by individual tracks */
+               if (can_ferret) {
+                       msg.add_button (_("Call for the Ferret!"), RESPONSE_APPLY);
+                       msg.set_secondary_text (_("Press OK to continue with this split operation\nor ask the Ferret dialog to tune the analysis"));
+               } else {
+                       msg.set_secondary_text (_("Press OK to continue with this split operation"));
+               }
 
-               if ((*i)->paste (position, times, *cut_buffer, nth)) {
-                       commit = true;
+               msg.set_title (_("Excessive split?"));
+               msg.present ();
+
+               int response = msg.run();
+               msg.hide ();
+               switch (response) {
+               case RESPONSE_OK:
+                       break;
+               case RESPONSE_APPLY:
+                       use_rhythmic_rodent = true;
+                       break;
+               default:
+                       return;
                }
        }
 
-       if (commit) {
-               commit_reversible_command ();
+       if (use_rhythmic_rodent) {
+               show_rhythm_ferret ();
+               return;
        }
-}
 
-void
-Editor::paste_named_selection (float times)
-{
-       TrackSelection::iterator t;
+       AnalysisFeatureList::const_iterator x;
+
+       nframes64_t pos = r->position();
+
+       XMLNode& before (pl->get_state());
 
-       Glib::RefPtr<TreeSelection> selected = named_selection_display.get_selection();
+       x = positions.begin();
+
+       while (x != positions.end()) {
+               if ((*x) > pos) {
+                       break;
+               }
+               ++x;
+       }
 
-       if (selected->count_selected_rows() != 1 || selection->tracks.empty()) {
+       if (x == positions.end()) {
                return;
        }
 
-       TreeModel::iterator i = selected->get_selected();
-       NamedSelection* ns = (*i)[named_selection_columns.selection];
+       pl->freeze ();
+       pl->remove_region (r);
 
-       list<Playlist*>::iterator chunk;
-       list<Playlist*>::iterator tmp;
+       while (x != positions.end()) {
 
-       chunk = ns->playlists.begin();
-               
-       begin_reversible_command (_("paste chunk"));
+               /* file start = original start + how far we from the initial position ?
+                */
 
-       for (t = selection->tracks.begin(); t != selection->tracks.end(); ++t) {
-               
-               AudioTimeAxisView* atv;
-               Playlist* pl;
-               AudioPlaylist* apl;
+               nframes64_t file_start = r->start() + (pos - r->position());
 
-               if ((atv = dynamic_cast<AudioTimeAxisView*> (*t)) == 0) {
-                       continue;
-               }
+               /* length = next position - current position
+                */
 
-               if ((pl = atv->playlist()) == 0) {
-                       continue;
-               }
+               nframes64_t len = (*x) - pos;
 
-               if ((apl = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
-                       continue;
-               }
+               /* XXX we do we really want to allow even single-sample regions?
+                  shouldn't we have some kind of lower limit on region size?
+               */
 
-               tmp = chunk;
-               ++tmp;
+               if (len <= 0) {
+                       break;
+               }
 
-               session->add_undo (apl->get_memento());
-               apl->paste (**chunk, edit_cursor->current_frame, times);
-               session->add_redo_no_execute (apl->get_memento());
+               string new_name;
 
-               if (tmp != ns->playlists.end()) {
-                       chunk = tmp;
+               if (session->region_name (new_name, r->name())) {
+                       break;
                }
-       }
 
-       commit_reversible_command();
-}
+               /* do NOT announce new regions 1 by one, just wait till they are all done */
 
-void
-Editor::duplicate_some_regions (AudioRegionSelection& regions, float times)
-{
-       Playlist *playlist; 
-       AudioRegionSelection sel = regions; // clear (below) will clear the argument list
-               
-       begin_reversible_command (_("duplicate region"));
+               boost::shared_ptr<Region> nr = RegionFactory::create (r->sources(), file_start, len, new_name, 0, Region::DefaultFlags, false);
+               pl->add_region (nr, pos);
 
-       selection->clear_audio_regions ();
+               pos += len;
+               ++x;
 
-       for (AudioRegionSelection::iterator i = sel.begin(); i != sel.end(); ++i) {
+               if (*x > r->last_frame()) {
 
-               Region& r ((*i)->region);
+                       /* add final fragment */
 
-               TimeAxisView& tv = (*i)->get_time_axis_view();
-               AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*> (&tv);
-               sigc::connection c = atv->view->AudioRegionViewAdded.connect (mem_fun(*this, &Editor::collect_new_region_view));
-               
-               playlist = (*i)->region.playlist();
-               session->add_undo (playlist->get_memento());
-               playlist->duplicate (r, r.last_frame(), times);
-               session->add_redo_no_execute (playlist->get_memento());
+                       file_start = r->start() + (pos - r->position());
+                       len = r->last_frame() - pos;
 
-               c.disconnect ();
+                       nr = RegionFactory::create (r->sources(), file_start, len, new_name, 0, Region::DefaultFlags);
+                       pl->add_region (nr, pos);
 
-               if (latest_regionview) {
-                       selection->add (latest_regionview);
+                       break;
                }
        }
-               
 
-       commit_reversible_command ();
+       pl->thaw ();
+
+       XMLNode& after (pl->get_state());
+
+       session->add_command (new MementoCommand<Playlist>(*pl, &before, &after));
 }
 
 void
-Editor::duplicate_selection (float times)
+Editor::tab_to_transient (bool forward)
 {
-       if (selection->time.empty() || selection->tracks.empty()) {
+       AnalysisFeatureList positions;
+
+       if (!session) {
                return;
        }
 
-       Playlist *playlist; 
-       vector<AudioRegion*> new_regions;
-       vector<AudioRegion*>::iterator ri;
-               
-       create_region_from_selection (new_regions);
+       nframes64_t pos = session->audible_frame ();
 
-       if (new_regions.empty()) {
-               return;
-       }
-       
-       begin_reversible_command (_("duplicate selection"));
+       if (!selection->tracks.empty()) {
 
-       ri = new_regions.begin();
+               for (TrackSelection::iterator t = selection->tracks.begin(); t != selection->tracks.end(); ++t) {
 
-       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
-               if ((playlist = (*i)->playlist()) == 0) {
-                       continue;
+                       RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*t);
+
+                       if (rtv) {
+                               boost::shared_ptr<Diskstream> ds = rtv->get_diskstream();
+                               if (ds) {
+                                       boost::shared_ptr<Playlist> pl = rtv->get_diskstream()->playlist ();
+                                       if (pl) {
+                                               nframes64_t result = pl->find_next_transient (pos, forward ? 1 : -1);
+
+                                               if (result >= 0) {
+                                                       positions.push_back (result);
+                                               }
+                                       }
+                               }
+                       }
                }
-               session->add_undo (playlist->get_memento());
-               playlist->duplicate (**ri, selection->time[clicked_selection].end, times);
-               session->add_redo_no_execute (playlist->get_memento());
 
-               ++ri;
-               if (ri == new_regions.end()) {
-                       --ri;
+       } else {
+
+               RegionSelection rs;
+
+               get_regions_for_action (rs);
+
+               if (rs.empty()) {
+                       return;
+               }
+
+               for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
+                       (*r)->region()->get_transients (positions);
                }
        }
 
-       commit_reversible_command ();
-}
+       TransientDetector::cleanup_transients (positions, session->frame_rate(), 3.0);
 
-void
-Editor::center_playhead ()
-{
-       float page = canvas_width * frames_per_unit;
+       if (forward) {
+               AnalysisFeatureList::iterator x;
 
-       center_screen_internal (playhead_cursor->current_frame, page);
-}
+               for (x = positions.begin(); x != positions.end(); ++x) {
+                       if ((*x) > pos) {
+                               break;
+                       }
+               }
+
+               if (x != positions.end ()) {
+                       session->request_locate (*x);
+               }
 
+       } else {
+               AnalysisFeatureList::reverse_iterator x;
+
+               for (x = positions.rbegin(); x != positions.rend(); ++x) {
+                       if ((*x) < pos) {
+                               break;
+                       }
+               }
+
+               if (x != positions.rend ()) {
+                       session->request_locate (*x);
+               }
+       }
+}
 void
-Editor::center_edit_cursor ()
+Editor::playhead_forward_to_grid ()
 {
-       float page = canvas_width * frames_per_unit;
-
-       center_screen_internal (edit_cursor->current_frame, page);
+       if (!session) return;
+       nframes64_t pos = playhead_cursor->current_frame;
+       if (pos < max_frames - 1) {
+               pos += 2;
+               snap_to_internal (pos, 1, false);
+               session->request_locate (pos);
+       }
 }
 
+
 void
-Editor::clear_playlist (Playlist& playlist)
+Editor::playhead_backward_to_grid ()
 {
-       begin_reversible_command (_("clear playlist"));
-       session->add_undo (playlist.get_memento());
-       playlist.clear ();
-       session->add_redo_no_execute (playlist.get_memento());
-       commit_reversible_command ();
+       if (!session) return;
+       nframes64_t pos = playhead_cursor->current_frame;
+       if (pos > 2) {
+               pos -= 2;
+               snap_to_internal (pos, -1, false);
+               session->request_locate (pos);
+       }
 }
 
 void
-Editor::nudge_track (bool use_edit_cursor, bool forwards)
+Editor::set_track_height (uint32_t h)
 {
-       Playlist *playlist; 
-       jack_nframes_t distance;
-       jack_nframes_t next_distance;
-       jack_nframes_t start;
+       TrackSelection& ts (selection->tracks);
 
-       if (use_edit_cursor) {
-               start = edit_cursor->current_frame;
-       } else {
-               start = 0;
+       for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
+               (*x)->set_height (h);
        }
+}
 
-       if ((distance = get_nudge_distance (start, next_distance)) == 0) {
-               return;
-       }
-       
-       if (selection->tracks.empty()) {
+void
+Editor::toggle_tracks_active ()
+{
+       TrackSelection& ts (selection->tracks);
+       bool first = true;
+       bool target = false;
+
+       if (ts.empty()) {
                return;
        }
-       
-       begin_reversible_command (_("nudge track"));
-       
-       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
 
-               if ((playlist = (*i)->playlist()) == 0) {
-                       continue;
-               }               
-               
-               session->add_undo (playlist->get_memento());
-               playlist->nudge_after (start, distance, forwards);
-               session->add_redo_no_execute (playlist->get_memento());
-       }
-       
-       commit_reversible_command ();                   
-}
+       for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
+               RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(*x);
 
-void
-Editor::toggle_xfades_active ()
-{
-       if (session) {
-               session->set_crossfades_active (!session->get_crossfades_active());
+               if (rtv) {
+                       if (first) {
+                               target = !rtv->_route->active();
+                               first = false;
+                       }
+                       rtv->_route->set_active (target);
+               }
        }
 }
 
 void
-Editor::set_xfade_visibility (bool yn)
+Editor::remove_tracks ()
 {
-       
-}
+       TrackSelection& ts (selection->tracks);
 
-void
-Editor::toggle_xfade_visibility ()
-{
-       set_xfade_visibility (!xfade_visibility());
-}
+       if (ts.empty()) {
+               return;
+       }
 
-void
-Editor::remove_last_capture ()
-{
        vector<string> choices;
        string prompt;
-       
-       if (!session) {
-               return;
+       int ntracks = 0;
+       int nbusses = 0;
+       const char* trackstr;
+       const char* busstr;
+       vector<boost::shared_ptr<Route> > routes;
+
+       for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
+               RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*x);
+               if (rtv) {
+                       if (rtv->is_track()) {
+                               ntracks++;
+                       } else {
+                               nbusses++;
+                       }
+               }
+               routes.push_back (rtv->_route);
        }
 
-       if (Config->get_verify_remove_last_capture()) {
-               prompt  = _("Do you really want to destroy the last capture?"
-                           "\n(This is destructive and cannot be undone)");
+       if (ntracks + nbusses == 0) {
+               return;
+       }
 
-               choices.push_back (_("Yes, destroy it."));
-               choices.push_back (_("No, do nothing."));
-               
-               Gtkmm2ext::Choice prompter (prompt, choices);
-               prompter.chosen.connect (ptr_fun (Main::quit));
-               prompter.show_all ();
+       if (ntracks > 1) {
+               trackstr = _("tracks");
+       } else {
+               trackstr = _("track");
+       }
 
-               Main::run ();
-               
-               if (prompter.get_choice() == 0) {
-                       session->remove_last_capture ();
+       if (nbusses > 1) {
+               busstr = _("busses");
+       } else {
+               busstr = _("bus");
+       }
+
+       if (ntracks) {
+               if (nbusses) {
+                       prompt  = string_compose (_("Do you really want to remove %1 %2 and %3 %4?\n"
+                                                   "(You may also lose the playlists associated with the %2)\n\n"
+                                                   "This action cannot be undone!"),
+                                                 ntracks, trackstr, nbusses, busstr);
+               } else {
+                       prompt  = string_compose (_("Do you really want to remove %1 %2?\n"
+                                                   "(You may also lose the playlists associated with the %2)\n\n"
+                                                   "This action cannot be undone!"),
+                                                 ntracks, trackstr);
                }
+       } else if (nbusses) {
+               prompt  = string_compose (_("Do you really want to remove %1 %2?"),
+                                         nbusses, busstr);
+       }
 
+       choices.push_back (_("No, do nothing."));
+       if (ntracks + nbusses > 1) {
+               choices.push_back (_("Yes, remove them."));
        } else {
-               session->remove_last_capture();
+               choices.push_back (_("Yes, remove it."));
+       }
+
+       Choice prompter (prompt, choices);
+
+       if (prompter.run () != 1) {
+               return;
+       }
+
+       for (vector<boost::shared_ptr<Route> >::iterator x = routes.begin(); x != routes.end(); ++x) {
+               session->remove_route (*x);
        }
 }
 
 void
-Editor::normalize_region ()
+Editor::do_insert_time ()
 {
-       if (!session) {
+       if (selection->tracks.empty()) {
                return;
        }
 
-       if (selection->audio_regions.empty()) {
+       ArdourDialog d (*this, _("Insert Time"));
+
+       nframes64_t const pos = get_preferred_edit_position ();
+
+       d.get_vbox()->set_border_width (12);
+       d.get_vbox()->set_spacing (4);
+
+       Table table (2, 2);
+       table.set_spacings (4);
+
+       Label time_label (_("Time to insert:"));
+       time_label.set_alignment (1, 0.5);
+       table.attach (time_label, 0, 1, 0, 1, FILL | EXPAND);
+       AudioClock clock ("insertTimeClock", true, X_("InsertTimeClock"), true, true, true);
+       clock.set (0);
+       clock.set_session (session);
+       clock.set_bbt_reference (pos);
+       table.attach (clock, 1, 2, 0, 1);
+
+       Label intersected_label (_("Intersected regions should:"));
+       intersected_label.set_alignment (1, 0.5);
+       table.attach (intersected_label, 0, 1, 1, 2, FILL | EXPAND);
+       ComboBoxText intersected_combo;
+       intersected_combo.append_text (_("stay in position"));
+       intersected_combo.append_text (_("move"));
+       intersected_combo.append_text (_("be split"));
+       intersected_combo.set_active (0);
+       table.attach (intersected_combo, 1, 2, 1, 2);
+
+       d.get_vbox()->pack_start (table);
+
+       CheckButton move_glued (_("Move glued regions"));
+       d.get_vbox()->pack_start (move_glued);
+       CheckButton move_markers (_("Move markers"));
+       d.get_vbox()->pack_start (move_markers);
+       CheckButton move_tempos (_("Move tempo and meter changes"));
+       d.get_vbox()->pack_start (move_tempos);
+
+       d.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
+       d.add_button (_("Insert time"), Gtk::RESPONSE_OK);
+       d.show_all ();
+
+       int response = d.run ();
+
+       if (response != RESPONSE_OK) {
                return;
        }
 
-       begin_reversible_command (_("normalize"));
+       nframes64_t distance = clock.current_duration (pos);
 
-       track_canvas.get_window()->set_cursor (*wait_cursor);
-       gdk_flush ();
+       if (distance == 0) {
+               return;
+       }
+
+       InsertTimeOption opt;
 
-       for (AudioRegionSelection::iterator r = selection->audio_regions.begin(); r != selection->audio_regions.end(); ++r) {
-               session->add_undo ((*r)->region.get_memento());
-               (*r)->region.normalize_to (0.0f);
-               session->add_redo_no_execute ((*r)->region.get_memento());
+       switch (intersected_combo.get_active_row_number ()) {
+       case 0:
+               opt = LeaveIntersected;
+               break;
+       case 1:
+               opt = MoveIntersected;
+               break;
+       case 2:
+               opt = SplitIntersected;
+               break;
        }
 
-       commit_reversible_command ();
-       track_canvas.get_window()->set_cursor (*current_canvas_cursor);
+       insert_time (pos, distance, opt, move_glued.get_active(), move_markers.get_active(), move_tempos.get_active());
 }
 
-
 void
-Editor::denormalize_region ()
+Editor::insert_time (nframes64_t pos, nframes64_t frames, InsertTimeOption opt,
+                    bool ignore_music_glue, bool markers_too, bool tempo_too)
 {
-       if (!session) {
+       bool commit = false;
+
+       if (Config->get_edit_mode() == Lock) {
                return;
        }
 
-       if (selection->audio_regions.empty()) {
-               return;
+       begin_reversible_command (_("insert time"));
+
+       for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
+               /* regions */
+               boost::shared_ptr<Playlist> pl = (*x)->playlist();
+
+               if (pl) {
+
+                       XMLNode &before = pl->get_state();
+
+                       if (opt == SplitIntersected) {
+                               pl->split (pos);
+                       }
+
+                       pl->shift (pos, frames, (opt == MoveIntersected), ignore_music_glue);
+
+                       XMLNode &after = pl->get_state();
+
+                       session->add_command (new MementoCommand<Playlist> (*pl, &before, &after));
+                       commit = true;
+               }
+
+               /* automation */
+               RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (*x);
+               if (rtav) {
+                       rtav->route ()->shift (pos, frames);
+                       commit = true;
+               }
        }
 
-       begin_reversible_command ("denormalize");
+       /* markers */
+       if (markers_too) {
+               bool moved = false;
+               XMLNode& before (session->locations()->get_state());
+               Locations::LocationList copy (session->locations()->list());
 
-       for (AudioRegionSelection::iterator r = selection->audio_regions.begin(); r != selection->audio_regions.end(); ++r) {
-               session->add_undo ((*r)->region.get_memento());
-               (*r)->region.set_scale_amplitude (1.0f);
-               session->add_redo_no_execute ((*r)->region.get_memento());
+               for (Locations::LocationList::iterator i = copy.begin(); i != copy.end(); ++i) {
+
+                       Locations::LocationList::const_iterator tmp;
+
+                       if ((*i)->start() >= pos) {
+                               (*i)->set_start ((*i)->start() + frames);
+                               if (!(*i)->is_mark()) {
+                                       (*i)->set_end ((*i)->end() + frames);
+                               }
+                               moved = true;
+                       }
+               }
+
+               if (moved) {
+                       XMLNode& after (session->locations()->get_state());
+                       session->add_command (new MementoCommand<Locations>(*session->locations(), &before, &after));
+               }
        }
 
-       commit_reversible_command ();
+       if (tempo_too) {
+               session->tempo_map().insert_time (pos, frames);
+       }
+
+       if (commit) {
+               commit_reversible_command ();
+       }
 }
 
+void
+Editor::fit_selected_tracks ()
+{
+       fit_tracks (selection->tracks);
+}
 
 void
-Editor::reverse_region ()
+Editor::fit_tracks (TrackSelection & tracks)
 {
-       if (!session) {
+       if (tracks.empty()) {
                return;
        }
 
-       Reverse rev (*session);
-       apply_filter (rev, _("reverse regions"));
-}
+       uint32_t child_heights = 0;
 
-void
-Editor::apply_filter (AudioFilter& filter, string command)
-{
-       if (selection->audio_regions.empty()) {
+       for (TrackSelection::iterator t = tracks.begin(); t != tracks.end(); ++t) {
+
+               if (!(*t)->marked_for_display()) {
+                       continue;
+               }
+
+               child_heights += (*t)->effective_height() - (*t)->current_height();
+       }
+
+       uint32_t h = (uint32_t) floor ((_canvas_height - child_heights - canvas_timebars_vsize) / tracks.size());
+       double first_y_pos = DBL_MAX;
+
+       if (h < TimeAxisView::hSmall) {
+               MessageDialog msg (*this, _("There are too many tracks to fit in the current window"));
+               /* too small to be displayed */
                return;
        }
 
-       begin_reversible_command (command);
+       undo_visual_stack.push_back (current_visual_state());
 
-       track_canvas.get_window()->set_cursor (*wait_cursor);
-       gdk_flush ();
+       /* operate on all tracks, hide unselected ones that are in the middle of selected ones */
 
-       for (AudioRegionSelection::iterator r = selection->audio_regions.begin(); r != selection->audio_regions.end(); ) {
+       bool prev_was_selected = false;
+       bool is_selected = tracks.contains (track_views.front());
+       bool next_is_selected;
 
-               AudioRegion& region ((*r)->region);
-               Playlist* playlist = region.playlist();
+       for (TrackViewList::iterator t = track_views.begin(); t != track_views.end(); ++t) {
 
-               AudioRegionSelection::iterator tmp;
-               
-               tmp = r;
-               ++tmp;
+               TrackViewList::iterator next;
 
-               if (region.apply (filter) == 0) {
+               next = t;
+               ++next;
 
-                       session->add_undo (playlist->get_memento());
-                       playlist->replace_region (region, *(filter.results.front()), region.position());
-                       session->add_redo_no_execute (playlist->get_memento());
+               if (next != track_views.end()) {
+                       next_is_selected = tracks.contains (*next);
                } else {
-                       goto out;
+                       next_is_selected = false;
                }
 
-               r = tmp;
+               if (is_selected) {
+                       (*t)->set_height (h);
+                       first_y_pos = std::min ((*t)->y_position (), first_y_pos);
+               } else {
+                       if (prev_was_selected && next_is_selected) {
+                               hide_track_in_display (**t);
+                       }
+               }
+
+               prev_was_selected = is_selected;
+               is_selected = next_is_selected;
        }
 
-       commit_reversible_command ();
-       selection->audio_regions.clear ();
+       /*
+          set the controls_layout height now, because waiting for its size
+          request signal handler will cause the vertical adjustment setting to fail
+       */
 
-  out:
-       track_canvas.get_window()->set_cursor (*current_canvas_cursor);
+       controls_layout.property_height () = full_canvas_height - canvas_timebars_vsize;
+       vertical_adjustment.set_value (first_y_pos);
+
+       redo_visual_stack.push_back (current_visual_state());
 }
 
 void
-Editor::region_selection_op (void (Region::*pmf)(void))
+Editor::save_visual_state (uint32_t n)
 {
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-               ((*i)->region.*pmf)();
+       while (visual_states.size() <= n) {
+               visual_states.push_back (0);
        }
-}
 
+       delete visual_states[n];
 
-void
-Editor::region_selection_op (void (Region::*pmf)(void*), void *arg)
-{
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-               ((*i)->region.*pmf)(arg);
-       }
+       visual_states[n] = current_visual_state (true);
+       gdk_beep ();
 }
 
 void
-Editor::region_selection_op (void (Region::*pmf)(bool), bool yn)
+Editor::goto_visual_state (uint32_t n)
 {
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-               ((*i)->region.*pmf)(yn);
+       if (visual_states.size() <= n) {
+               return;
        }
-}
 
-void
-Editor::external_edit_region ()
-{
-       if (!clicked_regionview) {
+       if (visual_states[n] == 0) {
                return;
        }
 
-       /* more to come */
+       use_visual_state (*visual_states[n]);
 }
 
 void
-Editor::brush (jack_nframes_t pos)
+Editor::start_visual_state_op (uint32_t n)
 {
-       AudioRegionSelection sel;
-       snap_to (pos);
-
-       if (selection->audio_regions.empty()) {
-               /* XXX get selection from region list */
-       } else { 
-               sel = selection->audio_regions;
-       }
-
-       if (sel.empty()) {
-               return;
-       }
-
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-               mouse_brush_insert_region ((*i), pos);
+       if (visual_state_op_connection.empty()) {
+               visual_state_op_connection = Glib::signal_timeout().connect (bind (mem_fun (*this, &Editor::end_visual_state_op), n), 1000);
        }
 }
 
 void
-Editor::toggle_gain_envelope_visibility ()
+Editor::cancel_visual_state_op (uint32_t n)
 {
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-               (*i)->set_envelope_visible (!(*i)->envelope_visible());
+       if (!visual_state_op_connection.empty()) {
+               visual_state_op_connection.disconnect();
+               goto_visual_state (n);
+       }  else {
+               //we land here if called from the menu OR if end_visual_state_op has been called
+               //so check if we are already in visual state n
+               // XXX not yet checking it at all, but redoing does not hurt
+               goto_visual_state (n);
        }
 }
 
-void
-Editor::toggle_gain_envelope_active ()
+bool
+Editor::end_visual_state_op (uint32_t n)
 {
-       for (AudioRegionSelection::iterator i = selection->audio_regions.begin(); i != selection->audio_regions.end(); ++i) {
-               AudioRegion* ar = dynamic_cast<AudioRegion*>(&(*i)->region);
-               if (ar) {
-                       ar->set_envelope_active (true);
-               }
-       }
+       visual_state_op_connection.disconnect();
+       save_visual_state (n);
+
+       PopUp* pup = new PopUp (WIN_POS_MOUSE, 1000, true);
+       char buf[32];
+       snprintf (buf, sizeof (buf), _("Saved view %u"), n+1);
+       pup->set_text (buf);
+       pup->touch();
+
+       return false; // do not call again
 }
+