2

I want this code :

(setq name "foobar")
(defun (intern name) ())

do the same thing as :

(defun foobar ())

I have tried this (from This question):

(defmacro make-my-function (name)
(list 'defun
(intern (format "my-%s-function" name)) ()
    (list 'interactive)
    '(message "It's work !")))

But I get a function called my-name-function but I want my-foobar-function. My goal is to define minors modes from an alist of keymap.

Thanks

Community
  • 1
  • 1
Bapt
  • 21
  • 2

2 Answers2

0

I can't recreate your issue with your make-my-function macro, but try it with the backquote:

(defmacro make-my-function (name)
  `(defun ,(intern (format "my-%s-function" name)) ()
    'interactive
    '(message "It works!")))
Johnson
  • 1,510
  • 8
  • 15
0

Your macro works fine. How did you try to invoke it? You should invoke like this: (make-my-function foo). Then M-: (symbol-function 'my-foo-function) shows you the function definition:

(lambda nil
  (interactive)
  (message "It's work !"))

The reason that your initial attempt, before defining the macro, did not work is that defun is a macro - it does not evaluate its arguments. So (defun (intern name) ()) is syntactically incorrect: defun needs a symbol as its first argument, and (intern name) is a list, not a symbol.


Updated, after your comment -

Your question is quite UNclear. Now it sounds like you want to call (make-my-function foo) in a context where you have a variable my-foo-function, whose value is a string.

If that's the case then use this:

(defmacro make-my-function (name)
  (list 'defun
        (intern (symbol-value (intern (format "my-%s-function" name))))
        ()
        (list 'interactive)
        '(message "It's work !")))

(setq my-foo-function "toto")
(make-my-function foo)

I cannot imagine such a use case, but I suspect that this too is not what you are trying to do. It does not seem like you have specified well what the required behavior is.

Drew
  • 29,895
  • 7
  • 74
  • 104
  • I think you misunderstand : I want to invoke my macro like this : (make-my-function a-variable-contain-the-name-as-string) and not like this : (make-my-function foobar) – Bapt Sep 24 '16 at 15:43
  • My main problem is to convert a string to symbol – Bapt Sep 24 '16 at 15:51
  • A macro does not evaluate its arguments, unless you tell it to. If you want `(make-my-function a-variable-contain-the-name-as-string)` to evaluate `a-variable-contain-the-name-as-string` then the macro body needs to explicitly evaluate that argument. – Drew Sep 24 '16 at 18:57