Index: /applications/editors/josm/plugins/utilsplugin2/build.xml
===================================================================
--- /applications/editors/josm/plugins/utilsplugin2/build.xml	(revision 27997)
+++ /applications/editors/josm/plugins/utilsplugin2/build.xml	(revision 27998)
@@ -30,5 +30,5 @@
 <project name="utilsplugin2" default="dist" basedir=".">
     <!-- enter the SVN commit message -->
-    <property name="commit.message" value="Utilsplugin2: fixing shortcut conflicts, one better key"/>
+    <property name="commit.message" value="Utilsplugin2: search speed-up, added adjacent and connected keywords (search for ways only)"/>
     <!-- enter the *lowest* JOSM version this plugin is currently compatible with -->
     <property name="plugin.main.version" value="4980"/>
Index: /applications/editors/josm/plugins/utilsplugin2/src/utilsplugin2/UtilsPlugin2.java
===================================================================
--- /applications/editors/josm/plugins/utilsplugin2/src/utilsplugin2/UtilsPlugin2.java	(revision 27997)
+++ /applications/editors/josm/plugins/utilsplugin2/src/utilsplugin2/UtilsPlugin2.java	(revision 27998)
@@ -2,4 +2,5 @@
 package utilsplugin2;
 
+import org.openstreetmap.josm.data.osm.Node;
 import utilsplugin2.customurl.ChooseURLAction;
 import utilsplugin2.customurl.OpenPageAction;
@@ -151,5 +152,5 @@
     public static class UtilsUnaryMatchFactory implements UnaryMatchFactory {
         private static Collection<String> keywords = Arrays.asList("inside",
-                "intersecting", "allintersecting");
+                "intersecting", "allintersecting", "adjacent", "connected");
         
         @Override
@@ -157,4 +158,8 @@
             if ("inside".equals(keyword))
                 return new InsideMatch(matchOperand);
+            else if ("adjacent".equals(keyword))
+                return new ConnectedMatch(matchOperand, false);
+            else if ("connected".equals(keyword))
+                return new ConnectedMatch(matchOperand, true);
             else if ("intersecting".equals(keyword))
                 return new IntersectingMatch(matchOperand, false);
@@ -178,5 +183,5 @@
         public InsideMatch(Match match) {
             super(match);
-            init();
+            //init();
         }
         
@@ -185,4 +190,5 @@
          */
         private void init() {
+            if (inside==null) init(); // lazy initialization
             Collection<OsmPrimitive> matchedAreas = new HashSet<OsmPrimitive>();
 
@@ -212,8 +218,10 @@
     public static class IntersectingMatch extends UnaryMatch {
         private Collection<Way> intersecting = null;
+        boolean all;
         
         public IntersectingMatch(Match match, boolean all) {
             super(match);
-            init(all);
+            this.all=all;
+            //init(all);
         }   
         
@@ -241,4 +249,5 @@
         @Override
         public boolean match(OsmPrimitive osm) {
+            if (intersecting==null) init(all); // lazy initialization
             if (osm instanceof Way)
                 return intersecting.contains((Way)osm);
@@ -246,3 +255,54 @@
         }
     }
+    
+    public static class ConnectedMatch extends UnaryMatch {
+        private Collection<Way> connected = null;
+        boolean all;
+        
+        public ConnectedMatch(Match match, boolean all) {
+            super(match);
+            this.all=all;
+        }   
+        
+        /**
+         * Find (all) ways intersecting ways which match the expression.
+         */
+        private void init(boolean all) {
+            Collection<Way> matchedWays = new HashSet<Way>();
+            Set<Node> matchedNodes = new HashSet<Node>();
+            
+            // find all ways that match the expression
+            Collection<Way> allWays = Main.main.getCurrentDataSet().getWays();
+            for (Way way : allWays) {
+                if (match.match(way))
+                    matchedWays.add(way);
+            }
+            
+            // find all nodes that match the expression
+            Collection<Node> allNodes = Main.main.getCurrentDataSet().getNodes();
+            for (Node node: allNodes) {
+                if (match.match(node))
+                    matchedNodes.add(node);
+            }
+            
+            Set<Way> newWays = new HashSet<Way>();
+            if (all) {
+                NodeWayUtils.addWaysConnectedToNodes(matchedNodes, newWays);
+                NodeWayUtils.addWaysConnectedToWaysRecursively(matchedWays, newWays);
+            } else {
+                NodeWayUtils.addWaysConnectedToNodes(matchedNodes, newWays);
+                NodeWayUtils.addWaysConnectedToWays(matchedWays, newWays);
+            }
+            connected = newWays;
+        }
+        
+        @Override
+        public boolean match(OsmPrimitive osm) {
+            if (connected==null) init(all); // lazy initialization
+            if (osm instanceof Way)
+                return connected.contains((Way)osm);
+            return false;
+        }
+    }   
+    
 }
Index: /applications/editors/josm/plugins/utilsplugin2/src/utilsplugin2/selection/AdjacentWaysAction.java
===================================================================
--- /applications/editors/josm/plugins/utilsplugin2/src/utilsplugin2/selection/AdjacentWaysAction.java	(revision 27997)
+++ /applications/editors/josm/plugins/utilsplugin2/src/utilsplugin2/selection/AdjacentWaysAction.java	(revision 27998)
@@ -38,8 +38,6 @@
         // select ways attached to already selected ways
         Set<Way> newWays = new HashSet<Way>();
+        NodeWayUtils.addWaysConnectedToWays(selectedWays, newWays);
         newWays.addAll(selectedWays);
-        for (Way w : selectedWays){
-            NodeWayUtils.addWaysConnectedToWay(w, newWays);
-        }
 
         // selecting ways attached to selected nodes
Index: /applications/editors/josm/plugins/utilsplugin2/src/utilsplugin2/selection/NodeWayUtils.java
===================================================================
--- /applications/editors/josm/plugins/utilsplugin2/src/utilsplugin2/selection/NodeWayUtils.java	(revision 27997)
+++ /applications/editors/josm/plugins/utilsplugin2/src/utilsplugin2/selection/NodeWayUtils.java	(revision 27998)
@@ -30,5 +30,5 @@
 public final class NodeWayUtils {
 
-    static final int maxLevel = Main.pref.getInteger("selection.maxrecursion", 5);
+    static final int maxLevel = Main.pref.getInteger("selection.maxrecursion", 15);
     static final int maxWays = Main.pref.getInteger("selection.maxfoundways", 2000);
     static final int maxWays1 = Main.pref.getInteger("selection.maxfoundways.intersection", 500);
@@ -73,7 +73,9 @@
          int s = ways.size();
         List<Node> nodes = w.getNodes();
+        boolean flag = ways.contains(w);
         for (Node n: nodes) {
             ways.addAll(OsmPrimitive.getFilteredList(n.getReferrers(), Way.class));
         }
+        if (!flag) ways.remove(w);
         return ways.size() - s;
     }
@@ -155,6 +157,12 @@
         return count;
     }
-
-    static int addWaysConnectedToNodes(Set<Node> selectedNodes, Set<Way> newWays) {
+    
+    public static void addWaysConnectedToWays(Collection<Way> ways, Set<Way> newWays) {
+        for (Way w : ways){
+            NodeWayUtils.addWaysConnectedToWay(w, newWays);
+        }
+    }
+
+    public static int addWaysConnectedToNodes(Set<Node> selectedNodes, Set<Way> newWays) {
         int s = newWays.size();
         for (Node node: selectedNodes) {
@@ -164,5 +172,5 @@
     }
 
-    static int addNodesConnectedToWays(Set<Way> initWays, Set<Node> newNodes) {
+    public static int addNodesConnectedToWays(Set<Way> initWays, Set<Node> newNodes) {
         int s = newNodes.size();
         for (Way w: initWays) {
@@ -202,5 +210,5 @@
     }
 
- static void addWaysConnectedToWaysRecursively
+    public static void addWaysConnectedToWaysRecursively
             (Collection<Way> initWays, Set<Way> newWays)
     {
@@ -493,3 +501,4 @@
 
 
+
 }
