Skip to content

Commit 7b21946

Browse files
committed
Add edit-buffer tool
* llm-tool-collection.el (edit-buffer): New tool.
1 parent 201d100 commit 7b21946

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

llm-tool-collection.el

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,31 @@ LIMIT specifies the maximum number of lines to return."
278278
(selected-lines (seq-subseq lines start end)))
279279
(string-join selected-lines "\n"))))
280280

281+
(llm-tool-collection-deftool edit-buffer
282+
(:category "buffers")
283+
((buffer-name "Name of the buffer to modify" :type string)
284+
(old-string "Text to replace (must match exactly)" :type string)
285+
(new-string "Text to replace old_string with" :type string))
286+
"Edits Emacs buffers"
287+
(with-current-buffer buffer-name
288+
(let ((case-fold-search nil))
289+
(save-excursion
290+
(goto-char (point-min))
291+
(let ((count 0))
292+
(while (search-forward old-string nil t)
293+
(setq count (1+ count)))
294+
(if (= count 0)
295+
(format
296+
"Error: Could not find text to replace in buffer %s" buffer-name)
297+
(if (> count 1)
298+
(format
299+
"Error: Found %d matches for the text to replace in buffer %s"
300+
count buffer-name)
301+
(goto-char (point-min))
302+
(search-forward old-string)
303+
(replace-match new-string t t)
304+
(format "Successfully edited buffer %s" buffer-name))))))))
305+
281306
(provide 'llm-tool-collection)
282307

283308
;;; llm-tool-collection.el ends here

0 commit comments

Comments
 (0)