From 0959aad655df416b090e56fa7adac7e19ce2ccb7 Mon Sep 17 00:00:00 2001 From: Felicity Tarnell Date: Sat, 8 Mar 2014 12:54:05 +0000 Subject: [PATCH] Prompt support in macros: $[Text]. --- tts.c | 48 ++++++++++++++++++++++++++++++++++++++++-------- ttsrc.sample | 7 ++++++- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/tts.c b/tts.c index a22e3e1..23d9b0e 100644 --- a/tts.c +++ b/tts.c @@ -741,14 +741,46 @@ input_char(win, c) WINDOW *win; wchar_t *c; { - if (macro_pos) { - if (*macro_pos) { - *c = *macro_pos++; - return 0; - } - free(macro_text); - macro_text = macro_pos = NULL; +wchar_t *pr, *s, *r; + if (!macro_pos) + return wget_wch(win, c); + + if (!*macro_pos) { + input_macro(NULL); + return wget_wch(win, c); } - return wget_wch(win, c); + if (macro_pos[0] != '$' || macro_pos[1] != '[') { + *c = *macro_pos++; + return 0; + } + +/* Handle $[prompt string] escapes */ + pr = calloc(sizeof(wchar_t), wcslen(macro_pos) - 1); + for (r = pr, s = macro_pos + 2; ; s++) { + if (*s == '\0') { + cmderr(L"Unterminated $[ prompt in macro"); + input_macro(NULL); + return wget_wch(win, c); + } + + if (*s == ']') + break; + + *r++ = *s; + } + + s++; + + macro_pos = NULL; + r = prompt(pr, NULL, NULL); + free(pr); + + pr = calloc(sizeof(wchar_t), wcslen(r) + wcslen(s) + 1); + wcscpy(pr, r); + wcscat(pr, s); + free(macro_text); + macro_text = macro_pos = pr; + *c = *macro_pos++; + return 0; } diff --git a/ttsrc.sample b/ttsrc.sample index 51a8f1d..b4a860c 100644 --- a/ttsrc.sample +++ b/ttsrc.sample @@ -49,7 +49,12 @@ bind k prev # string which will be executed as if it was typed. For example, the # following macro would add a new entry called "test", and set its timer to # 30 minutes. -#macro t "atest\n+30:00\n" +#macro t "atest\n+30m\n" + +# Macros can also prompt for input from the user using $[Prompt string]; the +# $[...] will be replaced with the user's input. For example, this macro will +# prompt for the name of a new entry, then set its duration to 30m: +#macro t "a$[Description:]\n+30m\n" #### Styling #