-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchatgpt-sideline.el
83 lines (65 loc) · 2.68 KB
/
chatgpt-sideline.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
;;; chatgpt-sideline.el --- Sideline support for chatgpt -*- lexical-binding: t; -*-
;; Copyright (C) 2023-2025 Shen, Jen-Chieh
;; Author: Shen, Jen-Chieh <[email protected]>
;; Maintainer: Shen, Jen-Chieh <[email protected]>
;; URL: https://github.com/emacs-openai/chatgpt-sideline
;; Version: 0.1.0
;; Package-Requires: ((emacs "27.1") (chatgpt "0.1.0") (sideline "0.1.0"))
;; Keywords: convenience chatgpt ai
;; This file is not part of GNU Emacs.
;; This program 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 of the License, or
;; (at your option) any later version.
;; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Sideline support for chatgpt.
;;
;;; Code:
(require 'cl-lib)
(require 'pcase)
(require 'simple)
(require 'subr-x)
(require 'chatgpt)
(require 'sideline)
(defgroup chatgpt-sideline nil
"Sideline support for chatgpt."
:prefix "chatgpt-sideline-"
:group 'tool
:link '(url-link :tag "Repository" "https://github.com/emacs-openai/chatgpt-sideline"))
;;;###autoload
(defun chatgpt-sideline (command)
"Backend sideline for chatgpt.
Argument COMMAND is required in sideline backend."
(cl-case command
(`candidates (cons :async #'chatgpt-sideline--show))
(`action #'chatgpt-sideline--on-action)))
(defun chatgpt-sideline--on-action (candidate &rest _)
"React to action when CANDIDATE is clicked."
(pcase candidate
("📝 Edit" (chatgpt-edit-start chatgpt-instance))
("📋 Copy"
(kill-new (chatgpt-current-content))
(let ((message-log-max)) (message "Copied!")))))
(defun chatgpt-sideline--editable-p ()
"Return non-nil when current section is ediatble."
(string= (chatgpt-user) (chatgpt-current-role)))
(defun chatgpt-sideline--copyable-p ()
"Return non-nil when current section is copyable."
(not (string= (chatgpt-user) (chatgpt-current-role))))
(defun chatgpt-sideline--show (callback &rest _)
"Execute CALLBACK to display with sideline."
(when (and chatgpt-chat-history
(eq major-mode #'chatgpt-mode))
(cond ((chatgpt-sideline--editable-p)
(funcall callback '("📝 Edit")))
((chatgpt-sideline--copyable-p)
(funcall callback '("📋 Copy"))))))
(provide 'chatgpt-sideline)
;;; chatgpt-sideline.el ends here