1

It appears that all the documentation about registers revolves around C-x r [action] [char] or M-x [command] RET [char].

But let's say I want to create my own command using the register, how do I go about it? For example, let's say I want to create a second clipboard using M-c and M-v to copy and paste respectively.

Naively, I tried

    (defun mycopy()
      (interactive)
      (copy-to-register "w"))

which of course doesn't work. I tried other variations on this command and a few other commands to no avail.

So, if I understand, registers are not meant to be used through elisp?

Perhaps I can mimic a key press with elisp code? Or should I forgo registers entirely for that purpose and simply create my own global variable to store information to?

1 Answer 1

1

Your problem is that you didn't consult the doc (using C-h f) of copy-to-register. You didn't pass it a register, which needs to be a character, not a string. And you didn't pass it the region boundaries, so it has no way of knowing what text you want to copy.

This should do what you want:


(defun mycopy (begin end)
  "..."
  (interactive "r")
  (copy-to-register ?w begin end))

(defun mypaste (&optional arg)
  "..."
  (interactive "P")
  (insert-register ?w (not arg)))

(global-set-key (kbd "M-c") 'mycopy)
(global-set-key (kbd "M-v") 'mypaste)

This is what C-h f copy-to-register says:

copy-to-register is an interactive compiled Lisp function in register.el.

It is bound to C-M-S-backspace, C-x r x, C-x r s.

(copy-to-register REGISTER START END &optional DELETE-FLAG REGION)

Copy region into register REGISTER.

With prefix arg, delete as well.

Called from program, takes five args: REGISTER, START, END, DELETE-FLAG, and REGION.

  • START and END are buffer positions indicating what to copy.
  • The optional argument REGION if non-nil, indicates that we're not just copying some text between START and END, but we're copying the region.

Interactively, reads the register using register-read-with-preview.

More generally, you can use set-register? C-h f says:

set-register is a compiled Lisp function in register.el.

(set-register REGISTER VALUE)

Set contents of Emacs register named REGISTER to VALUE. Returns VALUE. See the documentation of the variable register-alist for possible VALUEs.

Other than that, consult library register.el for how to create and access registers using Elisp.

4
  • The Emacs manual has a whole chapter on registers: (info "(emacs)Registers") or C-h i g(emacs)registers.
    – NickD
    Commented Jan 25, 2022 at 19:12
  • 1
    Thank you! I think I did look at the doc with C-h f but it meant nothing to me (and therefore, dismissed it too quickly). However, your answer prompted me to look at the doc for the interactive function and I looked up elisp character literals. In hindsight, it all makes sense. I am new to emacs but the bigger picture is building itself slowly but surely.
    – Alan
    Commented Jan 25, 2022 at 19:20
  • 1
    @NickD Yes. That is what I had looked at before (but on gnu.org instead). While it explains the bindings, it doesn't say anything about elisp.
    – Alan
    Commented Jan 25, 2022 at 19:23
  • That is true: you might want to look through the "intro to elisp" book: (info "(eintr)") or C-h i g(eintr) or on the web An Introduction to Programming in Emacs Lisp.
    – NickD
    Commented Jan 25, 2022 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.