1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 11:09:54 +00:00

Merge pull request #411 from LouisJackman/make-ctrl-c-interrupt-current-form

Make Ctrl-C Cancel the Current Form; Only Exit if Column 0 Outside of Form
This commit is contained in:
Calvin Rose 2020-05-23 11:33:19 -04:00 committed by GitHub
commit e9fdbe0c89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 16 deletions

View File

@ -2017,19 +2017,26 @@
(while going
(if (env :exit) (break))
(buffer/clear buf)
(chunks buf p)
(var pindex 0)
(var pstatus nil)
(def len (length buf))
(when (= len 0)
(parser/eof p)
(set going false))
(while (> len pindex)
(+= pindex (parser/consume p buf pindex))
(while (parser/has-more p)
(eval1 (parser/produce p)))
(when (= (parser/status p) :error)
(parse-err p where))))
(if (= (chunks buf p)
:cancel)
(do
# A :cancel chunk represents a cancelled form in the REPL, so reset.
(parser/flush p)
(buffer/clear buf))
(do
(var pindex 0)
(var pstatus nil)
(def len (length buf))
(when (= len 0)
(parser/eof p)
(set going false))
(while (> len pindex)
(+= pindex (parser/consume p buf pindex))
(while (parser/has-more p)
(eval1 (parser/produce p)))
(when (= (parser/status p) :error)
(parse-err p where))))))
# Check final parser state
(while (parser/has-more p)
(eval1 (parser/produce p)))

View File

@ -25,6 +25,7 @@
#endif
#include <janet.h>
#include <stdbool.h>
#ifdef _WIN32
#include <windows.h>
@ -40,6 +41,8 @@ void janet_line_deinit();
void janet_line_get(const char *p, JanetBuffer *buffer);
Janet janet_line_getter(int32_t argc, Janet *argv);
static JANET_THREAD_LOCAL bool gbl_cancel_current_repl_form = false;
/*
* Line Editing
*/
@ -54,7 +57,17 @@ Janet janet_line_getter(int32_t argc, Janet *argv) {
gbl_complete_env = (argc >= 3) ? janet_gettable(argv, 2) : NULL;
janet_line_get(str, buf);
gbl_complete_env = NULL;
return janet_wrap_buffer(buf);
Janet result;
if (gbl_cancel_current_repl_form) {
gbl_cancel_current_repl_form = false;
// Signal that the user bailed out of the current form
result = janet_ckeywordv("cancel");
} else {
result = janet_wrap_buffer(buf);
}
return result;
}
static void simpleline(JanetBuffer *buffer) {
@ -746,8 +759,7 @@ static int line() {
kleft();
break;
case 3: /* ctrl-c */
errno = EAGAIN;
gbl_sigint_flag = 1;
gbl_cancel_current_repl_form = true;
clearlines();
return -1;
case 4: /* ctrl-d, eof */