1

I am creating a keymap based on a configuration file.
The resulting keys all call the same function but with different arguments. Here is the code I am using for a single key:

(defun add-do-my-stuff-keybinding (config-pair)
  "Add config to do-my-stuff-map"
 (define-key do-my-stuff-map (kbd (config-get-key config-pair))
   `(lambda ()
      (interactive)
      (do-stuff ,(config-get-name config-pair)))))

My question is, is there a way to avoid the lambda so that the inspection of the keybindings does not show a lambda but a helpful function name?

Something like generating defuns oder named lambdas?

  • An alternative approach is to bind all keys in question to the same function. The function could check which key it was executed from and use a suitable "configuration" (whatever that is). – Lindydancer Jan 7 '16 at 12:04
  • That could be a solution, yes. I was aiming for something that allows me to execute "Prefix + C-h" to see what the keys themselves do with out inspecting every single. It's more to reduce memorization than anything else. – edt_devel Jan 7 '16 at 12:22
  • 2
    I haven't delved into it too much myself, but I would try using defmacro to define a macro returning a function generated from the config-file key + pair. The generated function could get a proper name and documentation string (using the declare form) and then be used instead of the lambda. – Erik Sjöstrand Jan 7 '16 at 13:17
1

I have found a solution. Thanks to Erik Sjöstrand for pointing me in the right direction. I was able to find this stackoverflow answer.

Simply aliasing the lambda function is enough.

(defun add-do-my-stuff-keybinding (config-pair)
  "Add config to do-my-stuff-map"
 (define-key do-my-stuff-map (kbd (config-get-key config-pair))
  (defalias (intern (concat "my-stuff-" (config-get-name config-pair))
  `(lambda ()
    ,(format "Call `do-my-stuff` for %s" (config-get-name config-pair))
    (interactive)
    (do-stuff ,(config-get-name config-pair)))))

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.