EmacsSwiftModule

Emacs Swift Compatibility OS License: GPL v3

一个用 Swift 编写 Emacs 插件的 Swift 库!

概述

Emacs Swift module 提供了一个方便的 API,用于使用 Swift 编写 Emacs 的动态模块。它将 Emacs Lisp 的动态特性与 Swift 强大的静态类型化相结合,并隐藏了原始 C API 的粗糙性以及该语言中更难的部分,例如缺少闭包和手动内存管理。它还会将 Emacs Lisp 错误和 Swift 异常相互转换。

快速浏览

EmacsSwiftModule 允许你使用 Swift 自己的类型从 Emacs Lisp 调用函数。

let two: Int = try env.funcall("+", with: 1, 1)
assert(two == 2)
try env.funcall("message", with: "%S %S", "Hello", 42)

并用 Swift 闭包定义你自己的 Lisp 函数

try env.defun("foo") {
  (x: Int, y: Int) in x + y
}
try env.defun("bar") {
  (input: [String]) in input.joined(separator: ", ")
}

这些函数可以很容易地在 Emacs Lisp 中使用

(foo 1 1) ;; => 2
(bar ["Hello" "World"]) ;; => "Hello, World"

它会处理两端的错误,因此用户几乎总是可以简单地忽略它们。

try env.defun("always-throws") { (x: Int) throws in
  throw MyError(x: x)
}
try env.defun("calls-afdsiufs") {
  (env: Environment) in
  do {
    try env.funcall("afdsiufs", with: 42)
  } catch EmacsError.signal {
    print("Whoops! It looks like 'afdsiufs' doesn't exist!")
  }
}

在 Lisp 端也是如此

(always-throws 42) ;; => raises (swift-error "Swift exception: MyError(x: 42)")
(calls-afdsiufs) ;; => nil because we caught the error

当在 Swift 中期望的类型要求未满足时,也会发生同样的情况。

(foo "Hello" "World") ;; => raises (wrong-type-argument numberp "Hello")

文档

可以在这里找到软件包的完整文档:https://savchenkovaleriy.github.io/emacs-swift-module/documentation/emacsswiftmodule/

安装

Swift Package Manager

将以下行添加到你的软件包依赖项中

.package("https://github.com/SavchenkoValeriy/emacs-swift-module.git", from: "1.3.4")

或直接通过 Xcode 添加 "https://github.com/SavchenkoValeriy/emacs-swift-module.git"

贡献

非常欢迎所有贡献!

它可以包括任何帮助:错误报告、关于如何使用它的问题、功能建议和文档更新。

许可证

GPL-3.0