emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Scrolling commands (was: scroll-top-bottom)


From: Juri Linkov
Subject: Scrolling commands (was: scroll-top-bottom)
Date: Wed, 31 Mar 2010 01:27:16 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (x86_64-pc-linux-gnu)

>>> This feature is implemented by CUA mode, but it would be very useful
>>> for users who don't use CUA.  To support it in the core, I've implemented
>>> a new user option `scroll-top-bottom' (that defines the scrolling behavior
>>> at the top/bottom of the buffer).
>>
>> When scrolling by a page at a time, this makes sense, but when scrolling
>> only by a single line at a time, it's very odd for point to suddenly
>> jump several lines at a time.  So I think the behavior should be refined
>> so it doesn't just "move to BEGV or ZV" but instead "when scrolling by
>> N lines can't be done, move by N lines instead".

This scrolling behavior is now implemented by the patch below
based on `cua-scroll-up' and `cua-scroll-down'.

> And `cua-scroll-up' and `cua-scroll-down' have the same problem.
> I'll try to create new commands based on `cua-scroll-up' and
> `cua-scroll-down' but without these problems.
>
>> And yes, I think this would be better done at the Lisp level by
>> introducing new commands so it doesn't affect other code that calls
>> scroll-(up|down).
>
> I see what you mean.  These new commands should be a wrapper
> like `next-line' for `forward-line'.

When looking for the command names, I discovered that XEmacs has
such wrapper commands: `scroll-up-command' and `scroll-down-command'.
I think these are good names.  From XEmacs I took only command names.
The code is from `cua-scroll-up' and `cua-scroll-down'.

Also there were requests for commands that scroll only one line.
Actually such commands already exist in Emacs, but are hidden
in emulation/ws-mode.el.  This patch moves them to simple.el
with some modifications:

=== modified file 'lisp/emulation/ws-mode.el'
--- lisp/emulation/ws-mode.el   2010-01-13 08:35:10 +0000
+++ lisp/emulation/ws-mode.el   2010-03-30 22:17:38 +0000
@@ -339,16 +339,6 @@ (defun wordstar-center-line ()
        (+ left-margin
          (/ (- fill-column left-margin line-length) 2))))))
 
-(defun scroll-down-line ()
-  "Scroll one line down."
-  (interactive)
-  (scroll-down 1))
-
-(defun scroll-up-line ()
-  "Scroll one line up."
-  (interactive)
-  (scroll-up 1))
-
 ;;;;;;;;;;;
 ;; wordstar special variables:
 
=== modified file 'lisp/simple.el'
--- lisp/simple.el      2010-03-30 18:30:35 +0000
+++ lisp/simple.el      2010-03-30 22:12:17 +0000
@@ -4870,6 +4870,81 @@ (define-globalized-minor-mode global-vis
   visual-line-mode turn-on-visual-line-mode
   :lighter " vl")
 
+;;; Scrolling commands.
+
+(defun scroll-up-command (&optional arg)
+  "Scroll text of selected window upward ARG lines; or near full screen if no 
ARG.
+If `scroll-up' cannot scroll window further, move cursor to the bottom line.
+A near full screen is `next-screen-context-lines' less than a full screen.
+Negative ARG means scroll downward.
+If ARG is the atom `-', scroll downward by nearly full screen."
+  (interactive "^P")
+  (cond
+   ((eq arg '-) (scroll-down-command nil))
+   ((< (prefix-numeric-value arg) 0)
+    (scroll-down-command (- (prefix-numeric-value arg))))
+   ((eobp)
+    (scroll-up arg))  ; signal error
+   (t
+    (condition-case nil
+       (scroll-up arg)
+      (end-of-buffer
+       (if arg
+          ;; When scrolling by ARG lines can't be done,
+          ;; move by ARG lines instead.
+          (forward-line arg)
+        ;; When ARG is nil for full-screen scrolling,
+        ;; move to the bottom of the buffer.
+        (goto-char (point-max))))))))
+
+(put 'scroll-up-command 'isearch-scroll t)
+
+(defun scroll-down-command (&optional arg)
+  "Scroll text of selected window down ARG lines; or near full screen if no 
ARG.
+If `scroll-down' cannot scroll window further, move cursor to the top line.
+A near full screen is `next-screen-context-lines' less than a full screen.
+Negative ARG means scroll upward.
+If ARG is the atom `-', scroll upward by nearly full screen."
+  (interactive "^P")
+  (cond
+   ((eq arg '-) (scroll-up-command nil))
+   ((< (prefix-numeric-value arg) 0)
+    (scroll-up-command (- (prefix-numeric-value arg))))
+   ((bobp)
+    (scroll-down arg))  ; signal error
+   (t
+    (condition-case nil
+       (scroll-down arg)
+      (beginning-of-buffer
+       (if arg
+          ;; When scrolling by ARG lines can't be done,
+          ;; move by ARG lines instead.
+          (forward-line (- arg))
+        ;; When ARG is nil for full-screen scrolling,
+        ;; move to the top of the buffer.
+        (goto-char (point-min))))))))
+
+(put 'scroll-down-command 'isearch-scroll t)
+
+(defun scroll-up-line (&optional arg)
+  "Scroll text of selected window upward ARG lines; or one line if no ARG.
+If ARG is omitted or nil, scroll upward by one line.
+This is different from `scroll-up-command' that scrolls a full screen."
+  (interactive "p")
+  (scroll-up (or arg 1)))
+
+(put 'scroll-up-line 'isearch-scroll t)
+
+(defun scroll-down-line (&optional arg)
+  "Scroll text of selected window down ARG lines; or one line if no ARG.
+If ARG is omitted or nil, scroll down by one line.
+This is different from `scroll-down-command' that scrolls a full screen."
+  (interactive "p")
+  (scroll-down (or arg 1)))
+
+(put 'scroll-down-line 'isearch-scroll t)
+
+
 (defun scroll-other-window-down (lines)
   "Scroll the \"other window\" down.
 For more details, see the documentation for `scroll-other-window'."

=== modified file 'lisp/bindings.el'
--- lisp/bindings.el    2010-01-13 08:35:10 +0000
+++ lisp/bindings.el    2010-03-30 22:14:02 +0000
@@ -873,8 +873,8 @@ (define-key global-map [left]               'backward
 (define-key global-map [up]            'previous-line)
 (define-key global-map [right]         'forward-char)
 (define-key global-map [down]          'next-line)
-(define-key global-map [prior]         'scroll-down)
-(define-key global-map [next]          'scroll-up)
+(define-key global-map [prior]         'scroll-down-command)
+(define-key global-map [next]          'scroll-up-command)
 (define-key global-map [C-up]          'backward-paragraph)
 (define-key global-map [C-down]                'forward-paragraph)
 (define-key global-map [C-prior]       'scroll-right)

-- 
Juri Linkov
http://www.jurta.org/emacs/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]