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?

crocefisso
  • 1,141
  • 7
  • 15
  • 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 May 29 '23 at 21:37
  • Does this answer your question? [How to assign the undo command to the Ctrl-z key binding?](https://emacs.stackexchange.com/questions/29174/how-to-assign-the-undo-command-to-the-ctrl-z-key-binding) – Drew May 29 '23 at 22:03
  • @Drew, I don't want to remove `(cua-mode t)`. I mean I would like to keep using `cua-mode`. – crocefisso May 29 '23 at 22:06
  • Maybe this helps? https://emacs.stackexchange.com/q/37277/. – Drew May 29 '23 at 22:13
  • Also, I wonder why `cua-mode` isn't adapted to Agenda views... – crocefisso May 29 '23 at 22:14
  • @Drew, I'm not sure to understand the second solution... – crocefisso May 29 '23 at 22:20

2 Answers2

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)

dalanicolai
  • 6,108
  • 7
  • 23
  • 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 May 30 '23 at 09:02
  • Ah okay, I see. I have updated the answer... – dalanicolai May 30 '23 at 12:24
  • Yes, I would need other `cua-mode` bindings in Agenda Views, like `C-c` (`cua-copy-handler`) for example. – crocefisso May 30 '23 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. – dalanicolai May 30 '23 at 13:41
  • With your solution, `C-c`, to copy selected text from Agenda Views, no longer works. – crocefisso May 30 '23 at 14:06
  • You're right, although I checked the code, somehow I did not do it correctly. Well, I have updated the answer again with (indeed a non-trivial) solution. But now I see that you have found a much more elegant solution already... – dalanicolai May 30 '23 at 15:32
  • 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 May 30 '23 at 15:38
  • Yeah, for sure, your answer should be the accepted answer :) – dalanicolai May 30 '23 at 15:42
2

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

(define-key key-translation-map (kbd "C-z") (kbd "C-_"))
crocefisso
  • 1,141
  • 7
  • 15