--- base/src/macports1.0/macports.tcl.orig	2009-01-28 18:05:33.000000000 -0700
+++ base/src/macports1.0/macports.tcl	2009-01-28 18:06:48.000000000 -0700
@@ -1555,88 +1555,23 @@
     mportsearch $pattern $case_sensitive $matchstyle $field
 }
 
-proc mportsearch {pattern {case_sensitive yes} {matchstyle regexp} {field name}} {
-    global macports::portdbpath macports::sources
-    set matches [list]
-    set easy [expr { $field == "name" }]
-    
+proc mportsearch_load_portindex {} {
+    global macports::sources
+    global macports::mportsearch_portindex
     set found 0
     foreach source $sources {
-        set flags [lrange $source 1 end]
-        set source [lindex $source 0]
-        if {[macports::getprotocol $source] == "mports"} {
-            array set attrs [list name $pattern]
-            set res [macports::index::search $macports::portdbpath $source [array get attrs]]
-            eval lappend matches $res
-        } else {
-            if {[catch {set fd [open [macports::getindex $source] r]} result]} {
-                ui_warn "Can't open index file for source: $source"
+        set sourceURL [lindex $source 0]
+        if {[macports::getprotocol $sourceURL] != "mports"} {
+            if {[catch {set fd [open [macports::getindex $sourceURL] r]} result]} {
+                ui_warn "Can't open index file for source $sourceURL"
             } else {
                 try {
                     incr found 1
                     while {[gets $fd line] >= 0} {
-                        array unset portinfo
                         set name [lindex $line 0]
                         set len [lindex $line 1]
                         set line [read $fd $len]
-                        
-                        if {$easy} {
-                            set target $name
-                        } else {
-                            array set portinfo $line
-                            if {![info exists portinfo($field)]} continue
-                            set target $portinfo($field)
-                        }
-                        
-                        switch $matchstyle {
-                            exact {
-                                set matchres [expr 0 == ( {$case_sensitive == "yes"} ? [string compare $pattern $target] : [string compare -nocase $pattern $target] )]
-                            }
-                            glob {
-                                set matchres [expr {$case_sensitive == "yes"} ? [string match $pattern $target] : [string match -nocase $pattern $target]]
-                            }
-                            regexp -
-                            default {
-                                set matchres [expr {$case_sensitive == "yes"} ? [regexp -- $pattern $target] : [regexp -nocase -- $pattern $target]]
-                            }
-                        }
-                        
-                        if {$matchres == 1} {
-                            if {$easy} {
-                                array set portinfo $line
-                            }
-                            switch -regexp -- [macports::getprotocol ${source}] {
-                                {^rsync$} {
-                                    # Rsync files are local
-                                    set source_url "file://[macports::getsourcepath $source]"
-                                }
-                                {^https?$|^ftp$} {
-                                    if {[_source_is_snapshot $source filename extension]} {
-                                        # daily snapshot tarball
-                                        set source_url "file://[macports::getsourcepath $source]"
-                                    } else {
-                                        # default action
-                                        set source_url $source
-                                    }
-                                }
-                                default {
-                                    set source_url $source
-                                }
-                            }
-                            if {[info exists portinfo(portarchive)]} {
-                                set porturl ${source_url}/$portinfo(portarchive)
-                            } elseif {[info exists portinfo(portdir)]} {
-                                set porturl ${source_url}/$portinfo(portdir)
-                            }
-                            if {[info exists porturl]} {
-                                lappend line porturl $porturl
-                                ui_debug "Found port in $porturl"
-                            } else {
-                                ui_debug "Found port info: $line"
-                            }
-                            lappend matches $name
-                            lappend matches $line
-                        }
+                        set macports::mportsearch_portindex([list $sourceURL $name]) $line
                     }
                 } catch {*} {
                     ui_warn "It looks like your PortIndex file may be corrupt."
@@ -1647,9 +1582,83 @@
             }
         }
     }
+
     if {!$found} {
         return -code error "No index(es) found! Have you synced your source indexes?"
     }
+}
+
+proc mportsearch {pattern {case_sensitive yes} {matchstyle regexp} {field name}} {
+    global macports::mportsearch_portindex
+    if {![array exists macports::mportsearch_portindex]} {
+        mportsearch_load_portindex
+    }
+
+    set matches [list]
+    set easy [expr { $field == "name" }]
+    
+    foreach {port_url_and_name line} [array get macports::mportsearch_portindex] {
+        array unset portinfo
+        set source [lindex $port_url_and_name 0]
+        set name [lindex $port_url_and_name 1]
+        if {$easy} {
+            set target $name
+        } else {
+            array set portinfo $line
+            if {![info exists portinfo($field)]} continue
+            set target $portinfo($field)
+        }
+
+        switch $matchstyle {
+            exact {
+                set matchres [expr 0 == ( {$case_sensitive == "yes"} ? [string compare $pattern $target] : [string compare -nocase $pattern $target] )]
+            }
+            glob {
+                set matchres [expr {$case_sensitive == "yes"} ? [string match $pattern $target] : [string match -nocase $pattern $target]]
+            }
+            regexp -
+            default {
+                set matchres [expr {$case_sensitive == "yes"} ? [regexp -- $pattern $target] : [regexp -nocase -- $pattern $target]]
+            }
+        }
+        
+        if {$matchres == 1} {
+            if {$easy} {
+                array set portinfo $line
+            }
+            switch -regexp -- [macports::getprotocol ${source}] {
+                {^rsync$} {
+                    # Rsync files are local
+                    set source_url "file://[macports::getsourcepath $source]"
+                }
+                {^https?$|^ftp$} {
+                    if {[_source_is_snapshot $source filename extension]} {
+                        # daily snapshot tarball
+                        set source_url "file://[macports::getsourcepath $source]"
+                    } else {
+                        # default action
+                        set source_url $source
+                    }
+                }
+                default {
+                    set source_url $source
+                }
+            }
+            if {[info exists portinfo(portarchive)]} {
+                set porturl ${source_url}/$portinfo(portarchive)
+            } elseif {[info exists portinfo(portdir)]} {
+                set porturl ${source_url}/$portinfo(portdir)
+            }
+            if {[info exists porturl]} {
+                lappend line porturl $porturl
+                ui_debug "Found port in $porturl"
+            } else {
+                ui_debug "Found port info: $line"
+            }
+            lappend matches $name
+            lappend matches $line
+        }
+    }
 
     return $matches
 }

