 ;; URL: http://ternjs.net/
 ;; Version: 0.0.1
 ;; Package-Requires: ((json "1.2") (cl-lib "0.5") (emacs "24"))
+
+;;; Code:
 
 (require 'cl-lib)
 (require 'json)

  (set (make-local-variable 'tern-buffer-is-dirty) (and (buffer-modified-p) (cons (point-min) (point-max))))
  (make-local-variable 'completion-at-point-functions)
  (push 'tern-completion-at-point completion-at-point-functions)
  (add-hook 'before-change-functions 'tern-before-change nil t)
  (add-hook 'post-command-hook 'tern-post-command nil t)
  (add-hook 'buffer-list-update-hook 'tern-left-buffer nil t))

(defun tern-mode-disable ()
  (setf completion-at-point-functions
        (remove 'tern-completion-at-point completion-at-point-functions))
  (remove-hook 'before-change-functions 'tern-before-change t)
  (remove-hook 'post-command-hook 'tern-post-command t)
  (remove-hook 'buffer-list-update-hook 'tern-left-buffer t))



;; Mode plumbing

(defun tern-after-change (start end prev-length)
  "Track the dirty area of the buffer."
  (if tern-buffer-is-dirty
      (setf tern-buffer-is-dirty (cons (min start (car tern-buffer-is-dirty))
                                       (max end   (cdr tern-buffer-is-dirty))))
    (setf tern-buffer-is-dirty (cons start end))))

(defvar tern-idle-time 1
  "The time Emacs is allowed to idle before updating Tern's representation of the file.")
(defvar tern-idle-timer nil
  "The timer on which `tern-reparse-on-idle' runs.")
(defun tern-reparse-on-idle ()
  "Do some mode plumbing and refresh tern's representation of the buffer."
  (when tern-mode
    (setf tern-last-point-pos nil)
    (when (and tern-last-argument-hints tern-buffer-is-dirty
               (<= (car tern-buffer-is-dirty) (car tern-last-argument-hints)))
      (setf tern-last-argument-hints nil))
    (when (and tern-last-completions tern-buffer-is-dirty
               (<= (car tern-buffer-is-dirty) (cadr tern-last-completions)))
      (setf tern-last-completions nil))
    (tern-send-buffer-to-server)))

(defun tern-post-command ()
  (unless (eq (point) tern-last-point-pos)
