diff --git a/command.fnl b/command.fnl index e8ee304..5185828 100644 --- a/command.fnl +++ b/command.fnl @@ -69,34 +69,6 @@ #$1.buffer.name]] (fn [{: buffer}] (buffer:back))) -(fn url-escape [s] - (string.gsub s - "([^A-Za-z0-9_])" - #(string.format "%%%02x" (string.byte $1)))) - -(local searchers { - :ddg (fn [term] - (.. "https://duckduckgo.com/?q=" (url-escape term))) - }) - - -(define-command - "search" - [[:term - #[(completion {:text $1})] - #""] - [:buffer - #(lume.map (Buffer.match $1) #(completion { :text $1.name :value $1 })) - #$1.buffer.name] - [:engine - #[(completion {:text "Duck Duck Go" :value :ddg}) - (completion {:text "Google" :value :google}) - (completion {:text "Luai (yoyo.org)" :value :luai})] - #"Duck Duck Go"]] - (fn [{: term : engine : buffer}] - (buffer:visit ((. searchers engine) term)))) - - (fn find-command [name] (. commands name)) diff --git a/dunlin.fnl b/dunlin.fnl index 2935ab0..d7a62c8 100644 --- a/dunlin.fnl +++ b/dunlin.fnl @@ -6,6 +6,8 @@ (local Frame (require :frame)) (local Buffer (require :buffer)) +(local search (require :search)) + ;;; when we decide how to do an init file/rc file, this will go in it (local my-keymap { diff --git a/search.fnl b/search.fnl new file mode 100644 index 0000000..eb38e53 --- /dev/null +++ b/search.fnl @@ -0,0 +1,52 @@ +(local { : Gtk } (require :lgi)) +(local { : view } (require :fennel)) +(local lume (require :lume)) + +(local {: define-command : completion } (require :command)) +(local Buffer (require :buffer)) + +(fn url-escape [s] + (string.gsub s + "([^A-Za-z0-9_])" + #(string.format "%%%02x" (string.byte $1)))) + +(local searchers + {:ddg + {:name "Duck Duck Go" + :function (fn [term] + (.. "https://duckduckgo.com/?q=" (url-escape term))) + } + :google + {:name "Google" + :function (fn [term] + (.. "https://www.google.com/search?q=" (url-escape term))) + } + :luai + {:name "Lua interactive reference manual" + :function (fn [term] + (.. "https://pgl.yoyo.org/luai/i/" (url-escape term))) + } + }) + + +(fn completions [term] + (icollect [keyword {: name : function} (pairs searchers)] + (if (string.match keyword term) + (completion { + :text keyword + :value keyword + :widget (Gtk.Button { :label name }) + })))) + + +(define-command + "search" + [[:term + #[(completion {:text $1})] + #""] + [:buffer + #(lume.map (Buffer.match $1) #(completion { :text $1.name :value $1 })) + #$1.buffer.name] + [:engine completions #:ddg]] + (fn [{: term : engine : buffer}] + (buffer:visit ((. (. searchers engine) :function) term))))