mg

port of mg editor (openbsd to linux)
Log | Files | Refs | README

commit 76c69fc6d0c304392dfab183c75abb37d5de7c41
parent 0a2cfe99e6abecc07b7cce786d21cd6ff1c1475c
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Fri, 28 Nov 2014 21:00:38 +0100

feature: add filter-region

Diffstat:
CHANGES | 6++++--
def.h | 1+
funmap.c | 1+
region.c | 81++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
4 files changed, 86 insertions(+), 3 deletions(-)

diff --git a/CHANGES b/CHANGES @@ -27,4 +27,6 @@ Compatibility: Features: --------- - Allow "y" and "n" aswell for save-buffers-kill-emacs instead of "yes" and "no". -- Add "indent-region": prefixes region with tab.- \ No newline at end of file +- Add "indent-region": prefixes region with tab. +- Add "filter-region": shell-command-on-region, on success: kill-region and + insert the output text.+ \ No newline at end of file diff --git a/def.h b/def.h @@ -593,6 +593,7 @@ int lowerregion(int, int); int upperregion(int, int); int prefixregion(int, int); int indentregion(int, int); +int filterregion(int, int); int setprefix(int, int); int region_get_data(struct region *, char *, int); void region_put_data(const char *, int); diff --git a/funmap.c b/funmap.c @@ -142,6 +142,7 @@ static struct funmap functnames[] = { {poptag, "pop-tag-mark",}, {prefixregion, "prefix-region",}, {indentregion, "indent-region",}, + {filterregion, "filter-region",}, {backline, "previous-line",}, {prevwind, "previous-window",}, {spawncli, "push-shell",}, diff --git a/region.c b/region.c @@ -324,6 +324,86 @@ prefixregion(int f, int n) return (TRUE); } +int +filterregion(int f, int n) +{ + /* shell-command-on-region */ + struct region region; + struct line *lp; + int len; + char *cmd, cmdbuf[NFILEN], *text; + char *argv[] = {"sh", "-c", (char *) NULL, (char *) NULL}; + + /* C-u M-| is not supported yet */ + if (n > 1) + return (ABORT); + + if (curwp->w_markp == NULL) { + dobeep(); + ewprintf("The mark is not set now, so there is no region"); + return (FALSE); + } + + if ((cmd = eread("Shell command on region: ", cmdbuf, sizeof(cmdbuf), + EFNEW | EFCR)) == NULL || (cmd[0] == '\0')) + return (ABORT); + + argv[2] = cmd; + + if (getregion(&region) != TRUE) + return (FALSE); + + len = region.r_size; + + if ((text = malloc(len + 1)) == NULL) { + dobeep(); + ewprintf("Cannot allocate memory."); + return (FALSE); + } + + region_get_data(&region, text, len); + + /* shellcmdoutput() */ + + struct buffer *bp; + char *shellp; + int ret; + + bp = bfind("*Shell Command Output*", TRUE); + bp->b_flag |= BFREADONLY; + if (bclear(bp) != TRUE) { + free(text); + return (FALSE); + } + + shellp = getenv("SHELL"); + + ret = pipeio(shellp, argv, text, len, bp); + + if (ret == TRUE) { + eerase(); + if (lforw(bp->b_headp) == bp->b_headp) + addline(bp, "(Shell command succeeded with no output)"); + goto cleanup; + } + /* kill region */ + killregion(f, n); + + /* insert text from buffer */ + for (lp = bfirstlp(bp); lp != bp->b_headp; lp = lforw(lp)) { + linsert_str(ltext(lp), llength(lp)); + lnewline(); + } + + /* close scratch window */ + killbuffer(bp); /* TODO: not sure if killbuffer is needed here */ + delwind(f, n); + +cleanup: + free(text); + return (ret); +} + /* * Set line prefix string. Used by prefixregion. */ @@ -495,7 +575,6 @@ shellcommand(int f, int n) int shellcmdoutput(char* const argv[], char* const text, int len) { - struct buffer *bp; char *shellp; int ret;