-
Notifications
You must be signed in to change notification settings - Fork 79
/
chatgpt-shell-prompt-compose.el
677 lines (611 loc) · 31.2 KB
/
chatgpt-shell-prompt-compose.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
;;; chatgpt-shell-prompt-compose.el --- A shell prompt compose buffer -*- lexical-binding: t -*-
;; Copyright (C) 2024 Alvaro Ramirez
;; Author: Alvaro Ramirez https://xenodium.com
;; URL: https://github.com/xenodium/chatgpt-shell
;; This package is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This package is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Prompt compose buffers enable crafting more involved queries and
;; simplify both response navigation and follow-up queries.
;;
;; Support the work https://github.com/sponsors/xenodium
;;; Code:
(eval-when-compile
(require 'cl-lib))
(require 'ring)
(require 'flymake)
(require 'shell-maker)
(declare-function chatgpt-shell-previous-source-block "chatgpt-shell")
(declare-function chatgpt-shell-next-source-block "chatgpt-shell")
(declare-function chatgpt-shell-swap-model "chatgpt-shell")
(declare-function chatgpt-shell-swap-system-prompt "chatgpt-shell")
(declare-function chatgpt-shell--minibuffer-prompt "chatgpt-shell")
(declare-function chatgpt-shell--put-source-block-overlays "chatgpt-shell")
(declare-function chatgpt-shell-send-to-buffer "chatgpt-shell")
(declare-function chatgpt-shell-execute-block-action-at-point "chatgpt-shell")
(declare-function chatgpt-shell-block-action-at-point "chatgpt-shell")
(declare-function chatgpt-shell-clear-buffer "chatgpt-shell")
(declare-function chatgpt-shell--primary-buffer "chatgpt-shell")
(declare-function chatgpt-shell--eshell-last-last-command "chatgpt-shell")
(declare-function chatgpt-shell-mark-block "chatgpt-shell")
(declare-function chatgpt-shell--region "chatgpt-shell")
(declare-function chatgpt-shell--pretty-smerge-insert "chatgpt-shell")
(declare-function chatgpt-shell-markdown-block-at-point "chatgpt-shell")
(defvar-local chatgpt-shell-prompt-compose--exit-on-submit nil
"Whether or not compose buffer should close after submission.
This is typically used to craft prompts and immediately jump over to
the shell to follow the response.")
(defvar-local chatgpt-shell-prompt-compose--last-known-region nil
"Last known region details.
Of the form
\((:buffer . buffer)
(:start . start)
(:end . end)
(:text . text))")
(defvar-local chatgpt-shell-prompt-compose--transient-frame-p nil
"Identifies whether or not buffer is running on a dedicated frame.
t if invoked from a transient frame (quitting closes the frame).")
(defvar chatgpt-shell-prompt-compose-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") #'chatgpt-shell-prompt-compose-send-buffer)
(define-key map (kbd "C-c C-k") #'chatgpt-shell-prompt-compose-cancel)
(define-key map (kbd "C-c C-s") #'chatgpt-shell-prompt-compose-swap-system-prompt)
(define-key map (kbd "C-c C-v") #'chatgpt-shell-prompt-compose-swap-model-version)
(define-key map (kbd "C-c C-o") #'chatgpt-shell-prompt-compose-other-buffer)
(define-key map (kbd "M-r") #'chatgpt-shell-prompt-compose-search-history)
(define-key map (kbd "M-p") #'chatgpt-shell-prompt-compose-previous-history)
(define-key map (kbd "M-n") #'chatgpt-shell-prompt-compose-next-history)
map))
(define-derived-mode chatgpt-shell-prompt-compose-mode fundamental-mode "ChatGPT Compose"
"Major mode for composing ChatGPT prompts from a dedicated buffer."
:keymap chatgpt-shell-prompt-compose-mode-map)
(defvar chatgpt-shell-prompt-compose-view-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "g") #'chatgpt-shell-prompt-compose-retry)
(define-key map (kbd "C-M-h") #'chatgpt-shell-mark-block)
(define-key map (kbd "n") #'chatgpt-shell-prompt-compose-next-item)
(define-key map (kbd "b") #'chatgpt-shell-prompt-compose-previous-interaction)
(define-key map (kbd "f") #'chatgpt-shell-prompt-compose-next-interaction)
(define-key map (kbd "p") #'chatgpt-shell-prompt-compose-previous-item)
(define-key map (kbd "<tab>") #'chatgpt-shell-prompt-compose-next-item)
(define-key map (kbd "<backtab>") #'chatgpt-shell-prompt-compose-previous-item)
(define-key map (kbd "r") #'chatgpt-shell-prompt-compose-reply)
(define-key map (kbd "q") #'chatgpt-shell-prompt-compose-quit-and-close-frame)
(define-key map (kbd "e") #'chatgpt-shell-prompt-compose-request-entire-snippet)
(define-key map (kbd "i") #'chatgpt-shell-prompt-compose-insert-block-at-point)
(define-key map (kbd "m") #'chatgpt-shell-prompt-compose-request-more)
(define-key map (kbd "o") #'chatgpt-shell-prompt-compose-other-buffer)
(set-keymap-parent map view-mode-map)
map)
"Keymap for `chatgpt-shell-prompt-compose-view-mode'.")
(define-minor-mode chatgpt-shell-prompt-compose-view-mode
"Like `view-mode`, but extended for ChatGPT Compose."
:lighter " ChatGPT view"
:keymap chatgpt-shell-prompt-compose-view-mode-map
(setq buffer-read-only chatgpt-shell-prompt-compose-view-mode))
;;;###autoload
(defun chatgpt-shell-prompt-compose (prefix)
"Compose and send prompt from a dedicated buffer.
With PREFIX, clear existing history (wipe asociated shell history).
Whenever `chatgpt-shell-prompt-compose' is invoked, appends any active
region (or flymake issue at point) to compose buffer.
Additionally, if point is at an error/warning raised by flymake,
automatically add context (error/warning + code) to expedite ChatGPT
for help to fix the issue.
The compose buffer always shows the latest interaction, but it's
backed by the shell history. You can always switch to the shell buffer
to view the history.
Editing: While compose buffer is in in edit mode, it offers a couple
of magit-like commit buffer bindings.
`\\[chatgpt-shell-prompt-compose-send-buffer]` to send the buffer query.
`\\[chatgpt-shell-prompt-compose-cancel]` to cancel compose buffer.
`\\[chatgpt-shell-prompt-compose-search-history]` search through history.
`\\[chatgpt-shell-prompt-compose-previous-history]` cycle through previous
item in history.
`\\[chatgpt-shell-prompt-compose-next-history]` cycle through next item in
history.
Read-only: After sending a query, the buffer becomes read-only and
enables additional key bindings.
`\\[chatgpt-shell-prompt-compose-send-buffer]` After sending offers to abort
query in-progress.
`\\[View-quit]` Exits the read-only buffer.
`\\[chatgpt-shell-prompt-compose-retry]` Refresh (re-send the query). Useful
to retry on disconnects.
`\\[chatgpt-shell-prompt-compose-next-item]` Jump to next source block.
`\\[chatgpt-shell-prompt-compose-previous-item]` Jump to next previous block.
`\\[chatgpt-shell-prompt-compose-reply]` Reply to follow-up with additional questions.
`\\[chatgpt-shell-prompt-compose-request-entire-snippet]` Send \"Show entire snippet\" query.
`\\[chatgpt-shell-prompt-compose-insert-block-at-point]` Insert block at point at last known location.
`\\[chatgpt-shell-prompt-compose-request-more]` Send \"Show me more\" query.
`\\[chatgpt-shell-prompt-compose-other-buffer]` Jump to other buffer (ie. the shell itself).
`\\[chatgpt-shell-mark-block]` Mark block at point."
(interactive "P")
(chatgpt-shell-prompt-compose-show-buffer :clear-history prefix))
(defvar-local chatgpt-shell--ring-index nil)
(cl-defun chatgpt-shell-prompt-compose-show-buffer (&key content clear-history transient-frame-p)
"Show a prompt compose buffer.
Prepopulate buffer with optional CONTENT.
Set CLEAR-HISTORY to wipe any existing shell history.
Set TRANSIENT-FRAME-P to also close frame on exit."
(let* ((exit-on-submit (derived-mode-p 'chatgpt-shell-mode))
(region-details)
(input-text (or content
(when-let ((region-active (region-active-p))
(region (buffer-substring (region-beginning)
(region-end))))
(setq region-details (chatgpt-shell--region))
(deactivate-mark)
(concat (if-let ((buffer-file-name (buffer-file-name))
(name (file-name-nondirectory buffer-file-name))
(is-key-file (seq-contains-p '(".babelrc"
".editorconfig"
".eslintignore"
".eslintrc"
".eslintrc.json"
".mocharc.json"
".prettierrc"
"package.json"
"tsconfig.json"
"wrangler.toml")
name)))
(format "%s: \n\n" name)
"")
"```"
(cond ((listp mode-name)
(downcase (car mode-name)))
((stringp mode-name)
(downcase mode-name))
(t
""))
"\n"
region
"\n"
"```"))
(when (derived-mode-p 'eshell-mode)
(chatgpt-shell--eshell-last-last-command))
(when-let* ((diagnostic (flymake-diagnostics (point)))
(line-start (line-beginning-position))
(line-end (line-end-position))
(top-context-start (max (line-beginning-position 1) (point-min)))
(top-context-end (max (line-beginning-position -5) (point-min)))
(bottom-context-start (min (line-beginning-position 2) (point-max)))
(bottom-context-end (min (line-beginning-position 7) (point-max)))
(current-line (buffer-substring line-start line-end)))
(concat
"Fix this code and only show me a diff without explanation\n\n"
(mapconcat #'flymake-diagnostic-text diagnostic "\n")
"\n\n"
(buffer-substring top-context-start top-context-end)
(buffer-substring line-start line-end)
" <--- issue is here\n"
(buffer-substring bottom-context-start bottom-context-end)))))
;; TODO: Consolidate, but until then keep in sync with
;; inlined instructions from `chatgpt-shell-prompt-compose-send-buffer'.
(instructions (concat "Type "
(propertize "C-c C-c" 'face 'help-key-binding)
" to send prompt. "
(propertize "C-c C-k" 'face 'help-key-binding)
" to cancel and exit. "))
(erase-buffer (or clear-history
(not input-text)
;; view-mode = old query, erase for new one.
(with-current-buffer (chatgpt-shell-prompt-compose-buffer)
chatgpt-shell-prompt-compose-view-mode))))
(with-current-buffer (chatgpt-shell-prompt-compose-buffer)
(chatgpt-shell-prompt-compose-mode)
(setq-local chatgpt-shell-prompt-compose--exit-on-submit exit-on-submit)
(setq-local chatgpt-shell-prompt-compose--transient-frame-p transient-frame-p)
(setq-local chatgpt-shell-prompt-compose--last-known-region region-details)
(visual-line-mode +1)
(when clear-history
(with-current-buffer (chatgpt-shell--primary-buffer)
(chatgpt-shell-clear-buffer)))
(when (or erase-buffer
(string-empty-p (string-trim (buffer-string))))
(chatgpt-shell-prompt-compose-view-mode -1)
(chatgpt-shell-prompt-compose--initialize))
(when input-text
(save-excursion
(goto-char (point-max))
(insert "\n\n")
(insert input-text)
(chatgpt-shell--put-source-block-overlays)))
;; TODO: Find a better alternative to prevent clash.
;; Disable "n"/"p" for region-bindings-mode-map, so it doesn't
;; clash with "n"/"p" selection binding.
(when (boundp 'region-bindings-mode-disable-predicates)
(add-to-list 'region-bindings-mode-disable-predicates
(lambda () buffer-read-only)))
(setq chatgpt-shell--ring-index nil)
(message instructions))
(unless transient-frame-p
(select-window (display-buffer (chatgpt-shell-prompt-compose-buffer))))
(chatgpt-shell-prompt-compose-buffer)))
(defun chatgpt-shell-prompt-compose-search-history ()
"Search prompt history, select, and insert to current compose buffer."
(interactive)
(unless (derived-mode-p 'chatgpt-shell-prompt-compose-mode)
(user-error "Not in a shell compose buffer"))
(let ((candidate (with-current-buffer (chatgpt-shell--primary-buffer)
(completing-read
"History: "
(delete-dups
(seq-filter
(lambda (item)
(not (string-empty-p item)))
(ring-elements comint-input-ring))) nil t))))
(insert candidate)))
(defun chatgpt-shell-prompt-compose-quit-and-close-frame ()
"Quit compose and close frame if it's the last window."
(interactive)
(unless (derived-mode-p 'chatgpt-shell-prompt-compose-mode)
(user-error "Not in a shell compose buffer"))
(let ((transient-frame-p chatgpt-shell-prompt-compose--transient-frame-p))
(quit-restore-window (get-buffer-window (current-buffer)) 'kill)
(when (and transient-frame-p
(< (chatgpt-shell-prompt-compose-frame-window-count) 2))
(delete-frame))))
(defun chatgpt-shell-prompt-compose-frame-window-count ()
"Get the number of windows per current frame."
(if-let ((window (get-buffer-window (current-buffer)))
(frame (window-frame window)))
(length (window-list frame))
0))
(defun chatgpt-shell-prompt-compose-previous-history ()
"Insert previous prompt from history into compose buffer."
(interactive)
(unless chatgpt-shell-prompt-compose-view-mode
(let* ((ring (with-current-buffer (chatgpt-shell--primary-buffer)
(seq-filter
(lambda (item)
(not (string-empty-p item)))
(ring-elements comint-input-ring))))
(next-index (unless (seq-empty-p ring)
(if chatgpt-shell--ring-index
(1+ chatgpt-shell--ring-index)
0))))
(if next-index
(if (>= next-index (seq-length ring))
(setq chatgpt-shell--ring-index (1- (seq-length ring)))
(setq chatgpt-shell--ring-index next-index))
(setq chatgpt-shell--ring-index nil))
(when chatgpt-shell--ring-index
(chatgpt-shell-prompt-compose--initialize
(seq-elt ring chatgpt-shell--ring-index))))))
(defun chatgpt-shell-prompt-compose-next-history ()
"Insert next prompt from history into compose buffer."
(interactive)
(unless chatgpt-shell-prompt-compose-view-mode
(let* ((ring (with-current-buffer (chatgpt-shell--primary-buffer)
(seq-filter
(lambda (item)
(not (string-empty-p item)))
(ring-elements comint-input-ring))))
(next-index (unless (seq-empty-p ring)
(if chatgpt-shell--ring-index
(1- chatgpt-shell--ring-index)
0))))
(if next-index
(if (< next-index 0)
(setq chatgpt-shell--ring-index nil)
(setq chatgpt-shell--ring-index next-index))
(setq chatgpt-shell--ring-index nil))
(when chatgpt-shell--ring-index
(chatgpt-shell-prompt-compose--initialize
(seq-elt ring chatgpt-shell--ring-index))))))
(defun chatgpt-shell-prompt-compose--initialize (&optional prompt)
"Initialize compose buffer.
Optionally set its PROMPT."
(unless (derived-mode-p 'chatgpt-shell-prompt-compose-mode)
(user-error "Not in a shell compose buffer"))
(let ((inhibit-read-only t))
(erase-buffer)
(insert (chatgpt-shell-prompt-compose--history-label))
(when prompt
(insert (propertize (concat prompt "\n\n")
'rear-nonsticky t
'prompt t
'face font-lock-doc-face)))))
(defun chatgpt-shell-prompt-compose-send-buffer ()
"Send compose buffer content to shell for processing."
(interactive)
(catch 'exit
(unless (derived-mode-p 'chatgpt-shell-prompt-compose-mode)
(user-error "Not in a shell compose buffer"))
(with-current-buffer (chatgpt-shell--primary-buffer)
(when shell-maker--busy
(unless (y-or-n-p "Abort?")
(throw 'exit nil))
(shell-maker-interrupt t)
(with-current-buffer (chatgpt-shell-prompt-compose-buffer)
(progn
(chatgpt-shell-prompt-compose-view-mode -1)
(chatgpt-shell-prompt-compose--initialize)))
(user-error "Aborted")))
(when (chatgpt-shell-block-action-at-point)
(chatgpt-shell-execute-block-action-at-point)
(throw 'exit nil))
(when (string-empty-p
(string-trim
(buffer-substring-no-properties
(point-min) (point-max))))
(chatgpt-shell-prompt-compose--initialize)
(user-error "Nothing to send"))
(if chatgpt-shell-prompt-compose-view-mode
(progn
(chatgpt-shell-prompt-compose-view-mode -1)
(chatgpt-shell-prompt-compose--initialize)
;; TODO: Consolidate, but until then keep in sync with
;; instructions from `chatgpt-shell-prompt-compose-show-buffer'.
(message (concat "Type "
(propertize "C-c C-c" 'face 'help-key-binding)
" to send prompt. "
(propertize "C-c C-k" 'face 'help-key-binding)
" to cancel and exit. ")))
(let ((prompt (string-trim
(chatgpt-shell-prompt-compose--text))))
(chatgpt-shell-prompt-compose--initialize prompt)
(chatgpt-shell--put-source-block-overlays)
(chatgpt-shell-prompt-compose-view-mode +1)
(setq view-exit-action 'kill-buffer)
(when (string-equal prompt "clear")
(view-mode -1)
(chatgpt-shell-prompt-compose--initialize))
(if chatgpt-shell-prompt-compose--exit-on-submit
(let ((view-exit-action nil))
(quit-window t (get-buffer-window (chatgpt-shell-prompt-compose-buffer)))
(chatgpt-shell-send-to-buffer prompt nil nil nil 'shell))
(chatgpt-shell-send-to-buffer prompt nil nil
(lambda (_input _output _success)
(with-current-buffer (chatgpt-shell-prompt-compose-buffer)
(chatgpt-shell--put-source-block-overlays)))
'inline))
;; Point should go to beginning of prompt after submission.
(goto-char (point-min))
(text-property-search-forward 'prompt t)))))
(defun chatgpt-shell-prompt-compose-next-interaction (&optional backwards)
"Show next interaction (request / response).
If BACKWARDS is non-nil, go to previous interaction."
(interactive)
(unless (eq (current-buffer) (chatgpt-shell-prompt-compose-buffer))
(error "Not in a compose buffer"))
(when-let ((shell-buffer (chatgpt-shell--primary-buffer))
(compose-buffer (chatgpt-shell-prompt-compose-buffer))
(next (with-current-buffer (chatgpt-shell--primary-buffer)
(shell-maker-next-command-and-response backwards))))
(chatgpt-shell-prompt-compose-replace-interaction
(car next) (cdr next))
(text-property-search-forward 'prompt t)
next))
(defun chatgpt-shell-prompt-compose-previous-interaction ()
"Show previous interaction (request / response)."
(interactive)
(chatgpt-shell-prompt-compose-next-interaction t))
(defun chatgpt-shell-prompt-compose-replace-interaction (prompt &optional response)
"Replace the current compose's buffer interaction with PROMPT and RESPONSE."
(unless (eq (current-buffer) (chatgpt-shell-prompt-compose-buffer))
(error "Not in a compose buffer"))
(let ((inhibit-read-only t))
(save-excursion
(chatgpt-shell-prompt-compose--initialize prompt)
(when response
(insert response))
(chatgpt-shell--put-source-block-overlays))
(chatgpt-shell-prompt-compose-view-mode +1)))
(defun chatgpt-shell-prompt-compose--history-label ()
"Return the position in history of the primary shell buffer."
(let ((pos (or (chatgpt-shell-prompt-compose--position)
(cons 1 1))))
(propertize (format "[%d/%d]\n\n" (car pos) (cdr pos))
'ignore t
'read-only t
'face font-lock-comment-face
'rear-nonsticky t)))
(defun chatgpt-shell-prompt-compose--position ()
"Return the position in history of the primary shell buffer."
(let* ((current (with-current-buffer (chatgpt-shell--primary-buffer)
(shell-maker--command-and-response-at-point)))
(history (with-current-buffer (chatgpt-shell--primary-buffer)
(shell-maker-history)))
(pos (seq-position history current)))
(cond ((and current history pos)
(cons (1+ pos) (length history)))
(history
(cons (1+ (length history))
(1+ (length history)))))))
(defun chatgpt-shell-prompt-compose--text ()
"Get the compose buffer text (excluding header)."
(unless (derived-mode-p 'chatgpt-shell-prompt-compose-mode)
(user-error "Not in a shell compose buffer"))
(let ((text (buffer-string))
(result "")
(pos 0))
(while (< pos (length text))
(let ((next (or (next-single-property-change pos 'ignore text)
(length text))))
(unless (get-text-property pos 'ignore text)
(setq result (concat result (substring text pos next))))
(setq pos next)))
(string-trim result)))
;; TODO: Delete and use chatgpt-shell-prompt-compose-quit-and-close-frame instead.
(defun chatgpt-shell-prompt-compose-cancel ()
"Cancel and close compose buffer."
(interactive)
(unless (derived-mode-p 'chatgpt-shell-prompt-compose-mode)
(user-error "Not in a shell compose buffer"))
(chatgpt-shell-prompt-compose-quit-and-close-frame))
(defun chatgpt-shell-prompt-compose-buffer-name ()
"Generate compose buffer name."
(concat (chatgpt-shell--minibuffer-prompt) "compose"))
(defun chatgpt-shell-prompt-compose-swap-system-prompt ()
"Swap the compose buffer's system prompt."
(interactive)
(unless (derived-mode-p 'chatgpt-shell-prompt-compose-mode)
(user-error "Not in a shell compose buffer"))
(with-current-buffer (chatgpt-shell--primary-buffer)
(chatgpt-shell-swap-system-prompt))
(rename-buffer (chatgpt-shell-prompt-compose-buffer-name)))
(defun chatgpt-shell-prompt-compose-swap-model-version ()
"Swap the compose buffer's model version."
(interactive)
(unless (derived-mode-p 'chatgpt-shell-prompt-compose-mode)
(user-error "Not in a shell compose buffer"))
(with-current-buffer (chatgpt-shell--primary-buffer)
(chatgpt-shell-swap-model))
(rename-buffer (chatgpt-shell-prompt-compose-buffer-name)))
(defun chatgpt-shell-prompt-compose-buffer ()
"Get the available shell compose buffer."
(unless (chatgpt-shell--primary-buffer)
(error "No shell to compose to"))
(let* ((buffer (get-buffer-create (chatgpt-shell-prompt-compose-buffer-name))))
(unless buffer
(error "No compose buffer available"))
buffer))
(defun chatgpt-shell-prompt-compose-retry ()
"Retry sending request to shell.
Useful if sending a request failed, perhaps from failed connectivity."
(interactive)
(unless (derived-mode-p 'chatgpt-shell-prompt-compose-mode)
(user-error "Not in a shell compose buffer"))
(when-let ((prompt (with-current-buffer (chatgpt-shell--primary-buffer)
(seq-first (delete-dups
(seq-filter
(lambda (item)
(not (string-empty-p item)))
(ring-elements comint-input-ring))))))
(inhibit-read-only t))
(chatgpt-shell-prompt-compose--initialize prompt)
(chatgpt-shell-send-to-buffer prompt nil nil
(lambda (_input _output _success)
(with-current-buffer (chatgpt-shell-prompt-compose-buffer)
(chatgpt-shell--put-source-block-overlays)))
'inline)))
(defun chatgpt-shell-prompt-compose-insert-block-at-point ()
"Insert block at point at last known location."
(interactive)
(save-excursion
(let* ((block (or (chatgpt-shell-markdown-block-at-point)
(error "No block at point")))
(body (buffer-substring-no-properties (or (map-elt block 'start)
(error "No block body found"))
(or (map-elt block 'end)
(error "No block body found"))))
(origin (or chatgpt-shell-prompt-compose--last-known-region
(user-error "Nowhere to insert to")))
(window-config (current-window-configuration)))
(switch-to-buffer-other-window (map-elt origin :buffer))
(with-current-buffer (map-elt origin :buffer)
(if (eq ?y (chatgpt-shell--pretty-smerge-insert
:text body
:start (map-elt origin :start)
:end (map-elt origin :end)
:buffer (map-elt origin :buffer)))
(progn
(map-put! origin :end (+ (map-elt origin :start)
(length body)))
(map-put! origin :text body)
(setq chatgpt-shell-prompt-compose--last-known-region origin))
(set-window-configuration window-config))))))
(defun chatgpt-shell-prompt-compose-next-item ()
"Jump to and select next item (block, link, interaction)."
(interactive)
(unless (derived-mode-p 'chatgpt-shell-prompt-compose-mode)
(user-error "Not in a shell compose buffer"))
(let* ((next-block (save-excursion
(call-interactively #'chatgpt-shell-next-source-block)))
(next-link (save-excursion
(call-interactively #'chatgpt-shell-next-link)))
(positions (delq nil (list next-block
next-link)))
(next-pos (when positions
(apply 'min positions))))
(cond ((not next-pos)
(chatgpt-shell-prompt-compose-next-interaction))
((eq next-pos next-block)
(deactivate-mark)
(goto-char next-block)
(call-interactively #'chatgpt-shell-mark-block))
((eq next-pos next-link)
(deactivate-mark)
(goto-char next-link)))))
(defun chatgpt-shell-prompt-compose-previous-item ()
"Jump to and select previous item (block, link, interaction)."
(interactive)
(unless (derived-mode-p 'chatgpt-shell-prompt-compose-mode)
(user-error "Not in a shell compose buffer"))
(let* ((previous-block (save-excursion
(call-interactively #'chatgpt-shell-previous-source-block)))
(previous-link (save-excursion
(call-interactively #'chatgpt-shell-previous-link)))
(positions (delq nil (list previous-block
previous-link)))
(previous-pos (when positions
(apply 'max positions))))
(cond ((eq (point) (save-excursion
(goto-char (point-min))
(text-property-search-forward 'prompt t)
(point)))
(deactivate-mark)
(chatgpt-shell-prompt-compose-previous-interaction))
((not previous-pos)
(deactivate-mark)
(goto-char (point-min))
(text-property-search-forward 'prompt t))
((eq previous-pos previous-block)
(deactivate-mark)
(goto-char previous-block)
(call-interactively #'chatgpt-shell-mark-block))
((eq previous-pos previous-link)
(deactivate-mark)
(goto-char previous-link)))))
(defun chatgpt-shell-prompt-compose-reply ()
"Reply as a follow-up and compose another query."
(interactive)
(unless (derived-mode-p 'chatgpt-shell-prompt-compose-mode)
(user-error "Not in a shell compose buffer"))
(with-current-buffer (chatgpt-shell--primary-buffer)
(when shell-maker--busy
(user-error "Busy, please wait"))
(goto-char (point-max)))
(chatgpt-shell-prompt-compose-view-mode -1)
(chatgpt-shell-prompt-compose--initialize))
(defun chatgpt-shell-prompt-compose-request-entire-snippet ()
"If the response code is incomplete, request the entire snippet."
(interactive)
(unless (derived-mode-p 'chatgpt-shell-prompt-compose-mode)
(user-error "Not in a shell compose buffer"))
(with-current-buffer (chatgpt-shell--primary-buffer)
(when shell-maker--busy
(user-error "Busy, please wait")))
(let ((prompt "show entire snippet")
(inhibit-read-only t))
(chatgpt-shell-prompt-compose--initialize prompt)
(chatgpt-shell-send-to-buffer prompt nil nil nil 'inline)))
(defun chatgpt-shell-prompt-compose-request-more ()
"Request more data. This is useful if you already requested examples."
(interactive)
(unless (derived-mode-p 'chatgpt-shell-prompt-compose-mode)
(user-error "Not in a shell compose buffer"))
(with-current-buffer (chatgpt-shell--primary-buffer)
(when shell-maker--busy
(user-error "Busy, please wait")))
(let ((prompt "give me more")
(inhibit-read-only t))
(chatgpt-shell-prompt-compose--initialize prompt)
(chatgpt-shell-send-to-buffer prompt nil nil nil 'inline)))
(defun chatgpt-shell-prompt-compose-other-buffer ()
"Jump to the shell buffer (compose's other buffer)."
(interactive)
(unless (derived-mode-p 'chatgpt-shell-prompt-compose-mode)
(user-error "Not in a shell compose buffer"))
(switch-to-buffer (chatgpt-shell--primary-buffer)))
(provide 'chatgpt-shell-prompt-compose)
;;; chatgpt-shell-prompt-compose.el ends here