Emacs Smooth Scrolling

If you want Emacs to scroll up or down one line at a time, setting the four scroll-* variables doesn't help. Instead, I implement it with the function move-to-window-line as follows:

First,

(setq truncate-lines t)

or put (setq default-truncate-lines t) in .emacs, and then

(defun point-of-beginning-of-bottom-line ()
  (save-excursion
    (move-to-window-line -1)
    (point)))

(defun point-of-beginning-of-line ()
  (save-excursion
    (beginning-of-line)
    (point)))

(defun next-one-line () (interactive)
  (if (= (point-of-beginning-of-bottom-line) (point-of-beginning-of-line))
      (progn (scroll-up 1)
             (next-line 1))
    (next-line 1)))

(defun point-of-beginning-of-top-line ()
  (save-excursion
    (move-to-window-line 0)
    (point)))

(defun previous-one-line () (interactive)
  (if (= (point-of-beginning-of-top-line) (point-of-beginning-of-line))
      (progn (scroll-down 1)
             (previous-line 1))
    (previous-line 1)))

(global-set-key (kbd "<down>") 'next-one-line)
(global-set-key (kbd "<up>") 'previous-one-line)

Still, this script fails in some special modes, such as in Tuareg interactive ocaml mode (M-X tuareg-run-caml), but it should work in most programming modes.