anchor.diff:  (in-package :cl-notebook)
   
   (define-closing-handler (js/base.js :content-type "application/javascript") ()
  -  (ps (defun dom-ready (callback)
  -	(chain document (add-event-listener "DOMContentLoaded" callback)))
  -
  -      (defun identity (thing) thing)
  -
  -      (defun map (fn thing)
  -	(if (object? thing)
  -	    (let ((res (-array)))
  -	      (for-in (k thing) (chain res (push (fn (aref thing k) k))))
  -	      res)
  -	    (loop for elem in thing collect (fn elem))))
  -
  -      (defun filter (fn thing)
  -	
  -	(loop for elem in thing when (fn elem) collect elem))
  -
  -      (defun append-new (list-a list-b)
  -	(let ((s (new (-set list-a))))
  -	  (chain 
  -	   list-a 
  -	   (concat
  -	    (loop for elem in list-b
  -	       unless (chain s (has elem))
  -	       collect elem)))))
  -
  -      (defun vals (obj) (map identity obj))
  -
  -      (defun prevent (ev) (chain ev (prevent-default)))
  -      
  -      (defun by-selector (selector)
  -	(chain document (query-selector selector)))
  -      (defun by-selector-all (selector)
  -	(chain document (query-selector-all selector)))
  -
  -      (defun rest (array) (chain array (slice 1)))
  -
  -      (defun get-page-hash ()
  -	(let ((hash (@ window location hash))
  -	      (res (create)))
  -	  (when hash
  -	    (loop for pair in (chain (rest hash) (split "&"))
  -	       for (k v) = (chain pair (split "="))
  -	       do (setf (aref res (decode-u-r-i k)) (decode-u-r-i v)))
  -	    res)))
  -	   
  -      (defun set-page-hash (hash-object)
  -	(setf (@ window location hash) (obj->params hash-object)))
  -
  -      (defun dom-append (elem markup)
  -	(let ((new-content (chain document (create-element "div"))))
  -	  (setf (@ new-content inner-h-t-m-l) markup)
  -	  (loop while (@ new-content first-child)
  -	     do (chain elem (append-child (@ new-content first-child))))))
  -
  -      (defun escape (string)
  -	(chain string
  -	       (replace "<" "&lt;")
  -	       (replace ">" "&gt;")))
  -
  -      (defun dom-set (elem markup)
  -	(setf (@ elem inner-h-t-m-l) markup))
  -
  -      (defun number? (obj) (string= "number" (typeof obj)))
  -      (defun string? (obj) (string= "string" (typeof obj)))
  -      (defun function? (obj) (string= "function" (typeof obj)))
  -
  -      (defun type? (obj type-string)
  -	(eql (chain -object prototype to-string (call obj)) type-string))
  -      (defun object? (obj) (type? obj "[object Object]"))
  -
  -      (defun join (strings &optional (separator "")) (chain strings (join separator)))
  -
  -      (defun encode (string)
  -	(encode-u-r-i-component string))
  -     
  -      (defun string->obj (string)
  -	(chain -j-s-o-n (parse string)))
  -
  -      (defun obj->string (object)
  -	(chain -j-s-o-n (stringify object)))
  -
  -      (defun obj->params (object)
  -	(join 
  -	 (map (lambda (v k) 
  -		(+ (encode k) "=" 
  -		   (encode (if (object? v) (obj->string v) v))))
  -	      object)
  -	 "&"))
  -
  -      (defun get (uri params callback)
  -	(let ((req (new (-x-m-l-http-request))))
  -	  (setf (@ req onreadystatechange)
  -		(lambda ()
  -		  (when (and (equal (@ req ready-state) 4)
  -			     (equal (@ req status) 200))
  -		    (let ((result (@ req response-text)))
  -		      (callback result)))))
  -	  (chain req (open :GET (if params (+ uri "?" (obj->params params)) uri) t))
  -	  (chain req (send))))
  -
  -      (defun post (uri params callback)
  -	(let ((req (new (-x-m-l-http-request)))
  -	      (encoded-params (obj->params params)))
  -	  (setf (@ req onreadystatechange)
  -		(lambda ()
  -		  (when (and (equal (@ req ready-state) 4)
  -			     (equal (@ req status) 200))
  -		    (let ((result (@ req response-text)))
  -		      (when (function? callback)
  -			(callback result))))))
  -	  (chain req (open :POST uri t))
  -	  (chain req (set-request-header "Content-type" "application/x-www-form-urlencoded"))
  -	  (chain req (set-request-header "Content-length" (length encoded-params)))
  -	  (chain req (set-request-header "Connection" "close"))
  -	  (chain req (send encoded-params))))
  -
  -      (defun post/json (uri params callback)
  -	(post uri params
  -	      (lambda (raw)
  -		(let ((res (string->obj raw)))
  -		  (callback res)))))))
  +  (ps 
  +    ;; basic functional stuff
  +    (defun identity (thing) thing)
  +
  +    (defun rest (array) (chain array (slice 1)))
  +
  +    (defun map (fn thing)
  +      (if (object? thing)
  +	  (let ((res (-array)))
  +	    (for-in (k thing) (chain res (push (fn (aref thing k) k))))
  +	    res)
  +	  (loop for elem in thing collect (fn elem))))
  +
  +    (defun filter (fn thing)	
  +      (loop for elem in thing when (fn elem) collect elem))
  +
  +    (defun append-new (list-a list-b)
  +      (let ((s (new (-set list-a))))
  +	(chain 
  +	 list-a 
  +	 (concat
  +	  (loop for elem in list-b
  +	     unless (chain s (has elem))
  +	     collect elem)))))
  +
  +    (defun join (strings &optional (separator "")) (chain strings (join separator)))
  +
  +    ;; basic hash stuff
  +    (defun vals (obj) (map identity obj))
  +
  +    ;; basic DOM/event stuff
  +    (defun prevent (ev) (chain ev (prevent-default)))
  +
  +    (defun dom-ready (callback)
  +      (chain document (add-event-listener "DOMContentLoaded" callback)))
  +
  +    (defun by-selector (selector)
  +      (chain document (query-selector selector)))
  +    (defun by-selector-all (selector)
  +      (chain document (query-selector-all selector)))
  +
  +    (defun escape (string)
  +      (chain string
  +	     (replace "<" "&lt;")
  +	     (replace ">" "&gt;")))
  +
  +    (defun dom-append (elem markup)
  +      (let ((new-content (chain document (create-element "div"))))
  +	(setf (@ new-content inner-h-t-m-l) markup)
  +	(loop while (@ new-content first-child)
  +	   do (chain elem (append-child (@ new-content first-child))))))
  +
  +    (defun dom-set (elem markup)
  +      (setf (@ elem inner-h-t-m-l) markup))
  +
  +    ;; basic type stuff
  +    (defun number? (obj) (string= "number" (typeof obj)))
  +    (defun string? (obj) (string= "string" (typeof obj)))
  +    (defun function? (obj) (string= "function" (typeof obj)))
  +
  +    (defun type? (obj type-string)
  +      (eql (chain -object prototype to-string (call obj)) type-string))
  +    (defun object? (obj) (type? obj "[object Object]"))
  +
  +    ;; basic encoding/decoding stuff
  +    (defun encode (string)
  +      (encode-u-r-i-component string))
  +
  +    (defun decode (string)
  +      (decode-u-r-i string))
  +    
  +    (defun string->obj (string)
  +      (chain -j-s-o-n (parse string)))
  +
  +    (defun obj->string (object)
  +      (chain -j-s-o-n (stringify object)))
  +
  +    (defun obj->params (object)
  +      (join 
  +       (map (lambda (v k) 
  +	      (+ (encode k) "=" 
  +		 (encode (if (object? v) (obj->string v) v))))
  +	    object)
  +       "&"))
  +
  +    ;; basic AJAX stuff
  +    (defun get-page-hash ()
  +      (let ((hash (@ window location hash))
  +	    (res (create)))
  +	(when hash
  +	  (loop for pair in (chain (rest hash) (split "&"))
  +	     for (k v) = (chain pair (split "="))
  +	     do (setf (aref res (decode k)) (decode v)))
  +	  res)))
  +    
  +    (defun set-page-hash (hash-object)
  +      (setf (@ window location hash) (obj->params hash-object)))
  +
  +    (defun get (uri params callback)
  +      (let ((req (new (-x-m-l-http-request))))
  +	(setf (@ req onreadystatechange)
  +	      (lambda ()
  +		(when (and (equal (@ req ready-state) 4)
  +			   (equal (@ req status) 200))
  +		  (let ((result (@ req response-text)))
  +		    (callback result)))))
  +	(chain req (open :GET (if params (+ uri "?" (obj->params params)) uri) t))
  +	(chain req (send))))
  +
  +    (defun post (uri params callback)
  +      (let ((req (new (-x-m-l-http-request)))
  +	    (encoded-params (obj->params params)))
  +	(setf (@ req onreadystatechange)
  +	      (lambda ()
  +		(when (and (equal (@ req ready-state) 4)
  +			   (equal (@ req status) 200))
  +		  (let ((result (@ req response-text)))
  +		    (when (function? callback)
  +		      (callback result))))))
  +	(chain req (open :POST uri t))
  +	(chain req (set-request-header "Content-type" "application/x-www-form-urlencoded"))
  +	(chain req (set-request-header "Content-length" (length encoded-params)))
  +	(chain req (set-request-header "Connection" "close"))
  +	(chain req (send encoded-params))))
  +
  +    (defun post/json (uri params callback)
  +      (post uri params
  +	    (lambda (raw)
  +	      (let ((res (string->obj raw)))
  +		(callback res)))))))
   
   (define-closing-handler (js/main.js :content-type "application/javascript") ()
     (ps
  +    ;; DOM templates
       (defun result-template (result)
         (when result
   	(+ (if (@ result :stdout) (who-ps-html (:p :class "stdout" (@ result :stdout))) "")
  
positive.code: 		 (by-selector (+ "#cell-" cell-id " .cell-contents"))
  		 options)))
  	mirror))
      
      (defvar *notebook*)
  
      (defun notebook-condense (notebook)
  
negative.code: 	  (:ul :class "cells"
  	       (join (map (lambda (cell) (cell-template cell))
  			  (notebook-cells notebook))))))
  
  
      ;; AJAX calls
      (defun server/eval (thing target-elem)
        (post/json "/eval" (create :thing thing)
  	    (lambda (res) (dom-set target-elem (result-template res)))))
  