Skip to content

Instantly share code, notes, and snippets.

@v2e4lisp
Forked from Wilfred/show-your-colors.el
Last active December 17, 2015 00:19
Show Gist options
  • Save v2e4lisp/5520129 to your computer and use it in GitHub Desktop.
Save v2e4lisp/5520129 to your computer and use it in GitHub Desktop.
updated-version of show-your-colors.el
;;; show-your-colours --- an HTTP server showing the current buffer fontified
;; Author: Wilfred Hughes <me@wilfred.me.uk>
;; Version: 0.1
;; Package-Requires: ((s "1.3.0"))
;;; Commentary:
;; Allow others to see what you're editing in Emacs. For live
;; screencasts, or general Emacs advocacy. If you have multiple
;; buffers open, this will show the currently focused buffer.
;; FIXME: Crashes with rainbow-delimeters.
;; UPDATE:
;; * We don't need `syc--buffer' any more. Because in fact
;; we can get the `current-buffer' by a liitle trick. (see the syc--buffer-as-html)
;; * syc--buffer-as-html is modified a little bit to make it simple and clear
;; * show-your-colours-buffer is deleted for not being used.
;; * advises are all deleted . We don't need them any more.
(require 'elnode)
(require 's)
(defvar syc--port 9000)
;; todo: is there a way we can just activate our advice?
;; DONE: Yes, you can put `activate' as an arg in defadvice.
;; e.g :
;; (defadvice ido-switch-buffer (after set-syc--buffer activate)
;; "Set `syc--buffer' to the current buffer.
;; Since `switch-to-buffer' is very low-level and used everywhere,
;; we assume a buffer change by a user always uses `ido-switch-buffer"
;; (setq syc--buffer (current-buffer)))
;; todo: automatic reload
;; DONE.
(defun syc--buffer-as-html ()
"Get the current buffer and return a string of HTML.
The HTML is styled according to the buffer's fontification."
(condition-case nil
;; While you cannot use `current-buffer' to actually get the current buffer,
;; since the elnode will automatically create a temp buffer for http connection.
;; What you can do is something like this: `(car (buffer-list))', this will return
;; the buffer you're editing or viewing, that's what we want.
;; Little trick, Done!
(with-current-buffer (car (buffer-list))
(with-current-buffer (htmlize-region (window-start) (window-end))
(buffer-substring-no-properties (point-min) (point-max))))
(error "<html><body>Could not fontify buffer.</body></html>")))
(defun syc--append-reload (html)
"Given a piece of HTML, return a new string with
a JS automatic reload script appended."
(s-replace
"</body>"
"<script type=\"text/javascript\">setTimeout(function() { location.reload(true); }, 100);</script>
</body>"
html))
;; todo: why can't I let-bind this inside syc--handler?
(setq elnode-error-log-to-messages nil)
(defun syc--handler (httpcon)
(elnode-http-start httpcon 200 '("Content-Type" . "text/html"))
(elnode-http-return httpcon (syc--append-reload
(syc--buffer-as-html))))
(elnode-start 'syc--handler :port 9000 :host "localhost")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment