5

I'd like the PgUp and PgDn keys to just move the contents of the shown file upwards or downwards, but the cursor (point in Emacs Lingo) should stay where it is (on the screen). Unfortunately the default Emacs behaviour is different. The default behaviour is difficult to describe, but if you press PgDn followed by PgUp you don't end up where you were before (!).

This is not a new problem and there exists a nice solution called sfp-page-up and sfp-page-down in the EmacsWiki.

(defun sfp-page-up ()
  (interactive)
  (setq this-command 'previous-line)
  (previous-line
   (- (window-text-height)
      next-screen-context-lines)))

There is one problem, however, in combination with cua-mode, which provides (among others) shift-selection (pressing Shift and a Cursor movement key like or PgDn starts highlighting a selected area):

cua-mode doesn't recognise the redefined PgUp/PgDn keys, i.e. they don't start a selection. The workaround is to press first the or key and then continue with PgUp/PgDn.

How can I make cua-mode play nicely with sfp-page-up/down?

Jasper
  • 2,166
  • 4
  • 30
  • 50
pesche
  • 3,054
  • 4
  • 34
  • 35

2 Answers2

3

If you add ^ to start of the (interactive "...") spec (inside the double quotes) of the functions, they will support shift selection in Emacs 23.1 and later.

JSON
  • 4,487
  • 22
  • 26
  • I changed the functions to `(defun sfp-page-down (arg) (interactive "^p") ...`, but they still can't start a selection. Should I patch cua-mode to `remap sfp-page-up` instead of `remap scroll-up`? BTW: which one is correct, `"^p"` or `"^P"`? – pesche Dec 23 '10 at 08:38
  • Thanks for the tip, it's half of the solution. When I disable `cua-mode`, the shift selection now indeed works with `sfp-page-xx`. But with `cua-mode` enabled it still doesn't work yet. – pesche Dec 23 '10 at 23:02
2

I found the other half of the solution in the thread if I set home key (...) then shift+home does not select text in cua-mode on gnu.emacs.help:

To participate in the shift selection of cua-mode, a function (in my case sfp-page-xxx) must have the symbol property CUA set to move:

(put 'sfp-page-up 'CUA 'move)

(For the first half of the solution see JSON's answer).

So here is my complete solution:

(defun sfp-page-down (&optional arg)
  (interactive "^P")
  (setq this-command 'next-line)
  (next-line
   (- (window-text-height)
      next-screen-context-lines)))
(put 'sfp-page-down 'isearch-scroll t)
(put 'sfp-page-down 'CUA 'move)

(defun sfp-page-up (&optional arg)
  (interactive "^P")
  (setq this-command 'previous-line)
  (previous-line
   (- (window-text-height)
      next-screen-context-lines)))
(put 'sfp-page-up 'isearch-scroll t)
(put 'sfp-page-up 'CUA 'move)
Community
  • 1
  • 1
pesche
  • 3,054
  • 4
  • 34
  • 35