Index: gtk2_ardour/editor_regions.cc
===================================================================
--- gtk2_ardour/editor_regions.cc	(revision 7841)
+++ gtk2_ardour/editor_regions.cc	(working copy)
@@ -189,6 +189,7 @@
 	}
 
 	if (region->hidden()) {
+
 		TreeModel::iterator iter = _model->get_iter ("0");
 		TreeModel::Row parent;
 		TreeModel::Row child;
@@ -212,38 +213,58 @@
 
 		row = *(_model->append (parent.children()));
 
-	} else if (region->whole_file()) {
+	} else if (!_show_automatic_regions){
+		row = row = *(_model->append());
+	} else {
 
+		/* find parent node, add as new child */
+
 		TreeModel::iterator i;
 		TreeModel::Children rows = _model->children();
+		bool found_parent = false;
 
 		for (i = rows.begin(); i != rows.end(); ++i) {
+
 			boost::shared_ptr<Region> rr = (*i)[_columns.region];
+			boost::shared_ptr<AudioRegion> r = boost::dynamic_pointer_cast<AudioRegion>(rr);
 
-			if (rr && region->region_list_equivalent (rr)) {
-				return;
+			if (r && region->source_equivalent (r) && r->automatic()) {
+
+					row = *(_model->append ((*i).children()));
+					found_parent = true;
+					
+					break;
 			}
+
+			TreeModel::iterator ii;
+			TreeModel::Children subrows = (*i).children();
+
+			for (ii = subrows.begin(); ii != subrows.end(); ++ii) {
+				boost::shared_ptr<Region> rrr = (*ii)[_columns.region];
+
+				if (region->region_list_equivalent (rrr)) {
+					return;
+
+				}
+			}
 		}
 
-		row = *(_model->append());
+		if (!found_parent) {
 
-		if (missing_source) {
-			c.set_rgb(65535,0,0);     // FIXME: error color from style
+			row = *(_model->append());
 
-		} else if (region->automatic()){
-			c.set_rgb(0,65535,0);     // FIXME: error color from style
+			if (missing_source) {
+				c.set_rgb(65535,0,0);     // FIXME: error color from style
 
-		} else {
-			set_color(c, rgba_from_style ("RegionListWholeFile", 0xff, 0, 0, 0, "fg", Gtk::STATE_NORMAL, false ));
+			} else {
+				c.set_rgb(0,65535,0);     // FIXME: error color from style
 
-		}
+			}
 
-		row[_columns.color_] = c;
+			row[_columns.color_] = c;
 
-		if (region->source()->name()[0] == '/') { // external file
+			if (region->source()->name()[0] == '/') { // external file
 
-			if (region->whole_file()) {
-
 				boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(region->source());
 				str = ".../";
 
@@ -252,81 +273,41 @@
 				} else {
 					str += region->source()->name();
 				}
-
 			} else {
-				str = region->name();
+				str += region_name_from_path (region->source()->name(), true);
 			}
 
-		} else {
-			str = region->name();
-		}
-
-		if (region->n_channels() > 1) {
-			std::stringstream foo;
-			foo << region->n_channels ();
-			str += " [";
-			str += foo.str();
-			str += "]";
-		}
-
-		row[_columns.name] = str;
-		row[_columns.region] = region;
-		row[_columns.property_toggles_visible] = false;
-
-		if (missing_source) {
-			row[_columns.path] = _("(MISSING) ") + region->source()->name();
-
-		} else {
-			row[_columns.path] = region->source()->name();
-
-		}
-
-		if (region->automatic()) {
-			return;
-		}
-
-	} else {
-
-		/* find parent node, add as new child */
-
-		TreeModel::iterator i;
-		TreeModel::Children rows = _model->children();
-		bool found_parent = false;
-
-		for (i = rows.begin(); i != rows.end(); ++i) {
-			boost::shared_ptr<Region> rr = (*i)[_columns.region];
-			boost::shared_ptr<AudioRegion> r = boost::dynamic_pointer_cast<AudioRegion>(rr);
-
-			if (r && r->whole_file()) {
-
-				if (region->source_equivalent (r)) {
-					row = *(_model->append ((*i).children()));
-					found_parent = true;
-					break;
-				}
+			if (region->n_channels() > 1) {
+				std::stringstream foo;
+				foo << region->n_channels ();
+				str += " [";
+				str += foo.str();
+				str += "]";
 			}
+			
+			/* Create an automatic region to serve as the parent node in the tree */
+			boost::shared_ptr<Region> r = RegionFactory::create (region, false, true);
+			
+			row[_columns.name] = str;
+			row[_columns.region] = r;
+			row[_columns.property_toggles_visible] = false;
 
-			TreeModel::iterator ii;
-			TreeModel::Children subrows = (*i).children();
+			if (missing_source) {
+				row[_columns.path] = _("(MISSING) ") + region->source()->name();
 
-			for (ii = subrows.begin(); ii != subrows.end(); ++ii) {
-				boost::shared_ptr<Region> rrr = (*ii)[_columns.region];
+			} else {
+				row[_columns.path] = region->source()->name();
 
-				if (region->region_list_equivalent (rrr)) {
-					return;
-
-				}
 			}
+			
+			/* Now add the region to the parent row just created */
+			row = *(_model->append (row.children()));			
 		}
-
-		if (!found_parent) {
-			row = *(_model->append());
-		}
-
-		row[_columns.property_toggles_visible] = true;
 	}
 
 	row[_columns.region] = region;
+	row[_columns.name] = region->name();
+	row[_columns.property_toggles_visible] = true;
 
 	populate_row(region, (*row));
 }
@@ -569,6 +550,7 @@
 	tmp_region_list.clear();
 
         const RegionFactory::RegionMap& regions (RegionFactory::regions());
+	
         for (RegionFactory::RegionMap::const_iterator i = regions.begin(); i != regions.end(); ++i) {
                 insert_into_tmp_regionlist (i->second);
         }
@@ -576,6 +558,7 @@
 	for (list<boost::shared_ptr<Region> >::iterator r = tmp_region_list.begin(); r != tmp_region_list.end(); ++r) {
 		add_region (*r);
 	}
+	
 	tmp_region_list.clear();
 
 	_display.set_model (_model);
@@ -949,7 +932,15 @@
 	}
 }
 
+
 void
+EditorRegions::delete_unused_regions ()
+{	
+	_session->cleanup_regions();	
+        redisplay();
+}
+
+void
 EditorRegions::toggle_show_auto_regions ()
 {
 	_show_automatic_regions = toggle_show_auto_regions_action()->get_active();
@@ -993,9 +984,12 @@
 	bool have_hidden = false;
 	
 	TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
+	
 	for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
+		
 		TreeIter t = _model->get_iter (*i);
 		boost::shared_ptr<Region> r = (*t)[_columns.region];
+		
 		if (r) {
 			if (r->hidden ()) {
 				have_hidden = true;
@@ -1380,6 +1374,7 @@
 	}
 
 	XMLProperty const * p = node.property (X_("sort-type"));
+	
 	if (p) {
 		Editing::RegionListSortType const t = static_cast<Editing::RegionListSortType> (string_2_enum (p->value(), _sort_type));
                 if (_sort_type != t) {
@@ -1391,6 +1386,7 @@
 	}
 
 	p = node.property (X_("sort-ascending"));
+	
 	if (p) {
 		bool const yn = string_is_affirmative (p->value ());
                 SortType old_sort_type;
@@ -1412,6 +1408,7 @@
 	}
 
 	p = node.property (X_("show-all"));
+	
 	if (p) {
 		bool const yn = string_is_affirmative (p->value ());
                 if (expanded != yn) {
@@ -1422,7 +1419,9 @@
 	}
 
 	p = node.property (X_("show-automatic-regions"));
+	
 	if (p) {
+
 		bool const yn = string_is_affirmative (p->value ());
                 if (yn != _show_automatic_regions) {
                         _show_automatic_regions = yn;
@@ -1498,6 +1497,12 @@
 	return ActionManager::get_action (X_("RegionList"), X_("rlShow"));
 }
 
+RefPtr<Action>
+EditorRegions::delete_unused_regions_action () const
+{
+	return ActionManager::get_action (X_("RegionList"), X_("removeUnusedRegions"));
+}
+
 RefPtr<ToggleAction>
 EditorRegions::toggle_full_action () const
 {
Index: gtk2_ardour/ardour.menus.in
===================================================================
--- gtk2_ardour/ardour.menus.in	(revision 7841)
+++ gtk2_ardour/ardour.menus.in	(working copy)
@@ -514,7 +514,7 @@
          <menuitem action='rlAudition'/>
          <menuitem action='rlHide'/>
          <menuitem action='rlShow'/>
-	  <separator/>
+	 <separator/>
          <menuitem action='rlShowAll'/>
          <menuitem action='rlShowAuto'/>
          <menu name='Sort' action='RegionListSort'>
@@ -533,6 +533,8 @@
                <menuitem action='SortBySourceFilesystem'/>
          </menu>
          <separator/>
-         <menuitem action='addExternalAudioToRegionList'/>	       
+         <menuitem action='addExternalAudioToRegionList'/>
+         <separator/>
+         <menuitem action='removeUnusedRegions'/>    
      </popup>
 </ui>
Index: gtk2_ardour/editor_actions.cc
===================================================================
--- gtk2_ardour/editor_actions.cc	(revision 7841)
+++ gtk2_ardour/editor_actions.cc	(working copy)
@@ -801,13 +801,16 @@
 
 	act = ActionManager::register_action (rl_actions, X_("rlAudition"), _("Audition"), sigc::mem_fun(*this, &Editor::audition_region_from_region_list));
 	ActionManager::region_list_selection_sensitive_actions.push_back (act);
+	
 	act = ActionManager::register_action (rl_actions, X_("rlHide"), _("Hide"), sigc::mem_fun(*this, &Editor::hide_region_from_region_list));
 	ActionManager::region_list_selection_sensitive_actions.push_back (act);
+	
 	act = ActionManager::register_action (rl_actions, X_("rlShow"), _("Show"), sigc::mem_fun(*this, &Editor::show_region_in_region_list));
 	ActionManager::region_list_selection_sensitive_actions.push_back (act);
+	
 	ActionManager::register_toggle_action (rl_actions, X_("rlShowAll"), _("Show All"), sigc::mem_fun(*_regions, &EditorRegions::toggle_full));
 	ActionManager::register_toggle_action (rl_actions, X_("rlShowAuto"), _("Show Automatic Regions"), sigc::mem_fun (*_regions, &EditorRegions::toggle_show_auto_regions));
-
+	
 	ActionManager::register_radio_action (rl_actions, sort_order_group, X_("SortAscending"),  _("Ascending"),
 			sigc::bind (sigc::mem_fun (*_regions, &EditorRegions::reset_sort_direction), true));
 	ActionManager::register_radio_action (rl_actions, sort_order_group, X_("SortDescending"),   _("Descending"),
@@ -834,6 +837,7 @@
 	ActionManager::register_radio_action (rl_actions, sort_type_group, X_("SortBySourceFilesystem"),  _("By Source Filesystem"),
 			sigc::bind (sigc::mem_fun (*_regions, &EditorRegions::reset_sort_type), BySourceFileFS, false));
 
+	ActionManager::register_action (rl_actions, X_("removeUnusedRegions"), _("Delete Unused"), sigc::mem_fun(*_regions, &EditorRegions::delete_unused_regions));
 
 	/* the next two are duplicate items with different names for use in two different contexts */
 
@@ -917,6 +921,7 @@
 	}
 
 	Glib::RefPtr<Action> act = ActionManager::get_action (X_("Rulers"), action);
+	
 	if (act) {
 		Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
 		update_ruler_visibility ();
Index: gtk2_ardour/editor_regions.h
===================================================================
--- gtk2_ardour/editor_regions.h	(revision 7841)
+++ gtk2_ardour/editor_regions.h	(working copy)
@@ -66,6 +66,8 @@
 	void unselect_all () {
 		_display.get_selection()->unselect_all ();
 	}
+	
+	void delete_unused_regions();
 
 	XMLNode& get_state () const;
 	void set_state (const XMLNode &);
@@ -131,6 +133,7 @@
 
 	bool key_press (GdkEventKey *);
 	bool button_press (GdkEventButton *);
+
 	void show_context_menu (int button, int time);
 
 	int sorter (Gtk::TreeModel::iterator, Gtk::TreeModel::iterator);
@@ -139,6 +142,7 @@
 
 	void add_region (boost::shared_ptr<ARDOUR::Region>);
 	void add_regions (std::vector<boost::shared_ptr<ARDOUR::Region> > & );
+	
 	void populate_row (boost::shared_ptr<ARDOUR::Region>, Gtk::TreeModel::Row const &);
         void populate_row_used (boost::shared_ptr<ARDOUR::Region> region, Gtk::TreeModel::Row const& row, uint32_t used);
         void populate_row_position (boost::shared_ptr<ARDOUR::Region> region, Gtk::TreeModel::Row const& row, uint32_t used);
@@ -155,7 +159,9 @@
         void populate_row_source (boost::shared_ptr<ARDOUR::Region> region, Gtk::TreeModel::Row const& row);
 
 	void update_row (boost::shared_ptr<ARDOUR::Region>);
+	
 	bool update_subrows (boost::shared_ptr<ARDOUR::Region>, Gtk::TreeModel::Row const &, int);
+	
 	void update_all_rows ();
 	void update_all_subrows (Gtk::TreeModel::Row const &, int);
 	void insert_into_tmp_regionlist (boost::shared_ptr<ARDOUR::Region>);
@@ -169,22 +175,30 @@
 
 	Glib::RefPtr<Gtk::Action> hide_action () const;
 	Glib::RefPtr<Gtk::Action> show_action () const;
+	Glib::RefPtr<Gtk::Action> delete_unused_regions_action() const;
 	Glib::RefPtr<Gtk::ToggleAction> toggle_full_action () const;
 	Glib::RefPtr<Gtk::ToggleAction> toggle_show_auto_regions_action () const;
 	
 	Gtk::Menu* _menu;
 	Gtk::ScrolledWindow _scroller;
 	Gtk::Frame _frame;
+	
 	Gtkmm2ext::DnDTreeView<boost::shared_ptr<ARDOUR::Region> > _display;
+	
 	Glib::RefPtr<Gtk::TreeStore> _model;
+	
 	bool _show_automatic_regions;
+	bool ignore_region_list_selection_change;
+	bool ignore_selected_region_change;
+	bool _no_redisplay;
+	
 	Editing::RegionListSortType _sort_type;
-	bool _no_redisplay;
+		
 	std::list<boost::shared_ptr<ARDOUR::Region> > tmp_region_list;
+	
 	PBD::ScopedConnection region_property_connection;
 	PBD::ScopedConnection check_new_region_connection;
-	bool ignore_region_list_selection_change;
-	bool ignore_selected_region_change;
+	
         bool expanded;
 };

Index: libs/ardour/ardour/session.h
===================================================================
--- libs/ardour/ardour/session.h	(revision 7841)
+++ libs/ardour/ardour/session.h	(working copy)
@@ -512,6 +512,7 @@
 	void add_source (boost::shared_ptr<Source>);
 	void remove_source (boost::weak_ptr<Source>);
 
+	void  cleanup_regions();
 	int  cleanup_sources (CleanupReport&);
 	int  cleanup_trash_sources (CleanupReport&);
 
Index: libs/ardour/ardour/region_factory.h
===================================================================
--- libs/ardour/ardour/region_factory.h	(revision 7841)
+++ libs/ardour/ardour/region_factory.h	(working copy)
@@ -56,7 +56,7 @@
 	static PBD::Signal1<void,boost::shared_ptr<Region> >  CheckNewRegion;
 
 	/** create a "pure copy" of Region @param other */
-	static boost::shared_ptr<Region> create (boost::shared_ptr<const Region> other, bool announce = false);
+	static boost::shared_ptr<Region> create (boost::shared_ptr<const Region> other, bool announce = false, bool automatic = false);
 
 	/** create a region from a single Source */
 	static boost::shared_ptr<Region> create (boost::shared_ptr<Source>, 
Index: libs/ardour/session_state.cc
===================================================================
--- libs/ardour/session_state.cc	(revision 7841)
+++ libs/ardour/session_state.cc	(working copy)
@@ -1082,11 +1082,15 @@
 	child = node->add_child ("Regions");
 
 	if (full_state) {
+	  
 		Glib::Mutex::Lock rl (region_lock);
                 const RegionFactory::RegionMap& region_map (RegionFactory::all_regions());
-                for (RegionFactory::RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) {
-                        boost::shared_ptr<Region> r = i->second;
-                        /* only store regions not attached to playlists */
+                
+		for (RegionFactory::RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) {
+
+			boost::shared_ptr<Region> r = i->second;
+                        
+			/* only store regions not attached to playlists */
                         if (r->playlist() == 0) {
                                 child->add_child_nocopy (r->state ());
                         }
@@ -2486,6 +2490,28 @@
 	return r.get_value_or (1);
 }
 
+void
+Session::cleanup_regions ()
+{		
+	const RegionFactory::RegionMap& regions (RegionFactory::regions());
+	
+	for (RegionFactory::RegionMap::const_iterator i = regions.begin(); i != regions.end(); ++i) {
+
+		boost::shared_ptr<AudioRegion> audioregion = boost::dynamic_pointer_cast<AudioRegion>( i->second);
+		uint32_t used = playlists->region_use_count (audioregion);
+		
+		if (used == 0){
+			RegionFactory::map_remove(i->second);
+		}
+        }
+        
+	/* dump the history list */
+	_history.clear ();
+	
+	save_state ("");
+}
+
+
 int
 Session::cleanup_sources (CleanupReport& rep)
 {
Index: libs/ardour/region_factory.cc
===================================================================
--- libs/ardour/region_factory.cc	(revision 7841)
+++ libs/ardour/region_factory.cc	(working copy)
@@ -45,7 +45,7 @@
 std::map<std::string, uint32_t> RegionFactory::region_name_map;
 
 boost::shared_ptr<Region>
-RegionFactory::create (boost::shared_ptr<const Region> region, bool announce)
+RegionFactory::create (boost::shared_ptr<const Region> region, bool announce, bool automatic)
 {
 	boost::shared_ptr<Region> ret;
 	boost::shared_ptr<const AudioRegion> ar;
@@ -73,10 +73,15 @@
 
 	if (ret) {
 		ret->set_name (new_region_name(ret->name()));
-		map_add (ret);
+		
+		if (!automatic){
+			map_add (ret);
+		}
+		else {
+		  ret->set_automatic(true);
+		}
 
 		/* pure copy constructor - no property list */
-		/* pure copy constructor - no CheckNewRegion emitted */
 		if (announce) {
 			CheckNewRegion (ret);
 		}
@@ -317,8 +322,6 @@
 
                 i = tmp;
         }
-
-
 }
 
 boost::shared_ptr<Region>
Index: libs/ardour/audio_diskstream.cc
===================================================================
--- libs/ardour/audio_diskstream.cc	(revision 7841)
+++ libs/ardour/audio_diskstream.cc	(working copy)
@@ -78,7 +78,6 @@
 	, channels (new ChannelList)
 {
 	/* prevent any write sources from being created */
-
 	in_set_state = true;
 	use_new_playlist ();
 	in_set_state = false;
@@ -1443,34 +1442,6 @@
 		string whole_file_region_name;
 		whole_file_region_name = region_name_from_path (c->front()->write_source->name(), true);
 
-		/* Register a new region with the Session that
-		   describes the entire source. Do this first
-		   so that any sub-regions will obviously be
-		   children of this one (later!)
-		*/
-
-		try {
-			PropertyList plist;
-
-			plist.add (Properties::start, c->front()->write_source->last_capture_start_frame());
-			plist.add (Properties::length, total_capture);
-			plist.add (Properties::name, whole_file_region_name);
-			boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
-			rx->set_automatic (true);
-			rx->set_whole_file (true);
-
-			region = boost::dynamic_pointer_cast<AudioRegion> (rx);
-			region->special_set_position (capture_info.front()->start);
-		}
-
-
-		catch (failed_constructor& err) {
-			error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
-			/* XXX what now? */
-		}
-
-		_last_capture_sources.insert (_last_capture_sources.end(), srcs.begin(), srcs.end());
-
 		// cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
 
                 _playlist->clear_changes ();
@@ -1491,7 +1462,7 @@
 				plist.add (Properties::start, buffer_position);
 				plist.add (Properties::length, (*ci)->frames);
 				plist.add (Properties::name, region_name);
-				
+
 				boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
 				region = boost::dynamic_pointer_cast<AudioRegion> (rx);
 			}
@@ -2217,7 +2188,7 @@
 
 		region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, plist));
 
-		region->set_automatic (true);
+		//region->set_automatic (true);
 		region->set_whole_file (true);
 		region->special_set_position (0);
 	}
