开发者

Using boundp with a symbol generated in a macro

开发者 https://www.devze.com 2023-02-01 12:10 出处:网络
I am trying to write the following code: (unless (boundp \'foo-1) (setq foo-1 bar-1)) (find-file foo-1) However, the symbols foo-1 and bar-1 should be generated via a macro.I tried something like t

I am trying to write the following code:

(unless (boundp 'foo-1)
(setq foo-1 bar-1))
(find-file foo-1)

However, the symbols foo-1 and bar-1 should be generated via a macro. I tried something like the following, but it looks like boundp is not functioning the way I want. I am not very experienced with macros; am I doing something obviously wrong? Also, am I abusing macros here?..

(defmacro foo-setq-bar (X)
  `(setq ,(intern (concat "foo-" (number-to-string X))) (assign-bar-to-foo ,X)))
(defmacro foo-find-file (X)
  `(progn
     (unless (boundp (quo开发者_C百科te ,(intern (concat "foo-" (number-to-string X)))))
       (foo-setq-bar ,X))
     (find-file ,(intern (concat "foo-" (number-to-string X))))))


The code you provided seems to run fine, although you didn't provide the code for assign-bar-to-foo so it's not clear what that does. In any case, this is a macro that should do what you explained in your example:

(defmacro find-foobar (X)
  (let* ((X-as-string (number-to-string X))
         (foo (intern (concat "foo-" X-as-string)))                  
         (bar (intern (concat "bar-" X-as-string))))          
  `(progn (unless (boundp (quote ,foo))             
            (setq ,foo ,bar))
          (find-file ,foo))))

Remember that you can check the code your macro expands to with (macroexpand FORM).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号