@liushihao456/pi-emacs 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +43 -0
- package/index.ts +192 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 liushihao
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# pi-emacs
|
|
2
|
+
|
|
3
|
+
Pi extension for opening `emacsclient` from Pi's TUI.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Starts an Emacs daemon automatically when Pi starts.
|
|
8
|
+
- Adds `/emacs` command.
|
|
9
|
+
- Adds `ctrl+g` shortcut to open `emacsclient -nw` in the terminal.
|
|
10
|
+
- Remembers the last file touched by Pi `edit` / `write` tools and opens it on next launch.
|
|
11
|
+
- Falls back to `dired` in the current working directory when no recent file exists.
|
|
12
|
+
- Enables terminal mouse support inside Emacs.
|
|
13
|
+
- Stops the daemon on Pi quit only if this extension started it.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pi install npm:@liushihao456/pi-emacs
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
For local development:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pi install /path/to/pi-emacs
|
|
25
|
+
# or copy this directory to ~/.pi/agent/extensions/pi-emacs
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Requirements
|
|
29
|
+
|
|
30
|
+
- Emacs available as `emacs`
|
|
31
|
+
- Emacs client available as `emacsclient`
|
|
32
|
+
- Pi interactive TUI mode
|
|
33
|
+
|
|
34
|
+
## Usage
|
|
35
|
+
|
|
36
|
+
- `/emacs` — open Emacs client
|
|
37
|
+
- `ctrl+g` — open Emacs client
|
|
38
|
+
|
|
39
|
+
## Publish
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
npm publish --access public
|
|
43
|
+
```
|
package/index.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtensionAPI,
|
|
3
|
+
ExtensionContext,
|
|
4
|
+
} from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import { spawn } from "node:child_process";
|
|
6
|
+
import { isAbsolute, resolve } from "node:path";
|
|
7
|
+
|
|
8
|
+
type EmacsState = {
|
|
9
|
+
startedEmacsServer: boolean;
|
|
10
|
+
serverStartPromise?: Promise<void>;
|
|
11
|
+
lastEditedFile?: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const state: EmacsState = ((globalThis as any).__piEmacsExtensionState ??= {
|
|
15
|
+
startedEmacsServer: false,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
type SpawnOptions = {
|
|
19
|
+
cwd?: string;
|
|
20
|
+
stdio?: "inherit" | "ignore";
|
|
21
|
+
timeoutMs?: number;
|
|
22
|
+
onBefore?: () => void;
|
|
23
|
+
onAfter?: () => void;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
function run(command: string, args: string[], options: SpawnOptions = {}) {
|
|
27
|
+
return new Promise<void>((resolve, reject) => {
|
|
28
|
+
options.onBefore?.();
|
|
29
|
+
|
|
30
|
+
const child = spawn(command, args, {
|
|
31
|
+
cwd: options.cwd,
|
|
32
|
+
env: process.env,
|
|
33
|
+
stdio: options.stdio ?? "ignore",
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const timer = options.timeoutMs
|
|
37
|
+
? setTimeout(() => {
|
|
38
|
+
child.kill("SIGTERM");
|
|
39
|
+
finish(new Error(`${command} timed out`));
|
|
40
|
+
}, options.timeoutMs)
|
|
41
|
+
: undefined;
|
|
42
|
+
|
|
43
|
+
let done = false;
|
|
44
|
+
const finish = (error?: Error) => {
|
|
45
|
+
if (done) return;
|
|
46
|
+
done = true;
|
|
47
|
+
if (timer) clearTimeout(timer);
|
|
48
|
+
options.onAfter?.();
|
|
49
|
+
error ? reject(error) : resolve();
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
child.on("error", finish);
|
|
53
|
+
child.on("close", (code, signal) => {
|
|
54
|
+
code === 0
|
|
55
|
+
? finish()
|
|
56
|
+
: finish(new Error(`${command} exited with ${signal ?? code}`));
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function emacsClient(args: string[], options: SpawnOptions = {}) {
|
|
62
|
+
return run("emacsclient", args, options);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function withTerminalMouse(expression: string) {
|
|
66
|
+
return `(progn (xterm-mouse-mode 1) (mouse-wheel-mode 1) ${expression})`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function findFileExpression(path: string) {
|
|
70
|
+
return withTerminalMouse(
|
|
71
|
+
[
|
|
72
|
+
`(let* ((file ${JSON.stringify(path)})`,
|
|
73
|
+
"(buf (find-buffer-visiting file)))",
|
|
74
|
+
"(if buf",
|
|
75
|
+
"(progn",
|
|
76
|
+
"(with-current-buffer buf",
|
|
77
|
+
"(when (and (not (buffer-modified-p))",
|
|
78
|
+
"(not (verify-visited-file-modtime buf)))",
|
|
79
|
+
"(revert-buffer :ignore-auto :noconfirm)))",
|
|
80
|
+
"(switch-to-buffer buf))",
|
|
81
|
+
"(find-file file)))",
|
|
82
|
+
].join(" "),
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function diredExpression(cwd: string) {
|
|
87
|
+
return withTerminalMouse(`(dired ${JSON.stringify(cwd)})`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function emacsClientArgs(cwd: string) {
|
|
91
|
+
return state.lastEditedFile
|
|
92
|
+
? ["-nw", "-a", "", "-e", findFileExpression(state.lastEditedFile)]
|
|
93
|
+
: ["-nw", "-a", "", "-e", diredExpression(cwd)];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function rememberEditedFile(input: unknown, cwd: string) {
|
|
97
|
+
const path = (input as { path?: string }).path;
|
|
98
|
+
if (!path) return;
|
|
99
|
+
state.lastEditedFile = isAbsolute(path) ? path : resolve(cwd, path);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function ensureEmacsServer() {
|
|
103
|
+
state.serverStartPromise ??= (async () => {
|
|
104
|
+
try {
|
|
105
|
+
await emacsClient(["--eval", "(emacs-pid)"], { timeoutMs: 2000 });
|
|
106
|
+
return;
|
|
107
|
+
} catch {
|
|
108
|
+
// No reachable server. Start daemon below.
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
console.log("[emacs] starting daemon...");
|
|
112
|
+
await run("emacs", ["--daemon"], { timeoutMs: 15000 });
|
|
113
|
+
state.startedEmacsServer = true;
|
|
114
|
+
console.log("[emacs] daemon started");
|
|
115
|
+
})();
|
|
116
|
+
|
|
117
|
+
return state.serverStartPromise;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function stopEmacsServer() {
|
|
121
|
+
if (!state.startedEmacsServer) return;
|
|
122
|
+
|
|
123
|
+
try {
|
|
124
|
+
await state.serverStartPromise;
|
|
125
|
+
} catch {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
await emacsClient(["--eval", "(kill-emacs)"], { timeoutMs: 5000 });
|
|
130
|
+
state.startedEmacsServer = false;
|
|
131
|
+
state.serverStartPromise = undefined;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function openEmacsClient(ctx: ExtensionContext) {
|
|
135
|
+
if (!ctx.hasUI) {
|
|
136
|
+
ctx.ui.notify("emacsclient requires TUI mode", "error");
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
await ctx.ui.custom((tui, _theme, _keybindings, done) => {
|
|
141
|
+
emacsClient(emacsClientArgs(ctx.cwd), {
|
|
142
|
+
cwd: ctx.cwd,
|
|
143
|
+
stdio: "inherit",
|
|
144
|
+
onBefore: () => {
|
|
145
|
+
tui.stop();
|
|
146
|
+
process.stdout.write("\x1B[2J\x1B[H");
|
|
147
|
+
process.stdout.write("\x1B[?1000l\x1B[?1002l\x1B[?1003l\x1B[?1006l");
|
|
148
|
+
},
|
|
149
|
+
onAfter: () => {
|
|
150
|
+
tui.start();
|
|
151
|
+
tui.requestRender(true);
|
|
152
|
+
},
|
|
153
|
+
})
|
|
154
|
+
.then(() => done(null))
|
|
155
|
+
.catch((error) => {
|
|
156
|
+
ctx.ui.notify(`Failed to start emacsclient: ${error.message}`, "error");
|
|
157
|
+
done(null);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
return { render: () => [], invalidate: () => {} };
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export default function (pi: ExtensionAPI) {
|
|
165
|
+
pi.on("session_start", () => {
|
|
166
|
+
ensureEmacsServer().catch((error) => {
|
|
167
|
+
console.error(`[emacs] failed to start server: ${error.message}`);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
pi.on("session_shutdown", async (event) => {
|
|
172
|
+
if (event.reason === "quit") await stopEmacsServer();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
pi.on("tool_result", (event, ctx) => {
|
|
176
|
+
if (!event.isError && ["edit", "write"].includes(event.toolName)) {
|
|
177
|
+
rememberEditedFile(event.input, ctx.cwd);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
pi.registerCommand("emacs", {
|
|
182
|
+
description: "Open emacsclient in popup terminal",
|
|
183
|
+
handler: async (_args, ctx) => openEmacsClient(ctx),
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
pi.registerShortcut("ctrl+g", {
|
|
187
|
+
description: "Open emacsclient",
|
|
188
|
+
handler: openEmacsClient,
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export { ensureEmacsServer, openEmacsClient, stopEmacsServer };
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@liushihao456/pi-emacs",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Pi extension that opens emacsclient in a popup terminal and tracks recently edited files.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"pi-package",
|
|
8
|
+
"pi-extension",
|
|
9
|
+
"emacs",
|
|
10
|
+
"emacsclient"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "liushihao",
|
|
14
|
+
"files": [
|
|
15
|
+
"index.ts",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "echo 'nothing to build'",
|
|
21
|
+
"check": "bun build ./index.ts --target=node --outfile=/tmp/pi-emacs-check.js --external @earendil-works/pi-coding-agent"
|
|
22
|
+
},
|
|
23
|
+
"pi": {
|
|
24
|
+
"extensions": [
|
|
25
|
+
"./index.ts"
|
|
26
|
+
]
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
30
|
+
},
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=20"
|
|
33
|
+
}
|
|
34
|
+
}
|