               (with-current-buffer buffer
                 (cond ((not err)
                        (dolist (file files)
-                         (when (equal (cdr (assoc 'type file)) "full")
-                           (with-current-buffer (find-file-noselect (concat tern-project-dir (cdr (assoc 'name file))))
+                         (when (equal (cdr (assq 'type file)) "full")
+                           (with-current-buffer (find-file-noselect (concat tern-project-dir (cdr (assq 'name file))))
                              (setf tern-buffer-is-dirty nil))))
                        (funcall f data offset))
                       ((and (eq (cadar err) 'connection-failed) (not retrying))

        (tern-run-request #'tern-do-complete "completions" (point)))))

(defun tern-do-complete (data offset)
  (let ((cs (loop for elt across (cdr (assoc 'completions data)) collect (cdr (assoc 'name elt))))
        (start (+ 1 offset (cdr (assoc 'from data))))
        (end (+ 1 offset (cdr (assoc 'to data)))))
    (setf tern-last-completions (list (buffer-substring-no-properties start end) start end cs))
    (completion-in-region start end cs)))


     ((not tern-buffer-is-dirty) (setf file-name (tern-project-relative-file)))
     ((and (not (eq mode :full-file)) (> (buffer-size) 8000))
      (push (tern-get-partial-file pos) files)
      (setf offset (cdr (assq 'offset (car files)))
            file-name "#0")
      (decf pos offset))
     (t
      (push `((type . "full") (text . ,(buffer-string)) (name . ,(tern-project-relative-file))) files)
      (setf file-name (tern-project-relative-file))))
    (when files (push `(files . ,(apply #'vector files)) doc))
    (push `(file . ,file-name) (cdr (assq 'query doc)))
    (push `(end . ,(1- pos)) (cdr (assq 'query doc)))
    (setf callback
          (lambda (port)
            (if port
