How to prevent emacs from watiting for key chords?

So when executing key chords , emacs waits indefinitely. However i want to bind SPC y to copy current selection and SPC yy to copy current line, how would i go about binding the keys like this?

(map!
 :leader
 :nv
 :desc "Copy line to system clipboard" "yy" #'copy-current-line-to-clipboard)

dosen’t work as y is not a prefix key

I don’t think there is a solution to this so im using SPC Y instead but if there is a way around this i would love to know

I believe Hlissner has said in a few places that you can’t prevent Emacs from waiting.

But you always can wait and timeout yourself. I’ve cobbled together something that should, for the most part, work:

(defun yank-selection-or-line ()
  (interactive)
  (let ((next-key (read-event nil nil 0.5)))
    (cl-case next-key
      (?y (call-interactively #'copy-current-line-to-clipboard))
       ('nil (call-interactively #'copy-region-as-kill))
       (t (progn (call-interactively #'copy-region-as-kill)
                 (setq unread-command-events (list next-key)))))))

alternative cleaner/shorter (but maybe slightly harder to understand) version:

shorter
(defun yank-selection-or-line ()
  (interactive)
  (let ((next-key (read-event nil nil 0.5)))
    (call-interactively
     (cl-case next-key
       (?y #'copy-current-line-to-clipboard)
       ('nil #'copy-region-as-kill)
       (t (prog1 #'copy-region-as-kill
                 (setq unread-command-events (list next-key))))))))

It’s not a general solution, and might not be correct code but it seems to work for me.

The core part is that we make a command that reads the next input with a timeout (0.5 seconds here),

  • and if that input is the y key it runs copy-current-line-to-clipboard (the function you try to bind in your post).
  • If there was no input, we copy the current selection, and if there was input other than “y”, we still copy the selection, but then set unread-command-events to tell Emacs to execute that input.

Problems:

  • this-command-keys may not be set when executing the next key (may be avoidable by setting unread-command-events to something like (t . next-key) but I didn’t feel certain how)
  • everything being called-interactively is a probably kludge
  • during the 0.5s timeout period timers won’t run (Emacs is considered non-idle)

out of curiosity, what’s the motivation for binding copying under leader anyway?

edit: I would also like to note that binding “SPC yy” actually involves binding “SPC y”, to a keymap. This is part of why you can’t bind a prefix to a function. So the only thing you can do is bind it to something that sets a keymap (and a timer for a delay), or kinda “emulates” a keymap like above.