Skip to content

Commit

Permalink
add ability to stop and resume editor (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
adsr committed May 6, 2020
1 parent 1296794 commit a19ea77
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
26 changes: 10 additions & 16 deletions cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <inttypes.h>
#include "mle.h"

Expand Down Expand Up @@ -962,25 +963,18 @@ int cmd_jump(cmd_context_t *ctx) {
return MLE_OK;
}

// Raise SIGTSTP
int cmd_suspend(cmd_context_t *ctx) {
(void)ctx;
tb_shutdown();
raise(SIGTSTP);
return MLE_OK;
}

// Force a redraw of the screen
static void _cmd_force_redraw(cmd_context_t *ctx) {
int w;
int h;
int x;
int y;
(void)ctx;
if (tb_width() >= 0) tb_shutdown();
tb_init();
tb_select_input_mode(TB_INPUT_ALT);
tb_set_cursor(-1, -1);
w = tb_width();
h = tb_height();
for (x = 0; x < w; x++) {
for (y = 0; y < h; y++) {
tb_change_cell(x, y, 160, 0, 0);
}
}
tb_present();
editor_force_redraw(ctx->editor);
}

// Indent or outdent line(s)
Expand Down
36 changes: 36 additions & 0 deletions editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ static kbinding_t *_editor_get_kbinding_node(kbinding_t *node, kinput_t *input,
static cmd_t *_editor_resolve_cmd(editor_t *editor, cmd_t **rcmd, char *cmd_name);
static int _editor_key_to_input(char *key, kinput_t *ret_input);
static void _editor_init_signal_handlers(editor_t *editor);
static void _editor_continue(int signum);
static void _editor_graceful_exit(int signum);
static void _editor_register_cmds(editor_t *editor);
static void _editor_init_kmaps(editor_t *editor);
Expand Down Expand Up @@ -991,6 +992,28 @@ int editor_notify_observers(editor_t *editor, char *event_name, void *event_data
return MLE_OK;
}

// Force a redraw of the screen
int editor_force_redraw(editor_t *editor) {
int w;
int h;
int x;
int y;
(void)editor;
if (tb_width() >= 0) tb_shutdown();
tb_init();
tb_select_input_mode(TB_INPUT_ALT);
tb_set_cursor(-1, -1);
w = tb_width();
h = tb_height();
for (x = 0; x < w; x++) {
for (y = 0; y < h; y++) {
tb_change_cell(x, y, 160, 0, 0);
}
}
tb_present();
return MLE_OK;
}

// If input == editor->macro_toggle_key, toggle macro mode and return 1. Else
// return 0.
static int _editor_maybe_toggle_macro(editor_t *editor, kinput_t *input) {
Expand Down Expand Up @@ -1354,15 +1377,26 @@ static int _editor_key_to_input(char *key, kinput_t *ret_input) {
static void _editor_init_signal_handlers(editor_t *editor) {
struct sigaction action;
(void)editor;

memset(&action, 0, sizeof(struct sigaction));

action.sa_handler = _editor_graceful_exit;
sigaction(SIGTERM, &action, NULL);
sigaction(SIGINT, &action, NULL);
sigaction(SIGQUIT, &action, NULL);
sigaction(SIGHUP, &action, NULL);

action.sa_handler = _editor_continue;
sigaction(SIGCONT, &action, NULL);

signal(SIGPIPE, SIG_IGN);
}

// Resume editor after cmd_suspend
static void _editor_continue(int signum) {
editor_force_redraw(&_editor);
}

// Gracefully exit
static void _editor_graceful_exit(int signum) {
bview_t *bview;
Expand Down Expand Up @@ -1454,6 +1488,7 @@ static void _editor_register_cmds(editor_t *editor) {
_editor_register_cmd_fn(editor, "cmd_show_help", cmd_show_help);
_editor_register_cmd_fn(editor, "cmd_split_horizontal", cmd_split_horizontal);
_editor_register_cmd_fn(editor, "cmd_split_vertical", cmd_split_vertical);
_editor_register_cmd_fn(editor, "cmd_suspend", cmd_suspend);
_editor_register_cmd_fn(editor, "cmd_toggle_anchor", cmd_toggle_anchor);
_editor_register_cmd_fn(editor, "cmd_uncut", cmd_uncut);
_editor_register_cmd_fn(editor, "cmd_undo", cmd_undo);
Expand Down Expand Up @@ -1588,6 +1623,7 @@ static void _editor_init_kmaps(editor_t *editor) {
MLE_KBINDING_DEF("cmd_perl", "M-w"),
MLE_KBINDING_DEF("cmd_jump", "M-j"),
MLE_KBINDING_DEF("cmd_close", "M-c"),
MLE_KBINDING_DEF("cmd_suspend", "F4"),
MLE_KBINDING_DEF("cmd_quit", "C-x"),
MLE_KBINDING_DEF(NULL, NULL)
});
Expand Down
2 changes: 2 additions & 0 deletions mle.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ int editor_get_input(editor_t *editor, loop_context_t *loop_ctx, cmd_context_t *
int editor_display(editor_t *editor);
int editor_debug_dump(editor_t *editor, FILE *fp);
int editor_input_to_key(editor_t *editor, kinput_t *input, char *keybuf);
int editor_force_redraw(editor_t *editor);

// bview functions
bview_t *bview_get_split_root(bview_t *self);
Expand Down Expand Up @@ -538,6 +539,7 @@ int cmd_shell(cmd_context_t *ctx);
int cmd_show_help(cmd_context_t *ctx);
int cmd_split_horizontal(cmd_context_t *ctx);
int cmd_split_vertical(cmd_context_t *ctx);
int cmd_suspend(cmd_context_t *ctx);
int cmd_toggle_anchor(cmd_context_t *ctx);
int cmd_uncut(cmd_context_t *ctx);
int cmd_undo(cmd_context_t *ctx);
Expand Down

0 comments on commit a19ea77

Please sign in to comment.