I want to perform the same action that one does by hitting C-Space
+ moving the arrow keys, but in elisp.
Failing to find the right function (if they just were logically grouped in namespaces or somehow tagged...). Which one is it?
I want to perform the same action that one does by hitting C-Space
+ moving the arrow keys, but in elisp.
Failing to find the right function (if they just were logically grouped in namespaces or somehow tagged...). Which one is it?
You can translate keystrokes to elisp using C-h k key.
You'll notice the elisp function for setting the mark set-mark-command
, takes one non-optional argument. Emacs uses the special interactive
function to allow elisp functions to be written naturally with arguments. This allows them to be generic and easy to reuse in other elisp programs, while still possible to invoke directly from a keystroke. It also has some of the C-u prefix logic built-in.
In the case of set-mark-command
, its first function is (interactive "P")
, which means the prefix is passed in as an argument when called from the keyboard. You can simulate this directly in elisp with:
(set-mark-command nil)
For example, to select the current line in elisp:
(defun my-select-current-line ()
(interactive)
(move-beginning-of-line nil)
(set-mark-command nil)
(move-end-of-line nil)
(setq deactivate-mark nil))
Note you have to tell emacs to leave the mark active at the end or else the region won't remain highlighted (although the point and mark will be where you left them).
You should use push-mark in emacs lisp code, as follows:
(defun mark-n (n)
"Programmtically mark the next N lines"
(interactive "nNum lines to mark: ")
(push-mark)
(next-line n))
Just in case this additional info is of use to someone else, I've found the following:
(point)
and (mark)
retrieve their respective positions, so one can do (set-mark (+ 5 (mark)))
, for instance.The region is the part of the buffer between point
and mark
.