From 975e322d37afaca6e565b37282f2d9e6038124bc Mon Sep 17 00:00:00 2001 From: Brendan Taylor Date: Thu, 13 Jan 2011 12:11:19 -0700 Subject: add a variable @_ that expands to the result of the last sync command --- src/uzbl-core.c | 9 ++++++++- src/uzbl-core.h | 3 ++- tests/test-command.c | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/uzbl-core.c b/src/uzbl-core.c index ba6b4de..8e14982 100644 --- a/src/uzbl-core.c +++ b/src/uzbl-core.c @@ -145,6 +145,7 @@ const struct var_name_to_ptr_t { { "SELECTED_URI", PTR_C_STR(uzbl.state.selected_url, NULL)}, { "NAME", PTR_C_STR(uzbl.state.instance_name, NULL)}, { "PID", PTR_C_STR(uzbl.info.pid_str, NULL)}, + { "_", PTR_C_STR(uzbl.state.last_result, NULL)}, { NULL, {.ptr.s = NULL, .type = TYPE_INT, .dump = 0, .writeable = 0, .func = NULL}} }; @@ -1347,6 +1348,11 @@ run_parsed_command(CommandInfo *c, GArray *a, GString *result) { send_event(COMMAND_EXECUTED, tmp->str, NULL); g_string_free(tmp, TRUE); } + + if(result) { + g_free(uzbl.state.last_result); + uzbl.state.last_result = g_strdup(result->str); + } } void @@ -2294,7 +2300,8 @@ initialize(int argc, char *argv[]) { uzbl.state.executable_path = g_strdup(argv[0]); uzbl.state.selected_url = NULL; - uzbl.state.searchtx = NULL; + uzbl.state.searchtx = NULL; + uzbl.state.last_result = NULL; GOptionContext* context = g_option_context_new ("[ uri ] - load a uri by default"); g_option_context_add_main_entries (context, entries, NULL); diff --git a/src/uzbl-core.h b/src/uzbl-core.h index ef948ca..049053a 100644 --- a/src/uzbl-core.h +++ b/src/uzbl-core.h @@ -98,7 +98,8 @@ typedef struct { gchar *selected_url; gchar *last_selected_url; gchar *executable_path; - gchar* searchtx; + gchar *searchtx; + gchar *last_result; gboolean verbose; gboolean events_stdout; GPtrArray *event_buffer; diff --git a/tests/test-command.c b/tests/test-command.c index 7b33405..20c6aa0 100644 --- a/tests/test-command.c +++ b/tests/test-command.c @@ -281,6 +281,21 @@ test_js (void) { g_string_free(result, TRUE); } +void +test_last_result (void) { + GString *result = g_string_new(""); + + /* the last result gets set */ + parse_cmd_line("js -1", result); + g_assert_cmpstr("-1", ==, uzbl.state.last_result); + + /* the last result can be used in a chain */ + parse_cmd_line("chain 'js 1' 'js \\@_ + 1'", result); + g_assert_cmpstr("2", ==, uzbl.state.last_result); + + g_string_free(result, TRUE); +} + void test_run_handler_arg_order (void) { run_handler("sync_spawn echo uvw xyz", "abc def"); @@ -320,6 +335,8 @@ main (int argc, char *argv[]) { g_test_add_func("/test-command/js", test_js); + g_test_add_func("/test-command/last-result", test_last_result); + /* the following aren't really "command" tests, but they're not worth * splitting into a separate file yet */ g_test_add_func("/test-command/run_handler/arg-order", test_run_handler_arg_order); -- cgit v1.2.3