From b978f935660482aa1c9fe23ccc2a5485493bb2b7 Mon Sep 17 00:00:00 2001 From: John Doty Date: Wed, 14 May 2025 01:26:45 +0000 Subject: [PATCH] [emacs] Editing tools --- site-lisp/doty-tools.el | 47 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/site-lisp/doty-tools.el b/site-lisp/doty-tools.el index 23010ac..8fb4fdb 100644 --- a/site-lisp/doty-tools.el +++ b/site-lisp/doty-tools.el @@ -43,7 +43,6 @@ (with-current-buffer "*Help*" (buffer-substring-no-properties (point-min) (point-max))))) -;; Emacs specific help (gptel-make-tool :name "emacs_describe_function" :function #'doty-tools--describe-function @@ -299,6 +298,52 @@ run." :confirm nil :include t) +;; === Editing tools + +(defun doty-tools--insert-at-line (buffer-or-file line-number text &optional at-end) + "Insert TEXT at LINE-NUMBER in BUFFER-OR-FILE. +If AT-END is non-nil, insert at end of line, otherwise at beginning." + (with-current-buffer (if (bufferp buffer-or-file) + buffer-or-file + (find-file-noselect buffer-or-file)) + (save-excursion + (save-restriction + (widen) + (goto-char (point-min)) + (forward-line (1- line-number)) + (if at-end + (end-of-line) + (beginning-of-line)) + (insert text) + (format "Inserted text at %s of line %d in %s" + (if at-end "end" "beginning") + line-number + (if (bufferp buffer-or-file) + (buffer-name buffer-or-file) + buffer-or-file)))))) + +(gptel-make-tool + :name "emacs_insert_at_line" + :function #'doty-tools--insert-at-line + :description "Insert text at the beginning or end of specified line." + :args '((:name "buffer_or_file" + :type string + :description "Buffer name or file path") + (:name "line_number" + :type integer + :description "Line to insert at") + (:name "text" + :type string + :description "Text to insert") + (:name "at_end" + :type boolean + :optional t + :description "If true, insert at end of line; otherwise at beginning (optional)")) + :category "editing" + :confirm t + :include t) + + ;; === System tools (defun doty-tools--run-async-command (callback command)