         [:.created (document/format-datetime modified)])
       (om/build tags/tags-list (:tags document))]]))
 
+(defn- run-search! [search owner]
+  (if-let [query (search/parse-query-string search)]
+    (async/take! (api/search-documents query)
+                 (fn [documents]
+                   (println "Found documents:" documents)
+                   (om/set-state! owner :documents documents)))
+    (om/set-state! owner :documents nil))
+  ;; Set the node's value to the right string
+  (when-let [node (om/get-node owner "search")]
+    (set! (.-value node) search)))
+
 (ui/defcomponent search-ui [[state column] owner]
-  (will-update [_ next-props next-state]
+  (will-mount [_]
+    ;; Run search (if we have a search string in the url)
+    (when-let [search (:search column)]
+      (run-search! search owner)))
+  (will-update [_ [next-props next-column] next-state]
+    ;; Run search if the search-string changes (note that an empty or
+    ;; nil search-string is fine and will just clear the results)
+    (let [search (:search next-column)]
+      (when-not (= search (:search column))
+        (run-search! search owner)))
+    ;; Fetch documents after the results of the search arrive
     (when-let [documents (:documents next-state)]
-      (when-not (= documents  (om/get-render-state owner :documents))
+      (when-not (= documents (om/get-render-state owner :documents))
+        ;; TODO: Only fetch documents we don't have locally
         (api/fetch-documents! documents))))
   (render-state [_ {:keys [documents]}]
     [:.column.search


(declare search-ui)

(defrecord SearchColumn [id]
  SpecialColumn
  (special-column-ui [_] search-ui))


  (let [entries (s/split param #",")
        pairs (mapv #(s/split % #":") entries)]
    (for [[k v] pairs]
      (if-let [key (case k
                       "d" :document
                       "f" :file
                       "i" :inbox
                       "n" :new-document
                       "s" :search
                       nil)]
        (let [value (if (= :search key)
                      (if v (gstring/urlDecode v) "")
                      (try (let [n (js/parseInt v 10)]
                             (when (integer? n) n))
                           (catch js/Error e nil)))]
          [key value])
        (js/console.warn "Invalid column-spec: " (str k))))))

(defn- serialize-column-param [columns]
  (s/join "," (for [column columns]
