Fix session range location by hooking into the undo system. Fixes #3654.
[ardour.git] / gtk2_ardour / editor_ops.cc
index 2e5055e755258f4c726da924a3ed478fef31b616..6eb837e5e489ac2c72d972cc60c8ece12b4c9a22 100644 (file)
@@ -55,6 +55,7 @@
 #include "ardour/quantize.h"
 #include "ardour/strip_silence.h"
 #include "ardour/route_group.h"
+#include "ardour/operations.h"
 
 #include "ardour_ui.h"
 #include "editor.h"
 #include "editor_regions.h"
 #include "quantize_dialog.h"
 #include "interthread_progress_window.h"
+#include "insert_time_dialog.h"
+#include "normalize_dialog.h"
+#include "editor_cursors.h"
+#include "mouse_cursors.h"
+#include "patch_change_dialog.h"
 
 #include "i18n.h"
 
@@ -112,7 +118,7 @@ Editor::redo (uint32_t n)
 }
 
 void
-Editor::split_regions_at (nframes64_t where, RegionSelection& regions)
+Editor::split_regions_at (framepos_t where, RegionSelection& regions)
 {
        list <boost::shared_ptr<Playlist > > used_playlists;
 
@@ -156,11 +162,10 @@ Editor::split_regions_at (nframes64_t where, RegionSelection& regions)
 
                boost::shared_ptr<Playlist> pl = (*a)->region()->playlist();
 
-                if (!pl) {
-                        cerr << "region " << (*a)->region()->name() << " has no playlist!\n";
-                        a = tmp;
-                        continue;
-                }
+               if (!pl) {
+                       a = tmp;
+                       continue;
+               }
 
                if (!pl->frozen()) {
                        /* we haven't seen this playlist before */
@@ -171,7 +176,7 @@ Editor::split_regions_at (nframes64_t where, RegionSelection& regions)
                }
 
                if (pl) {
-                        pl->clear_history ();
+                       pl->clear_changes ();
                        pl->split_region ((*a)->region(), where);
                        _session->add_command (new StatefulDiffCommand (pl));
                }
@@ -193,7 +198,7 @@ Editor::select_region_for_operation (int /*dir*/, TimeAxisView **tv)
 {
        RegionView* rv;
        boost::shared_ptr<Region> region;
-       nframes64_t start = 0;
+       framepos_t start = 0;
 
        if (selection->time.start () == selection->time.end_frame ()) {
 
@@ -236,7 +241,7 @@ Editor::extend_selection_to_end_of_region (bool next)
 {
        TimeAxisView *tv;
        boost::shared_ptr<Region> region;
-       nframes64_t start;
+       framepos_t start;
 
        if ((region = select_region_for_operation (next ? 1 : 0, &tv)) == 0) {
                return;
@@ -258,7 +263,7 @@ Editor::extend_selection_to_start_of_region (bool previous)
 {
        TimeAxisView *tv;
        boost::shared_ptr<Region> region;
-       nframes64_t end;
+       framepos_t end;
 
        if ((region = select_region_for_operation (previous ? -1 : 0, &tv)) == 0) {
                return;
@@ -303,13 +308,14 @@ Editor::nudge_backward_release (GdkEventButton* ev)
 void
 Editor::nudge_forward (bool next, bool force_playhead)
 {
-       nframes64_t distance;
-       nframes64_t next_distance;
-       RegionSelection rs;
+       framepos_t distance;
+       framepos_t next_distance;
 
-       get_regions_for_action (rs);
+       if (!_session) {
+               return;
+       }
 
-       if (!_session) return;
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (!force_playhead && !rs.empty()) {
 
@@ -324,7 +330,7 @@ Editor::nudge_forward (bool next, bool force_playhead)
                                distance = next_distance;
                        }
 
-                        r->clear_history ();
+                       r->clear_changes ();
                        r->set_position (r->position() + distance, this);
                        _session->add_command (new StatefulDiffCommand (r));
                }
@@ -351,20 +357,20 @@ Editor::nudge_forward (bool next, bool force_playhead)
                                        if (next) {
                                                distance = next_distance;
                                        }
-                                       if (max_frames - distance > loc->start() + loc->length()) {
+                                       if (max_framepos - distance > loc->start() + loc->length()) {
                                                loc->set_start (loc->start() + distance);
                                        } else {
-                                               loc->set_start (max_frames - loc->length());
+                                               loc->set_start (max_framepos - loc->length());
                                        }
                                } else {
                                        distance = get_nudge_distance (loc->end(), next_distance);
                                        if (next) {
                                                distance = next_distance;
                                        }
-                                       if (max_frames - distance > loc->end()) {
+                                       if (max_framepos - distance > loc->end()) {
                                                loc->set_end (loc->end() + distance);
                                        } else {
-                                               loc->set_end (max_frames);
+                                               loc->set_end (max_framepos);
                                        }
                                }
                                XMLNode& after (loc->get_state());
@@ -383,13 +389,14 @@ Editor::nudge_forward (bool next, bool force_playhead)
 void
 Editor::nudge_backward (bool next, bool force_playhead)
 {
-       nframes64_t distance;
-       nframes64_t next_distance;
-       RegionSelection rs;
+       framepos_t distance;
+       framepos_t next_distance;
 
-       get_regions_for_action (rs);
+       if (!_session) {
+               return;
+       }
 
-       if (!_session) return;
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (!force_playhead && !rs.empty()) {
 
@@ -404,7 +411,7 @@ Editor::nudge_backward (bool next, bool force_playhead)
                                distance = next_distance;
                        }
                         
-                        r->clear_history ();
+                       r->clear_changes ();
 
                        if (r->position() > distance) {
                                r->set_position (r->position() - distance, this);
@@ -476,63 +483,54 @@ Editor::nudge_backward (bool next, bool force_playhead)
 void
 Editor::nudge_forward_capture_offset ()
 {
-       nframes64_t distance;
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
-
-       if (!_session) return;
-
-       if (!rs.empty()) {
-
-               begin_reversible_command (_("nudge forward"));
-
-               distance = _session->worst_output_latency();
-
-               for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
-                       boost::shared_ptr<Region> r ((*i)->region());
-
-                        r->clear_history ();
-                       r->set_position (r->position() + distance, this);
-                       _session->add_command(new StatefulDiffCommand (r));
-               }
-
-               commit_reversible_command ();
+       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);
+               _session->add_command(new StatefulDiffCommand (r));
        }
+       
+       commit_reversible_command ();
 }
 
 void
 Editor::nudge_backward_capture_offset ()
 {
-       nframes64_t distance;
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
-
-       if (!_session) return;
-
-       if (!rs.empty()) {
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
-               begin_reversible_command (_("nudge forward"));
-
-               distance = _session->worst_output_latency();
-
-               for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
-                       boost::shared_ptr<Region> r ((*i)->region());
+       if (!_session || rs.empty()) {
+               return;
+       }
 
-                        r->clear_history ();
+       begin_reversible_command (_("nudge forward"));
+       
+       framepos_t const distance = _session->worst_output_latency();
 
-                       if (r->position() > distance) {
-                               r->set_position (r->position() - distance, this);
-                       } else {
-                               r->set_position (0, this);
-                       }
-                       _session->add_command(new StatefulDiffCommand (r));
+       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);
+               } else {
+                       r->set_position (0, this);
                }
-
-               commit_reversible_command ();
+               _session->add_command(new StatefulDiffCommand (r));
        }
+       
+       commit_reversible_command ();
 }
 
 /* DISPLAY MOTION */
@@ -553,7 +551,7 @@ Editor::move_to_end ()
 void
 Editor::build_region_boundary_cache ()
 {
-       nframes64_t pos = 0;
+       framepos_t pos = 0;
        vector<RegionPoint> interesting_points;
        boost::shared_ptr<Region> r;
        TrackViewList tracks;
@@ -596,8 +594,8 @@ Editor::build_region_boundary_cache ()
 
        while (pos < _session->current_end_frame() && !at_end) {
 
-               nframes64_t rpos;
-               nframes64_t lpos = max_frames;
+               framepos_t rpos;
+               framepos_t lpos = max_framepos;
 
                for (vector<RegionPoint>::iterator p = interesting_points.begin(); p != interesting_points.end(); ++p) {
 
@@ -620,7 +618,6 @@ Editor::build_region_boundary_cache ()
 
                        case SyncPoint:
                                rpos = r->sync_position ();
-                               //r->adjust_to_sync (r->first_frame());
                                break;
 
                        default:
@@ -646,7 +643,7 @@ Editor::build_region_boundary_cache ()
                           to sort later.
                        */
 
-                       vector<nframes64_t>::iterator ri;
+                       vector<framepos_t>::iterator ri;
 
                        for (ri = region_boundary_cache.begin(); ri != region_boundary_cache.end(); ++ri) {
                                if (*ri == rpos) {
@@ -668,20 +665,20 @@ Editor::build_region_boundary_cache ()
 }
 
 boost::shared_ptr<Region>
-Editor::find_next_region (nframes64_t frame, RegionPoint point, int32_t dir, TrackViewList& tracks, TimeAxisView **ontrack)
+Editor::find_next_region (framepos_t frame, RegionPoint point, int32_t dir, TrackViewList& tracks, TimeAxisView **ontrack)
 {
        TrackViewList::iterator i;
-       nframes64_t closest = max_frames;
+       framepos_t closest = max_framepos;
        boost::shared_ptr<Region> ret;
-       nframes64_t rpos = 0;
+       framepos_t rpos = 0;
 
        float track_speed;
-       nframes64_t track_frame;
+       framepos_t track_frame;
        RouteTimeAxisView *rtav;
 
        for (i = tracks.begin(); i != tracks.end(); ++i) {
 
-               nframes64_t distance;
+               framecnt_t distance;
                boost::shared_ptr<Region> r;
 
                track_speed = 1.0f;
@@ -707,7 +704,6 @@ Editor::find_next_region (nframes64_t frame, RegionPoint point, int32_t dir, Tra
 
                case SyncPoint:
                        rpos = r->sync_position ();
-                       // r->adjust_to_sync (r->first_frame());
                        break;
                }
 
@@ -731,17 +727,16 @@ Editor::find_next_region (nframes64_t frame, RegionPoint point, int32_t dir, Tra
        return ret;
 }
 
-nframes64_t
-Editor::find_next_region_boundary (nframes64_t pos, int32_t dir, const TrackViewList& tracks)
+framepos_t
+Editor::find_next_region_boundary (framepos_t pos, int32_t dir, const TrackViewList& tracks)
 {
-       nframes64_t distance = max_frames;
-       nframes64_t current_nearest = -1;
-
+       framecnt_t distance = max_framepos;
+       framepos_t current_nearest = -1;
 
        for (TrackViewList::const_iterator i = tracks.begin(); i != tracks.end(); ++i) {
-               nframes64_t contender;
-               nframes64_t d;
-
+               framepos_t contender;
+               framecnt_t d;
+                
                RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
 
                if (!rtv) {
@@ -763,10 +758,10 @@ Editor::find_next_region_boundary (nframes64_t pos, int32_t dir, const TrackView
        return current_nearest;
 }
 
-nframes64_t
-Editor::get_region_boundary (nframes64_t pos, int32_t dir, bool with_selection, bool only_onscreen)
+framepos_t
+Editor::get_region_boundary (framepos_t pos, int32_t dir, bool with_selection, bool only_onscreen)
 {
-       nframes64_t target;
+       framepos_t target;
        TrackViewList tvl;
 
        if (with_selection && Config->get_region_boundaries_from_selected_tracks()) {
@@ -801,8 +796,8 @@ Editor::get_region_boundary (nframes64_t pos, int32_t dir, bool with_selection,
 void
 Editor::cursor_to_region_boundary (bool with_selection, int32_t dir)
 {
-       nframes64_t pos = playhead_cursor->current_frame;
-       nframes64_t target;
+       framepos_t pos = playhead_cursor->current_frame;
+       framepos_t target;
 
        if (!_session) {
                return;
@@ -836,7 +831,7 @@ void
 Editor::cursor_to_region_point (EditorCursor* cursor, RegionPoint point, int32_t dir)
 {
        boost::shared_ptr<Region> r;
-       nframes64_t pos = cursor->current_frame;
+       framepos_t pos = cursor->current_frame;
 
        if (!_session) {
                return;
@@ -879,7 +874,6 @@ Editor::cursor_to_region_point (EditorCursor* cursor, RegionPoint point, int32_t
 
        case SyncPoint:
                pos = r->sync_position ();
-               // r->adjust_to_sync (r->first_frame());
                break;
        }
 
@@ -916,15 +910,12 @@ Editor::cursor_to_previous_region_point (EditorCursor* cursor, RegionPoint point
 void
 Editor::cursor_to_selection_start (EditorCursor *cursor)
 {
-       nframes64_t pos = 0;
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
+       framepos_t pos = 0;
 
        switch (mouse_mode) {
        case MouseObject:
-               if (!rs.empty()) {
-                       pos = rs.start();
+               if (!selection->regions.empty()) {
+                       pos = selection->regions.start();
                }
                break;
 
@@ -948,15 +939,12 @@ Editor::cursor_to_selection_start (EditorCursor *cursor)
 void
 Editor::cursor_to_selection_end (EditorCursor *cursor)
 {
-       nframes64_t pos = 0;
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
+       framepos_t pos = 0;
 
        switch (mouse_mode) {
        case MouseObject:
-               if (!rs.empty()) {
-                       pos = rs.end_frame();
+               if (!selection->regions.empty()) {
+                       pos = selection->regions.end_frame();
                }
                break;
 
@@ -980,7 +968,7 @@ Editor::cursor_to_selection_end (EditorCursor *cursor)
 void
 Editor::selected_marker_to_region_boundary (bool with_selection, int32_t dir)
 {
-       nframes64_t target;
+       framepos_t target;
        Location* loc;
        bool ignored;
 
@@ -989,7 +977,7 @@ Editor::selected_marker_to_region_boundary (bool with_selection, int32_t dir)
        }
 
        if (selection->markers.empty()) {
-               nframes64_t mouse;
+               framepos_t mouse;
                bool ignored;
 
                if (!mouse_frame (mouse, ignored)) {
@@ -1003,7 +991,7 @@ Editor::selected_marker_to_region_boundary (bool with_selection, int32_t dir)
                return;
        }
 
-       nframes64_t pos = loc->start();
+       framepos_t pos = loc->start();
 
        // so we don't find the current region again..
        if (dir > 0 || pos > 0) {
@@ -1033,7 +1021,7 @@ void
 Editor::selected_marker_to_region_point (RegionPoint point, int32_t dir)
 {
        boost::shared_ptr<Region> r;
-       nframes64_t pos;
+       framepos_t pos;
        Location* loc;
        bool ignored;
 
@@ -1109,7 +1097,7 @@ Editor::selected_marker_to_previous_region_point (RegionPoint point)
 void
 Editor::selected_marker_to_selection_start ()
 {
-       nframes64_t pos = 0;
+       framepos_t pos = 0;
        Location* loc;
        bool ignored;
 
@@ -1121,14 +1109,10 @@ Editor::selected_marker_to_selection_start ()
                return;
        }
 
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
-
        switch (mouse_mode) {
        case MouseObject:
-               if (!rs.empty()) {
-                       pos = rs.start();
+               if (!selection->regions.empty()) {
+                       pos = selection->regions.start();
                }
                break;
 
@@ -1148,7 +1132,7 @@ Editor::selected_marker_to_selection_start ()
 void
 Editor::selected_marker_to_selection_end ()
 {
-       nframes64_t pos = 0;
+       framepos_t pos = 0;
        Location* loc;
        bool ignored;
 
@@ -1160,14 +1144,10 @@ Editor::selected_marker_to_selection_end ()
                return;
        }
 
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
-
        switch (mouse_mode) {
        case MouseObject:
-               if (!rs.empty()) {
-                       pos = rs.end_frame();
+               if (!selection->regions.empty()) {
+                       pos = selection->regions.end_frame();
                }
                break;
 
@@ -1187,18 +1167,18 @@ Editor::selected_marker_to_selection_end ()
 void
 Editor::scroll_playhead (bool forward)
 {
-       nframes64_t pos = playhead_cursor->current_frame;
-       nframes64_t delta = (nframes64_t) floor (current_page_frames() / 0.8);
+       framepos_t pos = playhead_cursor->current_frame;
+       framecnt_t delta = (framecnt_t) floor (current_page_frames() / 0.8);
 
        if (forward) {
-               if (pos == max_frames) {
+               if (pos == max_framepos) {
                        return;
                }
 
-               if (pos < max_frames - delta) {
+               if (pos < max_framepos - delta) {
                        pos += delta ;
                } else {
-                       pos = max_frames;
+                       pos = max_framepos;
                }
 
        } else {
@@ -1220,8 +1200,8 @@ Editor::scroll_playhead (bool forward)
 void
 Editor::playhead_backward ()
 {
-       nframes64_t pos;
-       nframes64_t cnt;
+       framepos_t pos;
+       framepos_t cnt;
        float prefix;
        bool was_floating;
 
@@ -1229,15 +1209,15 @@ Editor::playhead_backward ()
                cnt = 1;
        } else {
                if (was_floating) {
-                       cnt = (nframes64_t) floor (prefix * _session->frame_rate ());
+                       cnt = (framepos_t) floor (prefix * _session->frame_rate ());
                } else {
-                       cnt = (nframes64_t) prefix;
+                       cnt = (framepos_t) prefix;
                }
        }
 
        pos = playhead_cursor->current_frame;
 
-       if ((nframes64_t) pos < cnt) {
+       if ((framepos_t) pos < cnt) {
                pos = 0;
        } else {
                pos -= cnt;
@@ -1254,8 +1234,8 @@ Editor::playhead_backward ()
 void
 Editor::playhead_forward ()
 {
-       nframes64_t pos;
-       nframes64_t cnt;
+       framepos_t pos;
+       framepos_t cnt;
        bool was_floating;
        float prefix;
 
@@ -1263,9 +1243,9 @@ Editor::playhead_forward ()
                cnt = 1;
        } else {
                if (was_floating) {
-                       cnt = (nframes64_t) floor (prefix * _session->frame_rate ());
+                       cnt = (framepos_t) floor (prefix * _session->frame_rate ());
                } else {
-                       cnt = (nframes64_t) floor (prefix);
+                       cnt = (framepos_t) floor (prefix);
                }
        }
 
@@ -1315,8 +1295,8 @@ Editor::cursor_align (bool playhead_to_edit)
 void
 Editor::edit_cursor_backward ()
 {
-       nframes64_t pos;
-       nframes64_t cnt;
+       framepos_t pos;
+       framepos_t cnt;
        float prefix;
        bool was_floating;
 
@@ -1324,9 +1304,9 @@ Editor::edit_cursor_backward ()
                cnt = 1;
        } else {
                if (was_floating) {
-                       cnt = (nframes64_t) floor (prefix * _session->frame_rate ());
+                       cnt = (framepos_t) floor (prefix * _session->frame_rate ());
                } else {
-                       cnt = (nframes64_t) prefix;
+                       cnt = (framepos_t) prefix;
                }
        }
 
@@ -1346,8 +1326,8 @@ Editor::edit_cursor_backward ()
 void
 Editor::edit_cursor_forward ()
 {
-       //nframes64_t pos;
-       nframes64_t cnt;
+       //framepos_t pos;
+       framepos_t cnt;
        bool was_floating;
        float prefix;
 
@@ -1355,9 +1335,9 @@ Editor::edit_cursor_forward ()
                cnt = 1;
        } else {
                if (was_floating) {
-                       cnt = (nframes64_t) floor (prefix * _session->frame_rate ());
+                       cnt = (framepos_t) floor (prefix * _session->frame_rate ());
                } else {
-                       cnt = (nframes64_t) floor (prefix);
+                       cnt = (framepos_t) floor (prefix);
                }
        }
 
@@ -1370,16 +1350,16 @@ Editor::goto_frame ()
 {
        float prefix;
        bool was_floating;
-       nframes64_t frame;
+       framepos_t frame;
 
        if (get_prefix (prefix, was_floating)) {
                return;
        }
 
        if (was_floating) {
-               frame = (nframes64_t) floor (prefix * _session->frame_rate());
+               frame = (framepos_t) floor (prefix * _session->frame_rate());
        } else {
-               frame = (nframes64_t) floor (prefix);
+               frame = (framepos_t) floor (prefix);
        }
 
        _session->request_locate (frame);
@@ -1388,19 +1368,19 @@ Editor::goto_frame ()
 void
 Editor::scroll_backward (float pages)
 {
-       nframes64_t frame;
-       nframes64_t one_page = (nframes64_t) rint (_canvas_width * frames_per_unit);
+       framepos_t frame;
+       framepos_t one_page = (framepos_t) rint (_canvas_width * frames_per_unit);
        bool was_floating;
        float prefix;
-       nframes64_t cnt;
+       framepos_t cnt;
 
        if (get_prefix (prefix, was_floating)) {
-               cnt = (nframes64_t) floor (pages * one_page);
+               cnt = (framepos_t) floor (pages * one_page);
        } else {
                if (was_floating) {
-                       cnt = (nframes64_t) floor (prefix * _session->frame_rate());
+                       cnt = (framepos_t) floor (prefix * _session->frame_rate());
                } else {
-                       cnt = (nframes64_t) floor (prefix * one_page);
+                       cnt = (framepos_t) floor (prefix * one_page);
                }
        }
 
@@ -1416,24 +1396,24 @@ Editor::scroll_backward (float pages)
 void
 Editor::scroll_forward (float pages)
 {
-       nframes64_t frame;
-       nframes64_t one_page = (nframes64_t) rint (_canvas_width * frames_per_unit);
+       framepos_t frame;
+       framepos_t one_page = (framepos_t) rint (_canvas_width * frames_per_unit);
        bool was_floating;
        float prefix;
-       nframes64_t cnt;
+       framepos_t cnt;
 
        if (get_prefix (prefix, was_floating)) {
-               cnt = (nframes64_t) floor (pages * one_page);
+               cnt = (framepos_t) floor (pages * one_page);
        } else {
                if (was_floating) {
-                       cnt = (nframes64_t) floor (prefix * _session->frame_rate());
+                       cnt = (framepos_t) floor (prefix * _session->frame_rate());
                } else {
-                       cnt = (nframes64_t) floor (prefix * one_page);
+                       cnt = (framepos_t) floor (prefix * one_page);
                }
        }
 
-       if (max_frames - cnt < leftmost_frame) {
-               frame = max_frames - cnt;
+       if (max_framepos - cnt < leftmost_frame) {
+               frame = max_framepos - cnt;
        } else {
                frame = leftmost_frame + cnt;
        }
@@ -1536,14 +1516,14 @@ Editor::temporal_zoom (gdouble fpu)
 {
        if (!_session) return;
 
-       nframes64_t current_page = current_page_frames();
-       nframes64_t current_leftmost = leftmost_frame;
-       nframes64_t current_rightmost;
-       nframes64_t current_center;
-       nframes64_t new_page_size;
-       nframes64_t half_page_size;
-       nframes64_t leftmost_after_zoom = 0;
-       nframes64_t where;
+       framepos_t current_page = current_page_frames();
+       framepos_t current_leftmost = leftmost_frame;
+       framepos_t current_rightmost;
+       framepos_t current_center;
+       framepos_t new_page_size;
+       framepos_t half_page_size;
+       framepos_t leftmost_after_zoom = 0;
+       framepos_t where;
        bool in_track_canvas;
        double nfpu;
        double l;
@@ -1556,7 +1536,7 @@ Editor::temporal_zoom (gdouble fpu)
 
        nfpu = fpu;
 
-       new_page_size = (nframes64_t) floor (_canvas_width * nfpu);
+       new_page_size = (framepos_t) floor (_canvas_width * nfpu);
        half_page_size = new_page_size / 2;
 
        switch (zoom_focus) {
@@ -1588,10 +1568,10 @@ Editor::temporal_zoom (gdouble fpu)
 
                if (l < 0) {
                        leftmost_after_zoom = 0;
-               } else if (l > max_frames) {
-                       leftmost_after_zoom = max_frames - new_page_size;
+               } else if (l > max_framepos) {
+                       leftmost_after_zoom = max_framepos - new_page_size;
                } else {
-                       leftmost_after_zoom = (nframes64_t) l;
+                       leftmost_after_zoom = (framepos_t) l;
                }
                break;
 
@@ -1614,10 +1594,10 @@ Editor::temporal_zoom (gdouble fpu)
 
                        if (l < 0) {
                                leftmost_after_zoom = 0;
-                       } else if (l > max_frames) {
-                               leftmost_after_zoom = max_frames - new_page_size;
+                       } else if (l > max_framepos) {
+                               leftmost_after_zoom = max_framepos - new_page_size;
                        } else {
-                               leftmost_after_zoom = (nframes64_t) l;
+                               leftmost_after_zoom = (framepos_t) l;
                        }
                }
 
@@ -1633,10 +1613,10 @@ Editor::temporal_zoom (gdouble fpu)
 
                        if (l < 0) {
                                leftmost_after_zoom = 0;
-                       } else if (l > max_frames) {
-                               leftmost_after_zoom = max_frames - new_page_size;
+                       } else if (l > max_framepos) {
+                               leftmost_after_zoom = max_framepos - new_page_size;
                        } else {
-                               leftmost_after_zoom = (nframes64_t) l;
+                               leftmost_after_zoom = (framepos_t) l;
                        }
 
                } else {
@@ -1655,13 +1635,11 @@ Editor::temporal_zoom (gdouble fpu)
 void
 Editor::temporal_zoom_region (bool both_axes)
 {
-
-       nframes64_t start = max_frames;
-       nframes64_t end = 0;
-       RegionSelection rs;
+       framepos_t start = max_framepos;
+       framepos_t end = 0;
        set<TimeAxisView*> tracks;
 
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (rs.empty()) {
                return;
@@ -1695,9 +1673,9 @@ Editor::temporal_zoom_region (bool both_axes)
                return;
        }
 
-       nframes64_t range = end - start;
+       framepos_t range = end - start;
        double new_fpu = (double)range / (double)_canvas_width;
-       nframes64_t extra_samples = (nframes64_t) floor (one_centimeter_in_pixels * new_fpu);
+       framepos_t extra_samples = (framepos_t) floor (one_centimeter_in_pixels * new_fpu);
 
        if (start > extra_samples) {
                start -= extra_samples;
@@ -1705,10 +1683,10 @@ Editor::temporal_zoom_region (bool both_axes)
                start = 0;
        }
 
-       if (max_frames - extra_samples > end) {
+       if (max_framepos - extra_samples > end) {
                end += extra_samples;
        } else {
-               end = max_frames;
+               end = max_framepos;
        }
 
        if (both_axes) {
@@ -1736,7 +1714,7 @@ 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, true);
                        }
                }
 
@@ -1764,8 +1742,8 @@ Editor::temporal_zoom_selection ()
                return;
        }
 
-       nframes64_t start = selection->time[clicked_selection].start;
-       nframes64_t end = selection->time[clicked_selection].end;
+       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");
 }
@@ -1776,12 +1754,18 @@ Editor::temporal_zoom_session ()
        ENSURE_GUI_THREAD (*this, &Editor::temporal_zoom_session)
 
        if (_session) {
-               temporal_zoom_by_frame (_session->current_start_frame(), _session->current_end_frame(), "zoom to _session");
+               framecnt_t const l = _session->current_end_frame() - _session->current_start_frame();
+               double s = _session->current_start_frame() - l * 0.01;
+               if (s < 0) {
+                       s = 0;
+               }
+               framecnt_t const e = _session->current_end_frame() + l * 0.01;
+               temporal_zoom_by_frame (framecnt_t (s), e, "zoom to _session");
        }
 }
 
 void
-Editor::temporal_zoom_by_frame (nframes64_t start, nframes64_t end, const string & /*op*/)
+Editor::temporal_zoom_by_frame (framepos_t start, framepos_t end, const string & /*op*/)
 {
        if (!_session) return;
 
@@ -1789,13 +1773,13 @@ Editor::temporal_zoom_by_frame (nframes64_t start, nframes64_t end, const string
                return;
        }
 
-       nframes64_t range = end - start;
+       framepos_t range = end - start;
 
        double new_fpu = (double)range / (double)_canvas_width;
 
-       nframes64_t new_page = (nframes64_t) floor (_canvas_width * new_fpu);
-       nframes64_t middle = (nframes64_t) floor( (double)start + ((double)range / 2.0f ));
-       nframes64_t new_leftmost = (nframes64_t) floor( (double)middle - ((double)new_page/2.0f));
+       framepos_t new_page = (framepos_t) floor (_canvas_width * new_fpu);
+       framepos_t middle = (framepos_t) floor( (double)start + ((double)range / 2.0f ));
+       framepos_t new_leftmost = (framepos_t) floor( (double)middle - ((double)new_page/2.0f));
 
        if (new_leftmost > middle) {
                new_leftmost = 0;
@@ -1805,7 +1789,7 @@ Editor::temporal_zoom_by_frame (nframes64_t start, nframes64_t end, const string
 }
 
 void
-Editor::temporal_zoom_to_frame (bool coarser, nframes64_t frame)
+Editor::temporal_zoom_to_frame (bool coarser, framepos_t frame)
 {
        if (!_session) {
                return;
@@ -1827,7 +1811,7 @@ Editor::temporal_zoom_to_frame (bool coarser, nframes64_t frame)
                return;
        }
 
-       nframes64_t new_leftmost = frame - (nframes64_t)range_before;
+       framepos_t new_leftmost = frame - (framepos_t)range_before;
 
        if (new_leftmost > frame) {
                new_leftmost = 0;
@@ -1890,22 +1874,22 @@ Editor::add_location_from_selection ()
                return;
        }
 
-       nframes64_t start = selection->time[clicked_selection].start;
-       nframes64_t end = selection->time[clicked_selection].end;
+       framepos_t start = selection->time[clicked_selection].start;
+       framepos_t end = selection->time[clicked_selection].end;
 
        _session->locations()->next_available_name(rangename,"selection");
-       Location *location = new Location (start, end, rangename, Location::IsRangeMarker);
+       Location *location = new Location (*_session, start, end, rangename, Location::IsRangeMarker);
 
        _session->begin_reversible_command (_("add marker"));
-        XMLNode &before = _session->locations()->get_state();
+       XMLNode &before = _session->locations()->get_state();
        _session->locations()->add (location, true);
-        XMLNode &after = _session->locations()->get_state();
+       XMLNode &after = _session->locations()->get_state();
        _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
        _session->commit_reversible_command ();
 }
 
 void
-Editor::add_location_mark (nframes64_t where)
+Editor::add_location_mark (framepos_t where)
 {
        string markername;
 
@@ -1915,11 +1899,11 @@ Editor::add_location_mark (nframes64_t where)
        if (!choose_new_marker_name(markername)) {
                return;
        }
-       Location *location = new Location (where, where, markername, Location::IsMark);
+       Location *location = new Location (*_session, where, where, markername, Location::IsMark);
        _session->begin_reversible_command (_("add marker"));
-        XMLNode &before = _session->locations()->get_state();
+       XMLNode &before = _session->locations()->get_state();
        _session->locations()->add (location, true);
-        XMLNode &after = _session->locations()->get_state();
+       XMLNode &after = _session->locations()->get_state();
        _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
        _session->commit_reversible_command ();
 }
@@ -1930,27 +1914,24 @@ Editor::add_location_from_playhead_cursor ()
        add_location_mark (_session->audible_frame());
 }
 
+/** Add a range marker around each selected region */
 void
-Editor::add_locations_from_audio_region ()
+Editor::add_locations_from_region ()
 {
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
-
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+       
        if (rs.empty()) {
                return;
        }
 
-       _session->begin_reversible_command (rs.size () > 1 ? _("add markers") : _("add marker"));
+       _session->begin_reversible_command (selection->regions.size () > 1 ? _("add markers") : _("add marker"));
        XMLNode &before = _session->locations()->get_state();
 
-       cerr << "Add locations\n";
-
        for (RegionSelection::iterator i = rs.begin (); i != rs.end (); ++i) {
 
                boost::shared_ptr<Region> region = (*i)->region ();
 
-               Location *location = new Location (region->position(), region->last_frame(), region->name(), Location::IsRangeMarker);
+               Location *location = new Location (*_session, region->position(), region->last_frame(), region->name(), Location::IsRangeMarker);
 
                _session->locations()->add (location, true);
        }
@@ -1960,13 +1941,12 @@ Editor::add_locations_from_audio_region ()
        _session->commit_reversible_command ();
 }
 
+/** Add a single range marker around all selected regions */
 void
-Editor::add_location_from_audio_region ()
+Editor::add_location_from_region ()
 {
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
-
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+       
        if (rs.empty()) {
                return;
        }
@@ -1976,7 +1956,7 @@ Editor::add_location_from_audio_region ()
 
        string markername;
 
-       if (rs.size() > 1) {            // more than one region selected
+       if (rs.size() > 1) {
                _session->locations()->next_available_name(markername, "regions");
        } else {
                RegionView* rv = *(rs.begin());
@@ -1988,10 +1968,8 @@ Editor::add_location_from_audio_region ()
                return;
        }
 
-       cerr << "Add location\n";
-
        // single range spanning all selected
-       Location *location = new Location (rs.start(), rs.end_frame(), markername, Location::IsRangeMarker);
+       Location *location = new Location (*_session, selection->regions.start(), selection->regions.end_frame(), markername, Location::IsRangeMarker);
        _session->locations()->add (location, true);
 
        XMLNode &after = _session->locations()->get_state();
@@ -2089,7 +2067,7 @@ Editor::jump_backward_to_mark ()
 void
 Editor::set_mark ()
 {
-       nframes64_t pos;
+       framepos_t pos;
        float prefix;
        bool was_floating;
        string markername;
@@ -2098,9 +2076,9 @@ Editor::set_mark ()
                pos = _session->audible_frame ();
        } else {
                if (was_floating) {
-                       pos = (nframes64_t) floor (prefix * _session->frame_rate ());
+                       pos = (framepos_t) floor (prefix * _session->frame_rate ());
                } else {
-                       pos = (nframes64_t) floor (prefix);
+                       pos = (framepos_t) floor (prefix);
                }
        }
 
@@ -2108,7 +2086,7 @@ Editor::set_mark ()
        if (!choose_new_marker_name(markername)) {
                return;
        }
-       _session->locations()->add (new Location (pos, 0, markername, Location::IsMark), true);
+       _session->locations()->add (new Location (*_session, pos, 0, markername, Location::IsMark), true);
 }
 
 void
@@ -2116,9 +2094,9 @@ Editor::clear_markers ()
 {
        if (_session) {
                _session->begin_reversible_command (_("clear markers"));
-                XMLNode &before = _session->locations()->get_state();
+               XMLNode &before = _session->locations()->get_state();
                _session->locations()->clear_markers ();
-                XMLNode &after = _session->locations()->get_state();
+               XMLNode &after = _session->locations()->get_state();
                _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
                _session->commit_reversible_command ();
        }
@@ -2129,7 +2107,7 @@ Editor::clear_ranges ()
 {
        if (_session) {
                _session->begin_reversible_command (_("clear ranges"));
-                XMLNode &before = _session->locations()->get_state();
+               XMLNode &before = _session->locations()->get_state();
 
                Location * looploc = _session->locations()->auto_loop_location();
                Location * punchloc = _session->locations()->auto_punch_location();
@@ -2139,7 +2117,7 @@ Editor::clear_ranges ()
                if (looploc) _session->locations()->add (looploc);
                if (punchloc) _session->locations()->add (punchloc);
 
-                XMLNode &after = _session->locations()->get_state();
+               XMLNode &after = _session->locations()->get_state();
                _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
                _session->commit_reversible_command ();
        }
@@ -2149,9 +2127,9 @@ void
 Editor::clear_locations ()
 {
        _session->begin_reversible_command (_("clear locations"));
-        XMLNode &before = _session->locations()->get_state();
+       XMLNode &before = _session->locations()->get_state();
        _session->locations()->clear ();
-        XMLNode &after = _session->locations()->get_state();
+       XMLNode &after = _session->locations()->get_state();
        _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
        _session->commit_reversible_command ();
        _session->locations()->clear ();
@@ -2186,7 +2164,7 @@ Editor::insert_region_list_drag (boost::shared_ptr<Region> region, int x, int y)
 {
        double wx, wy;
        double cx, cy;
-       nframes64_t where;
+       framepos_t where;
        RouteTimeAxisView *rtv = 0;
        boost::shared_ptr<Playlist> playlist;
 
@@ -2220,22 +2198,23 @@ Editor::insert_region_list_drag (boost::shared_ptr<Region> region, int x, int y)
        snap_to (where);
 
        begin_reversible_command (_("insert dragged region"));
-        playlist->clear_history ();
-       playlist->add_region (RegionFactory::create (region), where, 1.0);
+       playlist->clear_changes ();
+       playlist->add_region (RegionFactory::create (region, true), where, 1.0);
        _session->add_command(new StatefulDiffCommand (playlist));
        commit_reversible_command ();
 }
 
 void
-Editor::insert_route_list_drag (boost::shared_ptr<Route> route, int x, int y) {
+Editor::insert_route_list_drag (boost::shared_ptr<Route> route, int x, int y)
+{
        double wx, wy;
        double cx, cy;
-       nframes_t where;
+       framepos_t where;
        RouteTimeAxisView *dest_rtv = 0;
        RouteTimeAxisView *source_rtv = 0;
 
        track_canvas->window_to_world (x, y, wx, wy);
-       wx += _horizontal_position;
+       wx += horizontal_position ();
        wy += vertical_adjustment.get_value();
 
        GdkEvent event;
@@ -2298,8 +2277,8 @@ Editor::insert_region_list_selection (float times)
        }
 
        begin_reversible_command (_("insert region"));
-        playlist->clear_history ();
-       playlist->add_region ((RegionFactory::create (region)), get_preferred_edit_position(), times);
+       playlist->clear_changes ();
+       playlist->add_region ((RegionFactory::create (region, true)), get_preferred_edit_position(), times);
        _session->add_command(new StatefulDiffCommand (playlist));
        commit_reversible_command ();
 }
@@ -2361,8 +2340,8 @@ Editor::play_from_edit_point ()
 void
 Editor::play_from_edit_point_and_return ()
 {
-       nframes64_t start_frame;
-       nframes64_t return_frame;
+       framepos_t start_frame;
+       framepos_t return_frame;
 
        start_frame = get_preferred_edit_position (true);
 
@@ -2392,30 +2371,6 @@ Editor::play_selection ()
        _session->request_play_range (&selection->time, true);
 }
 
-void
-Editor::loop_selected_region ()
-{
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
-
-       if (!rs.empty()) {
-               RegionView *rv = *(rs.begin());
-               Location* tll;
-
-               if ((tll = transport_loop_location()) != 0)  {
-
-                       tll->set (rv->region()->position(), rv->region()->last_frame());
-
-                       // enable looping, reposition and start rolling
-
-                       _session->request_play_loop (true);
-                       _session->request_locate (tll->start(), false);
-                       _session->request_transport_speed (1.0f);
-               }
-       }
-}
-
 void
 Editor::play_location (Location& location)
 {
@@ -2470,7 +2425,7 @@ Editor::lower_region_to_bottom ()
 
 /** Show the region editor for the selected regions */
 void
-Editor::edit_region ()
+Editor::show_region_properties ()
 {
        selection->foreach_regionview (&RegionView::show_region_editor);
 }
@@ -2483,12 +2438,10 @@ Editor::show_midi_list_editor ()
 }
 
 void
-Editor::rename_region()
+Editor::rename_region ()
 {
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
-
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+       
        if (rs.empty()) {
                return;
        }
@@ -2511,7 +2464,7 @@ Editor::rename_region()
        d.set_size_request (300, -1);
        d.set_position (Gtk::WIN_POS_MOUSE);
 
-       entry.set_text (rs.front()->region()->name());
+       entry.set_text (selection->regions.front()->region()->name());
        entry.select_region (0, -1);
 
        entry.signal_activate().connect (sigc::bind (sigc::mem_fun (d, &Dialog::response), RESPONSE_OK));
@@ -2520,17 +2473,19 @@ Editor::rename_region()
 
        entry.grab_focus();
 
-       int ret = d.run();
+       int const ret = d.run();
 
        d.hide ();
 
-       if (ret == RESPONSE_OK) {
-               std::string str = entry.get_text();
-               strip_whitespace_edges (str);
-               if (!str.empty()) {
-                       rs.front()->region()->set_name (str);
-                       _regions->redisplay ();
-               }
+       if (ret != RESPONSE_OK) {
+               return;
+       }
+       
+       std::string str = entry.get_text();
+       strip_whitespace_edges (str);
+       if (!str.empty()) {
+               rs.front()->region()->set_name (str);
+               _regions->redisplay ();
        }
 }
 
@@ -2557,7 +2512,7 @@ Editor::audition_playlist_region_via_route (boost::shared_ptr<Region> region, Ro
 void
 Editor::play_edit_range ()
 {
-       nframes64_t start, end;
+       framepos_t start, end;
 
        if (get_edit_op_range (start, end)) {
                _session->request_bounded_roll (start, end);
@@ -2567,12 +2522,11 @@ Editor::play_edit_range ()
 void
 Editor::play_selected_region ()
 {
-       nframes64_t start = max_frames;
-       nframes64_t end = 0;
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
+       framepos_t start = max_framepos;
+       framepos_t end = 0;
 
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+       
        if (rs.empty()) {
                return;
        }
@@ -2606,17 +2560,17 @@ Editor::region_from_selection ()
                return;
        }
 
-       nframes64_t start = selection->time[clicked_selection].start;
-       nframes64_t end = selection->time[clicked_selection].end;
+       framepos_t start = selection->time[clicked_selection].start;
+       framepos_t end = selection->time[clicked_selection].end;
 
        TrackViewList tracks = get_tracks_for_range_action ();
 
-       nframes64_t selection_cnt = end - start + 1;
+       framepos_t selection_cnt = end - start + 1;
 
        for (TrackSelection::iterator i = tracks.begin(); i != tracks.end(); ++i) {
                boost::shared_ptr<Region> current;
                boost::shared_ptr<Playlist> pl;
-               nframes64_t internal_start;
+               framepos_t internal_start;
                string new_name;
 
                if ((pl = (*i)->playlist()) == 0) {
@@ -2648,15 +2602,15 @@ Editor::create_region_from_selection (vector<boost::shared_ptr<Region> >& new_re
                return;
        }
 
-       nframes64_t start = selection->time[clicked_selection].start;
-       nframes64_t end = selection->time[clicked_selection].end;
+       framepos_t start = selection->time[clicked_selection].start;
+       framepos_t end = selection->time[clicked_selection].end;
 
        sort_track_selection ();
 
        for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
                boost::shared_ptr<Region> current;
                boost::shared_ptr<Playlist> playlist;
-               nframes64_t internal_start;
+               framepos_t internal_start;
                string new_name;
 
                if ((playlist = (*i)->playlist()) == 0) {
@@ -2683,10 +2637,8 @@ Editor::create_region_from_selection (vector<boost::shared_ptr<Region> >& new_re
 void
 Editor::split_multichannel_region ()
 {
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
-
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+       
        if (rs.empty()) {
                return;
        }
@@ -2780,7 +2732,7 @@ Editor::separate_regions_between (const TimeSelection& ts)
 
                                if ((playlist = rtv->playlist()) != 0) {
 
-                                       playlist->clear_history ();
+                                       playlist->clear_changes ();
 
                                        /* XXX need to consider musical time selections here at some point */
 
@@ -2791,10 +2743,11 @@ Editor::separate_regions_between (const TimeSelection& ts)
 
                                                sigc::connection c = rtv->view()->RegionViewAdded.connect (
                                                                sigc::mem_fun(*this, &Editor::collect_new_region_view));
+
                                                latest_regionviews.clear ();
 
-                                               playlist->partition ((nframes64_t)((*t).start * speed),
-                                                               (nframes64_t)((*t).end * speed), true);
+                                               playlist->partition ((framepos_t)((*t).start * speed),
+                                                               (framepos_t)((*t).end * speed), false);
 
                                                c.disconnect ();
 
@@ -2803,11 +2756,20 @@ 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;
+                                                       playlist->rdiff (cmds);
+                                                       _session->add_commands (cmds);
+
+                                                       /* pick up changes to the playlist itself (adds/removes)
+                                                        */
 
                                                        _session->add_command(new StatefulDiffCommand (playlist));
                                                }
@@ -2825,6 +2787,11 @@ Editor::separate_regions_between (const TimeSelection& ts)
        }
 }
 
+struct PlaylistState {
+    boost::shared_ptr<Playlist> playlist;
+    XMLNode*  before;
+};
+
 /** Take tracks from get_tracks_for_range_action and cut any regions
  *  on those tracks so that the tracks are empty over the time
  *  selection.
@@ -2843,8 +2810,8 @@ Editor::separate_region_from_selection ()
 
        } else {
 
-               nframes64_t start;
-               nframes64_t end;
+               framepos_t start;
+               framepos_t end;
 
                if (get_edit_op_range (start, end)) {
 
@@ -2890,6 +2857,80 @@ Editor::separate_regions_using_location (Location& loc)
        separate_regions_between (ts);
 }
 
+/** Separate regions under the selected region */
+void
+Editor::separate_under_selected_regions ()
+{
+       vector<PlaylistState> playlists;
+
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+
+       if (!_session || rs.empty()) {
+               return;
+       }
+
+       begin_reversible_command (_("separate region under"));
+
+       list<boost::shared_ptr<Region> > regions_to_remove;
+
+       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+               // we can't just remove the region(s) in this loop because
+               // this removes them from the RegionSelection, and they thus
+               // disappear from underneath the iterator, and the ++i above
+               // SEGVs in a puzzling fashion.
+
+               // so, first iterate over the regions to be removed from rs and
+               // add them to the regions_to_remove list, and then
+               // iterate over the list to actually remove them.
+
+               regions_to_remove.push_back ((*i)->region());
+       }
+
+       for (list<boost::shared_ptr<Region> >::iterator rl = regions_to_remove.begin(); rl != regions_to_remove.end(); ++rl) {
+
+               boost::shared_ptr<Playlist> playlist = (*rl)->playlist();
+
+               if (!playlist) {
+                       // is this check necessary?
+                       continue;
+               }
+
+               vector<PlaylistState>::iterator i;
+
+               //only take state if this is a new playlist.
+               for (i = playlists.begin(); i != playlists.end(); ++i) {
+                       if ((*i).playlist == playlist) {
+                               break;
+                       }
+               }
+
+               if (i == playlists.end()) {
+
+                       PlaylistState before;
+                       before.playlist = playlist;
+                       before.before = &playlist->get_state();
+
+                       playlist->freeze ();
+                       playlists.push_back(before);
+               }
+
+               //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() );
+       }
+
+       vector<PlaylistState>::iterator pl;
+
+       for (pl = playlists.begin(); pl != playlists.end(); ++pl) {
+               (*pl).playlist->thaw ();
+               _session->add_command(new MementoCommand<Playlist>(*(*pl).playlist, (*pl).before, &(*pl).playlist->get_state()));
+       }
+
+       commit_reversible_command ();
+}
+
 void
 Editor::crop_region_to_selection ()
 {
@@ -2899,8 +2940,8 @@ Editor::crop_region_to_selection ()
 
        } else {
 
-               nframes64_t start;
-               nframes64_t end;
+               framepos_t start;
+               framepos_t end;
 
                if (get_edit_op_range (start, end)) {
                        crop_region_to (start, end);
@@ -2910,7 +2951,7 @@ Editor::crop_region_to_selection ()
 }
 
 void
-Editor::crop_region_to (nframes64_t start, nframes64_t end)
+Editor::crop_region_to (framepos_t start, framepos_t end)
 {
        vector<boost::shared_ptr<Playlist> > playlists;
        boost::shared_ptr<Playlist> playlist;
@@ -2944,9 +2985,9 @@ Editor::crop_region_to (nframes64_t start, nframes64_t end)
                return;
        }
 
-       nframes64_t the_start;
-       nframes64_t the_end;
-       nframes64_t cnt;
+       framepos_t the_start;
+       framepos_t the_end;
+       framepos_t cnt;
 
        begin_reversible_command (_("trim to selection"));
 
@@ -2964,16 +3005,16 @@ Editor::crop_region_to (nframes64_t start, nframes64_t end)
                   if the selection extends beyond the region
                */
 
-               the_start = max (the_start, (nframes64_t) region->position());
-               if (max_frames - the_start < region->length()) {
+               the_start = max (the_start, (framepos_t) region->position());
+               if (max_framepos - the_start < region->length()) {
                        the_end = the_start + region->length() - 1;
                } else {
-                       the_end = max_frames;
+                       the_end = max_framepos;
                }
                the_end = min (end, the_end);
                cnt = the_end - the_start + 1;
 
-                region->clear_history ();
+               region->clear_changes ();
                region->trim_to (the_start, cnt, this);
                _session->add_command (new StatefulDiffCommand (region));
        }
@@ -2984,18 +3025,15 @@ Editor::crop_region_to (nframes64_t start, nframes64_t end)
 void
 Editor::region_fill_track ()
 {
-       nframes64_t end;
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (!_session || rs.empty()) {
                return;
        }
 
-       end = _session->current_end_frame ();
+       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) {
 
@@ -3013,8 +3051,8 @@ Editor::region_fill_track ()
                        return;
                }
 
-                pl->clear_history ();
-               pl->add_region (RegionFactory::create (region), region->last_frame(), times);
+               pl->clear_changes ();
+               pl->add_region (RegionFactory::create (region, true), region->last_frame(), times);
                _session->add_command (new StatefulDiffCommand (pl));
        }
 
@@ -3037,8 +3075,8 @@ Editor::region_fill_selection ()
                return;
        }
 
-       nframes64_t start = selection->time[clicked_selection].start;
-       nframes64_t end = selection->time[clicked_selection].end;
+       framepos_t start = selection->time[clicked_selection].start;
+       framepos_t end = selection->time[clicked_selection].end;
 
        boost::shared_ptr<Playlist> playlist;
 
@@ -3046,10 +3084,10 @@ Editor::region_fill_selection ()
                return;
        }
 
-       nframes64_t selection_length = end - start;
+       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) {
 
@@ -3057,8 +3095,8 @@ Editor::region_fill_selection ()
                        continue;
                }
 
-                playlist->clear_history ();
-               playlist->add_region (RegionFactory::create (region), start, times);
+               playlist->clear_changes ();
+               playlist->add_region (RegionFactory::create (region, true), start, times);
                _session->add_command (new StatefulDiffCommand (playlist));
        }
 
@@ -3066,16 +3104,13 @@ Editor::region_fill_selection ()
 }
 
 void
-Editor::set_region_sync_from_edit_point ()
+Editor::set_region_sync_position ()
 {
-       nframes64_t where = get_preferred_edit_position ();
-       RegionSelection rs;
-       get_regions_for_action (rs);
-       set_sync_point (where, rs);
+       set_sync_point (get_preferred_edit_position (), get_regions_from_selection_and_edit_point ());
 }
 
 void
-Editor::set_sync_point (nframes64_t where, const RegionSelection& rs)
+Editor::set_sync_point (framepos_t where, const RegionSelection& rs)
 {
        bool in_command = false;
 
@@ -3092,7 +3127,7 @@ Editor::set_sync_point (nframes64_t where, const RegionSelection& rs)
                        in_command = true;
                }
 
-                region->clear_history ();
+               region->clear_changes ();
                region->set_sync_position (where);
                _session->add_command(new StatefulDiffCommand (region));
        }
@@ -3106,73 +3141,66 @@ Editor::set_sync_point (nframes64_t where, const RegionSelection& rs)
 void
 Editor::remove_region_sync ()
 {
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
-
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+       
        if (rs.empty()) {
                return;
        }
 
-       begin_reversible_command (_("remove sync"));
+       begin_reversible_command (_("remove region sync"));
+       
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
 
-                (*i)->region()->clear_history ();
+               (*i)->region()->clear_changes ();
                (*i)->region()->clear_sync_position ();
                _session->add_command(new StatefulDiffCommand ((*i)->region()));
        }
+       
        commit_reversible_command ();
 }
 
 void
-Editor::naturalize ()
+Editor::naturalize_region ()
 {
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (rs.empty()) {
                return;
        }
 
-       begin_reversible_command (_("naturalize"));
+       if (rs.size() > 1) {
+               begin_reversible_command (_("move regions to original position"));
+       } else {
+               begin_reversible_command (_("move region to original position"));
+       }
+               
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
-               (*i)->region()->clear_history ();
+               (*i)->region()->clear_changes ();
                (*i)->region()->move_to_natural_position (this);
                _session->add_command (new StatefulDiffCommand ((*i)->region()));
        }
+       
        commit_reversible_command ();
 }
 
 void
-Editor::align (RegionPoint what)
+Editor::align_regions (RegionPoint what)
 {
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
-       nframes64_t where = get_preferred_edit_position();
-
-       if (!rs.empty()) {
-               align_selection (what, where, rs);
-       } else {
-
-               RegionSelection rs;
-               get_regions_at (rs, where, selection->tracks);
-               align_selection (what, where, rs);
+       RegionSelection const rs = get_regions_from_selection_and_edit_point ();
+       
+       if (rs.empty()) {
+               return;
        }
-}
 
-void
-Editor::align_relative (RegionPoint what)
-{
-       nframes64_t where = get_preferred_edit_position();
-       RegionSelection rs;
+       begin_reversible_command (_("align selection"));
 
-       get_regions_for_action (rs);
+       framepos_t const position = get_preferred_edit_position ();
 
-       if (!rs.empty()) {
-               align_selection_relative (what, where, rs);
+       for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
+               align_region_internal ((*i)->region(), what, position);
        }
+
+       commit_reversible_command ();
 }
 
 struct RegionSortByTime {
@@ -3182,14 +3210,18 @@ struct RegionSortByTime {
 };
 
 void
-Editor::align_selection_relative (RegionPoint point, nframes64_t position, const RegionSelection& rs)
+Editor::align_regions_relative (RegionPoint point)
 {
+       RegionSelection const rs = get_regions_from_selection_and_edit_point ();
+       
        if (rs.empty()) {
                return;
        }
 
-       nframes64_t distance = 0;
-       nframes64_t pos = 0;
+       framepos_t const position = get_preferred_edit_position ();
+
+       framepos_t distance = 0;
+       framepos_t pos = 0;
        int dir = 1;
 
        list<RegionView*> sorted;
@@ -3238,7 +3270,7 @@ Editor::align_selection_relative (RegionPoint point, nframes64_t position, const
 
        /* move first one specially */
 
-        r->clear_history ();
+       r->clear_changes ();
        r->set_position (pos, this);
        _session->add_command(new StatefulDiffCommand (r));
 
@@ -3250,7 +3282,7 @@ Editor::align_selection_relative (RegionPoint point, nframes64_t position, const
 
                boost::shared_ptr<Region> region ((*i)->region());
 
-                region->clear_history ();
+               region->clear_changes ();
 
                if (dir > 0) {
                        region->set_position (region->position() + distance, this);
@@ -3266,23 +3298,7 @@ Editor::align_selection_relative (RegionPoint point, nframes64_t position, const
 }
 
 void
-Editor::align_selection (RegionPoint point, nframes64_t position, const RegionSelection& rs)
-{
-       if (rs.empty()) {
-               return;
-       }
-
-       begin_reversible_command (_("align selection"));
-
-       for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
-               align_region_internal ((*i)->region(), point, position);
-       }
-
-       commit_reversible_command ();
-}
-
-void
-Editor::align_region (boost::shared_ptr<Region> region, RegionPoint point, nframes64_t position)
+Editor::align_region (boost::shared_ptr<Region> region, RegionPoint point, framepos_t position)
 {
        begin_reversible_command (_("align region"));
        align_region_internal (region, point, position);
@@ -3290,9 +3306,9 @@ Editor::align_region (boost::shared_ptr<Region> region, RegionPoint point, nfram
 }
 
 void
-Editor::align_region_internal (boost::shared_ptr<Region> region, RegionPoint point, nframes64_t position)
+Editor::align_region_internal (boost::shared_ptr<Region> region, RegionPoint point, framepos_t position)
 {
-       region->clear_history ();
+       region->clear_changes ();
 
        switch (point) {
        case SyncPoint:
@@ -3328,25 +3344,31 @@ Editor::trim_region_back ()
 void
 Editor::trim_region (bool front)
 {
-       nframes64_t where = get_preferred_edit_position();
-       RegionSelection rs;
+       framepos_t where = get_preferred_edit_position();
+       RegionSelection rs = get_regions_from_selection_and_edit_point ();
 
-       get_regions_for_action (rs);
+       cerr << "trim regions\n";
 
        if (rs.empty()) {
+               cerr << " no regions\n";
                return;
        }
 
+       cerr << "where = " << where << endl;
+
        begin_reversible_command (front ? _("trim front") : _("trim back"));
 
        for (list<RegionView*>::const_iterator i = rs.by_layer().begin(); i != rs.by_layer().end(); ++i) {
                if (!(*i)->region()->locked()) {
-                        (*i)->region()->clear_history ();
+                        
+                       (*i)->region()->clear_changes ();
+                       
                        if (front) {
                                (*i)->region()->trim_front (where, this);
                        } else {
                                (*i)->region()->trim_end (where, this);
                        }
+                       
                        _session->add_command (new StatefulDiffCommand ((*i)->region()));
                }
        }
@@ -3374,12 +3396,11 @@ Editor::trim_region_to_punch ()
        }
        trim_region_to_location (*loc, _("trim to punch"));
 }
+
 void
 Editor::trim_region_to_location (const Location& loc, const char* str)
 {
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        begin_reversible_command (str);
 
@@ -3400,8 +3421,8 @@ Editor::trim_region_to_location (const Location& loc, const char* str)
                }
 
                float speed = 1.0;
-               nframes64_t start;
-               nframes64_t end;
+               framepos_t start;
+               framepos_t end;
 
                if (tav->track() != 0) {
                        speed = tav->track()->speed();
@@ -3410,7 +3431,7 @@ 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_history ();
+               rv->region()->clear_changes ();
                rv->region()->trim_to (start, (end - start), this);
                _session->add_command(new StatefulDiffCommand (rv->region()));
        }
@@ -3419,129 +3440,51 @@ Editor::trim_region_to_location (const Location& loc, const char* str)
 }
 
 void
-Editor::trim_region_to_edit_point ()
+Editor::trim_region_to_previous_region_end ()
+{
+       return trim_to_region(false);
+}
+
+void
+Editor::trim_region_to_next_region_start ()
 {
-       RegionSelection rs;
+       return trim_to_region(true);
+}
 
-       get_regions_for_action (rs);
+void
+Editor::trim_to_region(bool forward)
+{
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
-       nframes64_t where = get_preferred_edit_position();
+       begin_reversible_command (_("trim to region"));
 
-       begin_reversible_command (_("trim region start to edit point"));
+       boost::shared_ptr<Region> next_region;
 
        for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
-               RegionView* rv = (*x);
 
-               /* require region to cover trim */
-               if (!rv->region()->covers (where)) {
+               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*x);
+
+               if (!arv) {
                        continue;
                }
 
-               RouteTimeAxisView* tav = dynamic_cast<RouteTimeAxisView*> (&rv->get_time_axis_view());
-               if (!tav) {
+               AudioTimeAxisView* atav = dynamic_cast<AudioTimeAxisView*> (&arv->get_time_axis_view());
+
+               if (!atav) {
                        return;
                }
 
                float speed = 1.0;
 
-               if (tav->track() != 0) {
-                       speed = tav->track()->speed();
-               }
-
-                rv->region()->clear_history ();
-               rv->region()->trim_end (session_frame_to_track_frame(where, speed), this);
-               _session->add_command(new StatefulDiffCommand (rv->region()));
-       }
-
-       commit_reversible_command ();
-}
-
-void
-Editor::trim_region_from_edit_point ()
-{
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
-
-       nframes64_t where = get_preferred_edit_position();
-
-       begin_reversible_command (_("trim region end to edit point"));
-
-       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
-               RegionView* rv = (*x);
-
-               /* require region to cover trim */
-               if (!rv->region()->covers (where)) {
-                       continue;
-               }
-
-               RouteTimeAxisView* tav = dynamic_cast<RouteTimeAxisView*> (&rv->get_time_axis_view());
-               if (!tav) {
-                       return;
-               }
-
-               float speed = 1.0;
-
-               if (tav->track() != 0) {
-                       speed = tav->track()->speed();
-               }
-
-                rv->region()->clear_history ();
-               rv->region()->trim_front (session_frame_to_track_frame(where, speed), this);
-               _session->add_command(new StatefulDiffCommand (rv->region()));
-       }
-
-       commit_reversible_command ();
-}
-
-void
-Editor::trim_region_to_previous_region_end ()
-{
-       return trim_to_region(false);
-}
-
-void
-Editor::trim_region_to_next_region_start ()
-{
-       return trim_to_region(true);
-}
-
-void
-Editor::trim_to_region(bool forward)
-{
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
-
-       begin_reversible_command (_("trim to region"));
-
-       boost::shared_ptr<Region> next_region;
-
-       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
-
-               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*x);
-
-               if (!arv) {
-                       continue;
-               }
-
-               AudioTimeAxisView* atav = dynamic_cast<AudioTimeAxisView*> (&arv->get_time_axis_view());
-
-               if (!atav) {
-                       return;
-               }
-
-               float speed = 1.0;
-
-               if (atav->track() != 0) {
-                       speed = atav->track()->speed();
+               if (atav->track() != 0) {
+                       speed = atav->track()->speed();
                }
 
 
                boost::shared_ptr<Region> region = arv->region();
                boost::shared_ptr<Playlist> playlist (region->playlist());
 
-                region->clear_history ();
+               region->clear_changes ();
 
                if(forward){
 
@@ -3551,7 +3494,7 @@ Editor::trim_to_region(bool forward)
                        continue;
                    }
 
-                   region->trim_end((nframes64_t) (next_region->first_frame() * speed), this);
+                   region->trim_end((framepos_t) ( (next_region->first_frame() - 1) * speed), this);
                    arv->region_changed (PropertyChange (ARDOUR::Properties::length));
                }
                else {
@@ -3562,7 +3505,7 @@ Editor::trim_to_region(bool forward)
                        continue;
                    }
 
-                   region->trim_front((nframes64_t) ((next_region->last_frame() + 1) * speed), this);
+                   region->trim_front((framepos_t) ((next_region->last_frame() + 1) * speed), this);
 
                    arv->region_changed (ARDOUR::bounds_change);
                }
@@ -3606,6 +3549,19 @@ Editor::freeze_route ()
                return;
        }
 
+       if (!clicked_routeview->track()->bounceable()) {
+               RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (clicked_routeview);
+               if (rtv && !rtv->track()->bounceable()) {
+                       MessageDialog d (
+                               _("This route cannot be frozen because it has more outputs than inputs.  "
+                                 "You can fix this by increasing the number of inputs.")
+                               );
+                       d.set_title (_("Cannot freeze"));
+                       d.run ();
+                       return;
+               }
+       }
+
        InterThreadInfo itt;
        current_interthread_info = &itt;
 
@@ -3613,14 +3569,14 @@ Editor::freeze_route ()
 
        pthread_create_and_store (X_("freezer"), &itt.thread, _freeze_thread, this);
 
-       track_canvas->get_window()->set_cursor (Gdk::Cursor (Gdk::WATCH));
+       set_canvas_cursor (_cursors->wait);
 
        while (!itt.done && !itt.cancel) {
                gtk_main_iteration ();
        }
 
        current_interthread_info = 0;
-       track_canvas->get_window()->set_cursor (*current_canvas_cursor);
+       set_canvas_cursor (current_canvas_cursor);
 }
 
 void
@@ -3632,9 +3588,22 @@ Editor::bounce_range_selection (bool replace, bool enable_processing)
 
        TrackSelection views = selection->tracks;
 
-       nframes64_t start = selection->time[clicked_selection].start;
-       nframes64_t end = selection->time[clicked_selection].end;
-       nframes64_t cnt = end - start + 1;
+       for (TrackViewList::iterator i = views.begin(); i != views.end(); ++i) {
+               RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
+               if (rtv && !rtv->track()->bounceable()) {
+                       MessageDialog d (
+                               _("One or more selected tracks cannot be bounced because it has more outputs than inputs.  "
+                                 "You can fix this by increasing the number of inputs on that track.")
+                               );
+                       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;
 
        begin_reversible_command (_("bounce range"));
 
@@ -3654,7 +3623,9 @@ Editor::bounce_range_selection (bool replace, bool enable_processing)
 
                InterThreadInfo itt;
 
-                playlist->clear_history ();
+               playlist->clear_changes ();
+               playlist->clear_owned_changes ();
+               
                boost::shared_ptr<Region> r = rtv->track()->bounce_range (start, start+cnt, itt, enable_processing);
 
                if (replace) {
@@ -3664,6 +3635,10 @@ Editor::bounce_range_selection (bool replace, bool enable_processing)
                        playlist->add_region (r, start);
                }
 
+               vector<Command*> cmds;
+               playlist->rdiff (cmds);
+               _session->add_commands (cmds);
+
                _session->add_command (new StatefulDiffCommand (playlist));
        }
 
@@ -3780,7 +3755,7 @@ Editor::cut_copy (CutCopyOp op)
                /* we only want to cut regions if some are selected */
 
                if (!selection->regions.empty()) {
-                       get_regions_for_action (rs, false, false);
+                       rs = selection->regions;
                }
 
                switch (current_mouse_mode()) {
@@ -3816,7 +3791,7 @@ Editor::cut_copy (CutCopyOp op)
 
                case MouseRange:
                        if (selection->time.empty()) {
-                               nframes64_t start, end;
+                               framepos_t start, end;
                                if (!get_edit_op_range (start, end)) {
                                        return;
                                }
@@ -3871,6 +3846,14 @@ Editor::cut_copy_midi (CutCopyOp op)
        }
 }
 
+
+
+struct lt_playlist {
+    bool operator () (const PlaylistState& a, const PlaylistState& b) {
+           return a.playlist < b.playlist;
+    }
+};
+
 struct PlaylistMapping {
     TimeAxisView* tv;
     boost::shared_ptr<Playlist> pl;
@@ -3889,7 +3872,7 @@ Editor::remove_clicked_region ()
        boost::shared_ptr<Playlist> playlist = clicked_routeview->playlist();
 
        begin_reversible_command (_("remove region"));
-        playlist->clear_history ();
+       playlist->clear_changes ();
        playlist->remove_region (clicked_regionview->region());
        _session->add_command(new StatefulDiffCommand (playlist));
        commit_reversible_command ();
@@ -3900,14 +3883,9 @@ Editor::remove_clicked_region ()
 void
 Editor::remove_selected_regions ()
 {
-       RegionSelection rs;
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
-       if (!_session) {
-               return;
-       }
-
-       if (rs.empty()) {
+       if (!_session || rs.empty()) {
                return;
        }
 
@@ -3928,7 +3906,7 @@ Editor::remove_selected_regions ()
                regions_to_remove.push_back ((*i)->region());
        }
 
-        vector<boost::shared_ptr<Playlist> > playlists;
+       vector<boost::shared_ptr<Playlist> > playlists;
 
        for (list<boost::shared_ptr<Region> >::iterator rl = regions_to_remove.begin(); rl != regions_to_remove.end(); ++rl) {
 
@@ -3950,7 +3928,7 @@ Editor::remove_selected_regions ()
 
                if (i == playlists.end()) {
 
-                        playlist->clear_history ();
+                       playlist->clear_changes ();
                        playlist->freeze ();
 
                        playlists.push_back (playlist);
@@ -3981,10 +3959,10 @@ Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
 
        vector<PlaylistMapping> pmap;
 
-       nframes64_t first_position = max_frames;
+       framepos_t first_position = max_framepos;
 
        typedef set<boost::shared_ptr<Playlist> > FreezeList;
-        FreezeList freezelist;
+       FreezeList freezelist;
 
        /* get ordering correct before we cut/copy */
 
@@ -3992,7 +3970,7 @@ Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
 
        for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
 
-               first_position = min ((nframes64_t) (*x)->region()->position(), first_position);
+               first_position = min ((framepos_t) (*x)->region()->position(), first_position);
 
                if (op == Cut || op == Clear) {
                        boost::shared_ptr<Playlist> pl = (*x)->region()->playlist();
@@ -4008,14 +3986,14 @@ Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
                                }
 
                                if (fl == freezelist.end()) {
-                                        pl->clear_history();
+                                       pl->clear_changes();
                                        pl->freeze ();
                                        freezelist.insert (pl);
                                }
                        }
                }
 
-               TimeAxisView* tv = &(*x)->get_trackview();
+               TimeAxisView* tv = &(*x)->get_time_axis_view();
                vector<PlaylistMapping>::iterator z;
 
                for (z = pmap.begin(); z != pmap.end(); ++z) {
@@ -4034,11 +4012,14 @@ Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
                boost::shared_ptr<Playlist> pl = (*x)->region()->playlist();
 
                if (!pl) {
-                       /* impossible, but this handles it for the future */
+                       /* region not yet associated with a playlist (e.g. unfinished
+                          capture pass.
+                       */
+                       ++x;
                        continue;
                }
 
-               TimeAxisView& tv = (*x)->get_trackview();
+               TimeAxisView& tv = (*x)->get_time_axis_view();
                boost::shared_ptr<Playlist> npl;
                RegionSelection::iterator tmp;
 
@@ -4093,14 +4074,21 @@ Editor::cut_copy_regions (CutCopyOp op, RegionSelection& rs)
        /* the pmap is in the same order as the tracks in which selected regions occured */
 
        for (vector<PlaylistMapping>::iterator i = pmap.begin(); i != pmap.end(); ++i) {
-               (*i).pl->thaw();
-               foo.push_back ((*i).pl);
+               if ((*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;
+       }
         
        for (FreezeList::iterator pl = freezelist.begin(); pl != freezelist.end(); ++pl) {
                (*pl)->thaw ();
@@ -4138,7 +4126,7 @@ Editor::paste (float times)
 void
 Editor::mouse_paste ()
 {
-       nframes64_t where;
+       framepos_t where;
        bool ignored;
 
        if (!mouse_frame (where, ignored)) {
@@ -4150,7 +4138,7 @@ Editor::mouse_paste ()
 }
 
 void
-Editor::paste_internal (nframes64_t position, float times)
+Editor::paste_internal (framepos_t position, float times)
 {
        bool commit = false;
 
@@ -4164,11 +4152,11 @@ Editor::paste_internal (nframes64_t position, float times)
                }
        }
 
-       if (position == max_frames) {
+       if (position == max_framepos) {
                position = get_preferred_edit_position();
        }
 
-       begin_reversible_command (_("paste"));
+       begin_reversible_command (Operations::paste);
 
        TrackViewList ts;
        TrackViewList::iterator i;
@@ -4177,10 +4165,14 @@ Editor::paste_internal (nframes64_t position, float times)
        /* get everything in the correct order */
 
        if (!selection->tracks.empty()) {
+               /* there are some selected tracks, so paste to them */
                sort_track_selection ();
                ts = selection->tracks;
-       } else if (entered_track) {
-               ts.push_back (entered_track);
+       } else if (_last_cut_copy_source_track) {
+               /* otherwise paste to the track that the cut/copy came from;
+                  see discussion in mants #3333.
+               */
+               ts.push_back (_last_cut_copy_source_track);
        }
 
        for (nth = 0, i = ts.begin(); i != ts.end(); ++i, ++nth) {
@@ -4224,10 +4216,10 @@ Editor::duplicate_some_regions (RegionSelection& regions, float times)
        RegionSelection sel = regions; // clear (below) may  clear the argument list if its the current region selection
        RegionSelection foo;
 
-       nframes_t const start_frame = regions.start ();
-       nframes_t const end_frame = regions.end_frame ();
+       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 ();
 
@@ -4241,8 +4233,8 @@ Editor::duplicate_some_regions (RegionSelection& regions, float times)
                sigc::connection c = rtv->view()->RegionViewAdded.connect (sigc::mem_fun(*this, &Editor::collect_new_region_view));
 
                playlist = (*i)->region()->playlist();
-                playlist->clear_history ();
-               playlist->duplicate (r, end_frame + (r->first_frame() - start_frame) + 1, times);
+               playlist->clear_changes ();
+               playlist->duplicate (r, end_frame + (r->first_frame() - start_frame), times);
                _session->add_command(new StatefulDiffCommand (playlist));
 
                c.disconnect ();
@@ -4282,7 +4274,7 @@ Editor::duplicate_selection (float times)
                if ((playlist = (*i)->playlist()) == 0) {
                        continue;
                }
-                playlist->clear_history ();
+               playlist->clear_changes ();
                playlist->duplicate (*ri, selection->time[clicked_selection].end, times);
                _session->add_command (new StatefulDiffCommand (playlist));
 
@@ -4324,23 +4316,22 @@ Editor::center_edit_point ()
        center_screen_internal (get_preferred_edit_position(), page);
 }
 
+/** Caller must begin and commit a reversible command */
 void
 Editor::clear_playlist (boost::shared_ptr<Playlist> playlist)
 {
-       begin_reversible_command (_("clear playlist"));
-        playlist->clear_history ();
+       playlist->clear_changes ();
        playlist->clear ();
        _session->add_command (new StatefulDiffCommand (playlist));
-       commit_reversible_command ();
 }
 
 void
 Editor::nudge_track (bool use_edit, bool forwards)
 {
        boost::shared_ptr<Playlist> playlist;
-       nframes64_t distance;
-       nframes64_t next_distance;
-       nframes64_t start;
+       framepos_t distance;
+       framepos_t next_distance;
+       framepos_t start;
 
        if (use_edit) {
                start = get_preferred_edit_position();
@@ -4364,20 +4355,17 @@ Editor::nudge_track (bool use_edit, bool forwards)
                        continue;
                }
 
-                playlist->clear_history ();
-                playlist->clear_owned_history ();
+               playlist->clear_changes ();
+               playlist->clear_owned_changes ();
 
                playlist->nudge_after (start, distance, forwards);
-                
-                vector<StatefulDiffCommand*> cmds;
-
-                playlist->rdiff (cmds);
-
-                for (vector<StatefulDiffCommand*>::iterator c = cmds.begin(); c != cmds.end(); ++c) {
-                        _session->add_command (*c);
-                }
-
-                _session->add_command (new StatefulDiffCommand (playlist));
+               
+               vector<Command*> cmds;
+               
+               playlist->rdiff (cmds);
+               _session->add_commands (cmds);
+               
+               _session->add_command (new StatefulDiffCommand (playlist));
        }
 
        commit_reversible_command ();
@@ -4404,10 +4392,12 @@ Editor::remove_last_capture ()
 
                if (prompter.run () == 1) {
                        _session->remove_last_capture ();
+                       _regions->redisplay ();
                }
 
        } else {
                _session->remove_last_capture();
+               _regions->redisplay ();
        }
 }
 
@@ -4418,54 +4408,69 @@ Editor::normalize_region ()
                return;
        }
 
-       RegionSelection rs;
-       get_regions_for_action (rs);
-
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+       
        if (rs.empty()) {
                return;
        }
 
-       Dialog dialog (rs.size() > 1 ? _("Normalize regions") : _("Normalize region"));
-       HBox hbox;
-       hbox.set_spacing (6);
-       hbox.set_border_width (6);
-       hbox.pack_start (*manage (new Label (_("Normalize to:"))));
-       SpinButton spin (0.2, 2);
-       spin.set_range (-112, 0);
-       spin.set_increments (0.1, 1);
-       spin.set_value (0);
-       hbox.pack_start (spin);
-       spin.set_value (_last_normalization_value);
-       hbox.pack_start (*manage (new Label (_("dbFS"))));
-       hbox.show_all ();
-       dialog.get_vbox()->set_spacing (12);
-       dialog.get_vbox()->pack_start (hbox);
-       dialog.add_button (Stock::CANCEL, RESPONSE_CANCEL);
-       dialog.add_button (_("Normalize"), RESPONSE_ACCEPT);
+       NormalizeDialog dialog (rs.size() > 1);
 
        if (dialog.run () == RESPONSE_CANCEL) {
                return;
        }
 
-       begin_reversible_command (_("normalize"));
-
-       track_canvas->get_window()->set_cursor (*wait_cursor);
+       set_canvas_cursor (_cursors->wait);
        gdk_flush ();
 
+       /* XXX: should really only count audio regions here */
+       int const regions = rs.size ();
+
+       /* Make a list of the selected audio regions' maximum amplitudes, and also
+          obtain the maximum amplitude of them all.
+       */
+       list<double> max_amps;
+       double max_amp = 0;
+       for (RegionSelection::const_iterator i = rs.begin(); i != rs.end(); ++i) {
+               AudioRegionView const * arv = dynamic_cast<AudioRegionView const *> (*i);
+               if (arv) {
+                       dialog.descend (1.0 / regions);
+                       double const a = arv->audio_region()->maximum_amplitude (&dialog);
+
+                       if (a == -1) {
+                               /* the user cancelled the operation */
+                               set_canvas_cursor (current_canvas_cursor);
+                               return;
+                       }
+                       
+                       max_amps.push_back (a);
+                       max_amp = max (max_amp, a);
+                       dialog.ascend ();
+               }
+       }
+
+       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)
+               AudioRegionView* const arv = dynamic_cast<AudioRegionView*> (*r);
+               if (!arv) {
                        continue;
-                arv->region()->clear_history ();
-               arv->audio_region()->normalize_to (spin.get_value());
+               }
+
+               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()));
-                                       
+
+               ++a;
        }
 
        commit_reversible_command ();
-       track_canvas->get_window()->set_cursor (*current_canvas_cursor);
-
-       _last_normalization_value = spin.get_value ();
+       set_canvas_cursor (current_canvas_cursor);
 }
 
 
@@ -4476,9 +4481,7 @@ Editor::reset_region_scale_amplitude ()
                return;
        }
 
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (rs.empty()) {
                return;
@@ -4490,7 +4493,7 @@ Editor::reset_region_scale_amplitude ()
                AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*r);
                if (!arv)
                        continue;
-                arv->region()->clear_history ();
+               arv->region()->clear_changes ();
                arv->audio_region()->set_scale_amplitude (1.0f);
                _session->add_command (new StatefulDiffCommand (arv->region()));
        }
@@ -4499,51 +4502,33 @@ Editor::reset_region_scale_amplitude ()
 }
 
 void
-Editor::adjust_region_scale_amplitude (bool up)
+Editor::adjust_region_gain (bool up)
 {
-       if (!_session) {
-               return;
-       }
-
-       RegionSelection rs;
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
-       get_regions_for_action (rs);
-
-       if (rs.empty()) {
+       if (!_session || rs.empty()) {
                return;
        }
 
-       begin_reversible_command ("denormalize");
+       begin_reversible_command ("adjust region gain");
 
        for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
                AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*r);
                if (!arv) {
                        continue;
-                }
-
-                arv->region()->clear_history ();
-                
-               double fraction = gain_to_slider_position (arv->audio_region()->scale_amplitude ());
-
-               if (up) {
-                       fraction += 0.05;
-                       fraction = min (fraction, 1.0);
-               } else {
-                       fraction -= 0.05;
-                       fraction = max (fraction, 0.0);
                }
 
-               if (!up && fraction <= 0) {
-                       continue;
-               }
+               arv->region()->clear_changes ();
 
-               fraction = slider_position_to_gain (fraction);
+               double dB = accurate_coefficient_to_dB (arv->audio_region()->scale_amplitude ());
 
-               if (up && fraction >= 2.0) {
-                       continue;
+               if (up) {
+                       dB += 1;
+               } else {
+                       dB -= 1;
                }
 
-               arv->audio_region()->set_scale_amplitude (fraction);
+               arv->audio_region()->set_scale_amplitude (dB_to_coefficient (dB));
                _session->add_command (new StatefulDiffCommand (arv->region()));
        }
 
@@ -4569,36 +4554,39 @@ Editor::strip_region_silence ()
                return;
        }
 
-       RegionSelection rs;
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (rs.empty()) {
                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 ());
-               apply_filter (s, _("strip silence"));
-       }
+        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);
+       } 
 }
 
 Command*
 Editor::apply_midi_note_edit_op_to_region (MidiOperator& op, MidiRegionView& mrv)
 {
        Evoral::Sequence<Evoral::MusicalTime>::Notes selected;
-       mrv.selection_as_notelist (selected);
+       mrv.selection_as_notelist (selected, true);
 
        vector<Evoral::Sequence<Evoral::MusicalTime>::Notes> v;
        v.push_back (selected);
@@ -4609,10 +4597,9 @@ Editor::apply_midi_note_edit_op_to_region (MidiOperator& op, MidiRegionView& mrv
 void
 Editor::apply_midi_note_edit_op (MidiOperator& op)
 {
-       RegionSelection rs;
        Command* cmd;
 
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (rs.empty()) {
                return;
@@ -4638,16 +4625,71 @@ Editor::apply_midi_note_edit_op (MidiOperator& op)
        }
 
        commit_reversible_command ();
-       rs.clear ();
+}
+
+void
+Editor::fork_region ()
+{
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+
+       if (rs.empty()) {
+               return;
+       }
+
+       begin_reversible_command (_("Fork Region(s)"));
+
+       set_canvas_cursor (_cursors->wait);
+       gdk_flush ();
+
+       for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ) {
+               RegionSelection::iterator tmp = r;
+               ++tmp;
+
+               MidiRegionView* const mrv = dynamic_cast<MidiRegionView*>(*r);
+
+               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));
+               }
+
+               r = tmp;
+       }
+
+       commit_reversible_command ();
+
+       set_canvas_cursor (current_canvas_cursor);
 }
 
 void
 Editor::quantize_region ()
 {
+       int selected_midi_region_cnt = 0;
+
        if (!_session) {
                return;
        }
 
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+
+       if (rs.empty()) {
+               return;
+       }
+
+       for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ++r) {
+               MidiRegionView* const mrv = dynamic_cast<MidiRegionView*> (*r);
+               if (mrv) {
+                       selected_midi_region_cnt++;
+               }
+       }
+
+       if (selected_midi_region_cnt == 0) {
+               return;
+       }
+
        QuantizeDialog* qd = new QuantizeDialog (*this);
 
        qd->present ();
@@ -4665,11 +4707,36 @@ Editor::quantize_region ()
 }
 
 void
-Editor::apply_filter (Filter& filter, string command)
+Editor::insert_patch_change ()
 {
-       RegionSelection rs;
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+       if (rs.empty ()) {
+               return;
+       }
 
-       get_regions_for_action (rs);
+       framepos_t const p = get_preferred_edit_position (false);
+
+       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)
+{
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (rs.empty()) {
                return;
@@ -4677,9 +4744,12 @@ Editor::apply_filter (Filter& filter, string command)
 
        begin_reversible_command (command);
 
-       track_canvas->get_window()->set_cursor (*wait_cursor);
+       set_canvas_cursor (_cursors->wait);
        gdk_flush ();
 
+       int n = 0;
+       int const N = rs.size ();
+       
        for (RegionSelection::iterator r = rs.begin(); r != rs.end(); ) {
                RegionSelection::iterator tmp = r;
                ++tmp;
@@ -4688,10 +4758,14 @@ Editor::apply_filter (Filter& filter, string command)
                if (arv) {
                        boost::shared_ptr<Playlist> playlist = arv->region()->playlist();
 
-                       if (arv->audio_region()->apply (filter) == 0) {
+                       if (progress) {
+                               progress->descend (1.0 / N);
+                       }
+
+                       if (arv->audio_region()->apply (filter, progress) == 0) {
+
+                               playlist->clear_changes ();
 
-                               playlist->clear_history ();
-                                
                                if (filter.results.empty ()) {
 
                                        /* no regions returned; remove the old one */
@@ -4717,44 +4791,20 @@ Editor::apply_filter (Filter& filter, string command)
                        } else {
                                goto out;
                        }
+
+                       if (progress) {
+                               progress->ascend ();
+                       }
                }
 
                r = tmp;
+               ++n;
        }
 
        commit_reversible_command ();
-       rs.clear ();
 
   out:
-       track_canvas->get_window()->set_cursor (*current_canvas_cursor);
-}
-
-void
-Editor::region_selection_op (void (Region::*pmf)(void))
-{
-       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
-               Region* region = (*i)->region().get();
-               (region->*pmf)();
-       }
-}
-
-
-void
-Editor::region_selection_op (void (Region::*pmf)(void*), void *arg)
-{
-       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
-               Region* region = (*i)->region().get();
-               (region->*pmf)(arg);
-       }
-}
-
-void
-Editor::region_selection_op (void (Region::*pmf)(bool), bool yn)
-{
-       for (RegionSelection::iterator i = selection->regions.begin(); i != selection->regions.end(); ++i) {
-               Region* region = (*i)->region().get();
-               (region->*pmf)(yn);
-       }
+       set_canvas_cursor (current_canvas_cursor);
 }
 
 void
@@ -4764,15 +4814,12 @@ Editor::external_edit_region ()
 }
 
 void
-Editor::brush (nframes64_t pos)
+Editor::brush (framepos_t pos)
 {
-       RegionSelection sel;
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
-
        snap_to (pos);
 
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+
        if (rs.empty()) {
                return;
        }
@@ -4785,7 +4832,7 @@ Editor::brush (nframes64_t pos)
 void
 Editor::reset_region_gain_envelopes ()
 {
-       RegionSelection rs = get_equivalent_regions (selection->regions, ARDOUR::Properties::edit.property_id);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (!_session || rs.empty()) {
                return;
@@ -4810,7 +4857,11 @@ Editor::reset_region_gain_envelopes ()
 void
 Editor::toggle_gain_envelope_visibility ()
 {
-       RegionSelection rs = get_equivalent_regions (selection->regions, ARDOUR::Properties::edit.property_id);
+       if (_ignore_region_action) {
+               return;
+       }
+       
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (!_session || rs.empty()) {
                return;
@@ -4821,7 +4872,7 @@ Editor::toggle_gain_envelope_visibility ()
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
                AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
                if (arv) {
-                        arv->region()->clear_history ();
+                       arv->region()->clear_changes ();
                        arv->set_envelope_visible (!arv->envelope_visible());
                        _session->add_command (new StatefulDiffCommand (arv->region()));
                }
@@ -4833,7 +4884,11 @@ Editor::toggle_gain_envelope_visibility ()
 void
 Editor::toggle_gain_envelope_active ()
 {
-       RegionSelection rs = get_equivalent_regions (selection->regions, ARDOUR::Properties::edit.property_id);
+       if (_ignore_region_action) {
+               return;
+       }
+       
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (!_session || rs.empty()) {
                return;
@@ -4844,7 +4899,7 @@ Editor::toggle_gain_envelope_active ()
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
                AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
                if (arv) {
-                       arv->region()->clear_history ();
+                       arv->region()->clear_changes ();
                        arv->audio_region()->set_envelope_active (!arv->audio_region()->envelope_active());
                        _session->add_command (new StatefulDiffCommand (arv->region()));
                }
@@ -4856,16 +4911,20 @@ Editor::toggle_gain_envelope_active ()
 void
 Editor::toggle_region_lock ()
 {
-       RegionSelection rs = get_equivalent_regions (selection->regions, ARDOUR::Properties::edit.property_id);
+       if (_ignore_region_action) {
+               return;
+       }
+
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (!_session || rs.empty()) {
                return;
        }
 
-       _session->begin_reversible_command (_("region lock"));
+       _session->begin_reversible_command (_("toggle region lock"));
 
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
-               (*i)->region()->clear_history ();
+               (*i)->region()->clear_changes ();
                (*i)->region()->set_locked (!(*i)->region()->locked());
                _session->add_command (new StatefulDiffCommand ((*i)->region()));
        }
@@ -4876,7 +4935,11 @@ Editor::toggle_region_lock ()
 void
 Editor::toggle_region_lock_style ()
 {
-       RegionSelection rs = get_equivalent_regions (selection->regions, ARDOUR::Properties::edit.property_id);
+       if (_ignore_region_action) {
+               return;
+       }
+       
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (!_session || rs.empty()) {
                return;
@@ -4885,8 +4948,8 @@ Editor::toggle_region_lock_style ()
        _session->begin_reversible_command (_("region lock style"));
 
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
-                (*i)->region()->clear_history ();
-               Region::PositionLockStyle const ns = (*i)->region()->positional_lock_style() == Region::AudioTime ? Region::MusicTime : Region::AudioTime;
+               (*i)->region()->clear_changes ();
+               PositionLockStyle const ns = (*i)->region()->position_lock_style() == AudioTime ? MusicTime : AudioTime;
                (*i)->region()->set_position_lock_style (ns);
                _session->add_command (new StatefulDiffCommand ((*i)->region()));
        }
@@ -4894,40 +4957,23 @@ Editor::toggle_region_lock_style ()
        _session->commit_reversible_command ();
 }
 
-
 void
-Editor::toggle_region_mute ()
+Editor::toggle_opaque_region ()
 {
-       RegionSelection rs = get_equivalent_regions (selection->regions, ARDOUR::Properties::edit.property_id);
-
-       if (!_session || rs.empty()) {
+       if (_ignore_region_action) {
                return;
        }
-
-       _session->begin_reversible_command (_("region mute"));
-
-       for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
-               (*i)->region()->clear_history ();
-               (*i)->region()->set_muted (!(*i)->region()->muted());
-               _session->add_command (new StatefulDiffCommand ((*i)->region()));
-       }
-
-       _session->commit_reversible_command ();
-}
-
-void
-Editor::toggle_region_opaque ()
-{
-       RegionSelection rs = get_equivalent_regions (selection->regions, ARDOUR::Properties::edit.property_id);
+       
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (!_session || rs.empty()) {
                return;
        }
 
-       _session->begin_reversible_command (_("region opacity"));
+       _session->begin_reversible_command (_("change region opacity"));
 
        for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
-               (*i)->region()->clear_history ();
+               (*i)->region()->clear_changes ();
                (*i)->region()->set_opaque (!(*i)->region()->opaque());
                _session->add_command (new StatefulDiffCommand ((*i)->region()));
        }
@@ -4952,7 +4998,7 @@ Editor::toggle_record_enable ()
                        first = false;
                }
 
-               rtav->track()->set_record_enable(new_state, this);
+               rtav->track()->set_record_enable(new_state, this);
        }
 }
 
@@ -4960,9 +5006,7 @@ Editor::toggle_record_enable ()
 void
 Editor::set_fade_length (bool in)
 {
-       RegionSelection rs;
-
-       get_regions_for_action (rs, true);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (rs.empty()) {
                return;
@@ -4972,8 +5016,8 @@ Editor::set_fade_length (bool in)
 
        RegionView* rv = rs.front ();
 
-       nframes64_t pos = get_preferred_edit_position();
-       nframes64_t len;
+       framepos_t pos = get_preferred_edit_position();
+       framepos_t len;
        char const * cmd;
 
        if (pos > rv->region()->last_frame() || pos < rv->region()->first_frame()) {
@@ -5031,62 +5075,9 @@ Editor::set_fade_length (bool in)
 }
 
 void
-Editor::toggle_fade_active (bool in)
+Editor::set_fade_in_shape (FadeShape shape)
 {
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
-
-       if (rs.empty()) {
-               return;
-       }
-
-       const char* cmd = (in ? _("toggle fade in active") : _("toggle fade out active"));
-       bool have_switch = false;
-       bool yn = false;
-
-       begin_reversible_command (cmd);
-
-       for (RegionSelection::iterator x = rs.begin(); x != rs.end(); ++x) {
-               AudioRegionView* tmp = dynamic_cast<AudioRegionView*> (*x);
-
-               if (!tmp) {
-                       return;
-               }
-
-               boost::shared_ptr<AudioRegion> region (tmp->audio_region());
-
-               /* make the behaviour consistent across all regions */
-
-               if (!have_switch) {
-                       if (in) {
-                               yn = region->fade_in_active();
-                       } else {
-                               yn = region->fade_out_active();
-                       }
-                       have_switch = true;
-               }
-
-               region->clear_history ();
-
-               if (in) {
-                       region->set_fade_in_active (!yn);
-               } else {
-                       region->set_fade_out_active (!yn);
-               }
-               
-               _session->add_command(new StatefulDiffCommand (region));
-       }
-
-       commit_reversible_command ();
-}
-
-void
-Editor::set_fade_in_shape (AudioRegion::FadeShape shape)
-{
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (rs.empty()) {
                return;
@@ -5115,11 +5106,9 @@ Editor::set_fade_in_shape (AudioRegion::FadeShape shape)
 }
 
 void
-Editor::set_fade_out_shape (AudioRegion::FadeShape shape)
+Editor::set_fade_out_shape (FadeShape shape)
 {
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (rs.empty()) {
                return;
@@ -5149,9 +5138,7 @@ Editor::set_fade_out_shape (AudioRegion::FadeShape shape)
 void
 Editor::set_fade_in_active (bool yn)
 {
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (rs.empty()) {
                return;
@@ -5169,7 +5156,7 @@ Editor::set_fade_in_active (bool yn)
 
                boost::shared_ptr<AudioRegion> ar (tmp->audio_region());
                
-                ar->clear_history ();
+               ar->clear_changes ();
                ar->set_fade_in_active (yn);
                _session->add_command (new StatefulDiffCommand (ar));
        }
@@ -5180,9 +5167,7 @@ Editor::set_fade_in_active (bool yn)
 void
 Editor::set_fade_out_active (bool yn)
 {
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (rs.empty()) {
                return;
@@ -5199,7 +5184,7 @@ Editor::set_fade_out_active (bool yn)
 
                boost::shared_ptr<AudioRegion> ar (tmp->audio_region());
 
-                ar->clear_history ();
+               ar->clear_changes ();
                ar->set_fade_out_active (yn);
                _session->add_command(new StatefulDiffCommand (ar));
        }
@@ -5208,19 +5193,18 @@ Editor::set_fade_out_active (bool yn)
 }
 
 void
-Editor::toggle_selected_region_fades (int dir)
+Editor::toggle_region_fades (int dir)
 {
-       RegionSelection rs;
-       RegionSelection::iterator i;
        boost::shared_ptr<AudioRegion> ar;
        bool yn;
 
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (rs.empty()) {
                return;
        }
 
+       RegionSelection::iterator i;    
        for (i = rs.begin(); i != rs.end(); ++i) {
                if ((ar = boost::dynamic_pointer_cast<AudioRegion>((*i)->region())) != 0) {
                        if (dir == -1) {
@@ -5292,7 +5276,7 @@ Editor::update_xfade_visibility ()
 void
 Editor::set_edit_point ()
 {
-       nframes64_t where;
+       framepos_t where;
        bool ignored;
 
        if (!mouse_frame (where, ignored)) {
@@ -5322,7 +5306,7 @@ Editor::set_playhead_cursor ()
        if (entered_marker) {
                _session->request_locate (entered_marker->position(), _session->transport_rolling());
        } else {
-               nframes64_t where;
+               framepos_t where;
                bool ignored;
 
                if (!mouse_frame (where, ignored)) {
@@ -5338,13 +5322,18 @@ Editor::set_playhead_cursor ()
 }
 
 void
-Editor::split ()
+Editor::split_region ()
 {
-       RegionSelection rs;
+       if (((mouse_mode == MouseRange) || 
+            (mouse_mode != MouseObject && _join_object_range_state == JOIN_OBJECT_RANGE_RANGE)) && 
+           !selection->time.empty()) {
+               separate_regions_between (selection->time);
+               return;
+       } 
 
-       get_regions_for_action (rs, true);
+       RegionSelection rs = get_regions_from_selection_and_edit_point ();
 
-       nframes64_t where = get_preferred_edit_position();
+       framepos_t where = get_preferred_edit_position ();
 
        if (rs.empty()) {
                return;
@@ -5454,7 +5443,7 @@ Editor::ensure_track_visible(TimeAxisView *track)
        double const track_max_y = track->y_position () + track->effective_height ();
 
        if (track_min_y >= current_view_min_y &&
-            track_max_y <= current_view_max_y) {
+           track_max_y <= current_view_max_y) {
                return;
        }
 
@@ -5478,8 +5467,8 @@ Editor::set_loop_from_selection (bool play)
                return;
        }
 
-       nframes64_t start = selection->time[clicked_selection].start;
-       nframes64_t end = selection->time[clicked_selection].end;
+       framepos_t start = selection->time[clicked_selection].start;
+       framepos_t end = selection->time[clicked_selection].end;
 
        set_loop_range (start, end,  _("set loop range from selection"));
 
@@ -5496,8 +5485,8 @@ Editor::set_loop_from_edit_range (bool play)
                return;
        }
 
-       nframes64_t start;
-       nframes64_t end;
+       framepos_t start;
+       framepos_t end;
 
        if (!get_edit_op_range (start, end)) {
                return;
@@ -5514,12 +5503,10 @@ Editor::set_loop_from_edit_range (bool play)
 void
 Editor::set_loop_from_region (bool play)
 {
-       nframes64_t start = max_frames;
-       nframes64_t end = 0;
+       framepos_t start = max_framepos;
+       framepos_t end = 0;
 
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (rs.empty()) {
                return;
@@ -5549,8 +5536,8 @@ Editor::set_punch_from_selection ()
                return;
        }
 
-       nframes64_t start = selection->time[clicked_selection].start;
-       nframes64_t end = selection->time[clicked_selection].end;
+       framepos_t start = selection->time[clicked_selection].start;
+       framepos_t end = selection->time[clicked_selection].end;
 
        set_punch_range (start, end,  _("set punch range from selection"));
 }
@@ -5562,8 +5549,8 @@ Editor::set_punch_from_edit_range ()
                return;
        }
 
-       nframes64_t start;
-       nframes64_t end;
+       framepos_t start;
+       framepos_t end;
 
        if (!get_edit_op_range (start, end)) {
                return;
@@ -5575,12 +5562,10 @@ Editor::set_punch_from_edit_range ()
 void
 Editor::set_punch_from_region ()
 {
-       nframes64_t start = max_frames;
-       nframes64_t end = 0;
-
-       RegionSelection rs;
+       framepos_t start = max_framepos;
+       framepos_t end = 0;
 
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (rs.empty()) {
                return;
@@ -5599,11 +5584,9 @@ Editor::set_punch_from_region ()
 }
 
 void
-Editor::pitch_shift_regions ()
+Editor::pitch_shift_region ()
 {
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
        if (rs.empty()) {
                return;
@@ -5613,17 +5596,11 @@ Editor::pitch_shift_regions ()
 }
 
 void
-Editor::use_region_as_bar ()
+Editor::set_tempo_from_region ()
 {
-       if (!_session) {
-               return;
-       }
-
-       RegionSelection rs;
-
-       get_regions_for_action (rs);
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
-       if (rs.empty()) {
+       if (!_session || rs.empty()) {
                return;
        }
 
@@ -5635,16 +5612,16 @@ Editor::use_region_as_bar ()
 void
 Editor::use_range_as_bar ()
 {
-       nframes64_t start, end;
+       framepos_t start, end;
        if (get_edit_op_range (start, end)) {
                define_one_bar (start, end);
        }
 }
 
 void
-Editor::define_one_bar (nframes64_t start, nframes64_t end)
+Editor::define_one_bar (framepos_t start, framepos_t end)
 {
-       nframes64_t length = end - start;
+       framepos_t length = end - start;
 
        const Meter& m (_session->tempo_map().meter_at (start));
 
@@ -5732,15 +5709,9 @@ Editor::split_region_at_transients ()
 {
        AnalysisFeatureList positions;
 
-       if (!_session) {
-               return;
-       }
-
-       RegionSelection rs;
+       RegionSelection rs = get_regions_from_selection_and_entered ();
 
-       get_regions_for_action (rs);
-
-       if (rs.empty()) {
+       if (!_session || rs.empty()) {
                return;
        }
 
@@ -5784,7 +5755,7 @@ Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList
 
 
        if (positions.size() > 20) {
-               Glib::ustring msgstr = string_compose (_("You are about to split\n%1\ninto %2 pieces.\nThis could take a long time."), r->name(), positions.size() + 1);
+               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,
                                   Gtk::MESSAGE_INFO,
@@ -5802,6 +5773,7 @@ Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList
 
                int response = msg.run();
                msg.hide ();
+               
                switch (response) {
                case RESPONSE_OK:
                        break;
@@ -5820,19 +5792,10 @@ Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList
 
        AnalysisFeatureList::const_iterator x;
 
-       nframes64_t pos = r->position();
-
-        pl->clear_history ();
+       pl->clear_changes ();
 
        x = positions.begin();
 
-       while (x != positions.end()) {
-               if ((*x) > pos) {
-                       break;
-               }
-               ++x;
-       }
-
        if (x == positions.end()) {
                return;
        }
@@ -5840,18 +5803,26 @@ Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList
        pl->freeze ();
        pl->remove_region (r);
 
+       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()){
+                       ++x;
+                       continue;
+               }
 
                /* file start = original start + how far we from the initial position ?
                 */
 
-               nframes64_t file_start = r->start() + (pos - r->position());
+               framepos_t file_start = r->start() + pos;
 
                /* length = next position - current position
                 */
 
-               nframes64_t len = (*x) - pos;
-
+               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?
                */
@@ -5876,35 +5847,173 @@ Editor::split_region_at_points (boost::shared_ptr<Region> r, AnalysisFeatureList
                plist.add (ARDOUR::Properties::layer, 0);
 
                boost::shared_ptr<Region> nr = RegionFactory::create (r->sources(), plist, false);
-               pl->add_region (nr, pos);
+               pl->add_region (nr, r->position() + pos);
 
                pos += len;
                ++x;
+       }
 
-               if (*x > r->last_frame()) {
+       string new_name;
 
-                       /* add final fragment */
+       RegionFactory::region_name (new_name, r->name());
+       
+       /* Add the final region */
+       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);
+       plist.add (ARDOUR::Properties::layer, 0);
 
-                       file_start = r->start() + (pos - r->position());
-                       len = r->last_frame() - pos;
+       boost::shared_ptr<Region> nr = RegionFactory::create (r->sources(), plist, false);
+       pl->add_region (nr, r->position() + pos);
 
-                       PropertyList plist2; 
-                       
-                       plist2.add (ARDOUR::Properties::start, file_start);
-                       plist2.add (ARDOUR::Properties::length, len);
-                       plist2.add (ARDOUR::Properties::name, new_name);
-                       plist2.add (ARDOUR::Properties::layer, 0);
+       
+       pl->thaw ();
 
-                       nr = RegionFactory::create (r->sources(), plist2); 
-                       pl->add_region (nr, pos);
+       _session->add_command (new StatefulDiffCommand (pl));
+}
 
-                       break;
-               }
+void
+Editor::place_transient()
+{
+       if (!_session) {
+               return;
        }
 
-       pl->thaw ();
+       RegionSelection rs = get_regions_from_selection_and_edit_point ();
 
-       _session->add_command (new StatefulDiffCommand (pl));
+       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 ();
+}
+
+void
+Editor::remove_transient(ArdourCanvas::Item* item)
+{
+       if (!_session) {
+               return;
+       }
+
+       ArdourCanvas::SimpleLine* _line = reinterpret_cast<ArdourCanvas::SimpleLine*> (item);
+       assert (_line);
+
+       AudioRegionView* _arv = reinterpret_cast<AudioRegionView*> (item->get_data ("regionview"));
+       _arv->remove_transient(_line->property_x1());
+}
+
+void
+Editor::snap_regions_to_grid ()
+{
+       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) {
+               framepos_t start_frame = (*r)->region()->first_frame ();
+               snap_to (start_frame);
+               (*r)->region()->set_position (start_frame, this);
+       }
+       
+       _session->commit_reversible_command ();
+}
+
+void
+Editor::close_region_gaps ()
+{      
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+
+       if (!_session || rs.empty()) {
+               return;
+       }
+
+       Dialog dialog (_("Close Region Gaps"));
+
+       Table table (2, 3);
+       table.set_spacings (12);
+       table.set_border_width (12);
+       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);
+       table.attach (spin_crossfade, 1, 2, 0, 1);
+
+       table.attach (*manage (new Label (_("ms"))), 2, 3, 0, 1);
+
+       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_increments (1, 1);
+       spin_pullback.set_value (5);
+       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);
+       dialog.show_all ();
+
+       if (dialog.run () == RESPONSE_CANCEL) {
+               return;
+       }
+
+       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) {
+
+               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 );
+               
+               last_region = (*r)->region();
+               
+               idx++;
+       }
+       
+       _session->commit_reversible_command ();
 }
 
 void
@@ -5912,11 +6021,13 @@ Editor::tab_to_transient (bool forward)
 {
        AnalysisFeatureList positions;
 
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+
        if (!_session) {
                return;
        }
 
-       nframes64_t pos = _session->audible_frame ();
+       framepos_t pos = _session->audible_frame ();
 
        if (!selection->tracks.empty()) {
 
@@ -5929,7 +6040,7 @@ Editor::tab_to_transient (bool forward)
                                if (tr) {
                                        boost::shared_ptr<Playlist> pl = tr->playlist ();
                                        if (pl) {
-                                               nframes64_t result = pl->find_next_transient (pos, forward ? 1 : -1);
+                                               framepos_t result = pl->find_next_transient (pos, forward ? 1 : -1);
 
                                                if (result >= 0) {
                                                        positions.push_back (result);
@@ -5941,10 +6052,6 @@ Editor::tab_to_transient (bool forward)
 
        } else {
 
-               RegionSelection rs;
-
-               get_regions_for_action (rs);
-
                if (rs.empty()) {
                        return;
                }
@@ -5983,12 +6090,13 @@ Editor::tab_to_transient (bool forward)
                }
        }
 }
+
 void
 Editor::playhead_forward_to_grid ()
 {
        if (!_session) return;
-       nframes64_t pos = playhead_cursor->current_frame;
-       if (pos < max_frames - 1) {
+       framepos_t pos = playhead_cursor->current_frame;
+       if (pos < max_framepos - 1) {
                pos += 2;
                snap_to_internal (pos, 1, false);
                _session->request_locate (pos);
@@ -6000,7 +6108,7 @@ void
 Editor::playhead_backward_to_grid ()
 {
        if (!_session) return;
-       nframes64_t pos = playhead_cursor->current_frame;
+       framepos_t pos = playhead_cursor->current_frame;
        if (pos > 2) {
                pos -= 2;
                snap_to_internal (pos, -1, false);
@@ -6009,7 +6117,7 @@ Editor::playhead_backward_to_grid ()
 }
 
 void
-Editor::set_track_height (uint32_t h)
+Editor::set_track_height (Height h)
 {
        TrackSelection& ts (selection->tracks);
 
@@ -6037,7 +6145,7 @@ Editor::toggle_tracks_active ()
                                target = !rtv->_route->active();
                                first = false;
                        }
-                       rtv->_route->set_active (target);
+                       rtv->_route->set_active (target, this);
                }
        }
 }
@@ -6058,6 +6166,7 @@ Editor::remove_tracks ()
        const char* trackstr;
        const char* busstr;
        vector<boost::shared_ptr<Route> > routes;
+       bool special_bus = false;
 
        for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
                RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*x);
@@ -6069,8 +6178,30 @@ Editor::remove_tracks ()
                        }
                }
                routes.push_back (rtv->_route);
+
+               if (rtv->route()->is_master() || rtv->route()->is_monitor()) {
+                       special_bus = true;
+               }
        }
 
+       if (special_bus && !Config->get_allow_special_bus_removal()) {
+               MessageDialog msg (_("That would be bad news ...."),
+                                  false,
+                                  Gtk::MESSAGE_INFO,
+                                  Gtk::BUTTONS_OK);
+               msg.set_secondary_text (string_compose (_(
+                                                               "Removing the master or monitor bus is such a bad idea\n\
+that %1 is not going to allow it.\n\
+\n\
+If you really want to do this sort of thing\n\
+edit your ardour.rc file to set the\n\
+\"allow-special-bus-removal\" option to be \"yes\""), PROGRAM_NAME));
+
+               msg.present ();
+               msg.run ();
+               return;
+       }
+                
        if (ntracks + nbusses == 0) {
                return;
        }
@@ -6091,16 +6222,17 @@ Editor::remove_tracks ()
                if (nbusses) {
                        prompt  = string_compose (_("Do you really want to remove %1 %2 and %3 %4?\n"
                                                    "(You may also lose the playlists associated with the %2)\n\n"
-                                                   "This action cannot be undone!"),
+                                                   "This action cannot be undone, and the session file will be overwritten!"),
                                                  ntracks, trackstr, nbusses, busstr);
                } else {
                        prompt  = string_compose (_("Do you really want to remove %1 %2?\n"
                                                    "(You may also lose the playlists associated with the %2)\n\n"
-                                                   "This action cannot be undone!"),
+                                                   "This action cannot be undone, and the session file will be overwritten!"),
                                                  ntracks, trackstr);
                }
        } else if (nbusses) {
-               prompt  = string_compose (_("Do you really want to remove %1 %2?"),
+               prompt  = string_compose (_("Do you really want to remove %1 %2?\n\n"
+                                           "This action cannot be undon, and the session file will be overwritten"),
                                          nbusses, busstr);
        }
 
@@ -6136,81 +6268,34 @@ Editor::do_insert_time ()
                return;
        }
 
-       ArdourDialog d (*this, _("Insert Time"));
-
-       nframes64_t const pos = get_preferred_edit_position ();
-
-       d.get_vbox()->set_border_width (12);
-       d.get_vbox()->set_spacing (4);
-
-       Table table (2, 2);
-       table.set_spacings (4);
-
-       Label time_label (_("Time to insert:"));
-       time_label.set_alignment (1, 0.5);
-       table.attach (time_label, 0, 1, 0, 1, FILL | EXPAND);
-       AudioClock clock ("insertTimeClock", true, X_("InsertTimeClock"), true, false, true, true);
-       clock.set (0);
-       clock.set_session (_session);
-       clock.set_bbt_reference (pos);
-       table.attach (clock, 1, 2, 0, 1);
-
-       Label intersected_label (_("Intersected regions should:"));
-       intersected_label.set_alignment (1, 0.5);
-       table.attach (intersected_label, 0, 1, 1, 2, FILL | EXPAND);
-       ComboBoxText intersected_combo;
-       intersected_combo.append_text (_("stay in position"));
-       intersected_combo.append_text (_("move"));
-       intersected_combo.append_text (_("be split"));
-       intersected_combo.set_active (0);
-       table.attach (intersected_combo, 1, 2, 1, 2);
-
-       d.get_vbox()->pack_start (table);
-
-       CheckButton move_glued (_("Move glued regions"));
-       d.get_vbox()->pack_start (move_glued);
-       CheckButton move_markers (_("Move markers"));
-       d.get_vbox()->pack_start (move_markers);
-       CheckButton move_tempos (_("Move tempo and meter changes"));
-       d.get_vbox()->pack_start (move_tempos);
-
-       d.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
-       d.add_button (_("Insert time"), Gtk::RESPONSE_OK);
-       d.show_all ();
-
+       InsertTimeDialog d (*this);
        int response = d.run ();
 
        if (response != RESPONSE_OK) {
                return;
        }
 
-       nframes64_t distance = clock.current_duration (pos);
-
-       if (distance == 0) {
+       if (d.distance() == 0) {
                return;
        }
 
-       /* only setting this to keep GCC quiet */
-       InsertTimeOption opt = LeaveIntersected;
-
-       switch (intersected_combo.get_active_row_number ()) {
-       case 0:
-               opt = LeaveIntersected;
-               break;
-       case 1:
-               opt = MoveIntersected;
-               break;
-       case 2:
-               opt = SplitIntersected;
-               break;
-       }
+       InsertTimeOption opt = d.intersected_region_action ();
 
-       insert_time (pos, distance, opt, move_glued.get_active(), move_markers.get_active(), move_tempos.get_active());
+       insert_time (
+               get_preferred_edit_position(),
+               d.distance(),
+               opt,
+               d.move_glued(),
+               d.move_markers(),
+               d.move_glued_markers(),
+               d.move_locked_markers(),
+               d.move_tempos()
+               );
 }
 
 void
-Editor::insert_time (nframes64_t pos, nframes64_t frames, InsertTimeOption opt,
-                    bool ignore_music_glue, bool markers_too, bool tempo_too)
+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)
 {
        bool commit = false;
 
@@ -6226,8 +6311,8 @@ Editor::insert_time (nframes64_t pos, nframes64_t frames, InsertTimeOption opt,
 
                if (pl) {
 
-                       pl->clear_history ();
-                        pl->clear_owned_history ();
+                       pl->clear_changes ();
+                       pl->clear_owned_changes ();
 
                        if (opt == SplitIntersected) {
                                pl->split (pos);
@@ -6235,16 +6320,10 @@ Editor::insert_time (nframes64_t pos, nframes64_t frames, InsertTimeOption opt,
 
                        pl->shift (pos, frames, (opt == MoveIntersected), ignore_music_glue);
 
-                        vector<StatefulDiffCommand*> cmds;
-                        
-                        pl->rdiff (cmds);
-                        
-                        cerr << "Shift generated " << cmds.size() << " sdc's\n";
-
-                        for (vector<StatefulDiffCommand*>::iterator c = cmds.begin(); c != cmds.end(); ++c) {
-                                _session->add_command (*c);
-                        }
-                        
+                       vector<Command*> cmds;
+                       pl->rdiff (cmds);
+                       _session->add_commands (cmds);
+                       
                        _session->add_command (new StatefulDiffCommand (pl));
                        commit = true;
                }
@@ -6267,12 +6346,25 @@ Editor::insert_time (nframes64_t pos, nframes64_t frames, InsertTimeOption opt,
 
                        Locations::LocationList::const_iterator tmp;
 
-                       if ((*i)->start() >= pos) {
-                               (*i)->set_start ((*i)->start() + frames);
-                               if (!(*i)->is_mark()) {
-                                       (*i)->set_end ((*i)->end() + frames);
+                       bool const was_locked = (*i)->locked ();
+                       if (locked_markers_too) {
+                               (*i)->unlock ();
+                       }
+
+                       if ((*i)->position_lock_style() == AudioTime || glued_markers_too) {
+
+                               if ((*i)->start() >= pos) {
+                                       (*i)->set_start ((*i)->start() + frames);
+                                       if (!(*i)->is_mark()) {
+                                               (*i)->set_end ((*i)->end() + frames);
+                                       }
+                                       moved = true;
                                }
-                               moved = true;
+                               
+                       }
+
+                       if (was_locked) {
+                               (*i)->lock ();
                        }
                }
 
@@ -6318,7 +6410,7 @@ Editor::fit_tracks (TrackViewList & tracks)
        uint32_t h = (uint32_t) floor ((_canvas_height - child_heights - canvas_timebars_vsize) / tracks.size());
        double first_y_pos = DBL_MAX;
 
-       if (h < TimeAxisView::hSmall) {
+       if (h < TimeAxisView::preset_height (HeightSmall)) {
                MessageDialog msg (*this, _("There are too many tracks to fit in the current window"));
                /* too small to be displayed */
                return;
@@ -6326,20 +6418,31 @@ Editor::fit_tracks (TrackViewList & tracks)
 
        undo_visual_stack.push_back (current_visual_state());
 
+       /* build a list of all tracks, including children */
+
+       TrackViewList all;
+       for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
+               all.push_back (*i);
+               TimeAxisView::Children c = (*i)->get_child_list ();
+               for (TimeAxisView::Children::iterator j = c.begin(); j != c.end(); ++j) {
+                       all.push_back (j->get());
+               }
+       }
+
        /* operate on all tracks, hide unselected ones that are in the middle of selected ones */
 
        bool prev_was_selected = false;
-       bool is_selected = tracks.contains (track_views.front());
+       bool is_selected = tracks.contains (all.front());
        bool next_is_selected;
 
-       for (TrackViewList::iterator t = track_views.begin(); t != track_views.end(); ++t) {
+       for (TrackViewList::iterator t = all.begin(); t != all.end(); ++t) {
 
                TrackViewList::iterator next;
 
                next = t;
                ++next;
 
-               if (next != track_views.end()) {
+               if (next != all.end()) {
                        next_is_selected = tracks.contains (*next);
                } else {
                        next_is_selected = false;
@@ -6350,7 +6453,7 @@ Editor::fit_tracks (TrackViewList & tracks)
                        first_y_pos = std::min ((*t)->y_position (), first_y_pos);
                } else {
                        if (prev_was_selected && next_is_selected) {
-                               hide_track_in_display (**t);
+                               hide_track_in_display (*t);
                        }
                }
 
@@ -6433,3 +6536,33 @@ Editor::end_visual_state_op (uint32_t n)
        return false; // do not call again
 }
 
+void
+Editor::toggle_region_mute ()
+{
+       if (_ignore_region_action) {
+               return;
+       }
+       
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+
+       if (rs.empty ()) {
+               return;
+       }
+
+       if (rs.size() > 1) {
+               begin_reversible_command (_("mute regions"));
+       } 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 ();
+}
+