Emacs Control Skill
Use emacsctl to interact with running Emacs.
CLI Usage
CODEBLOCK0
emacsctl needs configure for both agent and Emacs side. Check https://github.com/calsys456/emacsctl for proper setup and notice user if possible when emacsctl not found or returned connection failure
Eval
Basically, emacsctl retrieve a string of S-expressions, either from first argument or stdin, then read and eval them with progn inside Emacs, and print the return value of the last expression.
CODEBLOCK1
Be careful with interactive or blocking functions (like read-string, y-or-n-p) as they will hang.
When mistake happened, suggest user to undo or revert. only undo in yourself (e.g. emacsctl '(undo)') if nothing important or you are confident to do so.
BE CAREFUL WHEN EVAL
Insert
When -i specified, emacsctl will perform insertion with given string, file or stdin:
CODEBLOCK2
Replace
When -r specified, emacsctl can perform buffer content replacement in two style: replacing range or replacing certain text.
Replacing Range
Specify --start-position and --end-position, or --start-line and --end-line to replace a range:
CODEBLOCK3
Replacing Certain Text
Give 2 strings or files, to replace the first (old-text) to second (new-text):
CODEBLOCK4
If there's multiple occurrence of old-text, only the one nearest from current point will be replaced. For bulk replacement or regex-based replacement, write ELisp instead.
Interface
Here is some specially designed ELisp functions for agent use, start with emacsctl- and return informative Lisp plist. Call them with Eval if possible.
Point and Mark
Use (emacsctl-point-info &optional (surrounding 2)) and (emacsctl-mark-info &optional (surrounding 2)) for information and surrounding contents for point and mark.
The current point is marked out using "█" and current mark is using "▄".
Buffer
Use (emacsctl-buffer-info &optional (buffer (current-buffer))) for buffer state and metadata.
Use (emacsctl-buffer-imenu &optional (buffer (current-buffer))) for buffer overview.
Fuzzy-match normal buffers using (emacsctl-buffer-list &optional match). Use (emacsctl-hidden-buffer-list &optional match) similarly for hidden buffers.
Grep Buffer
Use (emacsctl-grep pattern &optional (buffer (current-buffer))) to grep buffer content. It will return match result and surroundings.
Use Emacs-specific regular expression. Try to construct regex with rx or regexp-* functions if there's difficult.
Read Buffer
Function:
INLINECODE29
Examples:
CODEBLOCK5
Window
Use (emacsctl-query-window) to view the window layout of current frame. Use (emacsctl-select-window <index>) to switch to a window using the index returned by query.
Kill Ring (Pasteboard)
Use (emacsctl-query-kill-ring) for a brief view of the kill-ring. Write ELisp code to search in-detail.
Environment Inquiries
To search symbol, use (emacsctl-search-symbol <pattern>); For command, use (emacsctl-search-command <pattern>); For function, use (emacsctl-search-function <pattern>); For variable, use (emacsctl-search-variable <pattern>). They will return useful informations.
Pattern will be breaked down by dashes and matched in substring. Accurate query will get better detail.
Tips
The emacs-control skill is not for replacing other tool calls. Emacs is for bodied humans but not you. Use other file-based tools for efficient work, and emacsctl only if it is really needed or user asked to.
Emacs 控制技能
使用 emacsctl 与正在运行的 Emacs 交互。
命令行用法
emacsctl [选项]... [参数]...
选项:
-i, --insert 执行插入操作
-r, --replace 执行替换操作
-b, --buffer BUFFER 指定插入或替换的缓冲区,默认为当前缓冲区
-s, --save 在插入或替换后保存缓冲区
-p, --position INSERT_POSITION
-l, --line INSERT_LINE
-c, --column INSERT_COLUMN
--start-position REPLACESTARTPOSITION
--end-position REPLACEENDPOSITION
--start-line REPLACESTARTLINE
--end-line REPLACEENDLINE
-h, --help
emacsctl 需要在代理端和 Emacs 端都进行配置。请查看 https://github.com/calsys456/emacsctl 了解正确的设置方法,并在 emacsctl 未找到或返回连接失败时尽可能通知用户
求值
基本上,emacsctl 从第一个参数或 stdin 获取一个 S-表达式字符串,然后在 Emacs 内部使用 progn 进行 read 和 eval,并打印最后一个表达式的返回值。
bash
emacsctl (emacs-version)
使用管道
echo (emacs-version) | emacsctl
=> GNU Emacs XX.X.XX
使用 HEREDOC 进行多行输入或包含引号的输入
注意 HEREDOC 末尾有换行符
emacsctl <
=> return-to-me
注意交互式或阻塞函数(如 read-string、y-or-n-p)会导致挂起。
当发生错误时,建议用户撤销或回退。只有在没有重要内容或你有信心处理时,才自行撤销(例如 emacsctl (undo))。
求值时请务必小心
插入
当指定 -i 时,emacsctl 将使用给定的字符串、文件或 stdin 执行插入操作:
bash
在缓冲区 emacsctl.el 的第 50 行第 15 列插入 Hello
emacsctl -i -b emacsctl.el -l 50 -c 15 -p 100 Hello
在当前缓冲区的第 100 个位置插入你的代码
emacsctl -i -p 100 <
EOF
将 ~/gpl-3.0.txt 的内容插入到当前缓冲区的当前位置,并保存缓冲区
emacsctl -i -s ~/gpl-3.0.txt
替换
当指定 -r 时,emacsctl 可以以两种方式执行缓冲区内容替换:替换范围或替换特定文本。
替换范围
指定 --start-position 和 --end-position,或 --start-line 和 --end-line 来替换一个范围:
bash
将缓冲区 emacsctl.el 的前 5 个字符替换为 XXXXX
emacsctl -r -b emacsctl.el --start-position 1 --end-position 5 XXXXX
将当前缓冲区的第 50-100 行替换为你的代码,并保存缓冲区
emacsctl -r --start-line 50 --end-line 100 -s <
EOF
替换特定文本
提供两个字符串或文件,将第一个(旧文本)替换为第二个(新文本):
bash
将缓冲区 emacsctl.el 中最近的 (require function) 形式替换为 ~/function.el 的内容
emacsctl -r -b emacsctl.el (require function) ~/function.el
将当前缓冲区中的 to-be-refined 函数替换为 refined 函数
emacsctl -r <(cat <如果旧文本出现多次,只会替换距离当前点最近的那一处。对于批量替换或基于正则表达式的替换,请编写 ELisp 代码。
接口
以下是一些专门为代理使用设计的 ELisp 函数,以 emacsctl- 开头,并返回信息丰富的 Lisp plist。尽可能使用 Eval 调用它们。
点和标记
使用 (emacsctl-point-info &optional (surrounding 2)) 和 (emacsctl-mark-info &optional (surrounding 2)) 获取点和标记的信息及周围内容。
当前点用 █ 标记,当前标记用 ▄ 标记。
缓冲区
使用 (emacsctl-buffer-info &optional (buffer (current-buffer))) 获取缓冲区状态和元数据。
使用 (emacsctl-buffer-imenu &optional (buffer (current-buffer))) 获取缓冲区概览。
使用 (emacsctl-buffer-list &optional match) 模糊匹配普通缓冲区。类似地,使用 (emacsctl-hidden-buffer-list &optional match) 获取隐藏缓冲区。
搜索缓冲区
使用 (emacsctl-grep pattern &optional (buffer (current-buffer))) 搜索缓冲区内容。它将返回匹配结果及周围内容。
使用 Emacs 特定的正则表达式。如果遇到困难,尝试使用 rx 或 regexp-* 函数构建正则表达式。
读取缓冲区
函数:
(emacsctl-read-buffer &key (buffer (current-buffer))
line (start-line line) (end-line line)
position (start-position (or position (point-min))) (end-position (or position (point-max)))
(surrounding 2)
full-p)
示例:
elisp
;; 读取一个区域
(emacsctl-read-buffer :start-position <点编号> :end-position <点编号>)
;; 读取行
(emacsctl-read-buffer :start-line <行号> :end-line <行号>)
;; 获取行周围的区域
(emacsctl-read-buffer :line <行号> :surrounding 20)
;; 获取缓冲区的完整内容(谨慎使用)
(emacsctl-read-buffer :buffer <缓冲区> :full-p t)
窗口
使用 (emacsctl-query-window) 查看当前框架的窗口布局。使用 (emacsctl-select-window <索引>) 根据查询返回的索引切换到某个窗口。
剪贴环(粘贴板)
使用 (emacsctl-query-kill-ring) 简要查看剪贴环。编写 ELisp 代码进行详细搜索。
环境查询
搜索符号使用 (emacsctl-search-symbol <模式>);搜索命令使用 (emacsctl-search-command <模式>);搜索函数使用 (emacsctl-search-function <模式>);搜索变量使用 (emacsctl-search-variable <模式>)。它们将返回有用的信息。
模式会按破折号拆分并进行子字符串匹配。精确查询将获得更详细的结果。
提示
emacs 控制技能并非用于替代其他工具调用。Emacs 是为有身体的人类设计的,而不是为你。使用其他基于文件的工具进行高效工作,仅在确实需要或用户要求时才使用 emacsctl。