dunlin

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

search.fnl (1465B)


      1 (local { : Gtk  } (require :lgi))
      2 (local { : view } (require :fennel))
      3 (local lume (require :lume))
      4 
      5 (local {: define-command : completion } (require :command))
      6 (local Buffer (require :buffer))
      7 
      8 (fn url-escape [s]
      9   (string.gsub s
     10                "([^A-Za-z0-9_])"
     11                #(string.format "%%%02x" (string.byte $1))))
     12 
     13 (local searchers
     14        {:ddg
     15         {:name "Duck Duck Go"
     16          :function (fn [term]
     17                      (.. "https://duckduckgo.com/?q=" (url-escape term)))
     18          }
     19         :google
     20         {:name "Google"
     21          :function (fn [term]
     22                      (.. "https://www.google.com/search?q=" (url-escape term)))
     23          }
     24         :luai
     25         {:name "Lua interactive reference manual"
     26          :function (fn [term]
     27                      (.. "https://pgl.yoyo.org/luai/i/" (url-escape term)))
     28          }
     29         })
     30 
     31 
     32 (fn match-engines [term]
     33   (icollect [keyword {: name  : function} (pairs searchers)]
     34     (if (string.match keyword term)
     35         (completion {
     36                      :text keyword
     37                      :value keyword
     38                      :widget (Gtk.Label { :label name })
     39                      }))))
     40 
     41 
     42 (define-command
     43  "search"
     44  [[:term
     45    #[(completion {:text $1})]
     46    #""]
     47   [:buffer
     48    #(lume.map (Buffer.match $1) #(completion { :text $1.name :value $1 }))
     49    #$1.buffer.name]
     50   [:engine match-engines  #:ddg]]
     51  (fn [{: term : engine : buffer}]
     52    (buffer:visit ((. (. searchers engine) :function) term))))