123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- (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)
- (define-minor-mode tpu-cursor-free-mode
- "Minor mode to allow the cursor to move freely about the screen.
- With a prefix argument ARG, enable the mode if ARG is positive,
- and disable it otherwise. If called from Lisp, enable the mode
- if ARG is omitted or nil."
- :init-value nil
- (if (not tpu-cursor-free-mode)
- (tpu-trim-line-ends))
- (if (not tpu-cursor-free-mode)
- (message "The cursor is now bound to the flow of your text.")
- (message "The cursor will now move freely about the screen.")))
- (add-hook 'picture-mode-hook 'tpu-set-cursor-free)
- (defun tpu-trim-line-ends-if-needed ()
- "Eliminate whitespace at ends of lines, if the cursor is free."
- (if (and (buffer-modified-p) tpu-cursor-free-mode) (tpu-trim-line-ends)))
- (add-hook 'before-save-hook 'tpu-trim-line-ends-if-needed)
- (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-mode (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-mode)
- (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-mode (or (eobp) (picture-move-down num))
- (line-move 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-mode (picture-move-up num) (line-move (- 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-mode
- (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-mode
- (picture-end-of-line (- 1 num)))
- (t
- (end-of-line (- 1 num))))
- (tpu-top-check beg num)))
- (defun tpu-current-end-of-line ()
- "Move point to end of current line."
- (interactive)
- (let ((beg (point)))
- (if tpu-cursor-free-mode (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)))
- (forward-line num)
- (tpu-bottom-check beg num)))
- (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)))
- (forward-line (- num))
- (tpu-top-check beg num)))
- (defmacro tpu-with-position (&rest body)
- "Execute BODY with some position-related variables bound."
- `(let* ((left nil)
- (beg (tpu-current-line))
- (height (window-height))
- (top-percent
- (if (zerop tpu-top-scroll-margin) 10 tpu-top-scroll-margin))
- (bottom-percent
- (if (zerop 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)
- (point-at-bol (1- height)))))
- ,@body))
- (defun tpu-paragraph (num)
- "Move to the next paragraph in the current direction.
- A repeat count means move that many paragraphs."
- (interactive "p")
- (tpu-with-position
- (if tpu-advance
- (progn
- (tpu-next-paragraph num)
- (if (> (point) far)
- (if (zerop (setq left (save-excursion (forward-line height))))
- (recenter top-margin)
- (recenter (- left bottom-up-margin)))
- (and (> (point) bottom) (recenter bottom-margin))))
- (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")
- (tpu-with-position
- (if tpu-advance
- (progn
- (forward-page num)
- (if (> (point) far)
- (if (zerop (setq left (save-excursion (forward-line height))))
- (recenter top-margin)
- (recenter (- left bottom-up-margin)))
- (and (> (point) bottom) (recenter bottom-margin))))
- (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))))
- (line-move (- 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))))
- (line-move lines)
- (tpu-bottom-check beg lines)))
- (defun tpu-search-internal (pat &optional quiet)
- "Search for a string or regular expression."
- (tpu-with-position
- (tpu-search-internal-core pat quiet)
- (if tpu-searching-forward
- (progn
- (if (> (point) far)
- (if (zerop (setq left (save-excursion (forward-line height))))
- (recenter top-margin)
- (recenter (- left bottom-up-margin)))
- (and (> (point) bottom) (recenter bottom-margin))))
- (and (< (point) top) (recenter (min beg top-margin))))))
- (defadvice newline (around tpu-respect-bottom-scroll-margin activate disable)
- "Respect `tpu-bottom-scroll-margin'."
- (let ((beg (tpu-current-line))
- (num (prefix-numeric-value (ad-get-arg 0))))
- ad-do-it
- (tpu-bottom-check beg num)))
- (defadvice newline-and-indent (around tpu-respect-bottom-scroll-margin)
- "Respect `tpu-bottom-scroll-margin'."
- (let ((beg (tpu-current-line)))
- ad-do-it
- (tpu-bottom-check beg 1)))
- (defadvice do-auto-fill (around tpu-respect-bottom-scroll-margin)
- "Respect `tpu-bottom-scroll-margin'."
- (let ((beg (tpu-current-line)))
- ad-do-it
- (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 "")
- (setq tpu-top-scroll-margin
- (if (string= "%" (substring top -1))
- (string-to-number top)
- (/ (1- (+ (* (string-to-number top) 100) (window-height)))
- (window-height)))))
-
- (or (string= bottom "")
- (setq tpu-bottom-scroll-margin
- (if (string= "%" (substring bottom -1))
- (string-to-number bottom)
- (/ (1- (+ (* (string-to-number bottom) 100) (window-height)))
- (window-height)))))
- (dolist (f '(newline newline-and-indent do-auto-fill))
- (ad-enable-advice f 'around 'tpu-respect-bottom-scroll-margin)
- (ad-activate f))
-
- (and (called-interactively-p 'interactive)
- (message "Scroll margins set. Top = %s%%, Bottom = %s%%"
- tpu-top-scroll-margin tpu-bottom-scroll-margin)))
- (defun tpu-set-cursor-free ()
- "Allow the cursor to move freely about the screen."
- (interactive)
- (tpu-cursor-free-mode 1))
- (defun tpu-set-cursor-bound ()
- "Constrain the cursor to the flow of the text."
- (interactive)
- (tpu-cursor-free-mode -1))
|