summaryrefslogtreecommitdiff
path: root/src/library/spacing.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-11-17 15:16:37 +0100
committerLaurenz <laurmaedje@gmail.com>2019-11-17 15:16:37 +0100
commitf6cb4d725ee6e4fd09b92b5af7348d11ac951b10 (patch)
tree7cf8bc7b0158a8a453fd9e2a2fe5a857ef036d5e /src/library/spacing.rs
parent4d0bdc4ca4cb5e8ca1a70b38a0fc0ec37d9e4857 (diff)
Update standard library functions 🎁
Diffstat (limited to 'src/library/spacing.rs')
-rw-r--r--src/library/spacing.rs81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/library/spacing.rs b/src/library/spacing.rs
new file mode 100644
index 00000000..afd26d80
--- /dev/null
+++ b/src/library/spacing.rs
@@ -0,0 +1,81 @@
+use crate::func::prelude::*;
+
+/// `line.break`, `n`: Ends the current line.
+#[derive(Debug, PartialEq)]
+pub struct LineBreak;
+
+function! {
+ data: LineBreak,
+ parse: plain,
+ layout(_, _) { Ok(commands![FinishRun]) }
+}
+
+/// `paragraph.break`: Ends the current paragraph.
+///
+/// This has the same effect as two subsequent newlines.
+#[derive(Debug, PartialEq)]
+pub struct ParagraphBreak;
+
+function! {
+ data: ParagraphBreak,
+ parse: plain,
+ layout(_, _) { Ok(commands![FinishBox]) }
+}
+
+/// `page.break`: Ends the current page.
+#[derive(Debug, PartialEq)]
+pub struct PageBreak;
+
+function! {
+ data: PageBreak,
+ parse: plain,
+ layout(_, _) { Ok(commands![FinishLayout]) }
+}
+
+macro_rules! space_func {
+ ($ident:ident, $doc:expr, $var:ident => $command:expr) => (
+ #[doc = $doc]
+ #[derive(Debug, PartialEq)]
+ pub struct $ident(Spacing);
+
+ function! {
+ data: $ident,
+
+ parse(args, body, _ctx) {
+ parse!(forbidden: body);
+
+ let arg = args.get_pos::<ArgExpr>()?;
+ let spacing = match arg.val {
+ Expression::Size(s) => Spacing::Absolute(*s),
+ Expression::Num(f) => Spacing::Relative(*f as f32),
+ _ => err!("invalid spacing, expected size or number"),
+ };
+
+ Ok($ident(spacing))
+ }
+
+ layout(this, ctx) {
+ let $var = match this.0 {
+ Spacing::Absolute(s) => s,
+ Spacing::Relative(f) => f * ctx.style.font_size,
+ };
+
+ Ok(commands![$command])
+ }
+ }
+ );
+}
+
+/// Absolute or font-relative spacing.
+#[derive(Debug, PartialEq)]
+enum Spacing {
+ Absolute(Size),
+ Relative(f32),
+}
+
+// FIXME: h != primary and v != secondary.
+space_func!(HorizontalSpace, "📖 `h`: Adds horizontal whitespace.",
+ space => AddPrimarySpace(space));
+
+space_func!(VerticalSpace, "📑 `v`: Adds vertical whitespace.",
+ space => AddSecondarySpace(space));