Skip to content

Instantly share code, notes, and snippets.

@0robustus1
Created October 20, 2016 20:16
Show Gist options
  • Save 0robustus1/1379cb5eae2ae40a2c612a63f13f33fe to your computer and use it in GitHub Desktop.
Save 0robustus1/1379cb5eae2ae40a2c612a63f13f33fe to your computer and use it in GitHub Desktop.
core of opener.el
(defun opener-open-url-in-buffer (url)
; let's do a GET request to the url
(request
url
:parser 'buffer-string
; get response as a string and pass it together with an appropriate
; "file"-name to our buffer function
:complete (let ((buffer-name (opener-filename-for url)))
(function*
(lambda (&key data &allow-other-keys)
(opener-http-response-in-buffer buffer-name data))))))
(defun opener-http-response-in-buffer (buffer-name data)
; let's get a buffer, a storage solution that can be displayed inside
; of a window. Think of it like a virtual file.
(let ((buffer (get-buffer-create buffer-name)))
; ensure that we can return to where we started from
(save-excursion
; claim our new buffer as the current one for all operations
(with-current-buffer buffer
; clear our buffer (as might be necessary when we want to "refresh" a page)
(erase-buffer)
; activate multi-byte character support (necessary for unzipping)
(if enable-multibyte-characters (toggle-enable-multibyte-characters))
; write our HTTP response to the buffer
(insert data)
; try to decompress gzipped content
(zlib-decompress-region (point-min) (point-max))
; perform "file"-type like recognition in Emacs
; e.g. switches on syntax highlighting for json or XML
(normal-mode)
; perform some private hooks the user might set up, e.g. pretty printing/unminifying
(opener-perform-major-mode-hooks)))
; actually switch our current window display that new buffer
(switch-to-buffer buffer)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment