(require 'tpu-edt)
(defcustom tpu-top-scroll-margin 0
"*Scroll margin at the top of the screen.
Interpreted as a percent of the current window size."
:type 'integer
:group 'tpu)
(defcustom tpu-bottom-scroll-margin 0
"*Scroll margin at the bottom of the screen.
Interpreted as a percent of the current window size."
:type 'integer
:group 'tpu)
(defcustom tpu-backward-char-like-tpu t
"*If non-nil, in free cursor mode backward-char (left-arrow) works
just like TPU/edt. Otherwise, backward-char will move to the end of
the previous line when starting from a line beginning."
:type 'boolean
:group 'tpu)
(defvar tpu-cursor-free nil
"If non-nil, let the cursor roam free.")
(add-hook 'picture-mode-hook 'tpu-set-cursor-free)
(defun tpu-write-file-hook nil
"Eliminate whitespace at ends of lines, if the cursor is free."
(if (and (buffer-modified-p) tpu-cursor-free) (tpu-trim-line-ends)))
(or (memq 'tpu-write-file-hook write-file-functions)
(setq write-file-functions
(cons 'tpu-write-file-hook write-file-functions)))
(defun tpu-top-check (beg lines)
"Enforce scroll margin at the top of screen."
(let ((margin (/ (* (window-height) tpu-top-scroll-margin) 100)))
(cond ((< beg margin) (recenter beg))
((< (- beg lines) margin) (recenter margin)))))
(defun tpu-bottom-check (beg lines)
"Enforce scroll margin at the bottom of screen."
(let* ((height (window-height))
(margin (+ 1 (/ (* height tpu-bottom-scroll-margin) 100)))
(difference (- height margin 1)))
(cond ((> beg difference) (recenter beg))
((> (+ beg lines) difference) (recenter (- margin))))))
(defun tpu-forward-char (num)
"Move right ARG characters (left if ARG is negative)."
(interactive "p")
(if tpu-cursor-free (picture-forward-column num) (forward-char num)))
(defun tpu-backward-char (num)
"Move left ARG characters (right if ARG is negative)."
(interactive "p")
(cond ((not tpu-cursor-free)
(backward-char num))
(tpu-backward-char-like-tpu
(picture-backward-column num))
((bolp)
(backward-char 1)
(picture-end-of-line)
(picture-backward-column (1- num)))
(t
(picture-backward-column num))))
(defun tpu-next-line (num)
"Move to next line.
Prefix argument serves as a repeat count."
(interactive "p")
(let ((beg (tpu-current-line)))
(if tpu-cursor-free (or (eobp) (picture-move-down num))
(next-line-internal num))
(tpu-bottom-check beg num)
(setq this-command 'next-line)))
(defun tpu-previous-line (num)
"Move to previous line.
Prefix argument serves as a repeat count."
(interactive "p")
(let ((beg (tpu-current-line)))
(if tpu-cursor-free (picture-move-up num) (next-line-internal (- num)))
(tpu-top-check beg num)
(setq this-command 'previous-line)))
(defun tpu-next-beginning-of-line (num)
"Move to beginning of line; if at beginning, move to beginning of next line.
Accepts a prefix argument for the number of lines to move."
(interactive "p")
(let ((beg (tpu-current-line)))
(backward-char 1)
(forward-visible-line (- 1 num))
(tpu-top-check beg num)))
(defun tpu-next-end-of-line (num)
"Move to end of line; if at end, move to end of next line.
Accepts a prefix argument for the number of lines to move."
(interactive "p")
(let ((beg (tpu-current-line)))
(cond (tpu-cursor-free
(let ((beg (point)))
(if (< 1 num) (forward-line num))
(picture-end-of-line)
(if (<= (point) beg) (progn (forward-line) (picture-end-of-line)))))
(t
(forward-char)
(end-of-line num)))
(tpu-bottom-check beg num)))
(defun tpu-previous-end-of-line (num)
"Move EOL upward.
Accepts a prefix argument for the number of lines to move."
(interactive "p")
(let ((beg (tpu-current-line)))
(cond (tpu-cursor-free
(picture-end-of-line (- 1 num)))
(t
(end-of-line (- 1 num))))
(tpu-top-check beg num)))
(defun tpu-current-end-of-line nil
"Move point to end of current line."
(interactive)
(let ((beg (point)))
(if tpu-cursor-free (picture-end-of-line) (end-of-line))
(if (= beg (point)) (message "You are already at the end of a line."))))
(defun tpu-forward-line (num)
"Move to beginning of next line.
Prefix argument serves as a repeat count."
(interactive "p")
(let ((beg (tpu-current-line)))
(next-line-internal num)
(tpu-bottom-check beg num)
(beginning-of-line)))
(defun tpu-backward-line (num)
"Move to beginning of previous line.
Prefix argument serves as repeat count."
(interactive "p")
(let ((beg (tpu-current-line)))
(or (bolp) (>= 0 num) (setq num (- num 1)))
(next-line-internal (- num))
(tpu-top-check beg num)
(beginning-of-line)))
(defun tpu-paragraph (num)
"Move to the next paragraph in the current direction.
A repeat count means move that many paragraphs."
(interactive "p")
(let* ((left nil)
(beg (tpu-current-line))
(height (window-height))
(top-percent
(if (= 0 tpu-top-scroll-margin) 10 tpu-top-scroll-margin))
(bottom-percent
(if (= 0 tpu-bottom-scroll-margin) 15 tpu-bottom-scroll-margin))
(top-margin (/ (* height top-percent) 100))
(bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
(bottom-margin (max beg (- height bottom-up-margin 1)))
(top (save-excursion (move-to-window-line top-margin) (point)))
(bottom (save-excursion (move-to-window-line bottom-margin) (point)))
(far (save-excursion
(goto-char bottom) (forward-line (- height 2)) (point))))
(cond (tpu-advance
(tpu-next-paragraph num)
(cond((> (point) far)
(setq left (save-excursion (forward-line height)))
(if (= 0 left) (recenter top-margin)
(recenter (- left bottom-up-margin))))
(t
(and (> (point) bottom) (recenter bottom-margin)))))
(t
(tpu-previous-paragraph num)
(and (< (point) top) (recenter (min beg top-margin)))))))
(defun tpu-page (num)
"Move to the next page in the current direction.
A repeat count means move that many pages."
(interactive "p")
(let* ((left nil)
(beg (tpu-current-line))
(height (window-height))
(top-percent
(if (= 0 tpu-top-scroll-margin) 10 tpu-top-scroll-margin))
(bottom-percent
(if (= 0 tpu-bottom-scroll-margin) 15 tpu-bottom-scroll-margin))
(top-margin (/ (* height top-percent) 100))
(bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
(bottom-margin (max beg (- height bottom-up-margin 1)))
(top (save-excursion (move-to-window-line top-margin) (point)))
(bottom (save-excursion (move-to-window-line bottom-margin) (point)))
(far (save-excursion
(goto-char bottom) (forward-line (- height 2)) (point))))
(cond (tpu-advance
(forward-page num)
(cond((> (point) far)
(setq left (save-excursion (forward-line height)))
(if (= 0 left) (recenter top-margin)
(recenter (- left bottom-up-margin))))
(t
(and (> (point) bottom) (recenter bottom-margin)))))
(t
(backward-page num)
(and (< (point) top) (recenter (min beg top-margin)))))))
(defun tpu-scroll-window-down (num)
"Scroll the display down to the next section.
A repeat count means scroll that many sections."
(interactive "p")
(let* ((beg (tpu-current-line))
(height (1- (window-height)))
(lines (* num (/ (* height tpu-percent-scroll) 100))))
(next-line-internal (- lines))
(tpu-top-check beg lines)))
(defun tpu-scroll-window-up (num)
"Scroll the display up to the next section.
A repeat count means scroll that many sections."
(interactive "p")
(let* ((beg (tpu-current-line))
(height (1- (window-height)))
(lines (* num (/ (* height tpu-percent-scroll) 100))))
(next-line-internal lines)
(tpu-bottom-check beg lines)))
(defun tpu-search-internal (pat &optional quiet)
"Search for a string or regular expression."
(let* ((left nil)
(beg (tpu-current-line))
(height (window-height))
(top-percent
(if (= 0 tpu-top-scroll-margin) 10 tpu-top-scroll-margin))
(bottom-percent
(if (= 0 tpu-bottom-scroll-margin) 15 tpu-bottom-scroll-margin))
(top-margin (/ (* height top-percent) 100))
(bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
(bottom-margin (max beg (- height bottom-up-margin 1)))
(top (save-excursion (move-to-window-line top-margin) (point)))
(bottom (save-excursion (move-to-window-line bottom-margin) (point)))
(far (save-excursion
(goto-char bottom) (forward-line (- height 2)) (point))))
(tpu-search-internal-core pat quiet)
(if tpu-searching-forward
(cond((> (point) far)
(setq left (save-excursion (forward-line height)))
(if (= 0 left) (recenter top-margin)
(recenter (- left bottom-up-margin))))
(t
(and (> (point) bottom) (recenter bottom-margin))))
(and (< (point) top) (recenter (min beg top-margin))))))
(or (fboundp 'tpu-old-newline)
(fset 'tpu-old-newline (symbol-function 'newline)))
(or (fboundp 'tpu-old-do-auto-fill)
(fset 'tpu-old-do-auto-fill (symbol-function 'do-auto-fill)))
(or (fboundp 'tpu-old-newline-and-indent)
(fset 'tpu-old-newline-and-indent (symbol-function 'newline-and-indent)))
(defun newline (&optional num)
"Insert a newline. With arg, insert that many newlines.
In Auto Fill mode, can break the preceding line if no numeric arg.
This is the TPU-edt version that respects the bottom scroll margin."
(interactive "p")
(let ((beg (tpu-current-line)))
(or num (setq num 1))
(tpu-old-newline num)
(tpu-bottom-check beg num)))
(defun newline-and-indent nil
"Insert a newline, then indent according to major mode.
Indentation is done using the current indent-line-function.
In programming language modes, this is the same as TAB.
In some text modes, where TAB inserts a tab, this indents
to the specified left-margin column. This is the TPU-edt
version that respects the bottom scroll margin."
(interactive)
(let ((beg (tpu-current-line)))
(tpu-old-newline-and-indent)
(tpu-bottom-check beg 1)))
(defun do-auto-fill nil
"TPU-edt version that respects the bottom scroll margin."
(let ((beg (tpu-current-line)))
(tpu-old-do-auto-fill)
(tpu-bottom-check beg 1)))
(defun tpu-set-scroll-margins (top bottom)
"Set scroll margins."
(interactive
"sEnter top scroll margin (N lines or N%% or RETURN for current value): \
\nsEnter bottom scroll margin (N lines or N%% or RETURN for current value): ")
(or (string= top "")
(if (string= "%" (substring top -1))
(setq tpu-top-scroll-margin (string-to-number top))
(setq tpu-top-scroll-margin
(/ (1- (+ (* (string-to-number top) 100) (window-height)))
(window-height)))))
(or (string= bottom "")
(if (string= "%" (substring bottom -1))
(setq tpu-bottom-scroll-margin (string-to-number bottom))
(setq tpu-bottom-scroll-margin
(/ (1- (+ (* (string-to-number bottom) 100) (window-height)))
(window-height)))))
(and (interactive-p)
(message "Scroll margins set. Top = %s%%, Bottom = %s%%"
tpu-top-scroll-margin tpu-bottom-scroll-margin)))
(defun tpu-set-cursor-free nil
"Allow the cursor to move freely about the screen."
(interactive)
(setq tpu-cursor-free t)
(substitute-key-definition 'tpu-set-cursor-free
'tpu-set-cursor-bound
GOLD-map)
(message "The cursor will now move freely about the screen."))
(defun tpu-set-cursor-bound nil
"Constrain the cursor to the flow of the text."
(interactive)
(tpu-trim-line-ends)
(setq tpu-cursor-free nil)
(substitute-key-definition 'tpu-set-cursor-bound
'tpu-set-cursor-free
GOLD-map)
(message "The cursor is now bound to the flow of your text."))