Fairly major change to the way in which crossfades are handled;
[ardour.git] / gtk2_ardour / editor_ops.cc
index e29bcb130c79cce7b69878c69712187e4d4ddc86..fdca00918578ece9c994f291d2d5045a8034969f 100644 (file)
@@ -31,6 +31,7 @@
 #include "pbd/basename.h"
 #include "pbd/pthread_utils.h"
 #include "pbd/memento_command.h"
+#include "pbd/unwind.h"
 #include "pbd/whitespace.h"
 #include "pbd/stateful_diff_command.h"
 
 #include <gtkmm2ext/choice.h>
 #include <gtkmm2ext/popup.h>
 
-#include "ardour/audioengine.h"
-#include "ardour/session.h"
-#include "ardour/audioplaylist.h"
+#include "ardour/audio_track.h"
 #include "ardour/audioregion.h"
-#include "ardour/audio_diskstream.h"
-#include "ardour/utils.h"
+#include "ardour/dB.h"
 #include "ardour/location.h"
-#include "ardour/audio_track.h"
-#include "ardour/audioplaylist.h"
-#include "ardour/region_factory.h"
+#include "ardour/midi_region.h"
+#include "ardour/operations.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/region_factory.h"
+#include "ardour/reverse.h"
 #include "ardour/route_group.h"
+#include "ardour/session.h"
+#include "ardour/session_playlists.h"
+#include "ardour/strip_silence.h"
+#include "ardour/transient_detector.h"
+#include "ardour/utils.h"
 
 #include "ardour_ui.h"
+#include "debug.h"
 #include "editor.h"
 #include "time_axis_view.h"
 #include "route_time_axis.h"
@@ -84,6 +85,8 @@
 #include "normalize_dialog.h"
 #include "editor_cursors.h"
 #include "mouse_cursors.h"
+#include "patch_change_dialog.h"
+#include "transpose_dialog.h"
 
 #include "i18n.h"
 
@@ -102,6 +105,10 @@ using Gtkmm2ext::Keyboard;
 void
 Editor::undo (uint32_t n)
 {
+       if (_drags->active ()) {
+               _drags->abort ();
+       }
+       
        if (_session) {
                _session->undo (n);
        }
@@ -110,6 +117,10 @@ Editor::undo (uint32_t n)
 void
 Editor::redo (uint32_t n)
 {
+       if (_drags->active ()) {
+               _drags->abort ();
+       }
+       
        if (_session) {
                _session->redo (n);
        }
@@ -118,6 +129,8 @@ Editor::redo (uint32_t n)
 void
 Editor::split_regions_at (framepos_t where, RegionSelection& regions)
 {
+       bool frozen = false;
+
        list <boost::shared_ptr<Playlist > > used_playlists;
 
        if (regions.empty()) {
@@ -140,6 +153,9 @@ Editor::split_regions_at (framepos_t where, RegionSelection& regions)
                }
        } else {
                snap_to (where);
+
+               frozen = true;
+               EditorFreeze(); /* Emit Signal */
        }
 
        for (RegionSelection::iterator a = regions.begin(); a != regions.end(); ) {
@@ -189,94 +205,56 @@ Editor::split_regions_at (framepos_t where, RegionSelection& regions)
        }
 
        commit_reversible_command ();
-}
-
-boost::shared_ptr<Region>
-Editor::select_region_for_operation (int /*dir*/, TimeAxisView **tv)
-{
-       RegionView* rv;
-       boost::shared_ptr<Region> region;
-       framepos_t start = 0;
-
-       if (selection->time.start () == selection->time.end_frame ()) {
-
-               /* no current selection-> is there a selected regionview? */
-
-               if (selection->regions.empty()) {
-                       return region;
-               }
 
+       if (frozen){
+               EditorThaw(); /* Emit Signal */
        }
-
-       if (!selection->regions.empty()) {
-
-               rv = *(selection->regions.begin());
-               (*tv) = &rv->get_time_axis_view();
-               region = rv->region();
-
-       } else if (!selection->tracks.empty()) {
-
-               (*tv) = selection->tracks.front();
-
-               RouteTimeAxisView* rtv;
-
-               if ((rtv = dynamic_cast<RouteTimeAxisView*> (*tv)) != 0) {
-                       boost::shared_ptr<Playlist> pl;
-
-                       if ((pl = rtv->playlist()) == 0) {
-                               return region;
-                       }
-
-                       region = pl->top_region_at (start);
-               }
-       }
-
-       return region;
 }
 
+/** Move one extreme of the current range selection.  If more than one range is selected,
+ *  the start of the earliest range or the end of the latest range is moved.
+ *
+ *  @param move_end true to move the end of the current range selection, false to move
+ *  the start.
+ *  @param next true to move the extreme to the next region boundary, false to move to
+ *  the previous.
+ */
 void
-Editor::extend_selection_to_end_of_region (bool next)
+Editor::move_range_selection_start_or_end_to_region_boundary (bool move_end, bool next)
 {
-       TimeAxisView *tv;
-       boost::shared_ptr<Region> region;
-       framepos_t start;
-
-       if ((region = select_region_for_operation (next ? 1 : 0, &tv)) == 0) {
+       if (selection->time.start() == selection->time.end_frame()) {
                return;
        }
 
-       if (region && selection->time.start () == selection->time.end_frame ()) {
-               start = region->position();
-       } else {
-               start = selection->time.start ();
-       }
+       framepos_t start = selection->time.start ();
+       framepos_t end = selection->time.end_frame ();
 
-       begin_reversible_command (_("extend selection"));
-       selection->set (start, region->position() + region->length());
-       commit_reversible_command ();
-}
+       /* the position of the thing we may move */
+       framepos_t pos = move_end ? end : start;
+       int dir = next ? 1 : -1;
 
-void
-Editor::extend_selection_to_start_of_region (bool previous)
-{
-       TimeAxisView *tv;
-       boost::shared_ptr<Region> region;
-       framepos_t end;
+       /* so we don't find the current region again */
+       if (dir > 0 || pos > 0) {
+               pos += dir;
+       }
 
-       if ((region = select_region_for_operation (previous ? -1 : 0, &tv)) == 0) {
+       framepos_t const target = get_region_boundary (pos, dir, true, false);
+       if (target < 0) {
                return;
        }
 
-       if (region && selection->time.start () == selection->time.end_frame ()) {
-               end = region->position() + region->length();
+       if (move_end) {
+               end = target;
        } else {
-               end = selection->time.end_frame ();
+               start = target;
        }
 
-       /* Try to leave the selection with the same route if possible */
+       if (end < start) {
+               return;
+       }
 
-       begin_reversible_command (_("extend selection"));
-       selection->set (region->position(), end);
+       begin_reversible_command (_("alter selection"));
+       selection->set_preserving_all_ranges (start, end);
        commit_reversible_command ();
 }
 
@@ -329,7 +307,7 @@ Editor::nudge_forward (bool next, bool force_playhead)
                        }
 
                        r->clear_changes ();
-                       r->set_position (r->position() + distance, this);
+                       r->set_position (r->position() + distance);
                        _session->add_command (new StatefulDiffCommand (r));
                }
 
@@ -408,13 +386,13 @@ Editor::nudge_backward (bool next, bool force_playhead)
                        if (next) {
                                distance = next_distance;
                        }
-                        
+
                        r->clear_changes ();
 
                        if (r->position() > distance) {
-                               r->set_position (r->position() - distance, this);
+                               r->set_position (r->position() - distance);
                        } else {
-                               r->set_position (0, this);
+                               r->set_position (0);
                        }
                        _session->add_command (new StatefulDiffCommand (r));
                }
@@ -482,23 +460,23 @@ void
 Editor::nudge_forward_capture_offset ()
 {
        RegionSelection rs = get_regions_from_selection_and_entered ();
-       
+
        if (!_session || rs.empty()) {
                return;
        }
 
        begin_reversible_command (_("nudge forward"));
-       
+
        framepos_t const distance = _session->worst_output_latency();
-       
+
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
                boost::shared_ptr<Region> r ((*i)->region());
-               
+
                r->clear_changes ();
-               r->set_position (r->position() + distance, this);
+               r->set_position (r->position() + distance);
                _session->add_command(new StatefulDiffCommand (r));
        }
-       
+
        commit_reversible_command ();
 }
 
@@ -511,23 +489,23 @@ Editor::nudge_backward_capture_offset ()
                return;
        }
 
-       begin_reversible_command (_("nudge forward"));
-       
+       begin_reversible_command (_("nudge backward"));
+
        framepos_t const distance = _session->worst_output_latency();
 
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
                boost::shared_ptr<Region> r ((*i)->region());
-               
+
                r->clear_changes ();
-               
+
                if (r->position() > distance) {
-                       r->set_position (r->position() - distance, this);
+                       r->set_position (r->position() - distance);
                } else {
-                       r->set_position (0, this);
+                       r->set_position (0);
                }
                _session->add_command(new StatefulDiffCommand (r));
        }
-       
+
        commit_reversible_command ();
 }
 
@@ -583,11 +561,11 @@ Editor::build_region_boundary_cache ()
 
        TimeAxisView *ontrack = 0;
        TrackViewList tlist;
