9494; ;
9595; ;; Util
9696
97+ (defmacro chatgpt--with-no-redisplay (&rest body )
98+ " Execute BODY without any redisplay execution."
99+ (declare (indent 0 ) (debug t ))
100+ `(let ((inhibit-redisplay t )
101+ (inhibit-modification-hooks t )
102+ after-focus-change-function
103+ buffer-list-update-hook
104+ display-buffer-alist
105+ window-configuration-change-hook
106+ window-scroll-functions
107+ window-size-change-functions
108+ window-state-change-hook)
109+ ,@body ))
110+
97111(defun chatgpt--kill-buffer (buffer-or-name )
98- " Like function `kill-buffer' but in the safe way."
99- (when-let ((buffer (get-buffer chatgpt-input- buffer-name)))
112+ " Like function `kill-buffer' (BUFFER-OR-NAME) but in the safe way."
113+ (when-let ((buffer (get-buffer buffer-or -name)))
100114 (when (buffer-live-p buffer)
101115 (kill-buffer buffer))))
102116
@@ -128,7 +142,7 @@ Display buffer from BUFFER-OR-NAME."
128142(defun chatgpt--live-instances ()
129143 " Return a list of live instances."
130144 (let ((live-instances))
131- (ht-map (lambda (index buffer )
145+ (ht-map (lambda (_ buffer )
132146 (when (and (get-buffer buffer)
133147 (buffer-live-p buffer))
134148 (push buffer live-instances)))
@@ -139,7 +153,7 @@ Display buffer from BUFFER-OR-NAME."
139153 " Return a list of live instances that are displayed on the screen."
140154 (let ((live-instances (chatgpt--live-instances))
141155 (shown-instances))
142- (dolist (instance shown -instances)
156+ (dolist (instance live -instances)
143157 (when (get-buffer-window instance)
144158 (push instance shown-instances)))
145159 (reverse shown-instances)))
@@ -163,7 +177,6 @@ Display buffer from BUFFER-OR-NAME."
163177 (interactive )
164178 (let* ((instance chatgpt-instances)
165179 (index (car instance))
166- (buffer (cdr instance))
167180 (old-name))
168181 ; ; If buffer is alive, kill it!
169182 (chatgpt-with-instance instance
@@ -298,12 +311,15 @@ The data is consist of ROLE and CONTENT."
298311 (let ((dir (if (window-parameter nil 'window-side )
299312 'bottom 'down ))
300313 (buffer (get-buffer-create chatgpt-input-buffer-name)))
301- (with-current-buffer buffer
302- (chatgpt-input-mode)
303- (setq chatgpt-input-instance instance)
304- (erase-buffer )
305- (insert " Type response here..." )
306- (mark-whole-buffer )) ; waiting for deletion
314+ ; ; XXX: Without this, the highlighting at the end wouldn't work!?
315+ (chatgpt--with-no-redisplay
316+ (with-current-buffer buffer
317+ (chatgpt-input-mode)
318+ (setq chatgpt-input-instance instance)
319+ (erase-buffer )
320+ (insert " Type response here..." )
321+ (call-interactively #'set-mark-command )
322+ (goto-char (point-min )))) ; waiting for deletion
307323 (pop-to-buffer buffer `((display-buffer-in-direction)
308324 (direction . , dir )
309325 (dedicated . t )
@@ -312,12 +328,13 @@ The data is consist of ROLE and CONTENT."
312328(defun chatgpt-input-send ()
313329 " Send the input."
314330 (interactive )
331+ (user-error " fuk" )
315332 (cond
316333 ((not (eq major-mode #'chatgpt-input-mode )) ) ; does nothing
317334 (chatgpt-requesting-p
318335 (message " [BUSY] Waiting for OpanAI to response... " ))
319336 (t
320- (if (use- region-p )
337+ (if (region-active -p )
321338 (delete-region (region-beginning ) (region-end ))
322339 (let ((response (buffer-string )))
323340 (chatgpt-with-instance chatgpt-input-instance
@@ -390,8 +407,9 @@ The data is consist of ROLE and CONTENT."
390407; ;
391408; ;; Entry
392409
393- (defun chatgpt-mode-kill-buffer ()
410+ (defun chatgpt-mode-- kill-buffer-hook ()
394411 " "
412+ ; ; TODO: ..
395413 )
396414
397415(defvar chatgpt-mode-map
@@ -407,7 +425,7 @@ The data is consist of ROLE and CONTENT."
407425\\ <chatgpt-mode-map>"
408426 (setq-local buffer-read-only t )
409427 (font-lock-mode -1 )
410- (add-hook 'kill-buffer-hook #'chatgpt-mode-kill-buffer nil t ))
428+ (add-hook 'kill-buffer-hook #'chatgpt-mode-- kill-buffer-hook nil t ))
411429
412430(defun chatgpt-register-instance (index buffer-or-name )
413431 " Register BUFFER-OR-NAME with INDEX as an instance.
0 commit comments