extract search into a module, improve it
This commit is contained in:
parent
fe25203bad
commit
4b78434ea4
28
command.fnl
28
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))
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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))))
|
Loading…
Reference in New Issue