Skip to content

Commit 5ba068a

Browse files
committed
Add src
1 parent cf1f837 commit 5ba068a

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

chatgpt-sideline.el

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
;;; chatgpt-sideline.el --- Sideline support for chatgpt -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2023 Shen, Jen-Chieh
4+
5+
;; Author: Shen, Jen-Chieh <[email protected]>
6+
;; Maintainer: Shen, Jen-Chieh <[email protected]>
7+
;; URL: https://github.com/emacs-openai/chatgpt-sideline
8+
;; Version: 0.0.1
9+
;; Package-Requires: ((emacs "27.1") (chatgpt "0.1.0") (sideline "0.1.0"))
10+
;; Keywords: convenience chatgpt ai
11+
12+
;; This file is not part of GNU Emacs.
13+
14+
;; This program is free software: you can redistribute it and/or modify
15+
;; it under the terms of the GNU General Public License as published by
16+
;; the Free Software Foundation, either version 3 of the License, or
17+
;; (at your option) any later version.
18+
19+
;; This program is distributed in the hope that it will be useful,
20+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
;; GNU General Public License for more details.
23+
24+
;; You should have received a copy of the GNU General Public License
25+
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
26+
27+
;;; Commentary:
28+
;;
29+
;; Sideline support for chatgpt.
30+
;;
31+
32+
;;; Code:
33+
34+
(require 'cl-lib)
35+
(require 'pcase)
36+
(require 'simple)
37+
(require 'subr-x)
38+
39+
(require 'chatgpt)
40+
(require 'sideline)
41+
42+
(defgroup chatgpt-sideline nil
43+
"Sideline support for chatgpt."
44+
:prefix "chatgpt-sideline-"
45+
:group 'tool
46+
:link '(url-link :tag "Repository" "https://github.com/emacs-openai/chatgpt-sideline"))
47+
48+
;;;###autoload
49+
(defun chatgpt-sideline (command)
50+
"Backend sideline for chatgpt.
51+
52+
Argument COMMAND is required in sideline backend."
53+
(cl-case command
54+
(`candidates (cons :async #'chatgpt-sideline--show))
55+
(`action #'chatgpt-sideline--on-action)))
56+
57+
(defun chatgpt-sideline--on-action (candidate &rest _)
58+
"React to action when CANDIDATE is clicked."
59+
(pcase candidate
60+
("📝 Edit" )
61+
("📋 Copy" (kill-new (chatgpt-sideline--current-content)))))
62+
63+
(defun chatgpt-sideline--current-chat-point ()
64+
"Return current chat point."
65+
(let ((chat-point))
66+
(cl-some (lambda (pt)
67+
(when (<= pt (point))
68+
(setq chat-point pt)))
69+
chatgpt-chat-points)
70+
chat-point))
71+
72+
(defun chatgpt-sideline--chat-point-index (chat-point)
73+
"Return CHAT-POINT's index."
74+
(cl-position chat-point (chatgpt-chat-points)))
75+
76+
(defun chatgpt-sideline--current-message ()
77+
"Return current message."
78+
(let* ((chat-point (chatgpt-sideline--current-chat-point))
79+
(index (chatgpt-sideline--chat-point-index chat-point)))
80+
(elt chatgpt-chat-history index)))
81+
82+
(defun chatgpt-sideline--current-content ()
83+
"Return current content."
84+
(let ((msg (chatgpt-sideline--current-message)))
85+
(alist-get 'content msg)))
86+
87+
(defun chatgpt-sideline--current-role ()
88+
"Return current role."
89+
(let ((msg (chatgpt-sideline--current-message)))
90+
(alist-get 'role msg)))
91+
92+
(defun chatgpt-sideline--editable-p ()
93+
"Return non-nil when current section is ediatble."
94+
(string= (chatgpt-user) (chatgpt-sideline--current-role)))
95+
96+
(defun chatgpt-sideline--copyable-p ()
97+
"Return non-nil when current section is copyable."
98+
(not (string= (chatgpt-user) (chatgpt-sideline--current-role))))
99+
100+
(defun chatgpt-sideline--show (callback &rest _)
101+
"Execute CALLBACK to display with sideline."
102+
(when (and chatgpt-chat-history
103+
(eq major-mode 'chatgpt-mode))
104+
(cond ((chatgpt-sideline--editable-p)
105+
(funcall callback '("📝 Edit")))
106+
((chatgpt-sideline--copyable-p)
107+
(funcall callback '("📋 Copy"))))))
108+
109+
(provide 'chatgpt-sideline)
110+
;;; chatgpt-sideline.el ends here

0 commit comments

Comments
 (0)