 ;;; Code parsing:
 
 (defsubst geiser-syntax--skip-comment/string ()
-  (goto-char (or (nth 8 (syntax-ppss)) (point))))
+  (let ((pos (nth 8 (syntax-ppss))))
+    (goto-char (or pos (point)))
+    pos))
 
 (defsubst geiser-syntax--nesting-level ()
   (or (nth 0 (syntax-ppss)) 0))

(defsubst geiser-syntax--normalize (vars)
  (mapcar (lambda (i) (if (listp i) (car i) i)) vars))

(defun geiser-syntax--scan-locals (bfs sbfs form partial locals)
  (let ((form (if (listp form) (geiser-syntax--normalize form) form)))
    (cond ((or (null form) (not (listp form)))
           (geiser-syntax--normalize locals))
          ((not (geiser-syntax--binding-form-p bfs sbfs (car form)))
           (geiser-syntax--scan-locals bfs sbfs
                                       (car (last form)) partial locals))
          (t
           (let* ((head (car form))
                  (name (geiser-syntax--if-symbol (cadr form)))
                  (names (if name (geiser-syntax--if-list (caddr form))
                           (geiser-syntax--if-list (cadr form))))
                  (rest (if name (cdddr form) (cddr form)))
                  (use-names (or rest
                                 (not partial)
                                 (geiser-syntax--binding-form*-p sbfs
                                                                 head))))
             (when name (push name locals))
             (when use-names (dolist (n names) (push n locals)))
             (dolist (f (butlast rest))
               (when (and (listp f) (eq (car f) 'define))
                 (push (cadr f) locals)))
             (geiser-syntax--scan-locals bfs sbfs
                                         (car (last (or rest names)))
                                         partial
                                         locals))))))

(defun geiser-syntax--locals-around-point (bfs sbfs)
  (when (eq major-mode 'scheme-mode)
    (save-excursion
      (geiser-syntax--skip-comment/string)
      (let ((boundary (point)))
        (while (not (zerop (geiser-syntax--nesting-level)))
          (backward-up-list))
        (multiple-value-bind (form end)
            (geiser-syntax--form-after-point boundary)
          (geiser-syntax--scan-locals bfs sbfs form (> end boundary) '()))))))



    (nreverse path)))

(defsubst geiser-syntax--binding-form-p (bfs sbfs f)
  (or (memq f '(define define* lambda let let* letrec parameterize))
      (memq f bfs)
      (memq f sbfs)))

