I have a tool that allows me to use the same key (shortcut) C-c C-f to call different functions. Actually I can choose different options (different functions) to pass to the LaTeX compiler, binding them to the shortcut C-c C-f.

Here's my tool (to better clarify my question):

(defun ToggleTexFileBehaviour (&optional COMMAND) 

  (if COMMAND ;; For inside script use
      (define-key latex-mode-map "\C-c\C-f" (intern-soft COMMAND))
    ;; *ELSE*:
    (let ((Latex_setting
       (read-string "Choose your option:
            "tex-file"  nil (list "tex-file-eye-comfort"
      (define-key latex-mode-map "\C-c\C-f" (intern-soft Latex_setting))
      (command-execute (intern-soft Latex_setting))
(local-set-key  (kbd "C-s-c")'ToggleTexFileBehaviour)

My question is how can I call the function binded to my key/shortcut from inside a script? I mean something like:

(call-function-binded-to-key (kbd "C-cC-f"))
  • There's key-binding, which returns binding for a key you pass to it. You can use funcall or call-interactively to call that function. I don't think that's the right way to do it, though. You should be better off checking which function is bound to a key with C-h k and then just calling it or something, because you'd rely on keybindings being right in other case.
    – user12563
    Feb 24 '18 at 16:05
  • @DoMiNeLa10 perfetc! (call-interactively (key-binding "\C-c\C-f")) works fine! I want to call exactly the function bound to the key in that moment. If you take the time to post a short answer I'll accept it (see also the editing to my question). Feb 24 '18 at 16:15

key-binding returns a function bound to a key, which you can ten call with funcall or call-interactively, so you can define your function call-function-binded-to-key like this:

(defun call-function-binded-to-key (key)
  (call-interactively (key-binding key)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.