mg

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

commit 10fb7cf6131d215ffd58f63d8a39721e9e0c1308
parent 76c69fc6d0c304392dfab183c75abb37d5de7c41
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Fri, 28 Nov 2014 22:46:43 +0100

fix + simplify filter-region

Diffstat:
CHANGES | 3+--
region.c | 79++++++++++++++++++-------------------------------------------------------------
2 files changed, 19 insertions(+), 63 deletions(-)

diff --git a/CHANGES b/CHANGES @@ -29,4 +29,4 @@ Features: - Allow "y" and "n" aswell for save-buffers-kill-emacs instead of "yes" and "no". - 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 + insert the output text. diff --git a/region.c b/region.c @@ -26,9 +26,9 @@ static char leftover[BUFSIZ]; static int getregion(struct region *); static int iomux(int, char * const, int, struct buffer *); static int preadin(int, struct buffer *); -static void pwriteout(int, char **, int *); static int setsize(struct region *, RSIZE); static int shellcmdoutput(char * const[], char * const, int); +static void pwriteout(int, char **, int *); /* * Kill the region. Ask "getregion" to figure out the bounds of the region. @@ -327,81 +327,38 @@ prefixregion(int f, int n) int filterregion(int f, int n) { - /* shell-command-on-region */ - struct region region; + struct buffer *bp; 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); + size_t i; + char *p; + int r; - /* shellcmdoutput() */ + r = piperegion(f, n); + if(r == FALSE) + return FALSE; - struct buffer *bp; - char *shellp; - int ret; - - bp = bfind("*Shell Command Output*", TRUE); - bp->b_flag |= BFREADONLY; - if (bclear(bp) != TRUE) { - free(text); + bp = bfind("*Shell Command Output*", FALSE); + if (bp == NULL) 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)); + for (lp = bfirstlp(bp), i = 0; lp != bp->b_headp; lp = lforw(lp), i++) { + p = ltext(lp); + if(!i && !strcmp(p, "(Shell command succeeded with no output)")) + break; + linsert_str(p, llength(lp)); lnewline(); + i++; } /* close scratch window */ - killbuffer(bp); /* TODO: not sure if killbuffer is needed here */ + killbuffer(bp); delwind(f, n); -cleanup: - free(text); - return (ret); + return (TRUE); } /*