Skip to content

Instantly share code, notes, and snippets.

@ckopsa
Last active January 4, 2024 18:57
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment