Skip to content

jiacai2050/zig-emacs

Repository files navigation

Zig-emacs

https://github.com/jiacai2050/zig-emacs/actions/workflows/ci.yml/badge.svg https://github.com/jiacai2050/zig-emacs/actions/workflows/lisp-ci.yml/badge.svg

Zig binding for Emacs’s dynamic modules.

zig-emacs only support Zig master, any contribution is welcome. ⚒️

Example

const std = @import("std");
const emacs = @import("emacs");
usingnamespace emacs;

// This is required by emacs zig module.
pub const allocator = std.heap.c_allocator;

fn greeting(e: emacs.Env, username: []const u8) !emacs.Value {
    // `[]const u8` is allcated by emacs zig module, we need to ensure
    // is freed.
    defer allocator.free(username);

    const msg = try std.fmt.allocPrintZ(
        allocator,
        "hello {s}!",
        .{username},
    );
    defer allocator.free(msg);

    _ = e.message(msg);
    return e.makeString(msg);
}

fn add(e: emacs.Env, a: i32, b: i32) emacs.Value {
    return e.makeInteger(a + b);
}

// Emacs dynamic module entrypoint
pub fn init(env: emacs.Env) c_int {
    env.defineFunc(
        "zig-greeting",
        greeting,
        .{ .doc_string = "greeting written in Zig" },
    );

    env.defineFunc(
        "zig-add",
        add,
        // This make `zig-add` interactive.
        .{ .interactive_spec = "nFirst number: \nnSecond number: " },
    );

    return 0;
}

Compile this example with zig build, then load it into Emacs.

(module-load (expand-file-name "zig-out/lib/libzig-example.dylib"))
(zig-greeting "Jiacai")

If everything is OK, you should see our greeting message in minibuffer.

screenshot.jpg

Usage

zig-emacs support package manager introduced in Zig 0.11.

zig fetch --save=emacs https://github.com/jiacai2050/zig-emacs/archive/${COMMIT}.tar.gz

Replace ${COMMIT} with a real one.

License

MIT

About

Zig binding for Emacs's dynamic modules

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published