0

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) 
  "Description..."
  (interactive)

  (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"
                          "tex-file-draft"
                          "tex-file-boxed-figures"
                          "tex-file-colored-background"
                          "tex-file-linenumbers"
                          "tex-file-showframe"
                          "tex-file-pagebreaks-highlight"
                          ))))
      (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"))
Gabriele Nicolardi
  • 1,199
  • 8
  • 17
  • 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. –  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). – Gabriele Nicolardi Feb 24 '18 at 16:15

1 Answers1

3

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)))