Function: define-key-after
define-key-after is a byte-compiled function defined in subr.el.gz.
Signature
(define-key-after KEYMAP KEY DEFINITION &optional AFTER)
Documentation
Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
This is a legacy function; see keymap-set-after for the
recommended function to use instead.
This is like define-key except that the binding for KEY is placed
just after the binding for the event AFTER, instead of at the beginning
of the map. Note that AFTER must be an event type (like KEY), NOT a command
(like DEFINITION).
If AFTER is t or omitted, the new binding goes at the end of the keymap. AFTER should be a single event type--a symbol or a character, not a sequence.
Bindings are always added before any inherited map.
The order of bindings in a keymap matters only when it is used as a menu, so this function is not useful for non-menu keymaps.
Probably introduced at or before Emacs version 19.16.
Source Code
;; Defined in /usr/src/emacs/lisp/subr.el.gz
(defun define-key-after (keymap key definition &optional after)
"Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
This is a legacy function; see `keymap-set-after' for the
recommended function to use instead.
This is like `define-key' except that the binding for KEY is placed
just after the binding for the event AFTER, instead of at the beginning
of the map. Note that AFTER must be an event type (like KEY), NOT a command
\(like DEFINITION).
If AFTER is t or omitted, the new binding goes at the end of the keymap.
AFTER should be a single event type--a symbol or a character, not a sequence.
Bindings are always added before any inherited map.
The order of bindings in a keymap matters only when it is used as
a menu, so this function is not useful for non-menu keymaps."
(declare (indent defun))
(unless after (setq after t))
(or (keymapp keymap)
(signal 'wrong-type-argument (list 'keymapp keymap)))
(setq key
(if (<= (length key) 1) (aref key 0)
(setq keymap (lookup-key keymap
(apply #'vector
(butlast (mapcar #'identity key)))))
(aref key (1- (length key)))))
(let ((tail keymap) done inserted)
(while (and (not done) tail)
;; Delete any earlier bindings for the same key.
(if (eq (car-safe (car (cdr tail))) key)
(setcdr tail (cdr (cdr tail))))
;; If we hit an included map, go down that one.
(if (keymapp (car tail)) (setq tail (car tail)))
;; When we reach AFTER's binding, insert the new binding after.
;; If we reach an inherited keymap, insert just before that.
;; If we reach the end of this keymap, insert at the end.
(if (or (and (eq (car-safe (car tail)) after)
(not (eq after t)))
(eq (car (cdr tail)) 'keymap)
(null (cdr tail)))
(progn
;; Stop the scan only if we find a parent keymap.
;; Keep going past the inserted element
;; so we can delete any duplications that come later.
(if (eq (car (cdr tail)) 'keymap)
(setq done t))
;; Don't insert more than once.
(or inserted
(setcdr tail (cons (cons key definition) (cdr tail))))
(setq inserted t)))
(setq tail (cdr tail)))))