diff --git a/src/compare/diff.rs b/src/compare/diff.rs index 87dd403..5265a39 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -77,6 +77,7 @@ use crate::types::RadioTarget; use crate::types::RegularLink; use crate::types::RepeaterType; use crate::types::RepeaterWarningDelayValueType; +use crate::types::RetainLabels; use crate::types::Section; use crate::types::SpecialBlock; use crate::types::SrcBlock; @@ -1625,13 +1626,45 @@ fn compare_example_block<'b, 's>( } // Compare retain-labels - let retain_labels: Option = get_property_numeric(emacs, ":retain-labels")?; - if retain_labels != rust.retain_labels { - this_status = DiffStatus::Bad; - message = Some(format!( - "Retain labels mismatch (emacs != rust) {:?} != {:?}", - retain_labels, rust.retain_labels - )); + // retain-labels is t by default, nil if -r is set, or a number if -k is set. + let retain_labels = get_property_unquoted_atom(emacs, ":retain-labels")?; + if let Some(retain_labels) = retain_labels { + if retain_labels == "t" { + match rust.retain_labels { + RetainLabels::Yes => {} + _ => { + this_status = DiffStatus::Bad; + message = Some(format!( + "Retain labels mismatch (emacs != rust) {:?} != {:?}", + retain_labels, rust.retain_labels + )); + } + } + } else { + let retain_labels: CharOffsetInLine = get_property_numeric(emacs, ":retain-labels")?.expect("Cannot be None or else the earlier get_property_unquoted_atom would have been None."); + match (retain_labels, &rust.retain_labels) { + (e, RetainLabels::Keep(r)) if e == *r => {} + _ => { + this_status = DiffStatus::Bad; + message = Some(format!( + "Retain labels mismatch (emacs != rust) {:?} != {:?}", + retain_labels, rust.retain_labels + )); + } + } + // foo + } + } else { + match rust.retain_labels { + RetainLabels::No => {} + _ => { + this_status = DiffStatus::Bad; + message = Some(format!( + "Retain labels mismatch (emacs != rust) {:?} != {:?}", + retain_labels, rust.retain_labels + )); + } + } } // Compare use-labels diff --git a/src/parser/lesser_block.rs b/src/parser/lesser_block.rs index c225b4e..5321c9f 100644 --- a/src/parser/lesser_block.rs +++ b/src/parser/lesser_block.rs @@ -38,6 +38,7 @@ use crate::types::ExportBlock; use crate::types::LineNumber; use crate::types::Object; use crate::types::PlainText; +use crate::types::RetainLabels; use crate::types::SrcBlock; use crate::types::SwitchNumberLines; use crate::types::VerseBlock; @@ -168,7 +169,7 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>( parameters.label_format, ) } else { - (None, None, None, true, None) + (None, None, RetainLabels::Yes, true, None) } }; Ok(( @@ -331,7 +332,7 @@ fn _lesser_block_begin<'b, 'g, 'r, 's, 'c>( struct ExampleSwitches<'s> { source: &'s str, number_lines: Option, - retain_labels: Option, + retain_labels: RetainLabels, use_labels: bool, label_format: Option<&'s str>, } @@ -347,7 +348,7 @@ enum SwitchState { #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] fn example_switches<'s>(input: OrgSource<'s>) -> Res, ExampleSwitches<'s>> { let mut number_lines = None; - let mut retain_labels = None; + let mut retain_labels = RetainLabels::Yes; let mut use_labels = true; let mut label_format = None; let (remaining, (source, (words, _))) = @@ -366,6 +367,12 @@ fn example_switches<'s>(input: OrgSource<'s>) -> Res, ExampleSwitc } (SwitchState::Normal, "-r") => { use_labels = false; + match retain_labels { + RetainLabels::Yes => { + retain_labels = RetainLabels::No; + } + _ => {} + } } (SwitchState::Normal, "-l") => { state = SwitchState::LabelFormat; @@ -375,7 +382,7 @@ fn example_switches<'s>(input: OrgSource<'s>) -> Res, ExampleSwitc let character_offset = Into::<&str>::into(text_until_flag).chars().count(); let character_offset = CharOffsetInLine::try_from(character_offset) .expect("Character offset should fit in CharOffsetInLine"); - retain_labels = Some(character_offset); + retain_labels = RetainLabels::Keep(character_offset); } (SwitchState::NewLineNumber, _) => { let val = normalized_word.parse::(); diff --git a/src/types/lesser_element.rs b/src/types/lesser_element.rs index b0c261d..f6fe7c9 100644 --- a/src/types/lesser_element.rs +++ b/src/types/lesser_element.rs @@ -38,13 +38,21 @@ pub struct CommentBlock<'s> { pub type CharOffsetInLine = u16; +#[derive(Debug)] +pub enum RetainLabels { + No, + Yes, + /// When adding -k to the switches on an example or src block, the labels are kept in the source code and links will use line numbers. + Keep(CharOffsetInLine), +} + #[derive(Debug)] pub struct ExampleBlock<'s> { pub source: &'s str, pub name: &'s str, pub switches: Option<&'s str>, pub number_lines: Option, - pub retain_labels: Option, + pub retain_labels: RetainLabels, pub use_labels: bool, pub label_format: Option<&'s str>, pub contents: String, diff --git a/src/types/mod.rs b/src/types/mod.rs index 4072ae5..5242562 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -51,6 +51,7 @@ pub use lesser_element::LatexEnvironment; pub use lesser_element::LineNumber; pub use lesser_element::Paragraph; pub use lesser_element::Planning; +pub use lesser_element::RetainLabels; pub use lesser_element::SrcBlock; pub use lesser_element::SwitchNumberLines; pub use lesser_element::TableCell;