From 19432d91ab4f74511487621fd9a24d3008e5bfa8 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 25 Aug 2023 03:04:07 -0400 Subject: [PATCH] Get the emacs and org-mode versions when launching the compare script. --- src/compare/mod.rs | 2 ++ src/compare/parse.rs | 37 +++++++++++++++++++++++++++++++++++++ src/lib.rs | 4 ++++ src/main.rs | 18 ++++++++++++++---- 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/compare/mod.rs b/src/compare/mod.rs index 4f8602a..d5b2cf3 100644 --- a/src/compare/mod.rs +++ b/src/compare/mod.rs @@ -3,3 +3,5 @@ mod parse; mod util; pub use diff::compare_document; pub use parse::emacs_parse_org_document; +pub use parse::get_emacs_version; +pub use parse::get_org_mode_version; diff --git a/src/compare/parse.rs b/src/compare/parse.rs index bce786e..0b70120 100644 --- a/src/compare/parse.rs +++ b/src/compare/parse.rs @@ -49,3 +49,40 @@ where } output } + +pub fn get_emacs_version() -> Result> { + let elisp_script = r#"(progn + (message "%s" (version)) +)"#; + let mut cmd = Command::new("emacs"); + let proc = cmd + .arg("-q") + .arg("--no-site-file") + .arg("--no-splash") + .arg("--batch") + .arg("--eval") + .arg(elisp_script); + + let out = proc.output()?; + out.status.exit_ok()?; + Ok(String::from_utf8(out.stderr)?) +} + +pub fn get_org_mode_version() -> Result> { + let elisp_script = r#"(progn + (org-mode) + (message "%s" (org-version nil t nil)) +)"#; + let mut cmd = Command::new("emacs"); + let proc = cmd + .arg("-q") + .arg("--no-site-file") + .arg("--no-splash") + .arg("--batch") + .arg("--eval") + .arg(elisp_script); + + let out = proc.output()?; + out.status.exit_ok()?; + Ok(String::from_utf8(out.stderr)?) +} diff --git a/src/lib.rs b/src/lib.rs index d127623..6b38c30 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,10 @@ mod compare; pub use compare::compare_document; #[cfg(feature = "compare")] pub use compare::emacs_parse_org_document; +#[cfg(feature = "compare")] +pub use compare::get_emacs_version; +#[cfg(feature = "compare")] +pub use compare::get_org_mode_version; mod error; pub mod parser; diff --git a/src/main.rs b/src/main.rs index 03269cf..9d0c27d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,10 @@ use organic::compare_document; #[cfg(feature = "compare")] use organic::emacs_parse_org_document; #[cfg(feature = "compare")] +use organic::get_emacs_version; +#[cfg(feature = "compare")] +use organic::get_org_mode_version; +#[cfg(feature = "compare")] use organic::parser::sexp::sexp_with_padding; #[cfg(feature = "tracing")] @@ -49,10 +53,14 @@ fn read_stdin_to_string() -> Result> { #[cfg(feature = "compare")] fn run_compare>(org_contents: P) -> Result<(), Box> { - let (remaining, rust_parsed) = document(org_contents.as_ref()).expect("Org Parse failure"); + let emacs_version = get_emacs_version()?; + let org_mode_version = get_org_mode_version()?; + eprintln!("Using emacs version: {}", emacs_version.trim()); + eprintln!("Using org-mode version: {}", org_mode_version.trim()); + let (remaining, rust_parsed) = document(org_contents.as_ref()).map_err(|e| e.to_string())?; let org_sexp = emacs_parse_org_document(org_contents.as_ref())?; let (_remaining, parsed_sexp) = - sexp_with_padding(org_sexp.as_str()).expect("Sexp Parse failure"); + sexp_with_padding(org_sexp.as_str()).map_err(|e| e.to_string())?; println!("{}\n\n\n", org_contents.as_ref()); println!("{}", org_sexp); @@ -74,8 +82,10 @@ fn run_compare>(org_contents: P) -> Result<(), Box>(org_contents: P) -> Result<(), Box> { - eprintln!("This program was built with compare disabled. Dumping the AST from rust."); - let (remaining, rust_parsed) = document(org_contents.as_ref()).expect("Org Parse failure"); + eprintln!( + "This program was built with compare disabled. Only parsing with organic, not comparing." + ); + let (remaining, rust_parsed) = document(org_contents.as_ref()).map_err(|e| e.to_string())?; println!("{:#?}", rust_parsed); Ok(()) }