From d5e287e6fbbefe1c1de3d371f49f78868e226880 Mon Sep 17 00:00:00 2001
From: Tim Mayberry <mojofunk@gmail.com>
Date: Mon, 4 Sep 2017 10:13:35 +1000
Subject: [PATCH] Fix copying and pasting processors/plugins pre-fader

When processor index == 0 Route::before_processor_for_index() would always
return an empty processor sptr that resulted in trying to insert before the
Trim processor rather than before the first processor that is displayed to the
user (Fader/Amp).

Related to #6983
---
 libs/ardour/route.cc | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/libs/ardour/route.cc b/libs/ardour/route.cc
index e89b9b8..041f03b 100644
--- a/libs/ardour/route.cc
+++ b/libs/ardour/route.cc
@@ -725,7 +725,7 @@ Route::before_processor_for_placement (Placement p)
 }
 
 /** Supposing that we want to insert a Processor at a given index, return
- *  the processor to add the new one before (or 0 to add at the end).
+ *  the processor to add the new one before (or -1 to add at the end).
  */
 boost::shared_ptr<Processor>
 Route::before_processor_for_index (int index)
@@ -736,17 +736,15 @@ Route::before_processor_for_index (int index)
 
 	Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
 
-	ProcessorList::iterator i = _processors.begin ();
 	int j = 0;
-	while (i != _processors.end() && j < index) {
-		if ((*i)->display_to_user()) {
-			++j;
+	for (ProcessorList::const_iterator i = _processors.begin (); i != _processors.end(); ++i) {
+		if ((*i)->display_to_user() && j >= index) {
+			return *i;
 		}
-
-		++i;
+		j++;
 	}
 
-	return (i != _processors.end() ? *i : boost::shared_ptr<Processor> ());
+	return boost::shared_ptr<Processor> ();
 }
 
 /** Add a processor either pre- or post-fader
-- 
2.7.4

