Skip to content

Commit 1d70fdd

Browse files
committed
Add edit-buffer tool
* llm-tool-collection.el (edit-buffer): New tool.
1 parent d2b87dd commit 1d70fdd

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
@@ -277,6 +277,31 @@ LIMIT specifies the maximum number of lines to return."
277277
(selected-lines (seq-subseq lines start end)))
278278
(string-join selected-lines "\n"))))
279279

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

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

0 commit comments

Comments
 (0)