↓ Skip to Content Start of content



A function for refactoring PHP code. Moves the currently marked region to a new function and replaces it with a call to that function.


(defun php-extract-function (start end name)
  "Moves the currently marked text to a new function"

  ;; Prompt for new method name
  (interactive "r\nsNew Function Name: ")

  ;; Kill selected region
  (kill-region start end)

  ;; Insert call to new function
  (insert "\n" name "();\n")

  ;; Set a marker so we can jump back to this line
  (point-to-register 1)

  ;; Move to end of current function

  ;; Insert new function
  (insert "\n\nfunction " name "()\n{\n"
	  (car kill-ring-yank-pointer)

  ;; Jump back to where function was snipped from
  (jump-to-register 1))