 
     constructor: ->
         @$box = $("#search-box")
-        @$input = $("#search-input")
-        @$input.keyup (e) =>
+        @$search = $("#search-input")
+        @$search.keyup (e) =>
             if keybord_key(e) == "enter"
-                @enter()
+                @search()
+            else if keybord_key(e) == "down"
+                @search()
+            else if keybord_key(e) == "up"
+                @search(false)
+            @$search.focus()
+
+        @$replace = $("#replace-input")
+        @$replace.keyup (e) =>
+            if keybord_key(e) == "enter"
+                @replace()
+                @search()
+            else if keybord_key(e) == "down"
+                @search()
+            else if keybord_key(e) == "up"
+                @search(false)
+            @$replace.focus()
 
     envoke: =>
         esc()
         @$box.show()
-        @$input.focus()
-        @$input[0].selectionStart = 0
-        @$input[0].selectionEnd = @$input.val().length
-
-    enter: ->
-        query = @$input.val()
-
-        at = editor.$pad[0].selectionStart
-        end = editor.$pad[0].selectionEnd
-
-        bottom = editor.text[at+1...]
-        pos = bottom.indexOf(query)
-        if pos != -1
-            at = at + pos
+        @$search.focus()
+        @$search[0].selectionStart = 0
+        @$search[0].selectionEnd = @$search.val().length
+
+    search: (down=true)->
+        query = @$search.val()
+        [text, [at, end], scroll] = editor.get_text_state()
+        if down
+            bottom = text[at+1...]
+            pos = bottom.indexOf(query)
+            if pos != -1
+                at = at + pos + 1
+            else
+                pos = text.indexOf(query)
+                if pos != -1
+                    at = pos
+                else
+                    return
         else
-            pos = editor.text.indexOf(query)
+            top = text[...at]
+            pos = top.lastIndexOf(query)
             if pos != -1
                 at = pos
             else
-                return
-
-        editor.$pad[0].selectionStart = at + 1
-        editor.$pad[0].selectionEnd = at + query.length + 1
+                pos = text.lastIndexOf(query)
+                if pos != -1
+                    at = pos
+                else
+                    return
+
+        editor.$pad[0].selectionStart = at
+        editor.$pad[0].selectionEnd = at + query.length
         editor.update()
         editor.scroll_pos(at)
-        @$input.focus()
+
+    replace: ->
+        [text, [start, end], scroll] = editor.get_text_state()
+        query = @$search.val()
+        print "[", text.length, "]", start, end, text[start...end], "==", query
+        if text[start...end] == query
+            replace = @$replace.val()
+            text = text[...start] + replace + text[end...]
+            editor.set_text_state([text, [start, start + replace.length], scroll])
 
 
 class MiniMap

        return [start, end]

    tab: =>
        if @autocomplete()
            return
        @indent()

    autocomplete: =>
        [text, [start, end], s] = @get_text_state()

        @update()

    get_text_state: () ->
        start = @$pad[0].selectionStart
        end = @$pad[0].selectionEnd
        if start > end
            [start, end] = [end, start]
        text_state = [
            @$pad.val(),
            [start, end],
            @$holder.scrollTop(),
        ]
        print "get scrollTop", @$holder.scrollTop()
