Skip to content

gptel‐rewrite addons

karthink edited this page Dec 6, 2024 · 1 revision

Add inline-diffs to gptel-rewrite

gptel includes a few different ways to compare rewritten regions to their original contents, but you can add more. Here is an example of using the inline-diff package to show and act on changes inline:

gptel-rewrite-inline-diff.mp4

To add this to gptel, you'll need to install the inline-diff package. Use package-vc-install or your method of choice. Here's an example using the straight package manager:

(use-package inline-diff
  :straight (:repo "https://code.tecosaur.net/tec/inline-diff")
  :after gptel-rewrite) ;or use :defer

And here's how you can add it to gptel-rewrite

;; Updated version available at https://github.com/karthink/gptel/wiki
(use-package gptel-rewrite
  :after gptel
  :bind (:map gptel-rewrite-actions-map
         ("C-c C-i" . gptel--rewrite-inline-diff))
  :config
  (defun gptel--rewrite-inline-diff (&optional ovs)
    "Start an inline-diff session on OVS."
    (interactive (list (gptel--rewrite-overlay-at)))
    (unless (require 'inline-diff nil t)
      (user-error "Inline diffs require the inline-diff package."))
    (when-let* ((ov-buf (overlay-buffer (or (car-safe ovs) ovs)))
                ((buffer-live-p ov-buf)))
      (with-current-buffer ov-buf
        (cl-loop for ov in (ensure-list ovs)
                 for ov-beg = (overlay-start ov)
                 for ov-end = (overlay-end ov)
                 for response = (overlay-get ov 'gptel-rewrite)
                 do (delete-overlay ov)
                 (inline-diff-words
                  ov-beg ov-end response)))))
  (when (boundp 'gptel--rewrite-dispatch-actions)
    (add-to-list
     'gptel--rewrite-dispatch-actions '(?i "inline-diff")
     'append)))

Since inline-diff is a work-in-progress package without much documentation (yet), here is a short primer on how to use it: move the cursor into an inline diff and press M-a or M-k to accept or reject it. This moves you to the next inline diff automatically, but you can also navigate manually using M-n and M-p. You can check/change these keybindings in inline-diff-overlay-map, and in inline-diff-repeat-map if you use repeat-mode.