GNU Emacs: Modify any clipboard by using AI and Large Language Models
This method enables users to modify clipboard content from any application running under a window manager on GNU/Linux systems. When a clipboard change occurs, the data is sent to GNU Emacs, which processes it using the built-in AI and large language models (LLMs). After processing, the script returns focus to the original application and inserts the modified clipboard content.
Setup
The file rcd-ai-clipboard-modify.sh
The file rcd-ai-clipboard-modify.sh
has to be placed in the
executable $PATH, and you have to have xdotool
installed with the
Emacs running as server:
Here is the file:
#!/usr/bin/bash PREV_FOCUS=$(xdotool getwindowfocus) xdotool key --clearmodifiers ctrl+a sleep 0.5 xdotool key --clearmodifiers ctrl+c emacsclient -e "(rcd-ai-clipboard-modify)" xdotool windowactivate --sync $PREV_FOCUS xdotool key --clearmodifiers ctrl+a xdotool key --clearmodifiers ctrl+v
The function rcd-ai-clipboard-modify
Your function rcd-ai-clipboard-modify
is here:
(defun rcd-ai-clipboard-modify () “Modify clipboard by using AI.” (interactive) (select-frame-set-input-focus (selected-frame)) (with-temp-buffer (clipboard-yank) (mark-whole-buffer) (rcd-ai) (gui–set-last-clipboard-selection (buffer-string))))Your function
rcd-ai
can look like following, and you may need to rename it. You may download appropriate llamafile from: https://github.com/Mozilla-Ocho/llamafile/ and run it.(defun rcd-llamafile (prompt memory model) "Return answer by using llamafile." (let ((model) (buffer (let ((url-request-method "POST") (url-request-extra-headers '(("Content-Type" . "application/json") ("Authorization" . "Bearer no-key"))) (url-request-data (json-encode `((model . "LLaMA_CPP") (messages . [ ((role . "system") (content . "You are my AI assistant.")) ((role . "user") (content . ,prompt)) ]))))) (url-retrieve-synchronously "http://localhost:8080/v1/chat/completions")))) (when buffer (let* ((json-response (setq my-json (rcd-parse-http-json-string (buffer-to-string buffer)))) (content (cdr (assoc 'content (cdr (assoc 'message (aref (cdr (assq 'choices my-json)) 0)))))) (content (string-replace "" "\n" content))) content)))) (defun rcd-parse-http-json-string (string) "Parses a JSON string preceded by HTTP headers. Returns the parsed JSON object." (let ((json-start-index (string-match "\{" string))) (when json-start-index (json-read-from-string (substring string json-start-index)))))Window Manager settings
In this keys we place following in the
~/.icewm/key
file as I am using IceWM, your settings for corresponding Window Manager should be same:key F2 /home/data1/protected/bin/rcd/rcd-ai-clipboard.shVideo demonstration and explanation
Let us say you are using Mousepad editor, or Xournal++, so you may mark the text with Ctrl-A usually, and then invoke the Window Manager shortcut. In this example it is F2.
By pressing F2 the clipboard is then recorded. Your Emacs must also synchronize clipboard by its settings.
Function asks user for LLM prompt, you could modify the prompt.
The clipboard is recorded by Emacs.
The program
xdotool
transfers focus to previous program, and inserts the modified clipboard.
Leave Your Comment or Contact GNU.Support
Contact GNU.Support now. There is a simple rule at GNU.Support: if we can help you, we do, whenever and wherever necessary, and it's the way we've been doing business since 2002, and the only way we know