1

From the following code

(defun foo () 
  (defun bar1 () 
    (do-something)))
              
(defun bar2 () 
  (do-something))

I want to make the following

(defun foo () 
  (defun bar1 () 
    (do-something)))

(defun foo () 
  (defun bar2 () 
    (do-something)))

Note that, I have to make a copy of foo then replace bar1 with bar2 in the copied code

I wonder if I can shorten it by copying foo's outer region and wrap bar2 with the outer region

Surely, this can be done if the wrapped code is something simple like a parenthesis (For instance, smartparens can help with that)

2
  • Why would you want to have two definitions for the same foo? I suspect that this is an X-Y-problem. Tell us what you actually want to achieve.
    – Tobias
    Commented Aug 30, 2023 at 7:40
  • Well, I'm not trying to define foo twice, but (especially in lisp) I try to copy some skeleton of codes from somewhere, but (especially with smartparens mode) it's hard to copy the skeleton around my new code.
    – eugene
    Commented Aug 30, 2023 at 8:19

1 Answer 1

1

You can achieve it by writing a wrapper macro for it,

(defmacro my-foo-wrapper (body)
  `(defun foo ()
     ,body))

After evaluating the above code, you can use the following

(my-foo-wrapper
 (defun bar2 ()
   (print "test")))

Insert this example code in some buffer, place your cursor just after it, and do M-x pp-macroexpand-last-sexp to find the result you asked for.

Alternatively, you could create a skeleton or a (ya)snippet for it (or use any other alternative snippets package).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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