This is a solution to my previous post
about how to write an elisp function that will take a block of code and turn it into a function replacing the code with a call to the new function and passing in the appropriate parameter. One issue however is that at times we may want to take a list of forms (as would be in a progn) instead of just an sexpr.
(defun blink-list ()
(save-excursion
(forward-list)
(blink-matching-open)
(backward-up-list)))
(defun convert-to-funcall()
(interactive)
(let (fun-name new-param)
(save-excursion
(setq new-param (thing-at-point 'symbol))
(backward-up-list)
(blink-list)
(while (y-or-n-p "go up sexp?")
(backward-up-list)
(blink-list))
(kill-sexp)
(backward-paragraph)
(setq fun-name (read-string "Function Name: "))
(insert "n(defun " fun-name " (" new-param ")n ")
(yank)
(insert ")n"))
(insert "(" fun-name " " new-param ")")))
So to be clear if i have some code as below with my cursor represented by the | then when i call this function i will be able to pick how many times i want to go up the sexpr tree, with the parens blinking everytime i go up, and then i get to name the function and then a new function will be created which was just the code in the old function, with the old function being replaced with a call to the new function.
(defun double-if-even (num)
(if (equal 0 (mod num 2)) (* nu|m 2)))
now i call my new elisp function, i don't climb the tree, and i pass the name "double" and i will get
(defun double (num)
(* num 2))
(defun double-if-even (num)
(if (equal 0 (mod n|um 2)) (double num)))
noticed how i put the cursor back to num in the if expr. now i will call my function convert-to-funcall again, but this time i will need to climb the sexpr tree once. i will name my function "evenp"
(defun double (num)
(* num 2))
(defun evenp (num)
(equal 0 (mod num 2))
(defun double-if-even (num)
(if (evenp num) (double num))