Function: kmacro-bind-to-key

kmacro-bind-to-key is an interactive and byte-compiled function defined in kmacro.el.gz.

Signature

(kmacro-bind-to-key ARG)

Documentation

When not defining or executing a macro, offer to bind last macro to a key.

The key sequences \C-x C-k 0 through \C-x C-k 9 and \C-x C-k A through \C-x C-k Z are reserved for user bindings, and to bind to one of these sequences, just enter the digit or letter, rather than the whole sequence.

You can bind to any valid key sequence, but if you try to bind to a key with an existing command binding, you will be asked for confirmation whether to replace that binding. Note that the binding is made in the global-map keymap, so the macro binding may be shaded by a local key binding. The ARG parameter is unused.

View in manual

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/kmacro.el.gz
(defun kmacro-bind-to-key (_arg)
  "When not defining or executing a macro, offer to bind last macro to a key.
The key sequences \\`C-x C-k 0' through \\`C-x C-k 9' and \\`C-x C-k A'
through \\`C-x C-k Z' are reserved for user bindings, and to bind to
one of these sequences, just enter the digit or letter, rather than
the whole sequence.

You can bind to any valid key sequence, but if you try to bind to
a key with an existing command binding, you will be asked for
confirmation whether to replace that binding.  Note that the
binding is made in the `global-map' keymap, so the macro binding
may be shaded by a local key binding.
The ARG parameter is unused."
  (interactive "p")
  (if (or defining-kbd-macro executing-kbd-macro)
      (if defining-kbd-macro
	  (message "Cannot save macro while defining it."))
    (unless last-kbd-macro
      (error "No keyboard macro defined"))
    (let ((key-seq (read-key-sequence "Bind last macro to key: "))
	  ok cmd)
      (when (= (length key-seq) 1)
	(let ((ch (aref key-seq 0)))
	  (if (and (integerp ch)
		   (or (and (>= ch ?0) (<= ch ?9))
		       (and (>= ch ?A) (<= ch ?Z))))
	      (setq key-seq (concat "\C-x\C-k" key-seq)
		    ok t))))
      (when (and (not (equal key-seq "\^G"))
		 (or ok
		     (not (setq cmd (key-binding key-seq)))
		     (stringp cmd)
		     (vectorp cmd)
		     (yes-or-no-p (format "%s runs command %S.  Bind anyway? "
					  (format-kbd-macro key-seq)
					  cmd))))
	(define-key global-map key-seq (kmacro-ring-head))
	(message "Keyboard macro bound to %s" (format-kbd-macro key-seq))))))