-
+       
        if (!selection->tracks.empty()) {
-               tlist = selection->tracks;
+               tlist = selection->tracks.filter_to_unique_playlists ();
        } else {
-               tlist = track_views;
+               tlist = track_views.filter_to_unique_playlists ();
        }
 
        while (pos < _session->current_end_frame() && !at_end) {
@@ -734,7 +712,7 @@ Editor::find_next_region_boundary (framepos_t pos, int32_t dir, const TrackViewL
        for (TrackViewList::const_iterator i = tracks.begin(); i != tracks.end(); ++i) {
                framepos_t contender;
                framecnt_t d;
-                
+
                RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
 
                if (!rtv) {
@@ -861,7 +839,7 @@ Editor::cursor_to_region_point (EditorCursor* cursor, RegionPoint point, int32_t
                return;
        }
 
-       switch (point){
+       switch (point) {
        case Start:
                pos = r->first_frame ();
                break;
@@ -1052,7 +1030,7 @@ Editor::selected_marker_to_region_point (RegionPoint point, int32_t dir)
                return;
        }
 
-       switch (point){
+       switch (point) {
        case Start:
                pos = r->first_frame ();
                break;
@@ -1195,68 +1173,6 @@ Editor::scroll_playhead (bool forward)
        _session->request_locate (pos);
 }
 
-void
-Editor::playhead_backward ()
-{
-       framepos_t pos;
-       framepos_t cnt;
-       float prefix;
-       bool was_floating;
-
-       if (get_prefix (prefix, was_floating)) {
-               cnt = 1;
-       } else {
-               if (was_floating) {
-                       cnt = (framepos_t) floor (prefix * _session->frame_rate ());
-               } else {
-                       cnt = (framepos_t) prefix;
-               }
-       }
-
-       pos = playhead_cursor->current_frame;
-
-       if ((framepos_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::playhead_forward ()
-{
-       framepos_t pos;
-       framepos_t cnt;
-       bool was_floating;
-       float prefix;
-
-       if (get_prefix (prefix, was_floating)) {
-               cnt = 1;
-       } else {
-               if (was_floating) {
-                       cnt = (framepos_t) floor (prefix * _session->frame_rate ());
-               } else {
-                       cnt = (framepos_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::cursor_align (bool playhead_to_edit)
 {
@@ -1290,98 +1206,13 @@ Editor::cursor_align (bool playhead_to_edit)
        }
 }
 
-void
-Editor::edit_cursor_backward ()
-{
-       framepos_t pos;
-       framepos_t cnt;
-       float prefix;
-       bool was_floating;
-
-       if (get_prefix (prefix, was_floating)) {
-               cnt = 1;
-       } else {
-               if (was_floating) {
-                       cnt = (framepos_t) floor (prefix * _session->frame_rate ());
-               } else {
-                       cnt = (framepos_t) prefix;
-               }
-       }
-
-       if ((pos = get_preferred_edit_position()) < 0) {
-               return;
-       }
-
-       if (pos < cnt) {
-               pos = 0;
-       } else {
-               pos -= cnt;
-       }
-
-       // EDIT CURSOR edit_cursor->set_position (pos);
-}
-
-void
-Editor::edit_cursor_forward ()
-{
-       //framepos_t pos;
-       framepos_t cnt;
-       bool was_floating;
-       float prefix;
-
-       if (get_prefix (prefix, was_floating)) {
-               cnt = 1;
-       } else {
-               if (was_floating) {
-                       cnt = (framepos_t) floor (prefix * _session->frame_rate ());
-               } else {
-                       cnt = (framepos_t) floor (prefix);
-               }
-       }
-
-       // pos = edit_cursor->current_frame;
-       // EDIT CURSOR edit_cursor->set_position (pos+cnt);
-}
-
-void
-Editor::goto_frame ()
-{
-       float prefix;
-       bool was_floating;
-       framepos_t frame;
-
-       if (get_prefix (prefix, was_floating)) {
-               return;
-       }
-
-       if (was_floating) {
-               frame = (framepos_t) floor (prefix * _session->frame_rate());
-       } else {
-               frame = (framepos_t) floor (prefix);
-       }
-
-       _session->request_locate (frame);
-}
-
 void
 Editor::scroll_backward (float pages)
 {
-       framepos_t frame;
-       framepos_t one_page = (framepos_t) rint (_canvas_width * frames_per_unit);
-       bool was_floating;
-       float prefix;
-       framepos_t cnt;
-
-       if (get_prefix (prefix, was_floating)) {
-               cnt = (framepos_t) floor (pages * one_page);
-       } else {
-               if (was_floating) {
-                       cnt = (framepos_t) floor (prefix * _session->frame_rate());
-               } else {
-                       cnt = (framepos_t) floor (prefix * one_page);
-               }
-       }
+       framepos_t const one_page = (framepos_t) rint (_canvas_width * frames_per_unit);
+       framepos_t const cnt = (framepos_t) floor (pages * one_page);
 
+       framepos_t frame;
        if (leftmost_frame < cnt) {
                frame = 0;
        } else {
@@ -1394,22 +1225,10 @@ Editor::scroll_backward (float pages)
 void
 Editor::scroll_forward (float pages)
 {
-       framepos_t frame;
-       framepos_t one_page = (framepos_t) rint (_canvas_width * frames_per_unit);
-       bool was_floating;
-       float prefix;
-       framepos_t cnt;
-
-       if (get_prefix (prefix, was_floating)) {
-               cnt = (framepos_t) floor (pages * one_page);
-       } else {
-               if (was_floating) {
-                       cnt = (framepos_t) floor (prefix * _session->frame_rate());
-               } else {
-                       cnt = (framepos_t) floor (prefix * one_page);
-               }
-       }
+       framepos_t const one_page = (framepos_t) rint (_canvas_width * frames_per_unit);
+       framepos_t const cnt = (framepos_t) floor (pages * one_page);
 
+       framepos_t frame;
        if (max_framepos - cnt < leftmost_frame) {
                frame = max_framepos - cnt;
        } else {
@@ -1422,38 +1241,18 @@ Editor::scroll_forward (float pages)
 void
 Editor::scroll_tracks_down ()
 {
-       float prefix;
-       bool was_floating;
-       int cnt;
-
-       if (get_prefix (prefix, was_floating)) {
-               cnt = 1;
-       } else {
-               cnt = (int) floor (prefix);
-       }
-
-       double vert_value = vertical_adjustment.get_value() + (cnt *
-               vertical_adjustment.get_page_size());
+       double vert_value = vertical_adjustment.get_value() + 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);
 }
 
 void
 Editor::scroll_tracks_up ()
 {
-       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()));
+       vertical_adjustment.set_value (vertical_adjustment.get_value() - vertical_adjustment.get_page_size());
 }
 
 void
@@ -1464,7 +1263,7 @@ Editor::scroll_tracks_down_line ()
        if (vert_value > vertical_adjustment.get_upper() - _canvas_height) {
                vert_value = vertical_adjustment.get_upper() - _canvas_height;
        }
-       
+
        vertical_adjustment.set_value (vert_value);
 }
 
@@ -1479,11 +1278,17 @@ Editor::scroll_tracks_up_line ()
 void
 Editor::tav_zoom_step (bool coarser)
 {
-       ENSURE_GUI_THREAD (*this, &Editor::temporal_zoom_step, coarser)
-
        _routes->suspend_redisplay ();
 
-       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+       TrackViewList* ts;
+
+       if (selection->tracks.empty()) {
+               ts = &track_views;
+       } else {
+               ts = &selection->tracks;
+       }
+       
+       for (TrackViewList::iterator i = ts->begin(); i != ts->end(); ++i) {
                TimeAxisView *tv = (static_cast<TimeAxisView*>(*i));
                        tv->step_height (coarser);
        }
@@ -1491,6 +1296,38 @@ Editor::tav_zoom_step (bool coarser)
        _routes->resume_redisplay ();
 }
 
+void
+Editor::tav_zoom_smooth (bool coarser, bool force_all)
+{
+       _routes->suspend_redisplay ();
+
+       TrackViewList* ts;
+
+       if (selection->tracks.empty() || force_all) {
+               ts = &track_views;
+       } else {
+               ts = &selection->tracks;
+       }
+       
+       for (TrackViewList::iterator i = ts->begin(); i != ts->end(); ++i) {
+               TimeAxisView *tv = (static_cast<TimeAxisView*>(*i));
+               uint32_t h = tv->current_height ();
+
+               if (coarser) {
+                       if (h > 5) {
+                               h -= 5; // pixels
+                               if (h >= TimeAxisView::preset_height (HeightSmall)) {
+                                       tv->set_height (h);
+                               }
+                       }
+               } else {
+                       tv->set_height (h + 5);
+               }
+       }
+
+       _routes->resume_redisplay ();
+}
+
 void
 Editor::temporal_zoom_step (bool coarser)
 {
@@ -1687,16 +1524,15 @@ Editor::temporal_zoom_region (bool both_axes)
                end = max_framepos;
        }
 
-       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;
-       }
+       /* if we're zooming on both axes we need to save track heights etc.
+        */
 
-       temporal_zoom_by_frame (start, end, "zoom to region");
+       undo_visual_stack.push_back (current_visual_state (both_axes));
 
+       PBD::Unwinder<bool> nsv (no_save_visual, true);
+
+       temporal_zoom_by_frame (start, end);
+       
        if (both_axes) {
                uint32_t per_track_height = (uint32_t) floor ((_canvas_height - canvas_timebars_vsize - 10.0) / tracks.size());
 
@@ -1712,17 +1548,16 @@ Editor::temporal_zoom_region (bool both_axes)
 
                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);
+                               hide_track_in_display (*i);
                        }
                }
 
                _routes->resume_redisplay ();
 
                vertical_adjustment.set_value (0.0);
-               no_save_visual = false;
        }
 
-       redo_visual_stack.push_back (current_visual_state());
+       redo_visual_stack.push_back (current_visual_state (both_axes));
 }
 
 void
@@ -1743,7 +1578,7 @@ Editor::temporal_zoom_selection ()
        framepos_t start = selection->time[clicked_selection].start;
        framepos_t end = selection->time[clicked_selection].end;
 
-       temporal_zoom_by_frame (start, end, "zoom to selection");
+       temporal_zoom_by_frame (start, end);
 }
 
 void
@@ -1758,12 +1593,12 @@ Editor::temporal_zoom_session ()
                        s = 0;
                }
                framecnt_t const e = _session->current_end_frame() + l * 0.01;
-               temporal_zoom_by_frame (framecnt_t (s), e, "zoom to _session");
+               temporal_zoom_by_frame (framecnt_t (s), e);
        }
 }
 
 void
-Editor::temporal_zoom_by_frame (framepos_t start, framepos_t end, const string & /*op*/)
+Editor::temporal_zoom_by_frame (framepos_t start, framepos_t end)
 {
        if (!_session) return;
 
@@ -1783,6 +1618,10 @@ Editor::temporal_zoom_by_frame (framepos_t start, framepos_t end, const string &
                new_leftmost = 0;
        }
 
+       if (new_leftmost < 0) {
+               new_leftmost = 0;
+       }
+
        reposition_and_zoom (new_leftmost, new_fpu);
 }
 
@@ -1814,10 +1653,10 @@ Editor::temporal_zoom_to_frame (bool coarser, framepos_t frame)
        if (new_leftmost > frame) {
                new_leftmost = 0;
        }
-//     begin_reversible_command (_("zoom to frame"));
-//     _session->add_undo (sigc::bind (sigc::mem_fun(*this, &Editor::reposition_and_zoom), leftmost_frame, frames_per_unit));
-//     _session->add_redo (sigc::bind (sigc::mem_fun(*this, &Editor::reposition_and_zoom), new_leftmost, new_fpu));
-//     commit_reversible_command ();
+
+       if (new_leftmost < 0) {
+               new_leftmost = 0;
+       }
 
        reposition_and_zoom (new_leftmost, new_fpu);
 }
@@ -1917,7 +1756,7 @@ void
 Editor::add_locations_from_region ()
 {
        RegionSelection rs = get_regions_from_selection_and_entered ();
-       
+
        if (rs.empty()) {
                return;
        }
@@ -1944,7 +1783,7 @@ void
 Editor::add_location_from_region ()
 {
        RegionSelection rs = get_regions_from_selection_and_entered ();
-       
+
        if (rs.empty()) {
                return;
        }
@@ -1975,79 +1814,26 @@ Editor::add_location_from_region ()
        _session->commit_reversible_command ();
 }
 
+/* MARKS */
+
 void
-Editor::amplitude_zoom_step (bool in)
+Editor::jump_forward_to_mark ()
 {
-       gdouble zoom = 1.0;
+       if (!_session) {
+               return;
+       }
 
-       if (in) {
-               zoom *= 2.0;
+       Location *location = _session->locations()->first_location_after (playhead_cursor->current_frame);
+
+       if (location) {
+               _session->request_locate (location->start(), _session->transport_rolling());
        } else {
-               if (zoom > 2.0) {
-                       zoom /= 2.0;
-               } else {
-                       zoom = 1.0;
-               }
+               _session->request_locate (_session->current_end_frame());
        }
-
-#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 ()
+Editor::jump_backward_to_mark ()
 {
        if (!_session) {
                return;
@@ -2065,25 +1851,15 @@ Editor::jump_backward_to_mark ()
 void
 Editor::set_mark ()
 {
-       framepos_t pos;
-       float prefix;
-       bool was_floating;
-       string markername;
+       framepos_t const pos = _session->audible_frame ();
 
-       if (get_prefix (prefix, was_floating)) {
-               pos = _session->audible_frame ();
-       } else {
-               if (was_floating) {
-                       pos = (framepos_t) floor (prefix * _session->frame_rate ());
-               } else {
-                       pos = (framepos_t) floor (prefix);
-               }
-       }
+       string markername;
+       _session->locations()->next_available_name (markername, "mark");
 
-       _session->locations()->next_available_name(markername,"mark");
-       if (!choose_new_marker_name(markername)) {
+       if (!choose_new_marker_name (markername)) {
                return;
        }
+
        _session->locations()->add (new Location (*_session, pos, 0, markername, Location::IsMark), true);
 }
 
@@ -2109,11 +1885,13 @@ Editor::clear_ranges ()
 
                Location * looploc = _session->locations()->auto_loop_location();
                Location * punchloc = _session->locations()->auto_punch_location();
+               Location * sessionloc = _session->locations()->session_range_location();
 
                _session->locations()->clear_ranges ();
                // re-add these
                if (looploc) _session->locations()->add (looploc);
                if (punchloc) _session->locations()->add (punchloc);
+               if (sessionloc) _session->locations()->add (sessionloc);
 
                XMLNode &after = _session->locations()->get_state();
                _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
@@ -2207,7 +1985,6 @@ Editor::insert_route_list_drag (boost::shared_ptr<Route> route, int x, int y)
 {
        double wx, wy;
        double cx, cy;
-       framepos_t where;
        RouteTimeAxisView *dest_rtv = 0;
        RouteTimeAxisView *source_rtv = 0;
 
@@ -2220,7 +1997,7 @@ Editor::insert_route_list_drag (boost::shared_ptr<Route> route, int x, int y)
        event.button.x = wx;
        event.button.y = wy;
 
-       where = event_frame (&event, &cx, &cy);
+       event_frame (&event, &cx, &cy);
 
        std::pair<TimeAxisView*, int> const tv = trackview_by_y_position (cy);
        if (tv.first == 0) {
@@ -2397,28 +2174,101 @@ Editor::loop_location (Location& location)
        }
 }
 
+void
+Editor::do_layer_operation (LayerOperation op)
+{
+       if (selection->regions.empty ()) {
+               return;
+       }
+
+       bool const multiple = selection->regions.size() > 1;
+       switch (op) {
+       case Raise:
+               if (multiple) {
+                       begin_reversible_command (_("raise regions"));
+               } else {
+                       begin_reversible_command (_("raise region"));
+               }
+               break;
+
+       case RaiseToTop:
+               if (multiple) {
+                       begin_reversible_command (_("raise regions to top"));
+               } else {
+                       begin_reversible_command (_("raise region to top"));
+               }
+               break;
+               
+       case Lower:
+               if (multiple) {
+                       begin_reversible_command (_("lower regions"));
+               } else {
+                       begin_reversible_command (_("lower region"));
+               }
+               break;
+               
+       case LowerToBottom:
+               if (multiple) {
+                       begin_reversible_command (_("lower regions to bottom"));
+               } else {
+                       begin_reversible_command (_("lower region"));
+               }
+               break;
+       }
+
+       set<boost::shared_ptr<Playlist> > playlists = selection->regions.playlists ();
+       for (set<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
+               (*i)->clear_owned_changes ();
+       }
+       
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               boost::shared_ptr<Region> r = (*i)->region ();
+               switch (op) {
+               case Raise:
+                       r->raise ();
+                       break;
+               case RaiseToTop:
+                       r->raise_to_top ();
+                       break;
+               case Lower:
+                       r->lower ();
+                       break;
+               case LowerToBottom:
+                       r->lower_to_bottom ();
+               }
+       }
+
+       for (set<boost::shared_ptr<Playlist> >::iterator i = playlists.begin(); i != playlists.end(); ++i) {
+               vector<Command*> cmds;
+               (*i)->rdiff (cmds);
+               _session->add_commands (cmds);
+       }
+       
+       commit_reversible_command ();
+}
+
 void
 Editor::raise_region ()
 {
-       selection->foreach_region (&Region::raise);
+       do_layer_operation (Raise);
 }
 
 void
 Editor::raise_region_to_top ()
 {
-       selection->foreach_region (&Region::raise_to_top);
+       do_layer_operation (RaiseToTop);
 }
 
 void
 Editor::lower_region ()
 {
-       selection->foreach_region (&Region::lower);
+       do_layer_operation (Lower);
 }
 
 void
 Editor::lower_region_to_bottom ()
 {
-       selection->foreach_region (&Region::lower_to_bottom);
+       do_layer_operation (LowerToBottom);
 }
 
 /** Show the region editor for the selected regions */
@@ -2439,7 +2289,7 @@ void
 Editor::rename_region ()
 {
        RegionSelection rs = get_regions_from_selection_and_entered ();
-       
+
        if (rs.empty()) {
                return;
        }
@@ -2462,7 +2312,7 @@ Editor::rename_region ()
        d.set_size_request (300, -1);
        d.set_position (Gtk::WIN_POS_MOUSE);
 
-       entry.set_text (selection->regions.front()->region()->name());
+       entry.set_text (rs.front()->region()->name());
        entry.select_region (0, -1);
 
        entry.signal_activate().connect (sigc::bind (sigc::mem_fun (d, &Dialog::response), RESPONSE_OK));
@@ -2478,7 +2328,7 @@ Editor::rename_region ()
        if (ret != RESPONSE_OK) {
                return;
        }
-       
+
        std::string str = entry.get_text();
        strip_whitespace_edges (str);
        if (!str.empty()) {
@@ -2524,7 +2374,7 @@ Editor::play_selected_region ()
        framepos_t end = 0;
 
        RegionSelection rs = get_regions_from_selection_and_entered ();
-       
+
        if (rs.empty()) {
                return;
        }
@@ -2582,8 +2432,8 @@ Editor::region_from_selection ()
                internal_start = start - current->position();
                RegionFactory::region_name (new_name, current->name(), true);
 
-               PropertyList plist; 
-               
+               PropertyList plist;
+
                plist.add (ARDOUR::Properties::start, current->start() + internal_start);
                plist.add (ARDOUR::Properties::length, selection_cnt);
                plist.add (ARDOUR::Properties::name, new_name);
@@ -2603,9 +2453,10 @@ Editor::create_region_from_selection (vector<boost::shared_ptr<Region> >& new_re
        framepos_t start = selection->time[clicked_selection].start;
        framepos_t end = selection->time[clicked_selection].end;
 
-       sort_track_selection ();
+       TrackViewList ts = selection->tracks.filter_to_unique_playlists ();
+       sort_track_selection (ts);
 
-       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+       for (TrackSelection::iterator i = ts.begin(); i != ts.end(); ++i) {
                boost::shared_ptr<Region> current;
                boost::shared_ptr<Playlist> playlist;
                framepos_t internal_start;
@@ -2622,8 +2473,8 @@ Editor::create_region_from_selection (vector<boost::shared_ptr<Region> >& new_re
                internal_start = start - current->position();
                RegionFactory::region_name (new_name, current->name(), true);
 
-               PropertyList plist; 
-               
+               PropertyList plist;
+
                plist.add (ARDOUR::Properties::start, current->start() + internal_start);
                plist.add (ARDOUR::Properties::length, end - start + 1);
                plist.add (ARDOUR::Properties::name, new_name);
@@ -2636,7 +2487,7 @@ void
 Editor::split_multichannel_region ()
 {
        RegionSelection rs = get_regions_from_selection_and_entered ();
-       
+
        if (rs.empty()) {
                return;
        }
@@ -2659,7 +2510,7 @@ static void
 add_if_covered (RegionView* rv, const AudioRange* ar, RegionSelection* rs)
 {
        switch (rv->region()->coverage (ar->start, ar->end - 1)) {
-       case OverlapNone:
+       case Evoral::OverlapNone:
                break;
        default:
                rs->push_back (rv);
@@ -2701,7 +2552,7 @@ Editor::get_tracks_for_range_action () const
                t = selection->tracks;
        }
 
-       return t;
+       return t.filter_to_unique_playlists();
 }
 
 void
@@ -2712,7 +2563,7 @@ Editor::separate_regions_between (const TimeSelection& ts)
        RegionSelection new_selection;
 
        TrackViewList tmptracks = get_tracks_for_range_action ();
-       sort_track_selection (&tmptracks);
+       sort_track_selection (tmptracks);
 
        for (TrackSelection::iterator i = tmptracks.begin(); i != tmptracks.end(); ++i) {
 
@@ -2754,12 +2605,12 @@ Editor::separate_regions_between (const TimeSelection& ts)
                                                        rtv->view()->foreach_regionview (sigc::bind (
                                                                                sigc::ptr_fun (add_if_covered),
                                                                                &(*t), &new_selection));
-                                                       
+
                                                        if (!in_command) {
                                                                begin_reversible_command (_("separate"));
                                                                in_command = true;
                                                        }
-                                                        
+
                                                        /* pick up changes to existing regions */
 
                                                        vector<Command*> cmds;
@@ -2861,7 +2712,9 @@ Editor::separate_under_selected_regions ()
 {
        vector<PlaylistState> playlists;
 
-       RegionSelection rs = get_regions_from_selection_and_entered ();
+       RegionSelection rs;
+
+       rs = get_regions_from_selection_and_entered();
 
        if (!_session || rs.empty()) {
                return;
@@ -2914,7 +2767,7 @@ Editor::separate_under_selected_regions ()
 
                //Partition on the region bounds
                playlist->partition ((*rl)->first_frame() - 1, (*rl)->last_frame() + 1, true);
-               
+
                //Re-add region that was just removed due to the partition operation
                playlist->add_region( (*rl), (*rl)->first_frame() );
        }
@@ -2953,16 +2806,17 @@ Editor::crop_region_to (framepos_t start, framepos_t end)
 {
        vector<boost::shared_ptr<Playlist> > playlists;
        boost::shared_ptr<Playlist> playlist;
-       TrackViewList* ts;
+       TrackViewList ts;
 
        if (selection->tracks.empty()) {
-               ts = &track_views;
+               ts = track_views.filter_to_unique_playlists();
        } else {
-               sort_track_selection ();
-               ts = &selection->tracks;
+               ts = selection->tracks.filter_to_unique_playlists ();
        }
 
-       for (TrackSelection::iterator i = ts->begin(); i != ts->end(); ++i) {
+       sort_track_selection (ts);
+
+       for (TrackSelection::iterator i = ts.begin(); i != ts.end(); ++i) {
 
                RouteTimeAxisView* rtv;
 
@@ -3013,7 +2867,7 @@ Editor::crop_region_to (framepos_t start, framepos_t end)
                cnt = the_end - the_start + 1;
 
                region->clear_changes ();
-               region->trim_to (the_start, cnt, this);
+               region->trim_to (the_start, cnt);
                _session->add_command (new StatefulDiffCommand (region));
        }
 
@@ -3031,7 +2885,7 @@ Editor::region_fill_track ()
 
        framepos_t const end = _session->current_end_frame ();
 
-       begin_reversible_command (_("region fill"));
+       begin_reversible_command (Operations::region_fill);
 
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
 
@@ -3085,9 +2939,11 @@ Editor::region_fill_selection ()
        framepos_t selection_length = end - start;
        float times = (float)selection_length / region->length();
 
-       begin_reversible_command (_("fill selection"));
+       begin_reversible_command (Operations::fill_selection);
 
-       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+       TrackViewList ts = selection->tracks.filter_to_unique_playlists ();
+
+       for (TrackViewList::iterator i = ts.begin(); i != ts.end(); ++i) {
 
                if ((playlist = (*i)->playlist()) == 0) {
                        continue;
@@ -3140,20 +2996,20 @@ void
 Editor::remove_region_sync ()
 {
        RegionSelection rs = get_regions_from_selection_and_entered ();
-       
+
        if (rs.empty()) {
                return;
        }
 
        begin_reversible_command (_("remove region sync"));
-       
+
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
 
                (*i)->region()->clear_changes ();
                (*i)->region()->clear_sync_position ();
                _session->add_command(new StatefulDiffCommand ((*i)->region()));
        }
-       
+
        commit_reversible_command ();
 }
 
@@ -3171,13 +3027,13 @@ Editor::naturalize_region ()
        } else {
                begin_reversible_command (_("move region to original position"));
        }
-               
+
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
                (*i)->region()->clear_changes ();
-               (*i)->region()->move_to_natural_position (this);
+               (*i)->region()->move_to_natural_position ();
                _session->add_command (new StatefulDiffCommand ((*i)->region()));
        }
-       
+
        commit_reversible_command ();
 }
 
@@ -3185,7 +3041,7 @@ void
 Editor::align_regions (RegionPoint what)
 {
        RegionSelection const rs = get_regions_from_selection_and_edit_point ();
-       
+
        if (rs.empty()) {
                return;
        }
@@ -3211,7 +3067,7 @@ void
 Editor::align_regions_relative (RegionPoint point)
 {
        RegionSelection const rs = get_regions_from_selection_and_edit_point ();
-       
+
        if (rs.empty()) {
                return;
        }
@@ -3269,7 +3125,7 @@ Editor::align_regions_relative (RegionPoint point)
        /* move first one specially */
 
        r->clear_changes ();
-       r->set_position (pos, this);
+       r->set_position (pos);
        _session->add_command(new StatefulDiffCommand (r));
 
        /* move rest by the same amount */
@@ -3283,11 +3139,11 @@ Editor::align_regions_relative (RegionPoint point)
                region->clear_changes ();
 
                if (dir > 0) {
-                       region->set_position (region->position() + distance, this);
+                       region->set_position (region->position() + distance);
                } else {
-                       region->set_position (region->position() - distance, this);
+                       region->set_position (region->position() - distance);
                }
-                
+
                _session->add_command(new StatefulDiffCommand (region));
 
        }
@@ -3310,17 +3166,17 @@ Editor::align_region_internal (boost::shared_ptr<Region> region, RegionPoint poi
 
        switch (point) {
        case SyncPoint:
-               region->set_position (region->adjust_to_sync (position), this);
+               region->set_position (region->adjust_to_sync (position));
                break;
 
        case End:
                if (position > region->length()) {
-                       region->set_position (position - region->length(), this);
+                       region->set_position (position - region->length());
                }
                break;
 
        case Start:
-               region->set_position (position, this);
+               region->set_position (position);
                break;
        }
 
@@ -3358,15 +3214,15 @@ Editor::trim_region (bool front)
 
        for (list<RegionView*>::const_iterator i = rs.by_layer().begin(); i != rs.by_layer().end(); ++i) {
                if (!(*i)->region()->locked()) {
-                        
+
                        (*i)->region()->clear_changes ();
-                       
+
                        if (front) {
-                               (*i)->region()->trim_front (where, this);
+                               (*i)->region()->trim_front (where);
                        } else {
-                               (*i)->region()->trim_end (where, this);
+                               (*i)->region()->trim_end (where);
                        }
-                       
+
                        _session->add_command (new StatefulDiffCommand ((*i)->region()));
                }
        }
@@ -3407,7 +3263,7 @@ Editor::trim_region_to_location (const Location& loc, const char* str)
 
                /* require region to span proposed trim */
                switch (rv->region()->coverage (loc.start(), loc.end())) {
-               case OverlapInternal:
+               case Evoral::OverlapInternal:
                        break;
                default:
                        continue;
@@ -3428,9 +3284,9 @@ Editor::trim_region_to_location (const Location& loc, const char* str)
 
                start = session_frame_to_track_frame (loc.start(), speed);
                end = session_frame_to_track_frame (loc.end(), speed);
-                
+
                rv->region()->clear_changes ();
-               rv->region()->trim_to (start, (end - start), this);
+               rv->region()->trim_to (start, (end - start));
                _session->add_command(new StatefulDiffCommand (rv->region()));
        }
 
@@ -3484,15 +3340,15 @@ Editor::trim_to_region(bool forward)
 
                region->clear_changes ();
 
-               if(forward){
+               if (forward) {
 
                    next_region = playlist->find_next_region (region->first_frame(), Start, 1);
 
-                   if(!next_region){
+                   if (!next_region) {
                        continue;
                    }
 
-                   region->trim_end((framepos_t) ( (next_region->first_frame() - 1) * speed), this);
+                   region->trim_end((framepos_t) ( (next_region->first_frame() - 1) * speed));
                    arv->region_changed (PropertyChange (ARDOUR::Properties::length));
                }
                else {
@@ -3503,7 +3359,7 @@ Editor::trim_to_region(bool forward)
                        continue;
                    }
 
-                   region->trim_front((framepos_t) ((next_region->last_frame() + 1) * speed), this);
+                   region->trim_front((framepos_t) ((next_region->last_frame() + 1) * speed));
 
                    arv->region_changed (ARDOUR::bounds_change);
                }
@@ -3527,14 +3383,17 @@ Editor::unfreeze_route ()
 void*
 Editor::_freeze_thread (void* arg)
 {
-       SessionEvent::create_per_thread_pool ("freeze events", 64);
-
        return static_cast<Editor*>(arg)->freeze_thread ();
 }
 
 void*
 Editor::freeze_thread ()
 {
+       /* create event pool because we may need to talk to the session */
+       SessionEvent::create_per_thread_pool ("freeze events", 64);
+       /* create per-thread buffers for process() tree to use */
+       current_interthread_info->process_thread.init ();
+
        clicked_routeview->audio_track()->freeze_me (*current_interthread_info);
        current_interthread_info->done = true;
        return 0;
@@ -3543,10 +3402,51 @@ Editor::freeze_thread ()
 void
 Editor::freeze_route ()
 {
+       if (!_session) {
+               return;
+       }
+
+       /* stop transport before we start. this is important */
+
+       _session->request_transport_speed (0.0);
+       
+       /* wait for just a little while, because the above call is asynchronous */
+
+       ::usleep (250000);
+
        if (clicked_routeview == 0 || !clicked_routeview->is_audio_track()) {
                return;
        }
 
+       if (!clicked_routeview->track()->bounceable (clicked_routeview->track()->main_outs(), true)) {
+               MessageDialog d (
+                       _("This track/bus cannot be frozen because the signal adds or loses channels before reaching the outputs.\n"
+                         "This is typically caused by plugins that generate stereo output from mono input or vice versa.")
+                       );
+               d.set_title (_("Cannot freeze"));
+               d.run ();
+               return;
+       }
+
+       if (clicked_routeview->track()->has_external_redirects()) {
+               MessageDialog d (string_compose (_("<b>%1</b>\n\nThis track has at least one send/insert/return as part of its signal flow.\n\n"
+                                                  "Freezing will only process the signal as far as the first send/insert/return."),
+                                                clicked_routeview->track()->name()), true, MESSAGE_INFO, BUTTONS_NONE, true);
+
+               d.add_button (_("Freeze anyway"), Gtk::RESPONSE_OK);
+               d.add_button (_("Don't freeze"), Gtk::RESPONSE_CANCEL);
+               d.set_title (_("Freeze Limits"));
+
+               int response = d.run ();
+
+               switch (response) {
+               case Gtk::RESPONSE_CANCEL:
+                       return;
+               default:
+                       break;
+               }
+       }
+
        InterThreadInfo itt;
        current_interthread_info = &itt;
 
@@ -3573,6 +3473,25 @@ Editor::bounce_range_selection (bool replace, bool enable_processing)
 
        TrackSelection views = selection->tracks;
 
+       for (TrackViewList::iterator i = views.begin(); i != views.end(); ++i) {
+
+               if (enable_processing) {
+
+                       RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
+
+                       if (rtv && rtv->track() && replace && enable_processing && !rtv->track()->bounceable (rtv->track()->main_outs(), false)) {
+                               MessageDialog d (
+                                       _("You can't perform this operation because the processing of the signal "
+                                         "will cause one or more of the tracks will end up with a region with more channels than this track has inputs.\n\n"
+                                         "You can do this without processing, which is a different operation.")
+                                       );
+                               d.set_title (_("Cannot bounce"));
+                               d.run ();
+                               return;
+                       }
+               }
+       }
+
        framepos_t start = selection->time[clicked_selection].start;
        framepos_t end = selection->time[clicked_selection].end;
        framepos_t cnt = end - start + 1;
@@ -3597,8 +3516,18 @@ Editor::bounce_range_selection (bool replace, bool enable_processing)
 
                playlist->clear_changes ();
                playlist->clear_owned_changes ();
-               
-               boost::shared_ptr<Region> r = rtv->track()->bounce_range (start, start+cnt, itt, enable_processing);
+
+               boost::shared_ptr<Region> r;
+
+               if (enable_processing) {
+                       r = rtv->track()->bounce_range (start, start+cnt, itt, rtv->track()->main_outs(), false);
+               } else {
+                       r = rtv->track()->bounce_range (start, start+cnt, itt, boost::shared_ptr<Processor>(), false);
+               }
+
+               if (!r) {
+                       continue;
+               }
 
                if (replace) {
                        list<AudioRange> ranges;
@@ -3617,6 +3546,13 @@ Editor::bounce_range_selection (bool replace, bool enable_processing)
        commit_reversible_command ();
 }
 
+/** Delete selected regions, automation points or a time range */
+void
+Editor::delete_ ()
+{
+       cut_copy (Delete);
+}
+
 /** Cut selected regions, automation points or a time range */
 void
 Editor::cut ()
@@ -3669,6 +3605,9 @@ Editor::cut_copy (CutCopyOp op)
        string opname;
 
        switch (op) {
+       case Delete:
+               opname = _("delete");
+               break;
        case Cut:
                opname = _("cut");
                break;
@@ -3686,7 +3625,7 @@ Editor::cut_copy (CutCopyOp op)
           this function.
        */
 
-       if (op == Cut || op == Clear) {
+       if (op == Delete || op == Cut || op == Clear) {
                if (_drags->active ()) {
                        _drags->abort ();
                }
@@ -3726,12 +3665,8 @@ Editor::cut_copy (CutCopyOp op)
 
                /* we only want to cut regions if some are selected */
 
-               if (!selection->regions.empty()) {
-                       rs = selection->regions;
-               }
-
-               switch (current_mouse_mode()) {
-               case MouseObject:
+               if (doing_object_stuff()) {
+                       rs = get_regions_from_selection ();
                        if (!rs.empty() || !selection->points.empty()) {
 
                                begin_reversible_command (opname + _(" objects"));
@@ -3739,7 +3674,7 @@ Editor::cut_copy (CutCopyOp op)
                                if (!rs.empty()) {
                                        cut_copy_regions (op, rs);
 
-                                       if (op == Cut) {
+                                       if (op == Cut || op == Delete) {
                                                selection->clear_regions ();
                                        }
                                }
@@ -3747,21 +3682,20 @@ Editor::cut_copy (CutCopyOp op)
                                if (!selection->points.empty()) {
                                        cut_copy_points (op);
 
-                                       if (op == Cut) {
+                                       if (op == Cut || op == Delete) {
                                                selection->clear_points ();
                                        }
                                }
-
                                commit_reversible_command ();
-                               break; // terminate case statement here
+                               goto out;
                        }
-                       if (!selection->time.empty()) {
+                       if (!selection->time.empty() && (_join_object_range_state == JOIN_OBJECT_RANGE_NONE)) {
                                /* don't cause suprises */
-                               break;
+                               goto out;
                        }
-                       // fall thru if there was nothing selected
+               }
 
-               case MouseRange:
+               if (doing_range_stuff()) {
                        if (selection->time.empty()) {
                                framepos_t start, end;
                                if (!get_edit_op_range (start, end)) {
@@ -3774,18 +3708,14 @@ Editor::cut_copy (CutCopyOp op)
                        cut_copy_ranges (op);
                        commit_reversible_command ();
 
-                       if (op == Cut) {
+                       if (op == Cut || op == Delete) {
                                selection->clear_time ();
                        }
-
-                       break;
-
-               default:
-                       break;
                }
        }
 
-       if (op == Cut || op == Clear) {
+  out:
+       if (op == Delete || op == Cut || op == Clear) {
                _drags->abort ();
        }
 }
@@ -3799,6 +3729,7 @@ 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);
+               _last_cut_copy_source_track = atv;
 
                if (atv) {
                        atv->cut_copy_clear_objects (selection->points, op);
@@ -3845,7 +3776,16 @@ Editor::remove_clicked_region ()
 
        begin_reversible_command (_("remove region"));
        playlist->clear_changes ();
+       playlist->clear_owned_changes ();
        playlist->remove_region (clicked_regionview->region());
+
+       /* We might have removed regions, which alters other regions' layering_index,
+          so we need to do a recursive diff here.
+       */
+       vector<Command*> cmds;
+       playlist->rdiff (cmds);
+       _session->add_commands (cmds);
+       
        _session->add_command(new StatefulDiffCommand (playlist));
        commit_reversible_command ();
 }
@@ -3889,23 +3829,16 @@ Editor::remove_selected_regions ()
                        continue;
                }
 
-               vector<boost::shared_ptr<Playlist> >::iterator i;
-
-               //only prep history if this is a new playlist.
-               for (i = playlists.begin(); i != playlists.end(); ++i) {
-                       if ((*i) == playlist) {
-                               break;
-                       }
-               }
-
-               if (i == playlists.end()) {
-
-                       playlist->clear_changes ();
-                       playlist->freeze ();
+               /* get_regions_from_selection_and_entered() guarantees that
+                  the playlists involved are unique, so there is no need
+                  to check here.
+               */
 
-                       playlists.push_back (playlist);
-               }
+               playlists.push_back (playlist);
 
+               playlist->clear_changes ();
+               playlist->clear_owned_changes ();
+               playlist->freeze ();
                playlist->remove_region (*rl);
        }
 
@@ -3913,6 +3846,14 @@ Editor::remove_selected_regions ()
 
        for (pl = playlists.begin(); pl != playlists.end(); ++pl) {
                (*pl)->thaw ();
+
+               /* We might have removed regions, which alters other regions' layering_index,
+                  so we need to do a recursive diff here.
+               */
+               vector<Command*> cmds;
+               (*pl)->rdiff (cmds);
+               _session->add_commands (cmds);
+               
                _session->add_command(new StatefulDiffCommand (*pl));
        }
 
@@ -3944,13 +3885,13 @@ Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
 
                first_position = min ((framepos_t) (*x)->region()->position(), first_position);
 
-               if (op == Cut || op == Clear) {
+               if (op == Cut || op == Clear || op == Delete) {
                        boost::shared_ptr<Playlist> pl = (*x)->region()->playlist();
 
                        if (pl) {
                                FreezeList::iterator fl;
 
-                               //only take state if this is a new playlist.
+                               // only take state if this is a new playlist.
                                for (fl = freezelist.begin(); fl != freezelist.end(); ++fl) {
                                        if ((*fl) == pl) {
                                                break;
@@ -3959,6 +3900,7 @@ Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
 
                                if (fl == freezelist.end()) {
                                        pl->clear_changes();
+                                       pl->clear_owned_changes ();
                                        pl->freeze ();
                                        freezelist.insert (pl);
                                }
@@ -3998,22 +3940,25 @@ Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
                tmp = x;
                ++tmp;
 
-               vector<PlaylistMapping>::iterator z;
+               if (op != Delete) {
 
-               for (z = pmap.begin(); z != pmap.end(); ++z) {
-                       if ((*z).tv == &tv) {
-                               break;
+                       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;
                        }
-               }
-
-               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();
@@ -4022,6 +3967,10 @@ Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
                assert (r != 0);
 
                switch (op) {
+               case Delete:
+                       pl->remove_region (r);
+                       break;
+                       
                case Cut:
                        _xx = RegionFactory::create (r);
                        npl->add_region (_xx, r->position() - first_position);
@@ -4034,36 +3983,47 @@ Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
                        break;
 
                case Clear:
-                       pl->remove_region (r);
+                       pl->remove_region (r);  
                        break;
                }
 
                x = tmp;
        }
 
-       list<boost::shared_ptr<Playlist> > foo;
+       if (op != Delete) {
 
-       /* 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) {
-               if ((*i).pl) {
-                       (*i).pl->thaw();
-                       foo.push_back ((*i).pl);
+               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) {
+                       if ((*i).pl) {
+                               (*i).pl->thaw();
+                               foo.push_back ((*i).pl);
+                       }
+               }
+               
+               if (!foo.empty()) {
+                       cut_buffer->set (foo);
+               }
+               
+               if (pmap.empty()) {
+                       _last_cut_copy_source_track = 0;
+               } else {
+                       _last_cut_copy_source_track = pmap.front().tv;
                }
        }
 
-       if (!foo.empty()) {
-               cut_buffer->set (foo);
-       }
-
-       if (pmap.empty()) {
-               _last_cut_copy_source_track = 0;
-       } else {
-               _last_cut_copy_source_track = pmap.front().tv;
-       }
-        
        for (FreezeList::iterator pl = freezelist.begin(); pl != freezelist.end(); ++pl) {
                (*pl)->thaw ();
+
+               /* We might have removed regions, which alters other regions' layering_index,
+                  so we need to do a recursive diff here.
+               */
+               vector<Command*> cmds;
+               (*pl)->rdiff (cmds);
+               _session->add_commands (cmds);
+               
                _session->add_command (new StatefulDiffCommand (*pl));
        }
 }
@@ -4071,28 +4031,34 @@ Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
 void
 Editor::cut_copy_ranges (CutCopyOp op)
 {
-       TrackViewList* ts;
-       TrackViewList entered;
+       TrackViewList ts = selection->tracks.filter_to_unique_playlists ();
 
-       if (selection->tracks.empty()) {
+       /* Sort the track selection now, so that it if is used, the playlists
+          selected by the calls below to cut_copy_clear are in the order that
+          their tracks appear in the editor.  This makes things like paste
+          of ranges work properly.
+       */
+
+       sort_track_selection (ts);
+
+       if (ts.empty()) {
                if (!entered_track) {
                        return;
                }
-               entered.push_back (entered_track);
-               ts = &entered;
-       } else {
-               ts = &selection->tracks;
-       }
+               ts.push_back (entered_track);
+       } 
 
-       for (TrackSelection::iterator i = ts->begin(); i != ts->end(); ++i) {
+       for (TrackViewList::iterator i = ts.begin(); i != ts.end(); ++i) {
                (*i)->cut_copy_clear (*selection, op);
        }
 }
 
 void
-Editor::paste (float times)
+Editor::paste (float times, bool from_context)
 {
-       paste_internal (get_preferred_edit_position(), times);
+        DEBUG_TRACE (DEBUG::CutNPaste, "paste to preferred edit pos\n");
+
+       paste_internal (get_preferred_edit_position (false, from_context), times);
 }
 
 void
@@ -4112,7 +4078,7 @@ Editor::mouse_paste ()
 void
 Editor::paste_internal (framepos_t position, float times)
 {
-       bool commit = false;
+        DEBUG_TRACE (DEBUG::CutNPaste, string_compose ("apparent paste position is %1\n", position));
 
        if (internal_editing()) {
                if (cut_buffer->midi_notes.empty()) {
@@ -4126,10 +4092,9 @@ Editor::paste_internal (framepos_t position, float times)
 
        if (position == max_framepos) {
                position = get_preferred_edit_position();
+                DEBUG_TRACE (DEBUG::CutNPaste, string_compose ("preferred edit position is %1\n", position));
        }
 
-       begin_reversible_command (_("paste"));
-
        TrackViewList ts;
        TrackViewList::iterator i;
        size_t nth;
@@ -4138,20 +4103,20 @@ Editor::paste_internal (framepos_t position, float times)
 
        if (!selection->tracks.empty()) {
                /* there are some selected tracks, so paste to them */
-               sort_track_selection ();
-               ts = selection->tracks;
+               ts = selection->tracks.filter_to_unique_playlists ();
+               sort_track_selection (ts);
        } else if (_last_cut_copy_source_track) {
                /* otherwise paste to the track that the cut/copy came from;
-                  see discussion in mants #3333.
+                  see discussion in mantis #3333.
                */
                ts.push_back (_last_cut_copy_source_track);
        }
 
-       for (nth = 0, i = ts.begin(); i != ts.end(); ++i, ++nth) {
+       if (internal_editing ()) {
 
                /* undo/redo is handled by individual tracks/regions */
 
-               if (internal_editing()) {
+               for (nth = 0, i = ts.begin(); i != ts.end(); ++i, ++nth) {
 
                        RegionSelection rs;
                        RegionSelection::iterator r;
@@ -4167,16 +4132,18 @@ Editor::paste_internal (framepos_t position, float times)
                                        ++cb;
                                }
                        }
+               }
 
-               } else {
+       } else {
 
-                       if ((*i)->paste (position, times, *cut_buffer, nth)) {
-                               commit = true;
-                       }
+               /* we do redo (do you do voodoo?) */
+
+               begin_reversible_command (Operations::paste);
+
+               for (nth = 0, i = ts.begin(); i != ts.end(); ++i, ++nth) {
+                       (*i)->paste (position, times, *cut_buffer, nth);
                }
-       }
 
-       if (commit) {
                commit_reversible_command ();
        }
 }
@@ -4191,7 +4158,7 @@ Editor::duplicate_some_regions (RegionSelection& regions, float times)
        framepos_t const start_frame = regions.start ();
        framepos_t const end_frame = regions.end_frame ();
 
-       begin_reversible_command (_("duplicate region"));
+       begin_reversible_command (Operations::duplicate_region);
 
        selection->clear_regions ();
 
@@ -4242,7 +4209,9 @@ Editor::duplicate_selection (float times)
 
        ri = new_regions.begin();
 
-       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+       TrackViewList ts = selection->tracks.filter_to_unique_playlists ();
+
+       for (TrackViewList::iterator i = ts.begin(); i != ts.end(); ++i) {
                if ((playlist = (*i)->playlist()) == 0) {
                        continue;
                }
@@ -4321,7 +4290,9 @@ Editor::nudge_track (bool use_edit, bool forwards)
 
        begin_reversible_command (_("nudge track"));
 
-       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+       TrackViewList ts = selection->tracks.filter_to_unique_playlists ();
+
+       for (TrackViewList::iterator i = ts.begin(); i != ts.end(); ++i) {
 
                if ((playlist = (*i)->playlist()) == 0) {
                        continue;
@@ -4331,12 +4302,12 @@ Editor::nudge_track (bool use_edit, bool forwards)
                playlist->clear_owned_changes ();
 
                playlist->nudge_after (start, distance, forwards);
-               
+
                vector<Command*> cmds;
-               
+
                playlist->rdiff (cmds);
                _session->add_commands (cmds);
-               
+
                _session->add_command (new StatefulDiffCommand (playlist));
        }
 
@@ -4381,7 +4352,7 @@ Editor::normalize_region ()
        }
 
        RegionSelection rs = get_regions_from_selection_and_entered ();
-       
+
        if (rs.empty()) {
                return;
        }
@@ -4414,7 +4385,7 @@ Editor::normalize_region ()
                                set_canvas_cursor (current_canvas_cursor);
                                return;
                        }
-                       
+
                        max_amps.push_back (a);
                        max_amp = max (max_amp, a);
                        dialog.ascend ();
@@ -4424,7 +4395,7 @@ Editor::normalize_region ()
        begin_reversible_command (_("normalize"));
 
        list<double>::const_iterator a = max_amps.begin ();
-       
+
        for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
                AudioRegionView* const arv = dynamic_cast<AudioRegionView*> (*r);
                if (!arv) {
@@ -4434,7 +4405,7 @@ Editor::normalize_region ()
                arv->region()->clear_changes ();
 
                double const amp = dialog.normalize_individually() ? *a : max_amp;
-               
+
                arv->audio_region()->normalize (amp, dialog.target ());
                _session->add_command (new StatefulDiffCommand (arv->region()));
 
@@ -4491,28 +4462,16 @@ Editor::adjust_region_gain (bool up)
                }
 
                arv->region()->clear_changes ();
-                
-               double fraction = gain_to_slider_position (arv->audio_region()->scale_amplitude ());
+
+               double dB = accurate_coefficient_to_dB (arv->audio_region()->scale_amplitude ());
 
                if (up) {
-                       fraction += 0.05;
-                       fraction = min (fraction, 1.0);
+                       dB += 1;
                } else {
-                       fraction -= 0.05;
-                       fraction = max (fraction, 0.0);
-               }
-
-               if (!up && fraction <= 0) {
-                       continue;
+                       dB -= 1;
                }
 
-               fraction = slider_position_to_gain (fraction);
-
-               if (up && fraction >= 2.0) {
-                       continue;
-               }
-
-               arv->audio_region()->set_scale_amplitude (fraction);
+               arv->audio_region()->set_scale_amplitude (dB_to_coefficient (dB));
                _session->add_command (new StatefulDiffCommand (arv->region()));
        }
 
@@ -4544,20 +4503,24 @@ Editor::strip_region_silence ()
                return;
        }
 
-       std::list<boost::shared_ptr<AudioRegion> > ar;
+       std::list<RegionView*> audio_only;
 
        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 ());
+                       audio_only.push_back (arv);
                }
        }
 
-       StripSilenceDialog d (_session, ar);
+       StripSilenceDialog d (_session, audio_only);
        int const r = d.run ();
 
-       if (r == Gtk::RESPONSE_OK) {
-               StripSilence s (*_session, d.threshold (), d.minimum_length (), d.fade_length ());
+        d.drop_rects ();
+
+        if (r == Gtk::RESPONSE_OK) {
+                ARDOUR::AudioIntervalMap silences;
+                d.silences (silences);
+               StripSilence s (*_session, silences, d.fade_length());
                apply_filter (s, _("strip silence"), &d);
        }
 }
@@ -4571,7 +4534,10 @@ Editor::apply_midi_note_edit_op_to_region (MidiOperator& op, MidiRegionView& mrv
        vector<Evoral::Sequence<Evoral::MusicalTime>::Notes> v;
        v.push_back (selected);
 
-       return op (mrv.midi_region()->model(), v);
+       framepos_t pos_frames = mrv.midi_region()->position();
+       double     pos_beats  = _session->tempo_map().framewalk_to_beats(0, pos_frames);
+
+       return op (mrv.midi_region()->model(), pos_beats, v);
 }
 
 void
@@ -4630,7 +4596,7 @@ Editor::fork_region ()
                if (mrv) {
                        boost::shared_ptr<Playlist> playlist = mrv->region()->playlist();
                        boost::shared_ptr<MidiRegion> newregion = mrv->midi_region()->clone ();
-                        
+
                        playlist->clear_changes ();
                        playlist->replace_region (mrv->region(), newregion, mrv->region()->position());
                        _session->add_command(new StatefulDiffCommand (playlist));
@@ -4686,6 +4652,34 @@ Editor::quantize_region ()
        }
 }
 
+void
+Editor::insert_patch_change (bool from_context)
+{
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+
+       if (rs.empty ()) {
+               return;
+       }
+
+       const framepos_t p = get_preferred_edit_position (false, from_context);
+
+       Evoral::PatchChange<Evoral::MusicalTime> empty (0, 0, 0, 0);
+       PatchChangeDialog d (0, _session, empty, Gtk::Stock::ADD);
+
+       if (d.run() == RESPONSE_CANCEL) {
+               return;
+       }
+
+       for (RegionSelection::iterator i = rs.begin (); i != rs.end(); ++i) {
+               MidiRegionView* const mrv = dynamic_cast<MidiRegionView*> (*i);
+               if (mrv) {
+                       if (p >= mrv->region()->first_frame() && p <= mrv->region()->last_frame()) {
+                               mrv->add_patch_change (p - mrv->region()->position(), d.patch ());
+                       }
+               }
+       }
+}
+
 void
 Editor::apply_filter (Filter& filter, string command, ProgressReporter* progress)
 {
@@ -4702,7 +4696,7 @@ Editor::apply_filter (Filter& filter, string command, ProgressReporter* progress
 
        int n = 0;
        int const N = rs.size ();
-       
+
        for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ) {
                RegionSelection::iterator tmp = r;
                ++tmp;
@@ -4718,6 +4712,7 @@ Editor::apply_filter (Filter& filter, string command, ProgressReporter* progress
                        if (arv->audio_region()->apply (filter, progress) == 0) {
 
                                playlist->clear_changes ();
+                               playlist->clear_owned_changes ();
 
                                if (filter.results.empty ()) {
 
@@ -4740,6 +4735,13 @@ Editor::apply_filter (Filter& filter, string command, ProgressReporter* progress
 
                                }
 
+                               /* We might have removed regions, which alters other regions' layering_index,
+                                  so we need to do a recursive diff here.
+                               */
+                               vector<Command*> cmds;
+                               playlist->rdiff (cmds);
+                               _session->add_commands (cmds);
+                               
                                _session->add_command(new StatefulDiffCommand (playlist));
                        } else {
                                goto out;
@@ -4766,22 +4768,6 @@ Editor::external_edit_region ()
        /* more to come */
 }
 
-void
-Editor::brush (framepos_t pos)
-{
-       snap_to (pos);
-
-       RegionSelection rs = get_regions_from_selection_and_entered ();
-
-       if (rs.empty()) {
-               return;
-       }
-
-       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
-               mouse_brush_insert_region ((*i), pos);
-       }
-}
-
 void
 Editor::reset_region_gain_envelopes ()
 {
@@ -4808,30 +4794,27 @@ Editor::reset_region_gain_envelopes ()
 }
 
 void
-Editor::toggle_gain_envelope_visibility ()
+Editor::set_region_gain_visibility (RegionView* rv, bool yn)
 {
-       if (_ignore_region_action) {
-               return;
+       AudioRegionView* arv = dynamic_cast<AudioRegionView*> (rv);
+       if (arv) {
+               arv->set_envelope_visible (yn);
        }
-       
-       RegionSelection rs = get_regions_from_selection_and_entered ();
+}
 
-       if (!_session || rs.empty()) {
+void
+Editor::set_gain_envelope_visibility (bool yn)
+{
+       if (!_session) {
                return;
        }
 
-       _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) {
-                       arv->region()->clear_changes ();
-                       arv->set_envelope_visible (!arv->envelope_visible());
-                       _session->add_command (new StatefulDiffCommand (arv->region()));
+       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+               AudioTimeAxisView* v = dynamic_cast<AudioTimeAxisView*>(*i);
+               if (v) {
+                       v->audio_view()->foreach_regionview (sigc::bind (sigc::mem_fun (this, &Editor::set_region_gain_visibility), yn));
                }
        }
-
-       _session->commit_reversible_command ();
 }
 
 void
@@ -4840,7 +4823,7 @@ Editor::toggle_gain_envelope_active ()
        if (_ignore_region_action) {
                return;
        }
-       
+
        RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (!_session || rs.empty()) {
@@ -4891,7 +4874,7 @@ Editor::toggle_region_lock_style ()
        if (_ignore_region_action) {
                return;
        }
-       
+
        RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (!_session || rs.empty()) {
@@ -4916,7 +4899,7 @@ Editor::toggle_opaque_region ()
        if (_ignore_region_action) {
                return;
        }
-       
+
        RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (!_session || rs.empty()) {
@@ -4955,6 +4938,60 @@ Editor::toggle_record_enable ()
        }
 }
 
+void
+Editor::toggle_solo ()
+{
+       bool new_state = false;
+       bool first = true;
+       boost::shared_ptr<RouteList> rl (new RouteList);
+
+       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+               RouteTimeAxisView *rtav = dynamic_cast<RouteTimeAxisView *>(*i);
+
+               if (!rtav) {
+                       continue;
+               }
+
+               if (first) {
+                       new_state = !rtav->route()->soloed ();
+                       first = false;
+               }
+
+               rl->push_back (rtav->route());
+       }
+
+       _session->set_solo (rl, new_state, Session::rt_cleanup, true);
+}
+
+void
+Editor::toggle_mute ()
+{
+       bool new_state = false;
+       bool first = true;
+       boost::shared_ptr<RouteList> rl (new RouteList);
+
+       for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
+               RouteTimeAxisView *rtav = dynamic_cast<RouteTimeAxisView *>(*i);
+
+               if (!rtav) {
+                       continue;
+               }
+
+               if (first) {
+                       new_state = !rtav->route()->muted();
+                       first = false;
+               }
+
+               rl->push_back (rtav->route());
+       }
+
+       _session->set_mute (rl, new_state, Session::rt_cleanup, true);
+}
+
+void
+Editor::toggle_solo_isolate ()
+{
+}
 
 void
 Editor::set_fade_length (bool in)
@@ -5108,7 +5145,7 @@ Editor::set_fade_in_active (bool yn)
 
 
                boost::shared_ptr<AudioRegion> ar (tmp->audio_region());
-               
+
                ar->clear_changes ();
                ar->set_fade_in_active (yn);
                _session->add_command (new StatefulDiffCommand (ar));
@@ -5149,7 +5186,7 @@ void
 Editor::toggle_region_fades (int dir)
 {
        boost::shared_ptr<AudioRegion> ar;
-       bool yn;
+       bool yn = false;
 
        RegionSelection rs = get_regions_from_selection_and_entered ();
 
@@ -5157,7 +5194,7 @@ Editor::toggle_region_fades (int dir)
                return;
        }
 
-       RegionSelection::iterator i;    
+       RegionSelection::iterator i;
        for (i = rs.begin(); i != rs.end(); ++i) {
                if ((ar = boost::dynamic_pointer_cast<AudioRegion>((*i)->region())) != 0) {
                        if (dir == -1) {
@@ -5208,24 +5245,6 @@ Editor::update_region_fade_visibility ()
        }
 }
 
-/** Update crossfade visibility after its configuration has been changed */
-void
-Editor::update_xfade_visibility ()
-{
-       _xfade_visibility = _session->config.get_xfades_visible ();
-
-       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 ();
-                       }
-               }
-       }
-}
-
 void
 Editor::set_edit_point ()
 {
@@ -5277,12 +5296,12 @@ Editor::set_playhead_cursor ()
 void
 Editor::split_region ()
 {
-       if (((mouse_mode == MouseRange) || 
-            (mouse_mode != MouseObject && _join_object_range_state == JOIN_OBJECT_RANGE_RANGE)) && 
+       if (((mouse_mode == MouseRange) ||
+            (mouse_mode != MouseObject && _join_object_range_state == JOIN_OBJECT_RANGE_RANGE)) &&
            !selection->time.empty()) {
                separate_regions_between (selection->time);
                return;
-       } 
+       }
 
        RegionSelection rs = get_regions_from_selection_and_edit_point ();
 
@@ -5541,11 +5560,42 @@ Editor::pitch_shift_region ()
 {
        RegionSelection rs = get_regions_from_selection_and_entered ();
 
-       if (rs.empty()) {
+       RegionSelection audio_rs;
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               if (dynamic_cast<AudioRegionView*> (*i)) {
+                       audio_rs.push_back (*i);
+               }
+       }
+
+       if (audio_rs.empty()) {
+               return;
+       }
+
+       pitch_shift (audio_rs, 1.2);
+}
+
+void
+Editor::transpose_region ()
+{
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+
+       list<MidiRegionView*> midi_region_views;
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               MidiRegionView* mrv = dynamic_cast<MidiRegionView*> (*i);
+               if (mrv) {
+                       midi_region_views.push_back (mrv);
+               }
+       }
+
+       TransposeDialog d;
+       int const r = d.run ();
+       if (r != RESPONSE_ACCEPT) {
                return;
        }
 
-       pitch_shift (rs, 1.2);
+       for (list<MidiRegionView*>::iterator i = midi_region_views.begin(); i != midi_region_views.end(); ++i) {
+               (*i)->midi_region()->transpose (d.semitones ());
+       }
 }
 
 void
@@ -5584,7 +5634,9 @@ Editor::define_one_bar (framepos_t start, framepos_t end)
           we have frames per bar, and beats per bar, so ...
        */
 
-       double frames_per_beat = length / m.beats_per_bar();
+       /* XXXX METER MATH */
+
+       double frames_per_beat = length / m.divisions_per_bar();
 
        /* beats per minute = */
 
@@ -5617,7 +5669,7 @@ Editor::define_one_bar (framepos_t start, framepos_t end)
                        _("Do you want to set the global tempo or add a new tempo marker?"),
                        options
                        );
-               
+
                c.set_default_response (2);
 
                switch (c.run()) {
@@ -5692,12 +5744,14 @@ Editor::split_region_at_transients ()
 }
 
 void
-Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList& positions, bool can_ferret)
+Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList& positions, bool can_ferret, bool select_new)
 {
        bool use_rhythmic_rodent = false;
 
        boost::shared_ptr<Playlist> pl = r->playlist();
 
+       list<boost::shared_ptr<Region> > new_regions;
+
        if (!pl) {
                return;
        }
@@ -5707,7 +5761,7 @@ Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList
        }
 
 
-       if (positions.size() > 20) {
+       if (positions.size() > 20 && can_ferret) {
                std::string 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,
@@ -5726,7 +5780,7 @@ Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList
 
                int response = msg.run();
                msg.hide ();
-               
+
                switch (response) {
                case RESPONSE_OK:
                        break;
@@ -5746,6 +5800,7 @@ Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList
        AnalysisFeatureList::const_iterator x;
 
        pl->clear_changes ();
+       pl->clear_owned_changes ();
 
        x = positions.begin();
 
@@ -5759,9 +5814,9 @@ Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList
        framepos_t pos = 0;
 
        while (x != positions.end()) {
-         
+
                /* deal with positons that are out of scope of present region bounds */
-               if (*x <= 0 || *x > r->length()){
+               if (*x <= 0 || *x > r->length()) {
                        ++x;
                        continue;
                }
@@ -5775,7 +5830,7 @@ Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList
                 */
 
                framepos_t len = (*x) - pos;
-               
+
                /* XXX we do we really want to allow even single-sample regions?
                   shouldn't we have some kind of lower limit on region size?
                */
@@ -5792,16 +5847,21 @@ Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList
 
                /* do NOT announce new regions 1 by one, just wait till they are all done */
 
-               PropertyList plist; 
-               
+               PropertyList plist;
+
                plist.add (ARDOUR::Properties::start, file_start);
                plist.add (ARDOUR::Properties::length, len);
                plist.add (ARDOUR::Properties::name, new_name);
                plist.add (ARDOUR::Properties::layer, 0);
 
                boost::shared_ptr<Region> nr = RegionFactory::create (r->sources(), plist, false);
+
                pl->add_region (nr, r->position() + pos);
 
+               if (select_new) {
+                       new_regions.push_front(nr);
+               }
+
                pos += len;
                ++x;
        }
@@ -5809,10 +5869,10 @@ Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList
        string new_name;
 
        RegionFactory::region_name (new_name, r->name());
-       
+
        /* Add the final region */
-       PropertyList plist; 
-               
+       PropertyList plist;
+
        plist.add (ARDOUR::Properties::start, r->start() + pos);
        plist.add (ARDOUR::Properties::length, r->last_frame() - (r->position() + pos) + 1);
        plist.add (ARDOUR::Properties::name, new_name);
@@ -5821,10 +5881,27 @@ Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList
        boost::shared_ptr<Region> nr = RegionFactory::create (r->sources(), plist, false);
        pl->add_region (nr, r->position() + pos);
 
-       
+       if (select_new) {
+               new_regions.push_front(nr);
+       }
+
        pl->thaw ();
 
+       /* We might have removed regions, which alters other regions' layering_index,
+          so we need to do a recursive diff here.
+       */
+       vector<Command*> cmds;
+       pl->rdiff (cmds);
+       _session->add_commands (cmds);
+       
        _session->add_command (new StatefulDiffCommand (pl));
+
+       if (select_new) {
+
+               for (list<boost::shared_ptr<Region> >::iterator i = new_regions.begin(); i != new_regions.end(); ++i){
+                       set_selected_regionview_from_region_list ((*i), Selection::Add);
+               }
+       }
 }
 
 void
@@ -5839,16 +5916,16 @@ Editor::place_transient()
        if (rs.empty()) {
                return;
        }
-       
+
        framepos_t where = get_preferred_edit_position();
 
        _session->begin_reversible_command (_("place transient"));
-       
+
        for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
                framepos_t position = (*r)->region()->position();
                (*r)->region()->add_transient(where - position);
        }
-       
+
        _session->commit_reversible_command ();
 }
 
@@ -5859,36 +5936,57 @@ Editor::remove_transient(ArdourCanvas::Item* item)
                return;
        }
 
-       ArdourCanvas::SimpleLine* _line = reinterpret_cast<ArdourCanvas::SimpleLine*> (item);
+       ArdourCanvas::Line* _line = reinterpret_cast<ArdourCanvas::Line*> (item);
        assert (_line);
 
        AudioRegionView* _arv = reinterpret_cast<AudioRegionView*> (item->get_data ("regionview"));
-       _arv->remove_transient(_line->property_x1());
+       _arv->remove_transient (*(float*) _line->get_data ("position"));
 }
 
 void
 Editor::snap_regions_to_grid ()
 {
+       list <boost::shared_ptr<Playlist > > used_playlists;
+
        RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (!_session || rs.empty()) {
                return;
        }
-       
+
        _session->begin_reversible_command (_("snap regions to grid"));
-       
+
        for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
+
+               boost::shared_ptr<Playlist> pl = (*r)->region()->playlist();
+
+               if (!pl->frozen()) {
+                       /* we haven't seen this playlist before */
+
+                       /* remember used playlists so we can thaw them later */
+                       used_playlists.push_back(pl);
+                       pl->freeze();
+               }
+
                framepos_t start_frame = (*r)->region()->first_frame ();
                snap_to (start_frame);
-               (*r)->region()->set_position (start_frame, this);
+               (*r)->region()->set_position (start_frame);
        }
-       
+
+       while (used_playlists.size() > 0) {
+               list <boost::shared_ptr<Playlist > >::iterator i = used_playlists.begin();
+               (*i)->thaw();
+               used_playlists.pop_front();
+       }
+
        _session->commit_reversible_command ();
 }
 
 void
 Editor::close_region_gaps ()
-{      
+{
+       list <boost::shared_ptr<Playlist > > used_playlists;
+
        RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (!_session || rs.empty()) {
@@ -5903,11 +6001,11 @@ Editor::close_region_gaps ()
        Label* l = manage (new Label (_("Crossfade length")));
        l->set_alignment (0, 0.5);
        table.attach (*l, 0, 1, 0, 1);
-       
+
        SpinButton spin_crossfade (1, 0);
        spin_crossfade.set_range (0, 15);
        spin_crossfade.set_increments (1, 1);
-       spin_crossfade.set_value (3);
+       spin_crossfade.set_value (5);
        table.attach (spin_crossfade, 1, 2, 0, 1);
 
        table.attach (*manage (new Label (_("ms"))), 2, 3, 0, 1);
@@ -5915,15 +6013,15 @@ Editor::close_region_gaps ()
        l = manage (new Label (_("Pull-back length")));
        l->set_alignment (0, 0.5);
        table.attach (*l, 0, 1, 1, 2);
-       
+
        SpinButton spin_pullback (1, 0);
-       spin_pullback.set_range (0, 15);
+       spin_pullback.set_range (0, 100);
        spin_pullback.set_increments (1, 1);
-       spin_pullback.set_value (5);
+       spin_pullback.set_value(30);
        table.attach (spin_pullback, 1, 2, 1, 2);
 
        table.attach (*manage (new Label (_("ms"))), 2, 3, 1, 2);
-       
+
        dialog.get_vbox()->pack_start (table);
        dialog.add_button (Stock::CANCEL, RESPONSE_CANCEL);
        dialog.add_button (_("Ok"), RESPONSE_ACCEPT);
@@ -5933,39 +6031,55 @@ Editor::close_region_gaps ()
                return;
        }
 
-       framepos_t crossfade_len = spin_crossfade.get_value(); 
+       framepos_t crossfade_len = spin_crossfade.get_value();
        framepos_t pull_back_frames = spin_pullback.get_value();
 
        crossfade_len = lrintf (crossfade_len * _session->frame_rate()/1000);
        pull_back_frames = lrintf (pull_back_frames * _session->frame_rate()/1000);
 
        /* Iterate over the region list and make adjacent regions overlap by crossfade_len_ms */
-       
+
        _session->begin_reversible_command (_("close region gaps"));
-               
+
        int idx = 0;
        boost::shared_ptr<Region> last_region;
-       
+
        rs.sort_by_position_and_track();
-       
+
        for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
 
+               boost::shared_ptr<Playlist> pl = (*r)->region()->playlist();
+
+               if (!pl->frozen()) {
+                       /* we haven't seen this playlist before */
+
+                       /* remember used playlists so we can thaw them later */
+                       used_playlists.push_back(pl);
+                       pl->freeze();
+               }
+
                framepos_t position = (*r)->region()->position();
-         
+
                if (idx == 0 || position < last_region->position()){
                        last_region = (*r)->region();
                        idx++;
                        continue;
                }
-               
-               (*r)->region()->trim_front( (position - pull_back_frames), this );
-               last_region->trim_end( (position - pull_back_frames + crossfade_len), this );
-               
+
+               (*r)->region()->trim_front( (position - pull_back_frames));
+               last_region->trim_end( (position - pull_back_frames + crossfade_len));
+
                last_region = (*r)->region();
-               
+
                idx++;
        }
-       
+
+       while (used_playlists.size() > 0) {
+               list <boost::shared_ptr<Playlist > >::iterator i = used_playlists.begin();
+               (*i)->thaw();
+               used_playlists.pop_front();
+       }
+
        _session->commit_reversible_command ();
 }
 
@@ -5984,7 +6098,12 @@ Editor::tab_to_transient (bool forward)
 
        if (!selection->tracks.empty()) {
 
-               for (TrackSelection::iterator t = selection->tracks.begin(); t != selection->tracks.end(); ++t) {
+               /* don't waste time searching for transients in duplicate playlists.
+                */
+
+               TrackViewList ts = selection->tracks.filter_to_unique_playlists ();
+
+               for (TrackViewList::iterator t = ts.begin(); t != ts.end(); ++t) {
 
                        RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*t);
 
@@ -6075,7 +6194,7 @@ Editor::set_track_height (Height h)
        TrackSelection& ts (selection->tracks);
 
        for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
-               (*x)->set_height (h);
+               (*x)->set_height_enum (h);
        }
 }
 
@@ -6098,7 +6217,7 @@ Editor::toggle_tracks_active ()
                                target = !rtv->_route->active();
                                first = false;
                        }
-                       rtv->_route->set_active (target);
+                       rtv->_route->set_active (target, this);
                }
        }
 }
@@ -6154,9 +6273,9 @@ edit your ardour.rc file to set the\n\
                msg.run ();
                return;
        }
-                
+
        if (ntracks + nbusses == 0) {
-               return;
+               return;
        }
 
        if (ntracks > 1) {
@@ -6238,6 +6357,7 @@ Editor::do_insert_time ()
                get_preferred_edit_position(),
                d.distance(),
                opt,
+               d.all_playlists(),
                d.move_glued(),
                d.move_markers(),
                d.move_glued_markers(),
@@ -6247,8 +6367,10 @@ Editor::do_insert_time ()
 }
 
 void
-Editor::insert_time (framepos_t pos, framecnt_t frames, InsertTimeOption opt,
-                    bool ignore_music_glue, bool markers_too, bool glued_markers_too, bool locked_markers_too, bool tempo_too)
+Editor::insert_time (
+       framepos_t pos, framecnt_t frames, InsertTimeOption opt,
+       bool all_playlists, bool ignore_music_glue, bool markers_too, bool glued_markers_too, bool locked_markers_too, bool tempo_too
+       )
 {
        bool commit = false;
 
@@ -6258,26 +6380,49 @@ Editor::insert_time (framepos_t pos, framecnt_t frames, InsertTimeOption opt,
 
        begin_reversible_command (_("insert time"));
 
-       for (TrackSelection::iterator x = selection->tracks.begin(); x != selection->tracks.end(); ++x) {
+       TrackViewList ts = selection->tracks.filter_to_unique_playlists ();
+
+       for (TrackViewList::iterator x = ts.begin(); x != ts.end(); ++x) {
+
                /* regions */
-               boost::shared_ptr<Playlist> pl = (*x)->playlist();
 
-               if (pl) {
+               /* don't operate on any playlist more than once, which could
+                * happen if "all playlists" is enabled, but there is more
+                * than 1 track using playlists "from" a given track.
+                */
 
-                       pl->clear_changes ();
-                       pl->clear_owned_changes ();
+               set<boost::shared_ptr<Playlist> > pl;
+
+               if (all_playlists) {
+                       RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (*x);
+                       if (rtav) {
+                               vector<boost::shared_ptr<Playlist> > all = _session->playlists->playlists_for_track (rtav->track ());
+                               for (vector<boost::shared_ptr<Playlist> >::iterator p = all.begin(); p != all.end(); ++p) {
+                                       pl.insert (*p);
+                               }
+                       }
+               } else {
+                       if ((*x)->playlist ()) {
+                               pl.insert ((*x)->playlist ());
+                       }
+               }
+               
+               for (set<boost::shared_ptr<Playlist> >::iterator i = pl.begin(); i != pl.end(); ++i) {
+
+                       (*i)->clear_changes ();
+                       (*i)->clear_owned_changes ();
 
                        if (opt == SplitIntersected) {
-                               pl->split (pos);
+                               (*i)->split (pos);
                        }
 
-                       pl->shift (pos, frames, (opt == MoveIntersected), ignore_music_glue);
+                       (*i)->shift (pos, frames, (opt == MoveIntersected), ignore_music_glue);
 
                        vector<Command*> cmds;
-                       pl->rdiff (cmds);
+                       (*i)->rdiff (cmds);
                        _session->add_commands (cmds);
-                       
-                       _session->add_command (new StatefulDiffCommand (pl));
+
+                       _session->add_command (new StatefulDiffCommand (*i));
                        commit = true;
                }
 
@@ -6313,7 +6458,7 @@ Editor::insert_time (framepos_t pos, framecnt_t frames, InsertTimeOption opt,
                                        }
                                        moved = true;
                                }
-                               
+
                        }
 
                        if (was_locked) {
@@ -6339,7 +6484,31 @@ Editor::insert_time (framepos_t pos, framecnt_t frames, InsertTimeOption opt,
 void
 Editor::fit_selected_tracks ()
 {
-       fit_tracks (selection->tracks);
+        if (!selection->tracks.empty()) {
+                fit_tracks (selection->tracks);
+        } else {
+                TrackViewList tvl;
+
+                /* no selected tracks - use tracks with selected regions */
+
+                if (!selection->regions.empty()) {
+                        for (RegionSelection::iterator r = selection->regions.begin(); r != selection->regions.end(); ++r) {
+                                tvl.push_back (&(*r)->get_time_axis_view ());
+                        }
+
+                        if (!tvl.empty()) {
+                                fit_tracks (tvl);
+                        }
+                } else if (internal_editing()) {
+                        /* no selected tracks, or regions, but in internal edit mode, so follow the mouse and use
+                           the entered track
+                        */
+                        if (entered_track) {
+                                tvl.push_back (entered_track);
+                                fit_tracks (tvl);
+                        }
+                }
+        }
 }
 
 void
@@ -6350,6 +6519,7 @@ Editor::fit_tracks (TrackViewList & tracks)
        }
 
        uint32_t child_heights = 0;
+       int visible_tracks = 0;
 
        for (TrackSelection::iterator t = tracks.begin(); t != tracks.end(); ++t) {
 
@@ -6358,9 +6528,10 @@ Editor::fit_tracks (TrackViewList & tracks)
                }
 
                child_heights += (*t)->effective_height() - (*t)->current_height();
+               ++visible_tracks;
        }
 
-       uint32_t h = (uint32_t) floor ((_canvas_height - child_heights - canvas_timebars_vsize) / tracks.size());
+       uint32_t h = (uint32_t) floor ((_canvas_height - child_heights - canvas_timebars_vsize) / visible_tracks);
        double first_y_pos = DBL_MAX;
 
        if (h < TimeAxisView::preset_height (HeightSmall)) {
@@ -6369,7 +6540,8 @@ Editor::fit_tracks (TrackViewList & tracks)
                return;
        }
 
-       undo_visual_stack.push_back (current_visual_state());
+       undo_visual_stack.push_back (current_visual_state (true));
+       no_save_visual = true;
 
        /* build a list of all tracks, including children */
 
@@ -6401,12 +6573,14 @@ Editor::fit_tracks (TrackViewList & tracks)
                        next_is_selected = false;
                }
 
-               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);
+               if ((*t)->marked_for_display ()) {
+                       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);
+                               }
                        }
                }
 
@@ -6422,7 +6596,7 @@ Editor::fit_tracks (TrackViewList & tracks)
        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());
+       redo_visual_stack.push_back (current_visual_state (true));
 }
 
 void
@@ -6432,7 +6606,9 @@ Editor::save_visual_state (uint32_t n)
                visual_states.push_back (0);
        }
 
-       delete visual_states[n];
+       if (visual_states[n] != 0) {
+               delete visual_states[n];
+       }
 
        visual_states[n] = current_visual_state (true);
        gdk_beep ();
@@ -6455,38 +6631,19 @@ Editor::goto_visual_state (uint32_t n)
 void
 Editor::start_visual_state_op (uint32_t n)
 {
-       if (visual_state_op_connection.empty()) {
-               visual_state_op_connection = Glib::signal_timeout().connect (sigc::bind (sigc::mem_fun (*this, &Editor::end_visual_state_op), n), 1000);
-       }
-}
-
-void
-Editor::cancel_visual_state_op (uint32_t n)
-{
-       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);
-       }
-}
-
-bool
-Editor::end_visual_state_op (uint32_t n)
-{
-       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
+void
+Editor::cancel_visual_state_op (uint32_t n)
+{
+        goto_visual_state (n);
 }
 
 void
@@ -6495,7 +6652,7 @@ Editor::toggle_region_mute ()
        if (_ignore_region_action) {
                return;
        }
-       
+
        RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (rs.empty ()) {
@@ -6507,15 +6664,85 @@ Editor::toggle_region_mute ()
        } else {
                begin_reversible_command (_("mute region"));
        }
-       
+
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
-               
+
                (*i)->region()->playlist()->clear_changes ();
                (*i)->region()->set_muted (!(*i)->region()->muted ());
                _session->add_command (new StatefulDiffCommand ((*i)->region()->playlist()));
-               
+
        }
-       
+
+       commit_reversible_command ();
+}
+
+void
+Editor::combine_regions ()
+{
+       /* foreach track with selected regions, take all selected regions
+          and join them into a new region containing the subregions (as a
+          playlist)
+       */
+
+       typedef set<RouteTimeAxisView*> RTVS;
+       RTVS tracks;
+
+       if (selection->regions.empty()) {
+               return;
+       }
+
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
+
+               if (rtv) {
+                       tracks.insert (rtv);
+               }
+       }
+
+       begin_reversible_command (_("combine regions"));
+
+       vector<RegionView*> new_selection;
+
+       for (RTVS::iterator i = tracks.begin(); i != tracks.end(); ++i) {
+               RegionView* rv;
+
+               if ((rv = (*i)->combine_regions ()) != 0) {
+                       new_selection.push_back (rv);
+               }
+       }
+
+       selection->clear_regions ();
+       for (vector<RegionView*>::iterator i = new_selection.begin(); i != new_selection.end(); ++i) {
+               selection->add (*i);
+       }
+
+       commit_reversible_command ();
+}
+
+void
+Editor::uncombine_regions ()
+{
+       typedef set<RouteTimeAxisView*> RTVS;
+       RTVS tracks;
+
+       if (selection->regions.empty()) {
+               return;
+       }
+
+       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
+               RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&(*i)->get_time_axis_view());
+
+               if (rtv) {
+                       tracks.insert (rtv);
+               }
+       }
+
+       begin_reversible_command (_("uncombine regions"));
+
+       for (RTVS::iterator i = tracks.begin(); i != tracks.end(); ++i) {
+               (*i)->uncombine_regions ();
+       }
+
        commit_reversible_command ();
 }