From 9494940b996c6da20bccaf7d771674954f17b4dc Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Mon, 20 Jan 2025 08:56:46 +0100 Subject: [PATCH] (elisp-create-defun): New command --- lisp/progmodes/elisp-mode.el | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lisp/progmodes/elisp-mode.el b/lisp/progmodes/elisp-mode.el index 61614007510..f9cbc1023fc 100644 --- a/lisp/progmodes/elisp-mode.el +++ b/lisp/progmodes/elisp-mode.el @@ -2532,6 +2532,38 @@ interactively, this is the prefix argument." "C-n" #'elisp-next-occurrence "C-p" #'elisp-prev-occurrence) +(defun elisp-create-defun (fun-name arg-names) + "Insert a new (defun FUN-NAME ARG-NAMES) form below the current defun. + +FUN-NAME is a string and ARG-NAMES is a list of strings. + +Push a mark before moving point, and leave point inside the new form +where the body of the defun should go. + +Interactively, if point is in a list (FUN . ARGS), then FUN-NAME is the +name of FUN and ARG-NAMES is a list with placeholder argument names, one +for each element of ARGS." + (interactive + (save-excursion + (named-let rec ((ps (reverse (nth 9 (syntax-ppss))))) + (if (null ps) + (user-error "No function call at point") + (goto-char (car ps)) + (pcase (read (current-buffer)) + (`(,(and head (pred symbolp)) . ,tail) + (list (symbol-name head) + (mapcar (compose + (apply-partially #'concat "arg") + #'number-to-string) + (number-sequence 1 (length tail))))) + (_ (rec (cdr ps))))))) + emacs-lisp-mode) + (push-mark) + (beginning-of-defun-raw) + (goto-char (scan-sexps (point) 1)) + (insert "\n\n(defun " fun-name " (" (string-join arg-names " ") "))") + (backward-char)) + (defvar avy-action) (defvar elisp-extract-local-variable-name-history nil) -- 2.39.5