Question: I am trying to have a global override for a binding, where I am checking for some specific conditions to hold. If they don't, I would like that binding to do whatever it was going to without my intercept. I.e., something like the following:

```
(global-set-key <existing-binding> 'foo-wrapper)
(defun foo-wrapper ()
  (interactive)
  (if my-condition-holds #'my-foo <whatever existing-binding would have otherwise done here>)))) ;; 

```

How can I programmatically delegate to `<whatever existing-binding would have otherwise done here>`?

Please note: I am looking to **not** hard-code any commands into the wrapper that are not part of my custom logic. I'm looking at keybindings that are frequently overridden in various mode maps, and it seems impractical to have to manually figure out which command to dispatch.

Edited to add: concrete example is special handling of parentheses. In this case, `<existing-binding>` is simply `(kbd "(")`, and `my-condition` could be something like `(and current-prefix-arg (eq major-mode 'foo-mode))`. Crucially, if `my-condition` does not hold, I need to delegate to the default behavior, which is highly context-specific: it may be handled by paredit, or smartparens, or evil, or be a self-insert-command.

Answer: 
I had this problem before and came up with this solution. Make a minor mode
where you define your conditional commands:

```
(defvar conditional-overriding-mode-map (make-sparse-keymap))

(define-minor-mode conditional-overriding-mode
  "a minor mode for commands that only work if a certain condition holds."
  t nil conditional-overriding-mode-map)

```

The argument t means that the minor mode is on by default. Then bind keys
in this minor mode as follows:

```
(define-key conditional-overriding-mode-map KEY
  (lambda ()
    (interactive)
    (if COND
        COMMAND
      (conditional-overriding-mode 'toggle)
      (execute-kbd-macro (this-command-keys))
      (conditional-overriding-mode 'toggle))))

```

What this does is what you want. You will have to adjust this if you want to
pass arguments to your COMMAND or to whatever happens when COND fails.

For this to work all the time conditional-overriding-mode has to be at the front of minor-mode-map-alist. The answer in [How to set a rule for the order of minor-mode-map-alist](http://emacs.stackexchange.com/questions/13447/ddg#13450) gives a way to do this.
You can do this:

```el
;; Dynamic bindings. A filter function returns the command to
;; call.  If the function returns nil, Emacs treats it as if
;; no binding exists in that keymap, and continues looking for
;; a binding elsewhere.
(define-key <map> <key>
  `(menu-item "" <my-cmd> :filter ,(lambda (cmd)
                                     (if <my-predicate> cmd))))

```

E.g.:

```el
(define-key my-dynamic-override-map (kbd "(")
  `(menu-item "" my-foo :filter ,(lambda (cmd)
                                   (if (my-predicate) cmd))))

```

You (probably) won't want to use the global key map, because *any*
other active keymap with a binding would take precedence; so you'd
typically be using a minor mode key map, and taking steps to keep that
at the front of the minor mode map priority list (which is what I do).

The other answers have already indicated how to do that with a minor
mode, so for variety I'm using the even-higher-priority
`emulation-mode-map-alists` here. I don't feel it's quite intended
for this usage, but I suspect it'd be fine in practice.

```el
(defun my-foo ()
  (interactive)
  (insert "foo"))

(defun my-predicate ()
  (eql 0 (random 5)))

(defun my-filter (cmd)
  (if (my-predicate) cmd))

(defvar my-dynamic-override-map (make-sparse-keymap))

(define-key my-dynamic-override-map (kbd "(")
  `(menu-item "" my-foo :filter ,#'my-filter))

(defvar my-emulation-mode-map-alist
  `((my-dynamic-override-map . ,my-dynamic-override-map)))

(add-to-list 'emulation-mode-map-alists 'my-emulation-mode-map-alist)

```

(If you use a minor mode instead, simply define the key in the minor mode's keymap instead of the separate keymap I've made here, and ignore the 'emulation-mode-map' parts.)

See also: [How can I 'layer' a keybinding?](https://emacs.stackexchange.com/questions/26295/how-can-i-layer-a-keybinding)