first commit

This commit is contained in:
Joakim
2026-10-15 17:27:46 +02:00
parent 0232909b8a
commit 0e0febffda
38 changed files with 8068 additions and 0 deletions

111
joe-patch/README.md Normal file
View File

@@ -0,0 +1,111 @@
# joe-4.8-hedgedoc-0.7.13.patch
Patches **Joe's Own Editor 4.8** (the real editor, not a reimplementation)
so it can act as a live HedgeDoc client. JOE keeps owning the edit buffer
and all its normal keybindings; the patch only adds a side channel that
reports local edits out and applies remote edits in.
## What it touches
Six files, ~640 added lines, all under `joe/`:
| File | Change |
| --- | --- |
| `hd_ipc.c` | **New.** The whole integration: Unix-socket client, line protocol parser, applying remote edits to a buffer, moving the cursor. |
| `hd_ipc.h` | **New.** Its header. |
| `b.c` | Hooks in `bdel()`/`binsm()` (JOE's core buffer delete/insert primitives) to report local edits out. |
| `tty.c`, `main.c` | Calls `hd_ipc_poll()` from the main loop so remote edits get applied between keystrokes. |
| `Makefile.am` | Adds `hd_ipc.c` to the build. |
Without `JOE_HEDGE_SOCKET` set in the environment, every hook short-circuits
immediately — the patched binary behaves exactly like stock JOE, so there's
no reason to keep two builds around.
## Building
../build.sh
Clones joe 4.8, applies this patch, and builds it under `build/joe-4.8/`
(gitignored -- not part of the repo). Idempotent: safe to re-run, skips the
clone/patch step if `build/joe-4.8/joe/hd_ipc.c` already exists. `../build.sh
--clean` removes `build/` first for a from-scratch rebuild. The binary lands
at `build/joe-4.8/joe/joe`; `../install.sh` puts it wherever `joe` currently
resolves on your PATH (backing up whatever was there first -- see the
top-level README).
Needs a C toolchain, autotools, git, and ncurses headers
(`build-essential autoconf automake libncurses-dev git` on Debian/Ubuntu);
`build.sh` checks for these upfront and says what's missing rather than
failing deep into `make`.
<details>
<summary>Doing it by hand instead (e.g. against a source tree you already have)</summary>
# From the directory *containing* your joe-4.8/ checkout:
patch -p1 < joe-patch/joe-4.8-hedgedoc-0.7.13.patch
cd joe-4.8
autoreconf -fi # required: the patch changes Makefile.am, not
# the generated Makefile.in -- skip this and
# you get a binary silently missing hd_ipc.o
./configure --prefix=$HOME/.local
make
Set `JOE_SRC_DIR=/path/to/your/joe-4.8` before running `../build.sh` to have
it use an existing checkout (must be named `joe-4.8`, matching the patch's
own path assumptions) instead of cloning one.
</details>
## The wire protocol
A tiny line-oriented protocol over a private Unix stream socket, with the
Python agent on the other end. Hex encoding avoids every quoting, locale
and newline ambiguity.
JOE -> agent:
I <byte-offset> <hex-utf8> local insert
D <byte-offset> <byte-count> local delete
agent -> JOE:
I <byte-offset> <hex-utf8> apply remote insert
D <byte-offset> <byte-count> apply remote delete
R <hex-utf8> replace the whole buffer (unused by
default — see CHANGES-0.7.16.md)
C <byte-offset> move the cursor
M <hex-utf8> show a status-line message (offline/
back-online notices) via JOE's own
msgnw() -- the same mechanism behind
built-in notices like "Read only"
Environment variables JOE reads: `JOE_HEDGE_SOCKET` (socket path — also the
on/off switch) and `JOE_HEDGE_BUFFER` (the working file). `hd-joe` sets both.
## Two non-obvious correctness details
Worth knowing if you modify this, because both caused real bugs that took
a while to find (full write-ups in `../CHANGES-0.7.16.md`):
**Re-entrancy.** Applying a remote edit calls the same `bdel()`/`binsm()`
the local hooks are attached to. Without a guard, every remote edit would
be echoed straight back to the server as if the user had typed it.
`hd_ipc_applying()` is that guard — check it before reporting anything.
**Buffer identity.** `bdel()`/`binsm()` fire for *every* buffer JOE has
open, including the search/replace/filename prompt buffers (`TYPEPW`).
Without `hd_target_buf` scoping, typing in the search box would be sent to
the server as document content, and incoming remote edits could land in
whatever prompt happened to have focus. Anything new that hooks JOE
internals needs the same check.
**Cursor handling.** Never apply a large update as a whole-buffer replace
(`bdel` from BOF to EOF, then reinsert). That collapses every pointer in
the buffer — including the cursor — to position 0, with no way to recover
where it was. Incremental insert/delete commands only disturb pointers
inside the range that actually changed.
## Status
Built and tested against joe 4.8 on Linux. The editor-side behaviour
(BOF/EOF edits, cursor placement after AI block rewrites) has been verified
against a real HedgeDoc server; the rest of the stack has only been tested
against fake servers so far — see the top-level README.

View File

@@ -0,0 +1,811 @@
diff -ruN '--exclude=*.o' '--exclude=.deps' '--exclude=Makefile' '--exclude=Makefile.in' '--exclude=autoconf.h*' '--exclude=joe' '--exclude=.dirstamp' '--exclude=stamp-h1' joe-src/joe/Makefile.am build/joe-4.8/joe/Makefile.am
--- joe-src/joe/Makefile.am 2026-09-17 19:01:38.249981415 +0000
+++ build/joe-4.8/joe/Makefile.am 2026-09-17 19:06:56.898941072 +0000
@@ -12,7 +12,7 @@
ufile.h uformat.h uisrch.h umath.h undo.h usearch.h ushell.h utag.h \
utils.h va.h vfile.h vs.h w.h utf8.h syntax.h charmap.h mouse.h \
lattr.h gettext.h builtin.h vt.h mmenu.h state.h options.h selinux.h \
- unicode.h cclass.h frag.h colors.h
+ unicode.h cclass.h frag.h colors.h hd_ipc.h
bin_PROGRAMS = joe
AM_CPPFLAGS = -DJOERC="\"$(sysconf_joedir)/\"" -DJOEDATA="\"$(data_joedir)/\""
@@ -25,7 +25,7 @@
undo.c usearch.c ushell.c utag.c va.c vfile.c vs.c w.c utils.c syntax.c \
utf8.c selinux.c charmap.c mouse.c lattr.c gettext.c builtin.c \
builtins.c vt.c mmenu.c state.c options.c unicode.c \
- cclass.c frag.c colors.c unicat-@UNICODE_VERSION@.c
+ cclass.c frag.c colors.c hd_ipc.c unicat-@UNICODE_VERSION@.c
distclean-local:
rm -f *~
diff -ruN '--exclude=*.o' '--exclude=.deps' '--exclude=Makefile' '--exclude=Makefile.in' '--exclude=autoconf.h*' '--exclude=joe' '--exclude=.dirstamp' '--exclude=stamp-h1' joe-src/joe/b.c build/joe-4.8/joe/b.c
--- joe-src/joe/b.c 2026-09-17 19:01:43.044359058 +0000
+++ build/joe-4.8/joe/b.c 2026-09-20 19:31:23.308585958 +0000
@@ -6,6 +6,7 @@
* This file is part of JOE (Joe's Own Editor)
*/
#include "types.h"
+#include "hd_ipc.h"
#ifdef HAVE_PWD_H
#include <pwd.h>
@@ -1999,8 +2000,13 @@
void bdel(P *from, P *to)
{
if (to->byte - from->byte) {
+ off_t hd_pos = from->byte;
+ off_t hd_len = to->byte - from->byte;
B *b = bcut(from, to);
+ if (!hd_ipc_applying())
+ hd_ipc_local_delete(from->b, hd_pos, (ptrdiff_t)hd_len);
+
if (from->b->undo)
undodel(from->b->undo, from->byte, b);
else
@@ -2185,6 +2191,7 @@
H *h = NULL;
ptrdiff_t hdramnt = 0; /* Only used if h is set */
P *q;
+ off_t hd_pos = p->byte; /* captured BEFORE any mutation, mirrors bdel() */
if (!amnt)
return p;
@@ -2208,6 +2215,19 @@
inschn(q, a);
}
fixupins(q, amnt, nlines1, h, hdramnt);
+ if (!hd_ipc_applying()) {
+ /* Capture position via hd_pos (set above, before any mutation) for
+ * the same defensive reason bdel() captures hd_pos before bcut():
+ * fixupins() rewrites other pointers in the buffer's pointer list,
+ * including p, via pset(), when they shared p's pre-insertion byte
+ * offset. Testing found p->byte == hd_pos after fixupins() for
+ * ordinary typing (this was NOT the cause of the BOF/EOF bugs --
+ * see hd_joe_agent.py's drain_pending() for the actual EOF fix),
+ * but there's no correctness reason to read a value fixupins() is
+ * free to have rewritten when the pre-mutation value is already in
+ * hand. */
+ hd_ipc_local_insert(p->b, hd_pos, blk, amnt);
+ }
pcoalesce(q);
prm(q);
return p;
diff -ruN '--exclude=*.o' '--exclude=.deps' '--exclude=Makefile' '--exclude=Makefile.in' '--exclude=autoconf.h*' '--exclude=joe' '--exclude=.dirstamp' '--exclude=stamp-h1' joe-src/joe/hd_ipc.c build/joe-4.8/joe/hd_ipc.c
--- joe-src/joe/hd_ipc.c 1970-01-01 00:00:00.000000000 +0000
+++ build/joe-4.8/joe/hd_ipc.c 2026-09-20 19:31:23.306292960 +0000
@@ -0,0 +1,567 @@
+/*
+ * HedgeDoc <-> JOE buffer IPC
+ * Copyright (C) 2026
+ *
+ * The protocol is intentionally tiny and line-oriented:
+ *
+ * JOE -> helper:
+ * I <byte-offset> <hex-utf8>
+ * D <byte-offset> <byte-count>
+ *
+ * helper -> JOE:
+ * I <byte-offset> <hex-utf8>
+ * D <byte-offset> <byte-count>
+ * R <hex-utf8> replace the entire buffer atomically
+ * C <byte-offset> move the cursor (e.g. to the next chat prompt
+ * after an AI integration rewrites a block)
+ * M <hex-utf8> show a status-line message (e.g. an
+ * offline/back-online notice) -- same
+ * mechanism JOE's own "Read only" uses
+ *
+ * Hex avoids quoting/locale/newline ambiguity.
+ */
+
+#include "types.h"
+#include "hd_ipc.h"
+
+#ifndef __MSDOS__
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdint.h>
+
+static int hd_fd = -1;
+static int hd_active = 0;
+static int hd_in_apply = 0;
+static char hd_rx[65536];
+static size_t hd_rx_len = 0;
+
+/* The one buffer this session is allowed to touch. Locked onto the first
+ * real text window we see (never a prompt window), so search/replace/
+ * filename prompts and any other internal JOE buffer never leak edits to
+ * or from HedgeDoc. NULL until locked. */
+static B *hd_target_buf = NULL;
+
+typedef struct HdTx {
+ struct HdTx *next;
+ size_t off;
+ size_t len;
+ char *data;
+} HdTx;
+
+static HdTx *hd_tx_head = NULL;
+static HdTx *hd_tx_tail = NULL;
+static size_t hd_tx_bytes = 0;
+
+static void hd_tx_clear(void)
+{
+ HdTx *q = hd_tx_head;
+ while (q) {
+ HdTx *next = q->next;
+ free(q->data);
+ free(q);
+ q = next;
+ }
+ hd_tx_head = hd_tx_tail = NULL;
+ hd_tx_bytes = 0;
+}
+
+static void hd_tx_enqueue(const char *data, size_t len)
+{
+ HdTx *q;
+ if (!len) return;
+ q = (HdTx *)malloc(sizeof(*q));
+ if (!q) return;
+ q->data = (char *)malloc(len);
+ if (!q->data) {
+ free(q);
+ return;
+ }
+ memcpy(q->data, data, len);
+ q->next = NULL;
+ q->off = 0;
+ q->len = len;
+ if (hd_tx_tail)
+ hd_tx_tail->next = q;
+ else
+ hd_tx_head = q;
+ hd_tx_tail = q;
+ hd_tx_bytes += len;
+}
+
+/* Flush queued JOE -> helper messages without ever blocking the editor. */
+static void hd_tx_flush(void)
+{
+ while (hd_fd >= 0 && hd_tx_head) {
+ HdTx *q = hd_tx_head;
+ ssize_t n = write(hd_fd, q->data + q->off, q->len - q->off);
+ if (n > 0) {
+ q->off += (size_t)n;
+ hd_tx_bytes -= (size_t)n;
+ if (q->off == q->len) {
+ hd_tx_head = q->next;
+ if (!hd_tx_head)
+ hd_tx_tail = NULL;
+ free(q->data);
+ free(q);
+ }
+ } else if (n < 0 && (errno == EINTR)) {
+ continue;
+ } else if (n < 0 &&
+#ifdef EAGAIN
+#ifdef EWOULDBLOCK
+#if EAGAIN == EWOULDBLOCK
+ errno == EAGAIN
+#else
+ (errno == EAGAIN || errno == EWOULDBLOCK)
+#endif
+#else
+ errno == EAGAIN
+#endif
+#else
+ errno == EWOULDBLOCK
+#endif
+ ) {
+ return;
+ } else {
+ close(hd_fd);
+ hd_fd = -1;
+ hd_active = 0;
+ hd_rx_len = 0;
+ hd_tx_clear();
+ return;
+ }
+ }
+}
+
+static int hexval(int c)
+{
+ if (c >= '0' && c <= '9') return c - '0';
+ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
+ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
+ return -1;
+}
+
+static void send_local(const char *s, size_t n)
+{
+ if (hd_fd < 0 || hd_in_apply) return;
+ hd_tx_enqueue(s, n);
+ hd_tx_flush();
+}
+
+static void local_hex(const char *data, ptrdiff_t len, char *out)
+{
+ static const char hex[] = "0123456789abcdef";
+ ptrdiff_t i;
+ for (i = 0; i < len; ++i) {
+ unsigned char c = (unsigned char)data[i];
+ out[i * 2] = hex[c >> 4];
+ out[i * 2 + 1] = hex[c & 15];
+ }
+ out[len * 2] = 0;
+}
+
+void hd_ipc_init(void)
+{
+ const char *path = getenv("JOE_HEDGE_SOCKET");
+ struct sockaddr_un sa;
+ int fl;
+
+ if (!path || !*path || hd_fd >= 0) return;
+ if (strlen(path) >= sizeof(sa.sun_path)) return;
+
+ hd_fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (hd_fd < 0) {
+ hd_fd = -1;
+ return;
+ }
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sun_family = AF_UNIX;
+ strncpy(sa.sun_path, path, sizeof(sa.sun_path) - 1);
+
+ if (connect(hd_fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
+ close(hd_fd);
+ hd_fd = -1;
+ return;
+ }
+
+ fl = fcntl(hd_fd, F_GETFL, 0);
+ if (fl >= 0) (void)fcntl(hd_fd, F_SETFL, fl | O_NONBLOCK);
+ hd_active = 1;
+ hd_tx_flush();
+}
+
+static P *pointer_at_byte(B *b, off_t pos, const char *name)
+{
+ P *p;
+
+ if (!b || pos < 0 || pos > b->eof->byte)
+ return NULL;
+
+ /* pfwrd() returns NULL when it steps through the EOF sentinel. That is
+ * inconvenient for an insertion exactly at EOF, so use the real EOF
+ * pointer directly in that case. */
+ if (pos == b->eof->byte)
+ return pdup(b->eof, name);
+
+ p = pdup(b->bof, name);
+ if (!pfwrd(p, pos)) {
+ prm(p);
+ return NULL;
+ }
+ return p;
+}
+
+static void remote_insert(B *b, off_t pos, const unsigned char *data, size_t len)
+{
+ P *p;
+ if (!b || pos < 0 || pos > b->eof->byte || len == 0) return;
+ p = pointer_at_byte(b, pos, "hd_remote_insert");
+ if (!p) return;
+ hd_in_apply = 1;
+ binsm(p, (const char *)data, (ptrdiff_t)len);
+ hd_in_apply = 0;
+ prm(p);
+}
+
+static void remote_delete(B *b, off_t pos, off_t len)
+{
+ P *p, *q;
+ if (!b || pos < 0 || len <= 0 || pos + len > b->eof->byte) return;
+ p = pointer_at_byte(b, pos, "hd_remote_delete");
+ q = pointer_at_byte(b, pos + len, "hd_remote_delete");
+ if (!p || !q) {
+ if (p) prm(p);
+ if (q) prm(q);
+ return;
+ }
+ hd_in_apply = 1;
+ bdel(p, q);
+ hd_in_apply = 0;
+ prm(p);
+ prm(q);
+}
+
+static void remote_replace(B *b, const unsigned char *data, size_t len)
+{
+ P *p, *q;
+ if (!b || !b->bof || !b->eof) return;
+
+ p = pdup(b->bof, "hd_remote_replace");
+ q = pdup(b->eof, "hd_remote_replace");
+ if (!p || !q) {
+ if (p) prm(p);
+ if (q) prm(q);
+ return;
+ }
+
+ hd_in_apply = 1;
+ if (p->byte != q->byte)
+ bdel(p, q);
+ if (len)
+ binsm(p, (const char *)data, (ptrdiff_t)len);
+ hd_in_apply = 0;
+
+ prm(p);
+ prm(q);
+}
+
+/* Move a window's cursor to an absolute byte offset -- used after applying
+ * a remote update (e.g. an AI integration finishing an answer) so the user
+ * lands back at the next input prompt instead of wherever bdel()/binsm()'s
+ * pointer fixups happened to leave the cursor. bw is trusted to belong to
+ * hd_target_buf; callers must check that themselves. */
+static void remote_set_cursor(BW *bw, off_t pos)
+{
+ if (!bw || !bw->cursor || !bw->b || pos < 0 || pos > bw->b->eof->byte) return;
+ p_goto_bof(bw->cursor);
+ pfwrd(bw->cursor, pos);
+}
+
+/* Status-line message (e.g. "OFFLINE -- edits are not syncing"), shown the
+ * same way JOE's own "Read only" / "No such command" notices are: msgnw()
+ * only stores the POINTER it's given, so the string needs storage that
+ * outlives this call -- a static buffer, overwritten each time, rather
+ * than a stack buffer or a leaked strdup() for what's a low-frequency,
+ * single-slot notification (only one message is ever shown at a time). */
+#define HD_MSG_BUF_SIZE 256
+static char hd_msg_buf[HD_MSG_BUF_SIZE];
+
+static void remote_show_message(BW *bw, const char *msg, size_t len)
+{
+ if (!bw || !bw->parent) return;
+ if (len >= HD_MSG_BUF_SIZE) len = HD_MSG_BUF_SIZE - 1;
+ memcpy(hd_msg_buf, msg, len);
+ hd_msg_buf[len] = 0;
+ msgnw(bw->parent, hd_msg_buf);
+}
+
+/* Apply all complete newline-terminated commands. We intentionally process
+ * at most a bounded number per editor loop so a peer cannot starve keyboard
+ * input forever.
+ */
+void hd_ipc_poll(void)
+{
+ ssize_t n;
+ size_t start, end;
+ const char *nl;
+ B *target;
+ BW *target_bw;
+
+ if (hd_fd < 0) {
+ hd_ipc_init();
+ return;
+ }
+
+ hd_tx_flush();
+ if (hd_fd < 0)
+ return;
+
+ for (;;) {
+ n = read(hd_fd, hd_rx + hd_rx_len, sizeof(hd_rx) - hd_rx_len - 1);
+ if (n > 0) {
+ hd_rx_len += (size_t)n;
+ hd_rx[hd_rx_len] = 0;
+ } else if (n == 0) {
+ close(hd_fd);
+ hd_fd = -1;
+ hd_active = 0;
+ hd_rx_len = 0;
+ hd_tx_clear();
+ return;
+ } else if (errno == EINTR) {
+ continue;
+ } else if (
+#ifdef EAGAIN
+#ifdef EWOULDBLOCK
+#if EAGAIN == EWOULDBLOCK
+ errno == EAGAIN
+#else
+ (errno == EAGAIN || errno == EWOULDBLOCK)
+#endif
+#else
+ errno == EAGAIN
+#endif
+#else
+ errno == EWOULDBLOCK
+#endif
+ ) {
+ break;
+ } else {
+ close(hd_fd);
+ hd_fd = -1;
+ hd_active = 0;
+ hd_rx_len = 0;
+ hd_tx_clear();
+ return;
+ }
+ if ((size_t)n == 0) break;
+ if (hd_rx_len + 1 >= sizeof(hd_rx)) {
+ hd_rx_len = 0;
+ hd_tx_clear();
+ return;
+ }
+ }
+
+ /* Lock onto the note's buffer the first time we see a real text window.
+ * TYPEPW (prompt windows: search/replace/filename entry) is deliberately
+ * excluded from the lock -- those are ordinary B buffers too, and locking
+ * onto one by accident would route the note's remote edits into whatever
+ * the user last typed a filename into. */
+ if (!hd_target_buf && maint && maint->curwin &&
+ (maint->curwin->watom->what & TYPETW)) {
+ hd_target_buf = ((BW *)maint->curwin->object)->b;
+ }
+ if (!hd_target_buf) return;
+
+ target = NULL;
+ target_bw = NULL;
+ if (maint && maint->curwin && (maint->curwin->watom->what & (TYPETW | TYPEPW))) {
+ BW *bw = (BW *)maint->curwin->object;
+ if (bw->b == hd_target_buf) {
+ target_bw = bw;
+ target = bw->b;
+ }
+ }
+ if (!target) {
+ /* The locked buffer isn't the focused window right now (user has a
+ * prompt open, or split to another file). Apply directly to the
+ * buffer -- bdel()/binsm() only need P pointers into it, not a
+ * window -- and skip the redraw; it happens naturally once the
+ * user's view returns to this buffer. */
+ target = hd_target_buf;
+ }
+
+ start = 0;
+ while (start < hd_rx_len) {
+ nl = memchr(hd_rx + start, '\n', hd_rx_len - start);
+ if (!nl) break;
+ end = (size_t)(nl - hd_rx);
+ hd_rx[end] = 0;
+
+ if (hd_rx[start] == 'R') {
+ char *s = hd_rx + start + 1;
+ size_t hexlen;
+ size_t bytes;
+ unsigned char *buf = NULL;
+ size_t i;
+ while (*s == ' ') ++s;
+ hexlen = strlen(s);
+ if (!(hexlen & 1) && hexlen <= 65500) {
+ bytes = hexlen / 2;
+ buf = (unsigned char *)malloc(bytes ? bytes : 1);
+ if (buf) {
+ for (i = 0; i < bytes; ++i) {
+ int hi = hexval((unsigned char)s[i * 2]);
+ int lo = hexval((unsigned char)s[i * 2 + 1]);
+ if (hi < 0 || lo < 0) break;
+ buf[i] = (unsigned char)((hi << 4) | lo);
+ }
+ if (i == bytes)
+ remote_replace(target, buf, bytes);
+ free(buf);
+ }
+ }
+ } else if (hd_rx[start] == 'I') {
+ char *s = hd_rx + start + 1;
+ char *ep;
+ unsigned long long pos = strtoull(s, &ep, 10);
+ size_t hexlen;
+ size_t bytes;
+ unsigned char *buf;
+ size_t i;
+ while (*ep == ' ') ++ep;
+ hexlen = strlen(ep);
+ if (hexlen && !(hexlen & 1) && hexlen <= 65500) {
+ bytes = hexlen / 2;
+ buf = (unsigned char *)malloc(bytes);
+ if (buf) {
+ int hi, lo;
+ for (i = 0; i < bytes; ++i) {
+ hi = hexval((unsigned char)ep[i * 2]);
+ lo = hexval((unsigned char)ep[i * 2 + 1]);
+ if (hi < 0 || lo < 0) break;
+ buf[i] = (unsigned char)((hi << 4) | lo);
+ }
+ if (i == bytes)
+ remote_insert(target, (off_t)pos, buf, bytes);
+ free(buf);
+ }
+ }
+ } else if (hd_rx[start] == 'D') {
+ char *s = hd_rx + start + 1;
+ char *ep;
+ unsigned long long pos = strtoull(s, &ep, 10);
+ unsigned long long len;
+ while (*ep == ' ') ++ep;
+ len = strtoull(ep, &ep, 10);
+ remote_delete(target, (off_t)pos, (off_t)len);
+ } else if (hd_rx[start] == 'C') {
+ char *s = hd_rx + start + 1;
+ char *ep;
+ unsigned long long pos = strtoull(s, &ep, 10);
+ if (target_bw)
+ remote_set_cursor(target_bw, (off_t)pos);
+ } else if (hd_rx[start] == 'M') {
+ char *s = hd_rx + start + 1;
+ size_t hexlen;
+ while (*s == ' ') ++s;
+ hexlen = strlen(s);
+ if (target_bw && hexlen && !(hexlen & 1) && hexlen / 2 < HD_MSG_BUF_SIZE) {
+ size_t bytes = hexlen / 2, i;
+ unsigned char buf[HD_MSG_BUF_SIZE];
+ int hi, lo, ok = 1;
+ for (i = 0; i < bytes; ++i) {
+ hi = hexval((unsigned char)s[i * 2]);
+ lo = hexval((unsigned char)s[i * 2 + 1]);
+ if (hi < 0 || lo < 0) { ok = 0; break; }
+ buf[i] = (unsigned char)((hi << 4) | lo);
+ }
+ if (ok)
+ remote_show_message(target_bw, (const char *)buf, bytes);
+ }
+ }
+
+ start = end + 1;
+ hd_rx[end] = '\n';
+ }
+
+ if (start) {
+ size_t remain = hd_rx_len - start;
+ memmove(hd_rx, hd_rx + start, remain);
+ hd_rx_len = remain;
+ }
+
+ /* Force the text window itself to regenerate from the changed buffer. */
+ if (target_bw)
+ bwgen(target_bw, 1, 1);
+}
+
+void hd_ipc_local_insert(B *b, off_t pos, const char *data, ptrdiff_t len)
+{
+ size_t outlen;
+ char *out;
+ int n;
+
+ if (hd_fd < 0 || !hd_active || hd_in_apply || !b || len <= 0) return;
+ if (hd_target_buf && b != hd_target_buf) return; /* not the note buffer */
+ if ((size_t)len > 32700) return;
+ outlen = (size_t)len * 2 + 64;
+ out = (char *)malloc(outlen);
+ if (!out) return;
+ n = snprintf(out, outlen, "I %lld ", (long long)pos);
+ if (n > 0 && (size_t)n < outlen) {
+ local_hex(data, len, out + n);
+ n += (int)((size_t)len * 2);
+ out[n++] = '\n';
+ send_local(out, (size_t)n);
+ }
+ free(out);
+}
+
+void hd_ipc_local_delete(B *b, off_t pos, ptrdiff_t len)
+{
+ char out[128];
+ int n;
+ if (hd_fd < 0 || !hd_active || hd_in_apply || !b || len <= 0) return;
+ if (hd_target_buf && b != hd_target_buf) return; /* not the note buffer */
+ n = snprintf(out, sizeof(out), "D %lld %lld\n",
+ (long long)pos, (long long)len);
+ if (n > 0) send_local(out, (size_t)n);
+}
+
+int hd_ipc_applying(void)
+{
+ return hd_in_apply;
+}
+
+void hd_ipc_close(void)
+{
+ if (hd_fd >= 0) close(hd_fd);
+ hd_fd = -1;
+ hd_active = 0;
+ hd_rx_len = 0;
+ hd_tx_clear();
+}
+
+#else
+
+void hd_ipc_init(void) {}
+void hd_ipc_poll(void) {}
+void hd_ipc_local_insert(B *b, off_t pos, const char *data, ptrdiff_t len)
+{ (void)b; (void)pos; (void)data; (void)len; }
+void hd_ipc_local_delete(B *b, off_t pos, ptrdiff_t len)
+{ (void)b; (void)pos; (void)len; }
+int hd_ipc_applying(void) { return 0; }
+void hd_ipc_close(void) {}
+
+#endif
diff -ruN '--exclude=*.o' '--exclude=.deps' '--exclude=Makefile' '--exclude=Makefile.in' '--exclude=autoconf.h*' '--exclude=joe' '--exclude=.dirstamp' '--exclude=stamp-h1' joe-src/joe/hd_ipc.h build/joe-4.8/joe/hd_ipc.h
--- joe-src/joe/hd_ipc.h 1970-01-01 00:00:00.000000000 +0000
+++ build/joe-4.8/joe/hd_ipc.h 2026-09-17 19:06:56.900359508 +0000
@@ -0,0 +1,18 @@
+#ifndef HD_IPC_H
+#define HD_IPC_H
+
+/*
+ * Optional HedgeDoc editor bridge.
+ *
+ * JOE talks to a per-editor helper over a Unix domain socket. The helper
+ * owns Socket.IO/OT; JOE only reports local buffer mutations and applies
+ * remote mutations to the real B buffer.
+ */
+void hd_ipc_init(void);
+void hd_ipc_poll(void);
+void hd_ipc_local_insert(B *b, off_t pos, const char *data, ptrdiff_t len);
+void hd_ipc_local_delete(B *b, off_t pos, ptrdiff_t len);
+int hd_ipc_applying(void);
+void hd_ipc_close(void);
+
+#endif
diff -ruN '--exclude=*.o' '--exclude=.deps' '--exclude=Makefile' '--exclude=Makefile.in' '--exclude=autoconf.h*' '--exclude=joe' '--exclude=.dirstamp' '--exclude=stamp-h1' joe-src/joe/main.c build/joe-4.8/joe/main.c
--- joe-src/joe/main.c 2026-09-17 19:01:43.048359059 +0000
+++ build/joe-4.8/joe/main.c 2026-09-17 19:08:38.393715715 +0000
@@ -6,6 +6,7 @@
* This file is part of JOE (Joe's Own Editor)
*/
#include "types.h"
+#include "hd_ipc.h"
#ifdef MOUSE_GPM
#include <gpm.h>
@@ -131,6 +132,7 @@
vsrm(exmsg);
exmsg = NULL;
}
+ hd_ipc_poll();
edupd(1);
if (!ahead && !have)
ahead = 1;
diff -ruN '--exclude=*.o' '--exclude=.deps' '--exclude=Makefile' '--exclude=Makefile.in' '--exclude=autoconf.h*' '--exclude=joe' '--exclude=.dirstamp' '--exclude=stamp-h1' joe-src/joe/tty.c build/joe-4.8/joe/tty.c
--- joe-src/joe/tty.c 2026-09-17 19:01:43.052359059 +0000
+++ build/joe-4.8/joe/tty.c 2026-09-17 19:08:38.393715715 +0000
@@ -6,6 +6,9 @@
* This file is part of JOE (Joe's Own Editor)
*/
#include "types.h"
+#include "hd_ipc.h"
+#include <sys/select.h>
+#include <errno.h>
/* For MAC OS */
#ifdef HAVE_UTIL_H
@@ -307,6 +310,9 @@
tim *= 1000;
val.it_value.tv_sec = 0;
val.it_value.tv_usec = tim;
+ } else if (getenv("JOE_HEDGE_SOCKET")) {
+ val.it_value.tv_sec = 0;
+ val.it_value.tv_usec = 100000;
} else {
val.it_value.tv_sec = 1;
val.it_value.tv_usec = 0;
@@ -618,18 +624,66 @@
time_t last_time;
+static int hd_wait_input(int *mpx_ready, int *term_ready)
+{
+ fd_set rfds;
+ struct timeval tv;
+ int r;
+ int mfd = mpxfd;
+ int tfd = fileno(termin);
+ int maxfd = mfd > tfd ? mfd : tfd;
+
+ FD_ZERO(&rfds);
+ if (mfd >= 0)
+ FD_SET(mfd, &rfds);
+ if (tfd >= 0)
+ FD_SET(tfd, &rfds);
+ tv.tv_sec = 0;
+ tv.tv_usec = 100000;
+ r = select(maxfd + 1, &rfds, NULL, NULL, &tv);
+ if (r < 0) {
+ if (errno == EINTR)
+ return -1;
+ return -2;
+ }
+ if (r == 0) {
+ *mpx_ready = 0;
+ *term_ready = 0;
+ return 0;
+ }
+ *mpx_ready = (mfd >= 0 && FD_ISSET(mfd, &rfds));
+ *term_ready = (tfd >= 0 && FD_ISSET(tfd, &rfds));
+ return 1;
+}
+
char ttgetc(void)
{
MACRO *m;
ptrdiff_t mystat;
time_t new_time;
int flg;
-
+ int hd_mpx_ready = 1;
+ int hd_term_ready = 1;
+ int hd_enabled = (getenv("JOE_HEDGE_SOCKET") != NULL);
tickon();
loop:
flg = 0;
+ if (hd_enabled && !have) {
+ int hr = hd_wait_input(&hd_mpx_ready, &hd_term_ready);
+ if (hr == 0) {
+ hd_ipc_poll();
+ edupd(1);
+ goto loop;
+ }
+ if (hr == -1)
+ goto loop;
+ if (hr == -2) {
+ ttsig(0);
+ return 0;
+ }
+ }
/* Status line clock */
new_time = time(NULL);
if (new_time != last_time) {
@@ -657,11 +711,12 @@
ttflsh();
}
if (ticked) {
+ hd_ipc_poll();
edupd(flg);
ttflsh();
tickon();
}
- if (ackkbd != -1) {
+ if (ackkbd != -1 && (hd_mpx_ready || !hd_enabled)) {
if (!have) { /* Wait for input */
mystat = read(mpxfd, &pack, pack.data - (char *)&pack);
@@ -699,6 +754,15 @@
}
if (have) {
have = 0;
+ } else if (hd_enabled) {
+ if (!hd_term_ready)
+ goto loop;
+ if (read(fileno(termin), &havec, 1) < 1) {
+ if (winched || ticked)
+ goto loop;
+ else
+ ttsig(0);
+ }
} else {
if (read(fileno(termin), &havec, 1) < 1) {
if (winched || ticked)
Binary files joe-src/joe/util/checkwidths and build/joe-4.8/joe/util/checkwidths differ
Binary files joe-src/joe/util/stringify and build/joe-4.8/joe/util/stringify differ
Binary files joe-src/joe/util/termidx and build/joe-4.8/joe/util/termidx differ
Binary files joe-src/joe/util/uniproc and build/joe-4.8/joe/util/uniproc differ