From 6dca68b989c150269f8305dae834a11bc84d5de7 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 7 Mar 2026 12:20:35 +0200 Subject: [PATCH] scm: support --eval With the ~--eval~ option you can evaluate an expression in mu/scm environment. For example: $ mu scm --eval \ '(format #t "found ~d match(es)\n" (length (mfind "hello")))' found 7173 match(es) Add command-line parameter and implement. --- man/mu-scm.1.org | 9 +++++++++ mu/mu-cmd.cc | 6 ++++-- mu/mu-options.cc | 4 +++- mu/mu-options.hh | 3 ++- scm/mu-scm.cc | 19 ++++++++++++++++++- scm/mu-scm.hh | 19 ++++++++++++++++++- 6 files changed, 54 insertions(+), 6 deletions(-) diff --git a/man/mu-scm.1.org b/man/mu-scm.1.org index b29dce63..20ef5ae5 100644 --- a/man/mu-scm.1.org +++ b/man/mu-scm.1.org @@ -38,6 +38,15 @@ prefixed by *UNIX_CONNECT:* and ending with a newline. For instance: UNIX-CONNECT:/run/user/1000/mu-scm-socket-4eb5db40 #+end_example +** --eval + +With the ~--eval~ option you can evaluate an expression in mu/scm environment. For +example: +#+begin_example +$ mu scm --eval '(format #t "found ~d match(es)\n" (length (mfind "hello")))' +found 7173 match(es) +#+end_example + #+include: "muhome.inc" :minlevel 2 #+include: "common-options.inc" :minlevel 1 diff --git a/mu/mu-cmd.cc b/mu/mu-cmd.cc index 8efe3472..dbb9a95a 100644 --- a/mu/mu-cmd.cc +++ b/mu/mu-cmd.cc @@ -1,5 +1,5 @@ /* -** Copyright (C) 2010-2025 Dirk-Jan C. Binnema +** Copyright (C) 2010-2026 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -75,7 +75,9 @@ cmd_scm(const Store& store, const Options& opts) #else if (opts.scm.script_path) return Mu::Scm::run_script(store, opts, *opts.scm.script_path); - else + else if (opts.scm.eval) + return Mu::Scm::run_eval(store, opts, *opts.scm.eval); + else return Mu::Scm::run_repl(store, opts, opts.scm.socket_path.value_or("")); #endif /*BUILD_SCM*/ } diff --git a/mu/mu-options.cc b/mu/mu-options.cc index 26905296..2c518396 100644 --- a/mu/mu-options.cc +++ b/mu/mu-options.cc @@ -650,7 +650,9 @@ sub_scm(CLI::App& sub, Options& opts) ->excludes("--listen"); sub.add_option("script-args", opts.scm.params, "Parameters for script") ->type_name(""); - + sub.add_option("--eval", opts.scm.eval, "Expression to evaluate") + ->excludes("--listen") + ->excludes("script-path"); } static void diff --git a/mu/mu-options.hh b/mu/mu-options.hh index e8f8da35..f9575ce4 100644 --- a/mu/mu-options.hh +++ b/mu/mu-options.hh @@ -1,5 +1,5 @@ /* -** Copyright (C) 2022-2025 Dirk-Jan C. Binnema +** Copyright (C) 2022-2026 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -261,6 +261,7 @@ struct Options { bool listen; /**< Whether to start listening on a socket */ Option socket_path; /**< path for the '--listen' * Unix domain socket */ + Option eval; /**< expression to evaluate */ } scm; diff --git a/scm/mu-scm.cc b/scm/mu-scm.cc index ab162fb3..068c2c8e 100644 --- a/scm/mu-scm.cc +++ b/scm/mu-scm.cc @@ -1,5 +1,5 @@ /* -** Copyright (C) 2025 Dirk-Jan C. Binnema +** Copyright (C) 2025-2026 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -272,6 +272,23 @@ Mu::Scm::run_script(const Mu::Store& store, const Mu::Options& opts, return Ok(); } +Result +Mu::Scm::run_eval(const Mu::Store& store, const Mu::Options& opts, const std::string& expr) +{ + if (const auto res = prepare_run(opts, scm_args); !res) + return Err(res.error()); + + scm_args.emplace_back("-c"); + // a little hacky... + scm_args.emplace_back("(use-modules ((mu))) " + expr); + + run_scm(store, opts); + + return Ok(); +} + + + #ifdef BUILD_TESTS /* diff --git a/scm/mu-scm.hh b/scm/mu-scm.hh index e2133315..96cd72ce 100644 --- a/scm/mu-scm.hh +++ b/scm/mu-scm.hh @@ -63,7 +63,8 @@ namespace Mu::Scm { * based on the configuration. * * @param store a Store object - * @param opts options; opts.scm.script_path must set + * @param opts options + * @param a path to the script * * @return Ok() or some error */ @@ -71,6 +72,22 @@ namespace Mu::Scm { const std::string& script_path); + + /** + * Evaluate a Guile/SCM expression + * + * Initialize the Scm sub-system, then start evaluate some expression + * based on the configuration. + * + * @param store a Store object + * @param opts options + * @param expr some expression to evaluate + * + * @return Ok() or some error + */ + Result run_eval(const Mu::Store& store, const Mu::Options& opts, + const std::string& expr); + /** * Helpers *