From abcf2b32777ffb934788e3219cacc2bbc048b6a3 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 11 Apr 2021 15:31:32 +0530 Subject: add `selection-start` and `selection-end` primitives --- src/lisp/eval.rs | 13 +++++++------ src/lisp/expr.rs | 6 ++++++ src/lisp/number.rs | 7 +++++++ src/lisp/prelude.rs | 38 ++++++++++++++++++++++++++++++++++++-- 4 files changed, 56 insertions(+), 8 deletions(-) (limited to 'src/lisp') diff --git a/src/lisp/eval.rs b/src/lisp/eval.rs index 0cf267c..b404662 100644 --- a/src/lisp/eval.rs +++ b/src/lisp/eval.rs @@ -217,12 +217,13 @@ where pub fn eval_for(&mut self, args: &[LispExpr]) -> Result { let arity = Arity::Exact(2); - let valid_binding_stmt = |expr: &LispExpr| + let valid_binding_stmt = |expr: &LispExpr| { matches!( expr, - LispExpr::List(v) - if v.len() == 2 - && matches!(v[0], LispExpr::Ident(_))); + LispExpr::List(v) + if v.len() == 2 + && matches!(v[0], LispExpr::Ident(_))) + }; if !arity.check(args) { Err(arity.to_error()) @@ -258,8 +259,8 @@ where } _ => { error!("invalid for loop args"); - Err(EvalError::BadForm.into()) - }, + Err(EvalError::BadForm.into()) + } } } } diff --git a/src/lisp/expr.rs b/src/lisp/expr.rs index 692f951..d086ecf 100644 --- a/src/lisp/expr.rs +++ b/src/lisp/expr.rs @@ -310,6 +310,12 @@ impl TryFrom for LispNumber { } } +impl From for LispExpr { + fn from(num: i64) -> Self { + LispExpr::Number(num.into()) + } +} + impl<'a> TryFrom<&'a LispExpr> for &'a LispNumber { type Error = LispError; fn try_from(value: &'a LispExpr) -> Result { diff --git a/src/lisp/number.rs b/src/lisp/number.rs index 4824e21..06c6baa 100644 --- a/src/lisp/number.rs +++ b/src/lisp/number.rs @@ -1,5 +1,6 @@ use std::{ cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd}, + convert::From, fmt, ops::{Add, Mul, Sub}, }; @@ -113,3 +114,9 @@ impl fmt::Display for LispNumber { } } } + +impl From for LispNumber { + fn from(target: i64) -> Self { + LispNumber::Integer(target) + } +} diff --git a/src/lisp/prelude.rs b/src/lisp/prelude.rs index e24ade5..1d37a32 100644 --- a/src/lisp/prelude.rs +++ b/src/lisp/prelude.rs @@ -1,6 +1,6 @@ use crate::{ bitmap::MapPoint, - brush::Brush, + brush::{Brush, RectSelectBrush}, grid::GridKind, guide::Guide, lisp::{ @@ -11,7 +11,7 @@ use crate::{ }, primitive, undo::PaintRecord, - utils::load_script, + utils::{load_script, rect_coords}, }; use std::convert::TryInto; @@ -437,6 +437,40 @@ pub fn new_env() -> Result { } }); + primitive!(env, Arity::Exact(0), "selection-start", |_, app| { + if let Brush::RectSelect(RectSelectBrush { + start: Some(s), + end: Some(e), + .. + }) = app.brush + { + let pt = rect_coords(s, e).0; + Ok(LispExpr::DottedList(vec![ + (pt.x as i64).into(), + (pt.y as i64).into(), + ])) + } else { + Err(EvalError::CustomInternal("No active selection!").into()) + } + }); + + primitive!(env, Arity::Exact(0), "selection-end", |_, app| { + if let Brush::RectSelect(RectSelectBrush { + start: Some(s), + end: Some(e), + .. + }) = app.brush + { + let pt = rect_coords(s, e).1; + Ok(LispExpr::DottedList(vec![ + (pt.x as i64).into(), + (pt.y as i64).into(), + ])) + } else { + Err(EvalError::CustomInternal("No active selection!").into()) + } + }); + primitive!(env, Arity::Exact(2), "range", |args, _| { if type_match!( args, -- cgit v1.2.3