!7 module SimpleSnippetSearch { InputChooser inputChooser = new(this).setDelay(1000); bool fullTextSearch; // look at snippet texts too bool searchInBackground; // search when window is hidden transient int maxInputLength = 120; transient int maxResults = 1000; transient S query; transient L result; transient L snippetResults; transient WaitForStableValue waitForStableValue = new(1.0); transient volatile bool calculating; transient JTable table; transient ReliableSingleThread rst; transient SimpleLiveValue lvQuery = stringLiveValue(); transient SimpleLiveValue lvResultsTitle = stringLiveValue(); visualize { SimpleLiveValue lvFullTextSearch = dm_fieldLiveValue('fullTextSearch); lvFullTextSearch.onChange(r forceNewSearch); table = sexyTable(); tablePopupMenuItemsThreaded_top(table, "Edit", voidfunc(int i) enter { Snippet s = _get(snippetResults, i); dm_openSnippetInEditor(s.id); }, "Show in browser", voidfunc(int i) enter { Snippet s = _get(snippetResults, i); openURLInBrowser("http://tinybrain.de/" + psI(s.id)); }, "Load Module", voidfunc(int i) enter { Snippet s = _get(snippetResults, i); dm_addDynamicModuleDialog2(s); }); ret northAndCenter(wrap(inputChooser), withRightAlignedButtons( jLiveValueSection(lvResultsTitle, dataToTable_uneditable(result, table)), jLiveValueCheckBox("Full Text Search", lvFullTextSearch))); } S switchableFields() { ret 'searchInBackground; } void forceNewSearch { setField(query := null); } start { lvQuery.set(query); doEvery(500, rstUpdate()); } void update { inputChooser.update(); calc(inputChooser.input()); } void calc(S q) { if (!allowedToSearch() || eq(query, q) || empty(q)) ret; query = q; result = null; lvQuery.set(q); tempSetField(this, calculating := true); tbSearch_v2_quick.set(!fullTextSearch); tbSearch_v2_html.set("want"); // We want the HTML too L l = tbSearch_v2(maxResults, q); S html = tbSearch_v2_html!; S desc = regexpExtractIC("Found (\\d+\\+?) snippets?", html); lvResultsTitle.set(or(desc, "No snippet results") + " for " + quote(q) + (fullTextSearch ? " (full text search)" : "")); snippetResults = l; result = map(l, func(Snippet s) -> SS { litorderedmap("ID + Title" := snippetWithTitle(s) /*, "Type" := s.type*/ )}); JTable t = table; if (t != null) dataToTable_uneditable(result, t); } bool allowedToSearch() enter { ret searchInBackground || dm_isVisible(); } }