0

By default, C-_ runs the command undo (found in global-map). It also runs org-agenda-undo when in Agenda view.

As I use cua-mode, I undo changes with C-z. My issue is that it doesn't work in Agenda View. I tried to bind org-agenda-undo with C-z :

(define-key org-agenda-mode-map (kbd "C-z") 'org-agenda-undo)

or

(use-package org-agenda
  :ensure nil
  :bind
  ("C-z" . org-agenda-undo))

But it didn't work. How can I do it without altering cua-mode behavior?

6
  • I put the two blocks in my init and restarted emacs twice to test each alternatively. In my init, cua-mode is enabled first. I confirm that by default C-z doesn't work on Agenda View when cua-mode is enabled (I did a test with an init only containing (cua-mode t)). Thanks for the edit.
    – crocefisso
    Commented May 29, 2023 at 21:37
  • Does this answer your question? How to assign the undo command to the Ctrl-z key binding?
    – Drew
    Commented May 29, 2023 at 22:03
  • @Drew, I don't want to remove (cua-mode t). I mean I would like to keep using cua-mode.
    – crocefisso
    Commented May 29, 2023 at 22:06
  • Maybe this helps? emacs.stackexchange.com/q/37277.
    – Drew
    Commented May 29, 2023 at 22:13
  • Also, I wonder why cua-mode isn't adapted to Agenda views...
    – crocefisso
    Commented May 29, 2023 at 22:14

2 Answers 2

2

Associate all the commands triggered by C-_ with C-z:

(define-key key-translation-map (kbd "C-z") (kbd "C-_"))
2

EDIT

As you mention in the comments, the original answer defines the new keybinding globally. To set the keybdinging only in the org-agenda buffer you could use

(with-eval-after-load 'cua-base
  (defvar cua--org-agenda-keymap (copy-keymap cua--cua-keys-keymap))
  (define-key cua--org-agenda-keymap (kbd "C-z") #'org-agenda-undo))

(defun cua--org-agenda-override-keymap ()
  (setq-local cua--keymap-alist (copy-tree cua--keymap-alist))
  (setf (alist-get 'cua--ena-cua-keys-keymap cua--keymap-alist) cua--org-agenda-keymap))

(add-hook 'org-agenda-mode-hook #'cua--org-agenda-override-keymap)

END EDIT

From using C-h k C-z from within the org-agenda buffer, you can find that the cua-mode C-z binding is found in the cua--cua-keys-keymap. So, to define the new binding for it, just use that map (after activating cua-mode)

(define-key cua--cua-keys-keymap (kbd "C-z") 'org-agenda-undo)

8
  • 1
    With your solution, C-z is bound to org-agenda-undo in Agenda Views indeed. But the issue is that outside of Agenda Views it is also bound to org-agenda-undo and no longer to undo. So your solution is altering cua-mode behavior outside of Agenda Views.
    – crocefisso
    Commented May 30, 2023 at 9:02
  • Ah okay, I see. I have updated the answer... Commented May 30, 2023 at 12:24
  • Yes, I would need other cua-mode bindings in Agenda Views, like C-c (cua-copy-handler) for example.
    – crocefisso
    Commented May 30, 2023 at 13:34
  • That binding still works, see M-x describe-keymap cua--cua-keys-keymap for the commands that will be disabled. It seems to me that those bindings are not required in org-agenda. Commented May 30, 2023 at 13:41
  • 1
    Thank you @dalanicolai, I gave an upvote to your answer already. However, I think I'll accept mine (when available) as it is simpler.
    – crocefisso
    Commented May 30, 2023 at 15:38

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.