Skip to content

Instantly share code, notes, and snippets.

@ckopsa
Last active November 2, 2024 14:50
Show Gist options
  • Save ckopsa/c55bf8cc25df8a4a87c6993bdce3573e to your computer and use it in GitHub Desktop.
Save ckopsa/c55bf8cc25df8a4a87c6993bdce3573e to your computer and use it in GitHub Desktop.
Uses chatgpt-shell to make spelling and grammar fixes
(defun chatgpt-shell-check-and-correct-paragraph ()
"Check and correct the current paragraph using ChatGPT."
(interactive)
(let* ((bounds (bounds-of-thing-at-point 'paragraph))
(start (car bounds))
(end (cdr bounds))
(original-text (buffer-substring-no-properties start end))
(point-offset (- end (point))) ; Save the offset of point from end
(checked-text (chatgpt-shell-check-paragraph original-text)))
(when checked-text
(save-excursion
(goto-char start)
(delete-region start end)
(insert "\n") ; Insert a newline here
(insert checked-text))
;; Move the point to its new position relative to the end
(goto-char (- (cdr (bounds-of-thing-at-point 'paragraph)) point-offset)))))
(defun chatgpt-shell-check-paragraph (text)
"Send TEXT to ChatGPT for spell and grammar checking."
(let ((response (chatgpt-shell-post-prompt
(concat
"Please correct the spelling and grammar of the following paragraph."
"Maintain existing org-mode syntax expressions like =this= and *this*."
"Only return the corrected paragraph, not the original text or this prompt."
"Here's the text:\n\n" text)
nil "gpt-3.5-turbo" nil nil nil nil)))
response))
;; Optionally bind to a key
(global-set-key (kbd "C-c s") 'chatgpt-shell-check-and-correct-paragraph)
@grauschnabel
Copy link

great thing!

I tried to get it work also with ediff or any way to see what has changed, and maybe use another solution. Any ideas?

@sm101
Copy link

sm101 commented Oct 30, 2024

chatgpt-shell-post-prompt function is removed from https://github.com/xenodium/chatgpt-shell :(

@sm101
Copy link

sm101 commented Nov 1, 2024

This chatgpt-shell-check-paragraph version works with chatgpt-shell commit 06a66734f429638de5fc503adaa5e969f94ed2bd

(defun chatgpt-shell-check-paragraph (text)
  "Send TEXT to ChatGPT for spell and grammar checking."
  (chatgpt-shell-post-chatgpt-messages :messages `(((role . "user") (content . ,(concat
                     "Please correct the spelling and grammar of the following paragraph."
                     "Maintain existing org-mode syntax expressions like =this= and *this*."
                     "Only return the corrected paragraph, not the original text or this prompt."
                     "Here's the text:\n\n" text))))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment