diff options
author | Thanos Apollo <[email protected]> | 2023-03-29 15:00:27 +0300 |
---|---|---|
committer | Thanos Apollo <[email protected]> | 2023-03-29 15:00:27 +0300 |
commit | e66e975bad0ccb3fb2b4be0b1fa5a6657e7bed5e (patch) | |
tree | 1352873e11a3fd4ef87529c2f2ad642722dd9a8c /.emacs.d | |
parent | caaa5b471a233a88d272d886403922f4f7507c2d (diff) |
emacs: Add new map, redo chatgpt
Add new applications mode map, testing it for replacement of general,
add archlinux.org/feed/news at elfeed. Use chatgpt just through
quelpa-use-package since it works fine now that I'm using Arch
Diffstat (limited to '.emacs.d')
-rw-r--r-- | .emacs.d/init.el | 204 |
1 files changed, 31 insertions, 173 deletions
diff --git a/.emacs.d/init.el b/.emacs.d/init.el index 5ef949f..b3942cd 100644 --- a/.emacs.d/init.el +++ b/.emacs.d/init.el @@ -332,13 +332,6 @@ Contains the list of packages that need to be installed.") :config (setq multi-vterm-dedicated-window-height 25)) -(defvar thanos/vterm-map (make-sparse-keymap)) -(define-prefix-command 'thanos/vterm-map) -(define-key global-map (kbd "C-c v") 'thanos/vterm-map) -(define-key thanos/vterm-map (kbd "n") 'multi-vterm-next) -(define-key thanos/vterm-map (kbd "p") 'multi-vterm-prev) -(define-key thanos/vterm-map (kbd "d") 'multi-vterm-dedicated-open) - (use-package eshell :ensure nil :bind (("C-c e" . 'eshell)) @@ -367,171 +360,6 @@ Contains the list of packages that need to be installed.") ;; (setq eshell-prompt-function 'apollo-eshell-prompt) ;; (setq eshell-highlight-prompt t) -(require 'seq) -(eval-when-compile - (require 'cl-lib) - (require 'subr-x) - (require 'env) - (require 'json)) -(defgroup chatgpt nil - "ChatGPT frontend." - :group 'convenience - :prefix "chatgpt-") -(defcustom chatgpt-max-tokens 300 - "Upper limit on the number of tokens the API will return." - :type 'integer) -(defvar chatgpt-buffer "*ChatGPT*" - "Title of the buffer used to store the results of an OpenAI API query.") -(define-error 'chatgpt-error "An error related to the ChatGPT emacs package") -(define-error 'chatgpt-parsing-error - "An error caused by a failure to parse an OpenAI API Response") -(defmacro chatgpt-show-results-buffer-if-active () - "Show the results in other window if necessary." - `(if (and (not ;; visible - (get-buffer-window chatgpt-buffer)) - (called-interactively-p 'interactive)) - (lambda (&optional buf) (ignore buf) - (with-current-buffer buf - (view-mode t)) - (switch-to-buffer-other-window chatgpt-buffer)) - #'identity)) -;;;###autoload -(defun chatgpt-prompt (prompt callback) - "Query OpenAI with PROMPT calling the CALLBACK function on the resulting buffer. -Returns buffer containing the text from this query" - (interactive (list (read-string "Prompt ChatGPT with: ") - (lambda (buf) (with-current-buffer buf - (view-mode t)) - (switch-to-buffer-other-window chatgpt-buffer)))) - (chatgpt--query-open-api prompt - (lambda (results) - (with-current-buffer (get-buffer-create chatgpt-buffer) - ;; Erase contents of buffer after receiving response - (read-only-mode -1) - (erase-buffer) - (insert results) - ;; Return the chatgpt output buffer for non interactive usage - (funcall callback (current-buffer)))))) -;;;###autoload -(defun chatgpt-fix-region (BEG END) - "Takes a region BEG to END asks ChatGPT to explain whats wrong with it. -It then displays the answer in the `chatgpt-buffer'." - (interactive "r") - (let ((current-code (buffer-substring BEG END))) - (chatgpt-prompt (chatgpt--append-to-prompt - current-code - "Why doesn't this code work?") - (chatgpt-show-results-buffer-if-active)))) -;;;###autoload -(defun chatgpt-explain-region (BEG END) - "Takes a region BEG to END asks ChatGPT what it does. -The answer in the displays in `chatgpt-buffer'." - (interactive "r") - (let ((current-code (buffer-substring BEG END))) - (chatgpt-prompt (chatgpt--append-to-prompt - current-code - "What does this code do?") - (chatgpt-show-results-buffer-if-active)))) -;;;###autoload -(defun chatgpt-gen-tests-for-region (BEG END) - "Takes a region BEG to END asks ChatGPT to write a test for it. -It then displays the answer in the `chatgpt-buffer'." - (interactive "r") - (let ((current-code (buffer-substring BEG END))) - (chatgpt-prompt (chatgpt--append-to-prompt - current-code - "Write me a tests for this code") - (chatgpt-show-results-buffer-if-active)))) -;; TODO currently just says what changed but doesn't wanna show the code it's self -;; (defun chatgpt-optimize-region (BEG END) -;; "Takes a region BEG to END asks ChatGPT to optimize it for speed. -;; It then displays the answer in the `chatgpt-buffer'." -;; (interactive "r") -;; (let ((current-code (buffer-substring BEG END))) -;; (chatgpt-prompt (chatgpt--append-to-prompt -;; current-code -;; "Refactor this code for speed and tell me what you changed and why it's faster") -;; (chatgpt-show-results-buffer-if-active)))) -;;;###autoload -(defun chatgpt-refactor-region (BEG END) - "Takes a region BEG to END asks ChatGPT refactor it. -It then displays the answer in the `chatgpt-buffer'." - (interactive "r") - (let ((current-code (buffer-substring BEG END))) - (chatgpt-prompt (chatgpt--append-to-prompt - current-code - "Refactor this code and tell me what you changed") - (chatgpt-show-results-buffer-if-active)))) -;;;###autoload -(defun chatgpt-prompt-region (BEG END) - "Prompt ChatGPT with the region BEG END. -It then displays the results in a separate buffer `chatgpt-buffer'." - (interactive "r") - (chatgpt-prompt (buffer-substring BEG END) - ;; Show the results if not already being viewed - (chatgpt-show-results-buffer-if-active))) -;;;###autoload -(defun chatgpt-prompt-region-and-replace (BEG END) - "Replace region from BEG to END with the response from the ChatGPT API. -The region is BEG and until END" - (interactive "r") - (let ((og-buf (current-buffer))) - (chatgpt-prompt (buffer-substring BEG END) - (lambda (buf) - (save-excursion - (with-current-buffer og-buf - (delete-region BEG END) - (goto-char BEG) - (insert (with-current-buffer buf (buffer-string))))))))) -(defun chatgpt--append-to-prompt (prompt comment-str) - "Append the string COMMENT-STR extra information to a PROMPT as a comment." - (concat prompt - "\n" - comment-start - " " - comment-str)) -(defun chatgpt--extract-text-from-query (query-result) - "Extract the resulting text from a given OpenAI response QUERY-RESULT." - (condition-case err - (thread-last query-result - (assoc-default 'choices) - seq-first - (assoc-default 'text) - string-trim) - (error - (signal 'chatgpt-parsing-error err)))) -(defun chatgpt--parse-response (status callback) - "Ignoring STATUS and parse the response executing the CALLBACK function on the resulting string." - (ignore status) - ;; All this is ran inside the buffer containing the response - (goto-char 0) - (re-search-forward "^$") - (funcall callback (chatgpt--extract-text-from-query (json-read)))) -(defun chatgpt--query-open-api (prompt callback) - "Send a string PROMPT to OpenAI API and pass the resulting buffer to CALLBACK. -The environment variable OPENAI_API_KEY is used as your API key -You can register an account here -https://beta.openai.com/docs/introduction/key-concepts" - (let* ((api-key sercret-api) - (url-request-method (encode-coding-string "POST" 'us-ascii)) - (url-request-extra-headers `(("Content-Type" . "application/json") - ("Authorization" . ,(format "Bearer %s" api-key)))) - (url-request-data (json-encode - `(("model" . "text-davinci-003") - ("prompt" . ,prompt) - ("max_tokens" . ,chatgpt-max-tokens) - ("temperature" . 0))))) - (cl-assert (not (string= "" api-key)) - t - "Current contents of the environmental variable OPENAI_API_KEY -are '%s' which is not an appropriate OpenAI token please ensure -you have the correctly set the OPENAI_API_KEY variable" - api-key) - (url-retrieve - "https://api.openai.com/v1/completions" - 'chatgpt--parse-response - (list callback)))) - (defun apollo/html-boostrap-boilerplate () "Insert html boilerplate with boostrap link." (interactive) @@ -641,10 +469,29 @@ you have the correctly set the OPENAI_API_KEY variable" (global-set-key (kbd "<escape>") 'keyboard-escape-quit) (global-set-key (kbd "s-=") 'text-scale-increase) (global-set-key (kbd "s--") 'text-scale-decrease) +(global-set-key (kbd "C-k") 'copy-region-as-kill) ;; Pass (global-set-key (kbd "C-c p i") 'password-store-insert) (global-set-key (kbd "C-c p e") 'password-store-edit) + + +(define-prefix-command 'thanos/applications-map) +(define-key thanos/applications-map (kbd "q") 'chatgpt-query) +(define-key thanos/applications-map (kbd "a") 'mu4e) +(define-key thanos/applications-map (kbd "t") 'counsel-load-theme) +(define-key thanos/applications-map (kbd "f") 'elfeed) +(global-set-key (kbd "C-c a") 'thanos/applications-map) + + +(defvar thanos/vterm-map (make-sparse-keymap)) +(define-prefix-command 'thanos/vterm-map) +(define-key global-map (kbd "C-c v") 'thanos/vterm-map) +(define-key thanos/vterm-map (kbd "n") 'multi-vterm-next) +(define-key thanos/vterm-map (kbd "p") 'multi-vterm-prev) +(define-key thanos/vterm-map (kbd "d") 'multi-vterm-dedicated-open) +(define-key thanos/vterm-map (kbd "o") 'multi-vterm) + ;; my general's leader key! (defconst general-key "C-c g") @@ -699,6 +546,15 @@ you have the correctly set the OPENAI_API_KEY variable" :keymaps 'org-mode-map "3" 'org-insert-image-size-300) +(require 'quelpa-use-package) +(use-package chatgpt + :quelpa ((chatgpt :fetcher git :url "https://github.com/joshcho/ChatGPT.el.git") :upgrade t) + :init + (require 'python) + (setq chatgpt-repo-path (expand-file-name "chatgpt/" quelpa-build-dir)) + (setq python-interpreter "/usr/bin/python3") + :bind ("C-c q" . chatgpt-query)) + (add-hook 'org-mode-hook 'org-make-toc-mode) (defun apollo/org-theme-dracula () @@ -996,7 +852,9 @@ you have the correctly set the OPENAI_API_KEY variable" ("https://www.youtube.com/feeds/videos.xml?channel_id=UCq6VFHwMzcMXbuKyG7SQYIg" video moist) ("https://www.youtube.com/feeds/videos.xml?channel_id=UC05XpvbHZUQOfA6xk4dlmcw" - video djware)))) + video djware) + ("https://archlinux.org/feeds/news/" + ArchLinux Latest)))) (defun elfeed-v-mpv (url) "Watch a video from URL in MPV" |