     (set (make-local-variable 'live-py-dir) (file-name-directory buffer-file-name))
     (set (make-local-variable 'live-py-path) nil)
     (set (make-local-variable 'live-py-version) "python")
+    (set (make-local-variable 'live-py-window-start-pos) -1)
+    (set (make-local-variable 'live-py-point-line-nr) -1)
     (add-hook 'after-change-functions 'live-py-after-change-function nil t)
     (live-py-show-output-window)
     (live-py-after-change-function 0 0 0)
-    (add-hook 'post-command-hook 'live-py-check-to-scroll nil t)
-    )
+    (add-hook 'post-command-hook 'live-py-check-to-scroll nil t))
    ;; Turning the mode OFF.
    (t
     (remove-hook 'after-change-functions 'live-py-after-change-function t)

    (with-current-buffer live-py-output-buffer
      (setq buffer-read-only 1)
      (unless reused-buffer (toggle-truncate-lines 1))))
  (live-py-synchronize-scroll)
  (set 'live-py-timer nil))


(defun live-py-synchronize-scroll ()
  "Synchronise scrolling between Python and live-py buffers."
  (let ((code-window-start (+ (count-lines 1 (window-start)) 1))
        (position (line-number-at-pos)))
    (unless (window-valid-p live-py-output-window)
      ;; Recreate output window.
      (live-py-show-output-window))
    (with-selected-window live-py-output-window
      (goto-line code-window-start)
      (recenter-top-bottom 0)
      (goto-line position))))


(defun live-py-check-to-scroll ()
  "Check `this-command' to see if a scroll is to be done."
  ;; Take extra care to not let this function run into a non-handled error.
  ;; On such an error the debugger will not be entered to not block Emacs
  ;; interactivity when `debug-on-error' is active, so it is easily possible
  ;; to miss the error. And on such an error the function will be removed
  ;; from `post-command-hook' which can be quite confusing.
  (cond ((memq this-command '(next-line
                              previous-line
                              scroll-up
                              scroll-up-command
                              scroll-down
                              scroll-down-command
                              beginning-of-buffer
                              end-of-buffer))
         (if (buffer-live-p (and live-py-output-buffer
                                 (get-buffer live-py-output-buffer)))
             (progn
               (unless (window-valid-p live-py-output-window)
                 ;; Recreate output window.
                 (live-py-show-output-window))
               (set-window-buffer live-py-output-window
                                  live-py-output-buffer)
               (live-py-synchronize-scroll))
           ;; Recreate output buffer and output window.
           (live-py-trace)))))

(defun live-py-show-output-window ()
  "Show the live-py output window."

    (ignore-errors (kill-buffer live-py-output-buffer))
    (when (window-valid-p live-py-output-window)
      (delete-window live-py-output-window))
    (toggle-truncate-lines 0))))

(provide 'live-py-mode)

