use crate::builtins;
use crate::host::{call_function, el_nil, el_truthy, with_host, ElispHost, SubrFn};
use fusevm::Value;
type R = Result<Value, String>;
pub fn around(name: &str, f: SubrFn, args: &[Value]) -> Option<R> {
let r = match name {
"insert" | "--insert-before-markers--" => insert(f, args),
"insert-char" => insert_char(f, args),
"indent-to" => indent_to(args),
"delete-region" | "delete-and-extract-region" => delete_region(f, args),
"delete-char" => delete_char(f, args),
"erase-buffer" => erase_buffer(f, args),
"elisprs--replace-chars-in-place" => replace_chars(f, args),
"transpose-regions" => transpose_regions(f, args),
"replace-match" => replace_match(f, args),
"insert-file-contents" => {
let pt = point();
modify(pt, pt, None, || run(f, args))
}
"put-text-property"
| "add-text-properties"
| "remove-text-properties"
| "set-text-properties"
| "add-face-text-property" => text_props(name, f, args),
"line-beginning-position" => line_position(f, args, true),
"line-end-position" => line_position(f, args, false),
"beginning-of-line" => line_motion(f, args, true),
"end-of-line" => line_motion(f, args, false),
"set-window-buffer" => set_window_buffer(f, args),
"princ" | "prin1" | "print" | "terpri" | "write-char" => print_to_buffer(name, f, args),
"move-to-column" => move_to_column(f, args),
"call-process" => call_process(f, args),
"subst-char-in-region" => subst_char(f, args),
"read" => read_stream(f, args),
"kill-buffer" => kill_buffer(f, args),
"set-window-configuration" => set_window_configuration(f, args),
"split-window-internal" => split_window_internal(f, args),
"delete-other-windows-internal" => crate::window::delete_other_windows_internal(args),
_ => return None,
};
Some(r)
}
fn run(f: SubrFn, args: &[Value]) -> R {
with_host(|h| f(h, args))
}
fn point() -> usize {
with_host(|h| h.cur_buf_ref().point)
}
fn var(h: &mut ElispHost, name: &str) -> Value {
let sym = h.intern(name);
h.get_value(&sym).unwrap_or(Value::Undef)
}
struct ModState {
inhibit: bool,
prepare: bool,
}
fn mod_state(h: &mut ElispHost) -> ModState {
let inhibit = el_truthy(&var(h, "inhibit-modification-hooks"));
let b = h.cur_buf_ref();
let unmodified = b.mods.save_modiff >= b.mods.modiff;
let (intervals, overlays) = (b.has_intervals, !b.overlays.is_empty());
let undo = !matches!(var(h, "buffer-undo-list"), Value::Bool(true));
let before = !inhibit
&& (el_truthy(&var(h, "before-change-functions"))
|| (unmodified && el_truthy(&var(h, "first-change-hook")))
|| overlays);
ModState {
inhibit,
prepare: undo || intervals || before,
}
}
fn modify(beg: usize, end: usize, buffer: Option<Value>, body: impl FnOnce() -> R) -> R {
modify_with(beg, end, buffer, None, body)
}
fn modify_with(
beg: usize,
end: usize,
buffer: Option<Value>,
after: Option<(usize, usize, usize)>,
body: impl FnOnce() -> R,
) -> R {
change(beg, end, buffer, after, true, body)
}
fn change(
beg: usize,
end: usize,
buffer: Option<Value>,
after: Option<(usize, usize, usize)>,
text_changes: bool,
body: impl FnOnce() -> R,
) -> R {
let in_buffer = |f: &mut dyn FnMut(&mut ElispHost) -> ModState| {
with_host(|h| {
let saved = h.current;
if let Some(bi) = buffer.as_ref().and_then(|b| h.buffer_slot(b)) {
h.current = bi;
}
let r = f(h);
h.current = saved;
r
})
};
let state = if text_changes {
in_buffer(&mut mod_state)
} else {
ModState {
inhibit: true,
prepare: false,
}
};
let buf_arg = buffer.clone().unwrap_or(Value::Undef);
if state.prepare {
let fname = with_host(|h| h.intern("elisprs--prepare-to-modify"));
call_function(
&fname,
&[
Value::Int(beg as i64),
Value::Int(end as i64),
buf_arg.clone(),
],
)?;
}
if !state.inhibit {
in_buffer(&mut |h| {
let sym = h.intern("deactivate-mark");
let _ = h.set_value(&sym, Value::Bool(true));
ModState {
inhibit: false,
prepare: false,
}
});
}
let size = |h: &mut ElispHost| -> i64 {
let saved = h.current;
if let Some(bi) = buffer.as_ref().and_then(|b| h.buffer_slot(b)) {
h.current = bi;
}
let n = h.cur_buf_ref().text.len() as i64;
h.current = saved;
n
};
let size_before = with_host(size);
let r = body()?;
let size_after = with_host(size);
let run_after = with_host(|h| {
let saved = h.current;
if let Some(bi) = buffer.as_ref().and_then(|b| h.buffer_slot(b)) {
h.current = bi;
}
let b = h.cur_buf_ref();
let (intervals, overlays) = (b.has_intervals, !b.overlays.is_empty());
let out = !el_truthy(&var(h, "inhibit-modification-hooks"))
&& (!el_nil(&var(h, "after-change-functions")) || intervals || overlays);
h.current = saved;
out
});
if run_after {
let (a_beg, a_end, a_len) = after.map_or(
(
beg as i64,
end as i64 + (size_after - size_before),
(end - beg) as i64,
),
|(b, e, l)| (b as i64, e as i64, l as i64),
);
let fname = with_host(|h| h.intern("elisprs--signal-after-change"));
call_function(
&fname,
&[
Value::Int(a_beg),
Value::Int(a_end),
Value::Int(a_len),
buf_arg,
],
)?;
}
Ok(r)
}
fn insert_len(h: &ElispHost, v: &Value) -> Option<usize> {
if let Some(s) = h.str_text(v) {
return Some(s.chars().count());
}
matches!(v, Value::Int(_)).then_some(1)
}
fn insert(f: SubrFn, args: &[Value]) -> R {
for arg in args {
let one = std::slice::from_ref(arg);
match with_host(|h| insert_len(h, arg)) {
Some(n) if n > 0 => {
let pt = point();
modify(pt, pt, None, || run(f, one))?;
}
_ => {
run(f, one)?;
}
}
}
Ok(Value::Undef)
}
fn insert_char(f: SubrFn, args: &[Value]) -> R {
let (Some(Value::Int(c)), count) = (args.first(), args.get(1)) else {
return run(f, args);
};
let count = match count {
None | Some(Value::Undef) | Some(Value::Bool(false)) => 1,
Some(Value::Int(n)) => *n,
Some(_) => return run(f, args),
};
if count <= 0 || char::from_u32(*c as u32).is_none() {
return run(f, args);
}
let inherit = args.get(2).is_some_and(el_truthy);
if inherit && with_host(|h| h.cur_buf_ref().has_intervals) {
let ch = char::from_u32(*c as u32).unwrap_or('\u{fffd}');
let (fname, text) = with_host(|h| {
let text: String = std::iter::repeat_n(ch, count as usize).collect();
(h.intern("insert-and-inherit"), h.new_string(text))
});
call_function(&fname, &[text])?;
return Ok(Value::Undef);
}
let pt = point();
modify(pt, pt, None, || run(f, args))
}
fn indent_to(args: &[Value]) -> R {
let (mincol, tabs, spaces) = with_host(|h| builtins::indent_to_plan(h, args))?;
for (c, n) in [('\t', tabs), (' ', spaces)] {
if n > 0 {
let ins = [
Value::Int(c as i64),
Value::Int(n as i64),
Value::Bool(true),
];
insert_char(builtins::insert_char, &ins)?;
}
}
Ok(Value::Int(mincol))
}
fn delete_region(f: SubrFn, args: &[Value]) -> R {
let region = with_host(|h| builtins::validate_region(h, &args[0], &args[1]));
match region {
Ok((lo, hi)) if lo < hi => modify(lo, hi, None, || run(f, args)),
_ => run(f, args),
}
}
fn delete_char(f: SubrFn, args: &[Value]) -> R {
let Some(Value::Int(n)) = args.first() else {
return run(f, args);
};
let n = *n;
let (pt, begv, zv) = with_host(|h| {
let b = h.cur_buf_ref();
(b.point as i64, b.begv as i64, b.zv as i64)
});
let pos = pt + n;
if pos < begv || pos > zv {
return run(f, args);
}
if args.get(1).is_some_and(el_truthy) {
let fname = with_host(|h| h.intern("kill-forward-chars"));
return call_function(&fname, &[Value::Int(n)]);
}
let (lo, hi) = if n < 0 { (pos, pt) } else { (pt, pos) };
if lo == hi {
return run(f, args);
}
modify(lo as usize, hi as usize, None, || run(f, args))
}
fn erase_buffer(f: SubrFn, args: &[Value]) -> R {
let len = with_host(|h| {
h.widen();
h.cur_buf_ref().text.len()
});
if len == 0 {
return run(f, args);
}
modify(1, len + 1, None, || run(f, args))
}
fn replace_chars(f: SubrFn, args: &[Value]) -> R {
let region = with_host(|h| {
let (lo, hi) = builtins::validate_region(h, &args[0], &args[1]).ok()?;
let n = h.str_text(&args[2])?.chars().count();
(lo < hi && n == hi - lo).then_some((lo, hi))
});
match region {
Some((lo, hi)) => modify(lo, hi, None, || run(f, args)),
None => run(f, args),
}
}
fn transpose_regions(f: SubrFn, args: &[Value]) -> R {
if args.len() < 4 {
return run(f, args);
}
let span = with_host(|h| {
let (mut s1, mut e1) = builtins::validate_region(h, &args[0], &args[1]).ok()?;
let (mut s2, mut e2) = builtins::validate_region(h, &args[2], &args[3]).ok()?;
if s2 < e1 {
std::mem::swap(&mut s1, &mut s2);
std::mem::swap(&mut e1, &mut e2);
}
let noop = (s1 == e1 || s2 == e2) && e1 == s2;
(s2 >= e1 && !noop).then_some((s1, e2))
});
match span {
Some((lo, hi)) => modify(lo, hi, None, || run(f, args)),
None => run(f, args),
}
}
fn replace_match(f: SubrFn, args: &[Value]) -> R {
if args.get(3).is_some_and(el_truthy) {
return run(f, args);
}
let span = with_host(|h| {
let subexp = match args.get(4) {
None | Some(Value::Undef) | Some(Value::Bool(false)) => 0,
Some(Value::Int(n)) if *n >= 0 => *n as usize,
Some(_) => return None,
};
let (b, e) = h
.match_data
.as_ref()?
.spans
.get(subexp)
.copied()
.flatten()?;
let buf = h.cur_buf_ref();
(buf.begv <= b && b <= e && e <= buf.zv).then_some((b, e))
});
match span {
Some((b, e)) => modify(b, e, None, || run(f, args)),
None => run(f, args),
}
}
fn text_props(name: &str, f: SubrFn, args: &[Value]) -> R {
let object_index = match name {
"put-text-property" => 4,
"set-text-properties" => 3,
"add-face-text-property" => 4,
_ => 3,
};
let span = with_host(|h| builtins::text_prop_change_span(h, name, args, object_index));
match span {
Some((bi, lo, hi)) => {
let buffer = with_host(|h| h.buffers[bi].self_obj.clone());
modify(lo, hi, Some(buffer), || run(f, args))
}
None => run(f, args),
}
}
fn may_have_fields(h: &ElispHost) -> bool {
let b = h.cur_buf_ref();
b.has_intervals || !b.overlays.is_empty()
}
fn constrain(new_pos: Value, escape: bool) -> R {
let (fname, pt) = with_host(|h| (h.intern("constrain-to-field"), h.cur_buf_ref().point));
call_function(
&fname,
&[
new_pos,
Value::Int(pt as i64),
Value::Bool(escape),
Value::Bool(true),
Value::Undef,
],
)
}
fn bol_escapes(args: &[Value]) -> bool {
match args.first() {
None | Some(Value::Undef) | Some(Value::Bool(false)) => false,
Some(Value::Int(n)) => *n != 1,
Some(_) => true,
}
}
fn line_position(f: SubrFn, args: &[Value], bol: bool) -> R {
let pos = run(f, args)?;
if !with_host(|h| may_have_fields(h)) {
return Ok(pos);
}
constrain(pos, bol && bol_escapes(args))
}
fn line_motion(f: SubrFn, args: &[Value], bol: bool) -> R {
let (orig, may) = with_host(|h| (h.cur_buf_ref().point, may_have_fields(h)));
run(f, args)?;
if !may {
return Ok(Value::Undef);
}
let target = with_host(|h| {
let p = h.cur_buf_ref().point;
h.cur_buf().point = orig;
p
});
let pos = constrain(Value::Int(target as i64), bol && bol_escapes(args))?;
with_host(|h| {
if let Value::Int(p) = pos {
let b = h.cur_buf();
b.point = (p as usize).clamp(b.begv, b.zv);
}
});
Ok(Value::Undef)
}
fn set_window_buffer(f: SubrFn, args: &[Value]) -> R {
let changes = with_host(|h| {
let w = match args.first().map(|v| h.obj(v)) {
None | Some(None) => Some(h.selected_window),
Some(Some(crate::host::Obj::Window(w))) => Some(*w),
_ => None,
}?;
let wd = &h.windows[w];
let current = wd.buffer?;
let target = h.resolve_buffer(args.get(1)?)?;
(current != target && !matches!(wd.dedicated, Value::Bool(true)))
.then(|| wd.self_obj.clone())
});
if let Some(window) = changes {
let fname = with_host(|h| h.intern("record-window-buffer"));
call_function(&fname, &[window])?;
}
let r = run(f, args)?;
let window = args.first().cloned().unwrap_or(Value::Undef);
run_window_scroll_functions(window)?;
Ok(r)
}
fn run_window_scroll_functions(window: Value) -> R {
let fname = with_host(|h| h.intern("run-window-scroll-functions"));
call_function(&fname, &[window])
}
fn split_window_internal(f: SubrFn, args: &[Value]) -> R {
let new = run(f, args)?;
run_window_scroll_functions(new.clone())?;
Ok(new)
}
fn print_to_buffer(name: &str, f: SubrFn, args: &[Value]) -> R {
let pcf = usize::from(name != "terpri");
let target = with_host(|h| match builtins::print_dest(h, args.get(pcf)) {
Ok(builtins::PrintDest::Buffer(bi)) => Some((bi, h.buffers[bi].point)),
Ok(builtins::PrintDest::Marker(m)) => match h.obj(&m) {
Some(crate::host::Obj::Marker(md)) => {
let md = md.borrow();
md.buffer.map(|bi| (bi, md.pos))
}
_ => None,
},
_ => None,
});
match target {
Some((bi, pos)) => {
let (buffer, inserts) = with_host(|h| {
let b = &h.buffers[bi];
let inserts = match name {
"terpri" if args.get(1).is_some_and(el_truthy) => {
!(pos <= b.begv || b.text.get(pos - 2) == Some(&'\n'))
}
"princ" => match args.first() {
Some(v) => h
.str_text(v)
.map(str::to_string)
.or_else(|| h.sym_name(v))
.is_none_or(|s| !s.is_empty()),
None => true,
},
_ => true,
};
(b.self_obj.clone(), inserts)
});
change(pos, pos, Some(buffer), None, inserts, || run(f, args))
}
None => run(f, args),
}
}
fn move_to_column(f: SubrFn, args: &[Value]) -> R {
let (Some(Value::Int(goal)), Some(force)) = (args.first(), args.get(1)) else {
return run(f, args);
};
if *goal < 0 || el_nil(force) {
return run(f, args);
}
let goal = *goal;
let force_t = matches!(force, Value::Bool(true));
let (pos, mut col, prev_pos, prev_col) = with_host(|h| {
let zv = h.cur_buf_ref().zv;
let r = builtins::scan_for_column(h, zv, goal);
h.cur_buf().point = r.0;
r
});
if col > goal {
let tab = with_host(|h| h.cur_buf_ref().text[prev_pos - 1] == '\t');
if tab && prev_col < goal && prev_pos < pos {
with_host(|h| h.cur_buf().point = prev_pos);
let spaces = [
Value::Int(' ' as i64),
Value::Int(goal - prev_col),
Value::Bool(true),
];
insert_char(builtins::insert_char, &spaces)?;
let at = point();
let del = [Value::Int(at as i64), Value::Int(at as i64 + 1)];
delete_region(builtins::delete_region, &del)?;
indent_to(&[Value::Int(col)])?;
with_host(|h| h.cur_buf().point = at);
col = goal;
}
}
if col < goal && force_t {
col = goal;
indent_to(&[Value::Int(col)])?;
}
Ok(Value::Int(col))
}
fn call_process(f: SubrFn, args: &[Value]) -> R {
let inserts = args
.get(2)
.is_some_and(|d| el_truthy(d) && !matches!(d, Value::Int(0)));
if !inserts {
return run(f, args);
}
let pt = point();
modify(pt, pt, None, || run(f, args))
}
fn subst_char(f: SubrFn, args: &[Value]) -> R {
let Some((_, hi, first, last)) = with_host(|h| builtins::subst_char_span(h, args)) else {
return run(f, args);
};
let noundo = args.get(4).is_some_and(el_truthy);
let saved = noundo.then(|| {
with_host(|h| {
let sym = h.intern("buffer-undo-list");
let old = h.get_value(&sym).unwrap_or(Value::Undef);
let _ = h.set_value(&sym, Value::Bool(true));
old
})
});
let r = modify_with(first, hi, None, Some((first, last, last - first)), || {
run(f, args)
});
if let Some(old) = saved {
with_host(|h| {
let sym = h.intern("buffer-undo-list");
let _ = h.set_value(&sym, old);
});
}
r
}
fn read_stream(f: SubrFn, args: &[Value]) -> R {
let stream = match args.first() {
Some(s) if !el_nil(s) => s.clone(),
_ => with_host(|h| {
let sym = h.intern("standard-input");
h.get_value(&sym).unwrap_or(Value::Bool(true))
}),
};
let is_function = with_host(|h| {
!el_nil(&stream)
&& !matches!(stream, Value::Bool(true))
&& !h.is_string(&stream)
&& !matches!(
h.obj(&stream),
Some(crate::host::Obj::Buffer(_)) | Some(crate::host::Obj::Marker(_))
)
&& h.sym_name(&stream).as_deref() != Some("read-char")
});
if !is_function {
return run(f, args);
}
crate::lread_fn::read_from_function(&stream)
}
fn set_window_configuration(f: SubrFn, args: &[Value]) -> R {
let windows = with_host(|h| {
args.first()
.map(|c| crate::window::windows_changing_buffer(h, c))
.unwrap_or_default()
});
let record = with_host(|h| h.intern("record-window-buffer"));
for w in windows {
call_function(&record, &[w])?;
}
let r = run(f, args)?;
crate::window::after_set_window_configuration()?;
Ok(r)
}
fn kill_buffer(f: SubrFn, args: &[Value]) -> R {
let target = with_host(|h| match args.first() {
Some(v) if el_truthy(v) => h.resolve_buffer(v),
_ => Some(h.current),
});
let Some(b) = target.filter(|&b| with_host(|h| h.buffers[b].name.is_some())) else {
return run(f, args);
};
let (buffer, inhibit) =
with_host(|h| (h.buffers[b].self_obj.clone(), h.buffers[b].inhibit_hooks));
if !inhibit {
let saved = with_host(|h| std::mem::replace(&mut h.current, b));
let restore = |saved: usize| {
with_host(|h| {
if h.buffers[saved].name.is_some() {
h.current = saved;
}
})
};
let query = with_host(|h| {
let run = h.intern("run-hook-with-args-until-failure");
let hook = h.intern("kill-buffer-query-functions");
(run, hook)
});
let ok = call_function(&query.0, &[query.1]);
match ok {
Ok(v) if el_nil(&v) => {
restore(saved);
return Ok(Value::Undef);
}
Ok(_) => {}
Err(e) => {
restore(saved);
return Err(e);
}
}
let alive = with_host(|h| h.buffers[b].name.is_some());
if alive {
let (run_hooks, hook) =
with_host(|h| (h.intern("run-hooks"), h.intern("kill-buffer-hook")));
if let Err(e) = call_function(&run_hooks, &[hook]) {
restore(saved);
return Err(e);
}
}
restore(saved);
if !with_host(|h| h.buffers[b].name.is_some()) {
return Ok(Value::Bool(true));
}
}
let (fbound, minibuf) = with_host(|h| {
let sym = h.intern("replace-buffer-in-windows");
let mini = h.frames[h.selected_frame].mini;
(h.is_fbound(&sym), h.windows[mini].buffer == Some(b))
});
if minibuf {
return Ok(Value::Undef);
}
if fbound {
let fname = with_host(|h| h.intern("replace-buffer-in-windows"));
call_function(&fname, std::slice::from_ref(&buffer))?;
if !with_host(|h| h.buffers[b].name.is_some()) {
return Ok(Value::Bool(true));
}
}
run(f, std::slice::from_ref(&buffer))
}