1

I have the following global keyboard shortcut in Emacs:

(global-set-key (kbd "C-<right>") 'forward-word)

For the org-mode I decided to redefine this shortcut. If the cursor stands on a link, then go to the link location. Otherwise - use forward-word function.

(defun is-link-p ()
      (if (org-in-regexp org-bracket-link-regexp)
            t))

(defun follow-link-or-next-word ()
      (interactive)
      (if (is-link-p)
            (org-open-at-point)
            (forward-word)))

(add-hook 'org-mode-hook (lambda () 
      (define-key org-mode-map (kbd "C-<right>") 'follow-link-or-next-word)))

Is it possible to change org-mode shortcut in the following manner: instead of calling (forward-word), find what function is globally bound to "C-<right>" and call it instead.

Thus I won't need to change (forward-word) twice in case I decide to change the global shortcut.

Paulo Tomé
  • 1,910
  • 3
  • 18
  • 27
user4035
  • 22,508
  • 11
  • 59
  • 94
  • 1
    If you explicitly want the global binding as your fallback then this isn't a duplicate; however the following are definitely related: [Elisp: Conditionally change keybinding](http://stackoverflow.com/questions/16090517/elisp-conditionally-change-keybinding) and [Emacs key binding fallback](http://stackoverflow.com/questions/2494096/emacs-key-binding-fallback). – phils Jun 01 '13 at 03:04

1 Answers1

4

I think you're looking for the function (lookup-key keymap key &optional accept-defaults)

This function returns the definition of key in keymap. All the other functions described in this chapter that look up keys use lookup-key. Here are examples:

      (lookup-key (current-global-map) "\C-x\C-f")
          ⇒ find-file
      (lookup-key (current-global-map) (kbd "C-x C-f"))
          ⇒ find-file

You could extend your functions:

(defun is-link-p ()
    (if (org-in-regexp org-bracket-link-regexp)
         t))

(defun follow-link-or-default-action()
    (interactive)
       (let ((global-default (lookup-key (current-global-map) (kbd "C-<right>"))))
           (if (is-link-p)
               (org-open-at-point)
               (funcall global-default))))

(add-hook 'org-mode-hook (lambda () 
      (define-key org-mode-map (kbd "C-<right>") 'follow-link-or-default-action)))
djf
  • 6,592
  • 6
  • 44
  • 62