# 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`.
Doing it by hand instead (e.g. against a source tree you already have) # 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.
## 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 local insert D local delete agent -> JOE: I apply remote insert D apply remote delete R replace the whole buffer (unused by default — see CHANGES-0.7.16.md) C move the cursor M 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.