Skip to content

Commit

Permalink
add perl cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
adsr committed Dec 8, 2017
1 parent b2a1e3a commit abd239c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 32 deletions.
91 changes: 59 additions & 32 deletions cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static int _cmd_indent_line(bline_t* bline, int use_tabs, int outdent);
static void _cmd_help_inner(char* buf, kbinding_t* trie, str_t* h);
static void _cmd_insert_smart_newline(cmd_context_t* ctx);
static void _cmd_insert_smart_closing_bracket(cmd_context_t* ctx);
static void _cmd_shell_apply_cmd(cmd_context_t* ctx, char* cmd);

// Insert data
int cmd_insert_data(cmd_context_t* ctx) {
Expand Down Expand Up @@ -807,10 +808,6 @@ int cmd_set_opt(cmd_context_t* ctx) {
// Shell
int cmd_shell(cmd_context_t* ctx) {
char* cmd;
char* input;
bint_t input_len;
char* output;
size_t output_len;

// Get shell cmd
if (ctx->static_param) {
Expand All @@ -820,36 +817,32 @@ int cmd_shell(cmd_context_t* ctx) {
if (!cmd) return MLE_OK;
}

// Loop for each cursor
MLE_MULTI_CURSOR_CODE(ctx->cursor,
// Get data to send to stdin
if (ctx->cursor->is_anchored) {
mark_get_between_mark(cursor->mark, cursor->anchor, &input, &input_len);
// Add a newline
input = realloc(input, input_len + 2);
input[input_len] = '\n';
input[input_len+1] = '\0';
input_len += 1;
} else {
input = NULL;
input_len = 0;
}
// Apply shell cmd
_cmd_shell_apply_cmd(ctx, cmd);

// Run cmd
output = NULL;
output_len = 0;
if (util_shell_exec(ctx->editor, cmd, 1, input, input_len, 0, NULL, &output, &output_len) == MLE_OK && output_len > 0) {
// Write output to buffer
if (cursor->is_anchored) {
mark_delete_between_mark(cursor->mark, cursor->anchor);
}
mark_insert_before(cursor->mark, output, output_len);
}
free(cmd);
return MLE_OK;
}

// Free input and output
if (input) free(input);
if (output) free(output);
); // Loop for next cursor
// Perl
int cmd_perl(cmd_context_t* ctx) {
char* code;
char* cmd;

// Get perl code
if (ctx->static_param) {
code = strdup(ctx->static_param);
} else {
editor_prompt(ctx->editor, "perl: Code?", NULL, &code);
if (!code) return MLE_OK;
}

// Format cmd
asprintf(&cmd, "perl -lp -E 'BEGIN{$i=0}' -E %s 2>/dev/null", code);
free(code);

// Apply perl cmd
_cmd_shell_apply_cmd(ctx, cmd);

free(cmd);
return MLE_OK;
Expand Down Expand Up @@ -1527,3 +1520,37 @@ static void _cmd_insert_smart_closing_bracket(cmd_context_t* ctx) {
if (this_line) free(this_line);
if (prev_line) free(prev_line);
}

// Apply cmd for each cursor
static void _cmd_shell_apply_cmd(cmd_context_t* ctx, char* cmd) {
char* input;
bint_t input_len;
char* output;
size_t output_len;

// Loop for each cursor
MLE_MULTI_CURSOR_CODE(ctx->cursor,
// Get data to send to stdin
if (ctx->cursor->is_anchored) {
mark_get_between_mark(cursor->mark, cursor->anchor, &input, &input_len);
} else {
input = NULL;
input_len = 0;
}

// Run cmd
output = NULL;
output_len = 0;
if (util_shell_exec(ctx->editor, cmd, 1, input, input_len, 0, NULL, &output, &output_len) == MLE_OK && output_len > 0) {
// Write output to buffer
if (cursor->is_anchored) {
mark_delete_between_mark(cursor->mark, cursor->anchor);
}
mark_insert_before(cursor->mark, output, output_len);
}

// Free input and output
if (input) free(input);
if (output) free(output);
); // Loop for next cursor
}
2 changes: 2 additions & 0 deletions editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,7 @@ static void _editor_register_cmds(editor_t* editor) {
_editor_register_cmd_fn(editor, "cmd_open_replace_file", cmd_open_replace_file);
_editor_register_cmd_fn(editor, "cmd_open_replace_new", cmd_open_replace_new);
_editor_register_cmd_fn(editor, "cmd_outdent", cmd_outdent);
_editor_register_cmd_fn(editor, "cmd_perl", cmd_perl);
_editor_register_cmd_fn(editor, "cmd_pop_kmap", cmd_pop_kmap);
_editor_register_cmd_fn(editor, "cmd_prev", cmd_prev);
_editor_register_cmd_fn(editor, "cmd_push_kmap", cmd_push_kmap);
Expand Down Expand Up @@ -1424,6 +1425,7 @@ static void _editor_init_kmaps(editor_t* editor) {
MLE_KBINDING_DEF("cmd_outdent", "M-,"),
MLE_KBINDING_DEF("cmd_ctag", "F3"),
MLE_KBINDING_DEF("cmd_shell", "M-e"),
MLE_KBINDING_DEF("cmd_perl", "M-w"),
MLE_KBINDING_DEF("cmd_close", "M-c"),
MLE_KBINDING_DEF("cmd_quit", "C-x"),
MLE_KBINDING_DEF(NULL, NULL)
Expand Down
1 change: 1 addition & 0 deletions mle.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ int cmd_open_new(cmd_context_t* ctx);
int cmd_open_replace_file(cmd_context_t* ctx);
int cmd_open_replace_new(cmd_context_t* ctx);
int cmd_outdent(cmd_context_t* ctx);
int cmd_perl(cmd_context_t* ctx);
int cmd_pop_kmap(cmd_context_t* ctx);
int cmd_prev(cmd_context_t* ctx);
int cmd_push_kmap(cmd_context_t* ctx);
Expand Down

0 comments on commit abd239c

Please sign in to comment.