Emacs: Is there a better PgDn/PgUp behaviour with cua-mode?

2k views Asked by At

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?

2

There are 2 answers

0
pesche On BEST ANSWER

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)
2
JSON On

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.