Skip to content

Commit

Permalink
fix: Better compatible with input/edit mode when restart
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Nov 13, 2023
1 parent dfc5179 commit 53c285c
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions chatgpt.el
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@
window-state-change-hook)
,@body))

(defmacro chatgpt--with-buffer (buffer &rest body)
"Execute BODY with in BUFFER."
(declare (indent 1) (debug t))
`(when (get-buffer ,buffer)
(with-current-buffer ,buffer ,@body)))

;; TODO: Use function `string-pixel-width' after 29.1
(defun chatgpt--string-pixel-width (str)
"Return the width of STR in pixels."
Expand Down Expand Up @@ -220,7 +226,7 @@
(when (timerp timer)
(cancel-timer timer)))

(defun chatgpt--pop-to-buffer (buffer-or-name)
(defun chatgpt--display-buffer (buffer-or-name)
"Wrapper to function `pop-to-buffer'.
Display buffer from BUFFER-OR-NAME."
Expand Down Expand Up @@ -330,9 +336,8 @@ Display buffer from BUFFER-OR-NAME."
(setq target (ht-size chatgpt-instances))) ; Create a new one!
target))

(defun chatgpt-restart ()
(defun chatgpt-restart-session ()
"Restart session."
(interactive)
(when (eq major-mode #'chatgpt-mode)
(let* ((instance chatgpt-instance)
(index (car instance))
Expand Down Expand Up @@ -785,17 +790,22 @@ The data is consist of ROLE and CONTENT."
;;
;;; Entry

(defun chatgpt-mode--kill-buffer-hook ()
(defun chatgpt--clear-side-fields ()
"Kill side fileds."
(let ((instances chatgpt-instances))
(chatgpt--with-buffer chatgpt-input-buffer-name
(when (equal instances chatgpt-instances)
(kill-this-buffer)))
(chatgpt--with-buffer chatgpt-edit-buffer-name
(when (equal instances chatgpt-instances)
(kill-this-buffer)))))

(defun chatgpt-mode--kill-buffer-hook (&rest _)
"Kill buffer hook."
(ht-clear chatgpt-data)
(spinner-stop chatgpt-spinner)
(chatgpt--cancel-text-timer)
(let ((instance chatgpt-instances))
(when (get-buffer chatgpt-input-buffer-name)
(with-current-buffer chatgpt-input-buffer-name
;; kill input if it's the right session
(when (equal instance chatgpt-instances)
(kill-this-buffer))))))
(chatgpt--clear-side-fields))

(defun chatgpt-header-line ()
"The display for header line."
Expand Down Expand Up @@ -855,7 +865,7 @@ Caution, this will overwrite the existing instance!"
(when (get-buffer new-buffer-name)
(user-error "Internal Error: creating instance that already exists"))
(chatgpt-register-instance new-index new-buffer-name)
(chatgpt--pop-to-buffer new-buffer-name)))
(chatgpt--display-buffer new-buffer-name)))

;;;###autoload
(defun chatgpt ()
Expand All @@ -864,11 +874,28 @@ Caution, this will overwrite the existing instance!"
(let ((live-instances (chatgpt--live-instances))
(shown-instances (chatgpt--shown-instances)))
(cond (shown-instances
(chatgpt--pop-to-buffer (nth 0 shown-instances)))
(chatgpt--display-buffer (nth 0 shown-instances)))
(live-instances
(chatgpt--pop-to-buffer (nth 0 live-instances)))
(chatgpt--display-buffer (nth 0 live-instances)))
(t
(chatgpt-new)))))

;;;###autoload
(defun chatgpt-restart ()
"Restart the current ChatGPT instance."
(interactive)
(save-window-excursion
(let ((instance
(cl-case major-mode
(`chatgpt-mode (current-buffer))
(`chatgpt-input-mode (with-current-buffer chatgpt-input-buffer-name
(cdr chatgpt-input-instance)))
(`chatgpt-edit-mode (with-current-buffer chatgpt-edit-buffer-name
(cdr chatgpt-edit-instance))))))
(when instance
(with-current-buffer instance
(chatgpt--clear-side-fields)
(chatgpt-restart-session))))))

(provide 'chatgpt)
;;; chatgpt.el ends here

0 comments on commit 53c285c

Please sign in to comment.