Skip to content

Allow changing the directory for history and cache #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions copilot.el
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,37 @@
:type 'string
:group 'copilot)

(defcustom copilot-cache-directory-alist nil
"Alist of filename patterns and directory names.
Each element looks like (REGEXP . DIRECTORY).

See the documentation of the `backup-directory-alist' variable
for details."
:group 'copilot
:type '(repeat (cons (regexp :tag "Regexp matching filename")
(directory :tag "Directory to store cache and prompt"))))

(defun copilot-make-cache-file-name (file suffix)
"Create the cache file name for FILE.
Normally this is the file's name with \".\" prepended and
\"SUFFIX\" appended.

A match for FILE is sought in `copilot-cache-directory-alist'
\(see the documentation of that variable for details\). If the
directory for the backup doesn't exist, it is created."
(let* ((backup-directory-alist copilot-cache-directory-alist)
(name (make-backup-file-name-1 file)))
(concat (file-name-directory name) "." (file-name-nondirectory name)
suffix)))

;;;###autoload
(defun copilot-complete ()
(interactive)
(let* ((spot (point))
(inhibit-quit t)
(curfile (buffer-file-name))
(cash (concat curfile ".cache"))
(hist (concat curfile ".prompt"))
(cash (copilot-make-cache-file-name curfile ".cache"))
(hist (copilot-make-cache-file-name curfile ".prompt"))
(lang (file-name-extension curfile))

;; extract current line, to left of caret
Expand Down Expand Up @@ -120,9 +143,11 @@ Writing English explanations is forbidden. ")
(kill-buffer (current-buffer))))

;; append prompt for current interaction to the big old prompt
(make-directory (file-name-directory hist) t)
(write-region prompt nil hist 'append 'silent)

;; run llamafile streaming stdout into buffer catching ctrl-g
(make-directory (file-name-directory cash) t)
(with-local-quit
(call-process copilot-bin nil (list (current-buffer) nil) t
"--prompt-cache" cash
Expand Down