8000 GitHub - nez/xmas · GitHub
[go: up one dir, main page]

Skip to content
nezPublic

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

78 Commits

Folders and files

Repository files navigation

xmas

An editor with the Emacs paradigm, in Clojure.

Buffers, keymaps, modes and hooks, extended with plain Clojure — no Emacs Lisp runtime and no ~/.emacs.d. See PRD.md for scope and non-goals, and ROADMAP.md for milestones.

Architecture

Everything is a buffer, every action is a state transition.

xmas.buf — Buffer data type

A buffer is an immutable map containing a gap-buffer :text, a cursor position, and metadata.

Three operations define the type:

  • make — Constructor. Name, optional text, optional file path.
  • edit — The single edit primitive. Splices repl into text[from..to], adjusts point, records undo, marks modified. Every mutation (insert, delete, kill) bottoms out here.
  • undo — Pops the last edit. Returns nil when empty.

set-point clamps a movement function's result to valid range — all cursor movement goes through it.

xmas.text — Text primitives

Pure functions on strings. No editor state, no buffers — just text and positions.

  • next-pos / prev-pos — Step by one Unicode codepoint (handles surrogate pairs).
  • word-forward / word-backward — Scan to next/previous word boundary.
  • char-width — Terminal display width of a codepoint (2 for CJK/fullwidth, 0 for combining marks, 1 otherwise).
  • display-width — Total terminal columns for a text range.
  • pos-at-col — Find the position at a target display column.

xmas.ed — Editor core

A single atom holds the entire editor state:

{:buf "*scratch*"          ;; name of active buffer
 :bufs {"*scratch*" {...}} ;; all buffers by name
 :kill []                  ;; kill ring (clipboard stack)
 :msg nil                  ;; echo area message
 :mini nil                 ;; minibuffer state (when prompting)
 :scroll 0, :rows R, :cols C, :last-key K}

cur and update-cur bridge editor state to the current buffer. edit and set-point delegate to buf/ through this bridge.

Commands are pure state -> state functions. forward-char, kill-line, yank, save-buffer — they all take editor state and return new state. No side effects except save-buffer (which spits to disk).

Minibuffer — Prompts (find-file, switch-buffer) reuse the buffer abstraction. mini-start creates a temporary buffer named " *mini*" and swaps it in as active. Normal editing commands just work inside it. mini-accept reads the input and calls the callback.

Keybindings — A nested map. Simple keys map to command functions. [:ctrl \x] maps to a sub-map for prefix sequences (C-x C-f, C-x C-s, etc.). handle-key dispatches, with chars falling through to self-insert. User bindings live on state under :bindings and take precedence over the built-in map; xmas.api installs them from key strings.

xmas.api / xmas.keymap — Extension surface

xmas.api is what configuration uses: bind!, add-hook!, register-mode!. It mutates the editor atom in the dynamic var *state* — the live editor normally, a private atom during M-: — so calls are safe inside the command loop's transaction. xmas.keymap parses Emacs key syntax ("C-x C-f") and installs handlers, creating or replacing prefix maps as needed.

Configuration

~/.xmas/init.clj runs once at startup, after persisted state loads. It is plain Clojure:

(ns my.init
  (:require [xmas.api :as api]))

(defn hello
  "Say hello in the echo area."
  [s] (assoc s :msg "hello"))

(api/bind! "C-c h" #'hello)          ; named commands also appear in M-x
(api/add-hook! :clojure-mode-hook
  (fn [s] s))
(api/register-mode! :notes-mode :major
  {"C-c C-c" (fn [s] (assoc s :msg "compiled"))})

M-: evaluates a Clojure form in the same context — it can open a (ns ...), require, or call the API directly. A failing init.clj reports the error in the echo area and the editor keeps running.

xmas.term — Terminal I/O

Raw terminal control without JLine. Two halves:

  • Input: enter-raw-mode! via stty, then reads bytes from System.in. read-key decodes raw bytes into semantic keys — chars, [:ctrl \f], [:meta \b], :up, :backspace, etc. Multi-byte UTF-8 is decoded into codepoints. CSI escape sequences are parsed for arrow keys and special keys.

  • Output: Thin wrappers over ANSI escape codes for cursor movement, clearing, and graphics/colors. Everything writes to System.out directly.

xmas.view — Screen rendering

Converts editor state into terminal output:

  • cursor-pos — Maps a text offset to a [row, col] screen position relative to scroll.
  • render — Draws the full screen: text lines, a mode line (Emacs-style -- *scratch* (fundamental)), and the minibuffer/echo area. Returns the new scroll position.

xmas.log — Debug logging

Append-only file logger to ~/.xmas/debug.log. Every keypress byte and decoded key is logged.

nREPL — live hacking

xmas starts an nREPL server on port 7888. Connect from Emacs/CIDER:

M-x cider-connect RET localhost RET 7888

Or from the command line:

emacsclient -e '(cider-connect-clj (list :host "localhost" :port 7888))'

Once connected, the running editor is fully inspectable and modifiable:

;; inspect state
(keys @xmas.ed/editor)
(:text (xmas.ed/cur @xmas.ed/editor))

;; inject text into the live editor
(swap! xmas.ed/editor xmas.ed/edit 0 0 "hello from emacs!\n")

;; define a new command and bind it (it also appears in M-x)
(defn my-cmd "It works." [s] (xmas.ed/msg s "it works"))
(xmas.api/bind! "M-z" #'my-cmd)

;; open a file programmatically
(swap! xmas.ed/editor xmas.ed/find-file "/tmp/test.txt")

;; switch buffer
(swap! xmas.ed/editor xmas.ed/switch-buffer "*scratch*")

Every command is a pure state -> state function. swap! applies it atomically. Changes appear on the next redisplay cycle (any keypress in xmas).

Data flow

read-key -> handle-key(state, key) -> new state -> render(state) -> terminal
     ^                                                                  |
     '----------------------- command-loop ----------------------------'

The main loop is tight: read a key, dispatch to a command, get new state, render, repeat. The atom is only reset!'d at the loop boundary — commands themselves are pure.

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0