index rss mastodon twitter github linkedin email
Álvaro Ramírez

Álvaro Ramírez

06 June 2020 Emacs, search


Paul Hudson authors excellent Swift material at I regularly land on the site while searching for snippets from the browser. I was wondering if I could search for snippets directly from Emacs.

Turns out, hackingwithswift uses a JSON HTTP request for querying code examples. With this in mind, we can use ivy-read like Oleh Krehel's counsel-search and search for Swift snippets from our favorite editor:

(require 'request)
(require 'json)

(defun ar/counsel-hacking-with-swift-search ()
  "Ivy interface to query"
  (ivy-read "hacking with swift: "
            (lambda (input)
               (let ((request-curl-options (list "-H" (string-trim (url-http-user-agent-string)))))
                   :type "GET"
                   :params (list
                            (cons "search" input))
                   :parser 'json-read
                   :success (cl-function
                             (lambda (&key data &allow-other-keys)
                                (mapcar (lambda (item)
                                          (let-alist item
                                            (propertize .title 'url .url)))
            :action (lambda (selection)
                      (browse-url (concat ""
                                          (get-text-property 0 'url selection))))
            :dynamic-collection t
            :caller 'ar/counsel-hacking-with-swift-search))