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

I would like the PgUp and PgDn keys to simply move the contents of the displayed file up or down, but the cursor (the point in Emacs Lingo) should stay where it is (on the screen). Unfortunately, the default behavior of Emacs is different. The default behavior is hard to describe, but if you press PgDn and then PgUp , you won’t be where you were before (!).

This is not a new problem, and there is a good solution called sfp-page-up and sfp-page-down in EmacsWiki .

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

However, there is one problem in combination with cua- mode, which provides (among other things) shift-selection (pressing Shift and the cursor key of type ← or PgDn starts selecting the selected area):

cua-mode does not recognize overridden PgUp / PgDn keys, i.e. they do not start the selection. The workaround is to press the ↑ or ↓ key first, and then continue with PgUp / PgDn .

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

+4
source share
2 answers

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

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

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

(For the first half of the solution, see the JSON 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) 
+1
source

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

+3
source

Source: https://habr.com/ru/post/1333098/


All Articles