This diff allows a user pipe text from current region in mg to
external command. Also adds a command to mark whole buffer. Comments?

Index: def.h
===================================================================
RCS file: /home/sunil/cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.118
diff -u -p -r1.118 def.h
--- def.h       10 Dec 2011 14:09:48 -0000      1.118
+++ def.h       25 Dec 2011 19:18:58 -0000
@@ -567,6 +567,8 @@ int          prefixregion(int, int);
 int             setprefix(int, int);
 int             region_get_data(struct region *, char *, int);
 void            region_put_data(const char *, int);
+int             markbuffer(int, int);
+int             cmdregion(int, int);
 
 /* search.c X */
 int             forwsearch(int, int);
Index: funmap.c
===================================================================
RCS file: /home/sunil/cvs/src/usr.bin/mg/funmap.c,v
retrieving revision 1.35
diff -u -p -r1.35 funmap.c
--- funmap.c    28 Nov 2011 04:41:39 -0000      1.35
+++ funmap.c    25 Dec 2011 19:24:00 -0000
@@ -119,6 +119,7 @@ static struct funmap functnames[] = {
        {localbind, "local-set-key",},
        {localunbind, "local-unset-key",},
        {makebkfile, "make-backup-files",},
+       {markbuffer, "mark-whole-buffer",},
        {do_meta, "meta-key-mode",},    /* better name, anyone? */
        {negative_argument, "negative-argument",},
        {newline, "newline",},
@@ -172,6 +173,7 @@ static struct funmap functnames[] = {
        {setfillcol, "set-fill-column",},
        {setmark, "set-mark-command",},
        {setprefix, "set-prefix-string",},
+       {cmdregion, "shell-command-on-region",},
        {shrinkwind, "shrink-window",},
 #ifdef NOTAB
        {space_to_tabstop, "space-to-tabstop",},
Index: keymap.c
===================================================================
RCS file: /home/sunil/cvs/src/usr.bin/mg/keymap.c,v
retrieving revision 1.46
diff -u -p -r1.46 keymap.c
--- keymap.c    28 Nov 2011 04:41:39 -0000      1.46
+++ keymap.c    25 Dec 2011 19:19:06 -0000
@@ -137,7 +137,7 @@ static PF cXcar[] = {
 #endif /* !NO_MACRO */
        setfillcol,             /* f */
        gotoline,               /* g */
-       rescan,                 /* h */
+       markbuffer,             /* h */
        fileinsert,             /* i */
        rescan,                 /* j */
        killbuffer_cmd,         /* k */
@@ -259,7 +259,7 @@ static PF metal[] = {
        rescan,                 /* y */
        rescan,                 /* z */
        gotobop,                /* { */
-       rescan,                 /* | */
+       cmdregion,              /* | */
        gotoeop                 /* } */
 };
 
Index: mg.1
===================================================================
RCS file: /home/sunil/cvs/src/usr.bin/mg/mg.1,v
retrieving revision 1.57
diff -u -p -r1.57 mg.1
--- mg.1        28 Nov 2011 07:58:23 -0000      1.57
+++ mg.1        25 Dec 2011 19:53:20 -0000
@@ -196,6 +196,8 @@ call-last-kbd-macro
 set-fill-column
 .It C-x g
 goto-line
+.It C-x h
+mark-whole-buffer
 .It C-x i
 insert-file
 .It C-x k
@@ -260,6 +262,8 @@ copy-region-as-kill
 execute-extended-command
 .It M-{
 backward-paragraph
+.It M-|
+shell-command-on-region
 .It M-}
 forward-paragraph
 .It M-~
@@ -572,6 +576,9 @@ Bind a key mapping in the local (topmost
 Unbind a key mapping in the local (topmost) mode.
 .It make-backup-files
 Toggle generation of backup files.
+.It mark-whole-buffer
+Marks whole buffer as a region by putting dot at the beginning and mark
+at the end of buffer.
 .It meta-key-mode
 When disabled, the meta key can be used to insert extended-ascii (8-bit)
 characters.
@@ -734,6 +741,8 @@ Used by auto-fill-mode.
 Sets the mark in the current window to the current dot location.
 .It set-prefix-string
 Sets the prefix string to be used by the 'prefix-region' command.
+.It shell-command-on-region
+Provide the text in region to the shell command as input.
 .It shrink-window
 Shrink current window by one line.
 The window immediately below is expanded to pick up the slack.
Index: region.c
===================================================================
RCS file: /home/sunil/cvs/src/usr.bin/mg/region.c,v
retrieving revision 1.29
diff -u -p -r1.29 region.c
--- region.c    5 Jun 2009 18:02:06 -0000       1.29
+++ region.c    25 Dec 2011 19:38:22 -0000
@@ -9,6 +9,8 @@
  * internal use.
  */
 
+#include <stdio.h>
+
 #include "def.h"
 
 static int     getregion(struct region *);
@@ -366,4 +368,66 @@ region_put_data(const char *buf, int len
                else
                        linsert(1, buf[i]);
        }
+}
+
+/*
+ * Mark  whole buffer by first traversing to end-of-buffer
+ * and then to beginning-of-buffer. Mark, dot are implicitly
+ * set to eob, bob respectively during traversal.
+ */
+int
+markbuffer(int f, int n)
+{
+       if (gotoeob(f,n) == FALSE)
+               return (FALSE);
+       if (gotobob(f,n) == FALSE)
+               return (FALSE);
+       return (TRUE);
+}
+
+/*
+ * Pipe text from current region to external command.
+ */
+int
+cmdregion(int f, int n)
+{
+       struct line *linep;
+       struct region region;
+       FILE *p;
+       int c, loffs, s;
+       char *cmd, cmdbuf[NFILEN];
+
+       /* C-u M-| is not supported yet */
+       if (n > 1)
+               return (FALSE);
+       if (curwp->w_markp == NULL) {
+               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)
+               return (FALSE);
+       else if (cmd[0] == '\0')
+               return (FALSE);
+       if ((p = popen(cmd, "w")) == NULL) {
+               ewprintf("popen %s: failed", cmd);
+               return (FALSE);
+       }
+       if ((s = getregion(&region)) != TRUE)
+               return (s);
+       linep = region.r_linep;
+       loffs = region.r_offset;
+       while (region.r_size--) {
+               if (loffs == llength(linep)) {
+                       linep = lforw(linep);
+                       loffs = 0;
+                       fputc('\n', p);
+               } else {
+                       c = lgetc(linep, loffs);
+                       fputc(c, p);
+                       ++loffs;
+               }
+       }
+       pclose(p);
+       return (TRUE);
 }

Reply via email to