1
0
Fork 0
forked from wry/wry

config: add create-mark, jump-to-mark, and copy-mark actions

This commit is contained in:
Julian Orth 2025-07-20 15:44:50 +02:00
parent e30e2595a1
commit eb625b34cc
19 changed files with 1193 additions and 9 deletions

View file

@ -1,7 +1,7 @@
use {
crate::{
config::{
Action,
Action, SimpleCommand,
context::Context,
extractor::{Extractor, ExtractorError, arr, bol, n32, opt, str, val},
parser::{DataType, ParseResult, Parser, UnexpectedDataType},
@ -17,6 +17,7 @@ use {
input::{InputParser, InputParserError},
keymap::{KeymapParser, KeymapParserError},
log_level::{LogLevelParser, LogLevelParserError},
mark_id::{MarkIdParser, MarkIdParserError},
output::{OutputParser, OutputParserError},
output_match::{OutputMatchParser, OutputMatchParserError},
repeat_rate::{RepeatRateParser, RepeatRateParserError},
@ -81,6 +82,12 @@ pub enum ActionParserError {
MoveToOutput(#[source] OutputMatchParserError),
#[error("Could not parse a set-repeat-rate action")]
RepeatRate(#[source] RepeatRateParserError),
#[error("Could not parse a create-mark action")]
CreateMark(#[source] MarkIdParserError),
#[error("Could not parse a jump-to-mark action")]
JumpToMark(#[source] MarkIdParserError),
#[error("Could not parse a copy-mark action")]
CopyMark(#[source] MarkIdParserError),
}
pub struct ActionParser<'a>(pub &'a Context<'a>);
@ -142,6 +149,8 @@ impl ActionParser<'_> {
"focus-below" => FocusLayerRel(LayerDirection::Below),
"focus-above" => FocusLayerRel(LayerDirection::Above),
"focus-tiles" => FocusTiles,
"create-mark" => CreateMark,
"jump-to-mark" => JumpToMark,
_ => {
return Err(
ActionParserError::UnknownSimpleAction(string.to_string()).spanned(span)
@ -368,6 +377,43 @@ impl ActionParser<'_> {
name: name.value.to_string(),
})
}
fn parse_create_mark(&mut self, ext: &mut Extractor<'_>) -> ParseResult<Self> {
let (id,) = ext.extract((opt(val("id")),))?;
let Some(id) = id else {
return Ok(Action::SimpleCommand {
cmd: SimpleCommand::CreateMark,
});
};
let id = id
.parse(&mut MarkIdParser(self.0))
.map_spanned_err(ActionParserError::CreateMark)?;
Ok(Action::CreateMark(id))
}
fn parse_jump_to_mark(&mut self, ext: &mut Extractor<'_>) -> ParseResult<Self> {
let (id,) = ext.extract((opt(val("id")),))?;
let Some(id) = id else {
return Ok(Action::SimpleCommand {
cmd: SimpleCommand::JumpToMark,
});
};
let id = id
.parse(&mut MarkIdParser(self.0))
.map_spanned_err(ActionParserError::JumpToMark)?;
Ok(Action::JumpToMark(id))
}
fn parse_copy_mark(&mut self, ext: &mut Extractor<'_>) -> ParseResult<Self> {
let (src, dst) = ext.extract((val("src"), val("dst")))?;
let src = src
.parse(&mut MarkIdParser(self.0))
.map_spanned_err(ActionParserError::CopyMark)?;
let dst = dst
.parse(&mut MarkIdParser(self.0))
.map_spanned_err(ActionParserError::CopyMark)?;
Ok(Action::CopyMark(src, dst))
}
}
impl Parser for ActionParser<'_> {
@ -422,6 +468,9 @@ impl Parser for ActionParser<'_> {
"define-action" => self.parse_define_action(&mut ext),
"undefine-action" => self.parse_undefine_action(&mut ext),
"named" => self.parse_named_action(&mut ext),
"create-mark" => self.parse_create_mark(&mut ext),
"jump-to-mark" => self.parse_jump_to_mark(&mut ext),
"copy-mark" => self.parse_copy_mark(&mut ext),
v => {
ext.ignore_unused();
return Err(ActionParserError::UnknownType(v.to_string()).spanned(ty.span));

View file

@ -0,0 +1,61 @@
use {
crate::{
config::{
context::Context,
extractor::{Extractor, ExtractorError, opt, str},
keycodes::KEYCODES,
parser::{DataType, ParseResult, Parser, UnexpectedDataType},
},
toml::{
toml_span::{Span, Spanned, SpannedExt},
toml_value::Value,
},
},
indexmap::IndexMap,
thiserror::Error,
};
#[derive(Debug, Error)]
pub enum MarkIdParserError {
#[error(transparent)]
Expected(#[from] UnexpectedDataType),
#[error(transparent)]
Extract(#[from] ExtractorError),
#[error("MarkId must have exactly one field set")]
ExactlyOneField,
#[error("Unknown key {0}")]
UnknownKey(String),
}
pub struct MarkIdParser<'a>(pub &'a Context<'a>);
impl Parser for MarkIdParser<'_> {
type Value = u32;
type Error = MarkIdParserError;
const EXPECTED: &'static [DataType] = &[DataType::Table];
fn parse_table(
&mut self,
span: Span,
table: &IndexMap<Spanned<String>, Spanned<Value>>,
) -> ParseResult<Self> {
let mut ext = Extractor::new(self.0, span, table);
let (key, name) = ext.extract((opt(str("key")), opt(str("name"))))?;
let id = match (key, name) {
(None, None) | (Some(_), Some(_)) => {
return Err(MarkIdParserError::ExactlyOneField.spanned(span));
}
(Some(key), _) => match KEYCODES.get(key.value) {
Some(c) => *c,
_ => return Err(key.map(|s| MarkIdParserError::UnknownKey(s.to_string()))),
},
(_, Some(name)) => {
let mn = &mut *self.0.mark_names.borrow_mut();
let len = mn.len() as u32;
*mn.entry(name.value.to_string())
.or_insert(u32::MAX - 8 - len)
}
};
Ok(id)
}
}