Re: Edit selected/hilighted text



Michelle Konzack wrote:
Am 2005-08-01 18:42:37, schrieb Roland Illig:

It had been possible for a short while, somewhere in 2004. I had replaced some parts of the editor with code that did exactly what you want. But because some developers told me "not to replace working code with your poorly tested new code", I removed it again.


Can your patch easyly applyed to the Debian-Version of mc ?
I like to see in in the editor.

To apply the patch to the Debian version you have to add the files src/pipethrough.{c,h} from current CVS HEAD. Then it will work, too.

I would be happy to reintroduce my "poorly tested code" again, as it had worked great for me. :)


Then we should encurage the mc-developers to check your patch.

And here it is, against HEAD.

Roland
Index: src/Makefile.am
===================================================================
RCS file: /cvsroot/mc/mc/src/Makefile.am,v
retrieving revision 1.44
diff -u -p -r1.44 Makefile.am
--- src/Makefile.am	31 Jul 2005 18:30:43 -0000	1.44
+++ src/Makefile.am	1 Aug 2005 19:33:57 -0000
@@ -60,7 +60,7 @@ SRCS =	achown.c achown.h background.c ba
 	tree.c tree.h treestore.c treestore.h tty.h user.c user.h	\
 	util.c util.h utilunix.c view.c view.h vfsdummy.h widget.c	\
 	widget.h win.c win.h wtools.c wtools.h unixcompat.h		\
-	x11conn.h x11conn.c ecs.h ecs.c
+	x11conn.h x11conn.c ecs.h ecs.c pipethrough.h pipethrough.c
 
 if CHARSET
 mc_SOURCES = $(SRCS) $(CHARSET_SRC)
Index: edit/editcmd.c
===================================================================
RCS file: /cvsroot/mc/mc/edit/editcmd.c,v
retrieving revision 1.140
diff -u -p -r1.140 editcmd.c
--- edit/editcmd.c	24 Jul 2005 13:37:58 -0000	1.140
+++ edit/editcmd.c	1 Aug 2005 19:33:58 -0000
@@ -54,6 +54,7 @@
 #include "../src/wtools.h"	/* message() */
 #include "../src/charsets.h"
 #include "../src/selcodepage.h"
+#include "../src/pipethrough.h"
 
 #define edit_get_load_file(f,h) input_expand_dialog (h, _(" Enter file name: "), f)
 #define edit_get_save_file(f,h) input_expand_dialog (h, _(" Enter file name: "), f)
@@ -2424,36 +2425,72 @@ int edit_sort_cmd (WEdit * edit)
     return 0;
 }
 
-/*
- * Ask user for a command, execute it and paste its output back to the
- * editor.
- */
+/* Ask the user for a command, feed the currently selected block as its
+ * input and paste its output back to the editor. */
 int
 edit_ext_cmd (WEdit *edit)
 {
-    char *exp;
-    int e;
-
-    exp =
-	input_dialog (_("Paste output of external command"),
-		      _("Enter shell command(s):"), NULL);
-
-    if (!exp)
-	return 1;
+    const char *title_i18n = _(" External Command ");
+    char *command = NULL;
+    char *block = NULL;
+    const char *error = NULL;
+    long start_mark, end_mark;
+    struct pipe_inbuffer inbuf = {NULL, 0};
+    struct pipe_outbuffer outbuf = {NULL, 0};
+    struct pipe_outbuffer errbuf = {NULL, 0};
+    int block_len;
+    int status;
+
+    if (eval_marks (edit, &start_mark, &end_mark) == 0 /* OK */) {
+        block = edit_get_block (edit, start_mark, end_mark, &block_len);
+        if (block == NULL) {
+    	    error = _(" Could not get the current block. ");
+            goto cleanup;
+        }
+    }
+
+    command = input_dialog (title_i18n, _(" Enter a shell command: "), "");
+    if (command == NULL)
+        goto cleanup;
+
+    inbuf.data = block;
+    inbuf.size = block_len;
+    if (pipethrough(command, &inbuf, &outbuf, &errbuf, &status) == -1) {
+    	error = get_sys_error (_(" Error executing the shell command. "));
+    	goto cleanup;
+    }
+
+    if (WIFEXITED(status)) {
+    	if (WEXITSTATUS(status) != 0) {
+            if (query_dialog(title_i18n, _(" The command returned unsuccessfully. Continue anyway? "),
+                             D_ERROR, 2, _(" &Yes "), _(" &No ")) != 0 /* first button */)
+                goto cleanup;
+    	}
+    } else {
+    	error = _(" The command died unexpectedly. ");
+    	goto cleanup;
+    }
 
-    e = system (catstrs (exp, " > ", home_dir, TEMP_FILE, (char *) NULL));
-    g_free (exp);
+    if (edit_block_delete_cmd (edit) != 0)
+        goto cleanup;
 
-    if (e) {
-	edit_error_dialog (_("External command"),
-			   get_sys_error (_("Cannot execute command")));
-	return -1;
+    if (outbuf.data != NULL) {
+        size_t i;
+        for (i = 0; i < outbuf.size; i++)
+            edit_insert (edit, ((char *) outbuf.data)[i]);
     }
-
     edit->force |= REDRAW_COMPLETELY;
 
-    edit_insert_file (edit, catstrs (home_dir, TEMP_FILE, (char *) NULL));
-    return 0;
+cleanup:
+    if (error != NULL) {
+        edit_error_dialog (title_i18n, error);
+    }
+    	
+    pipe_outbuffer_finalize (&outbuf);
+    pipe_outbuffer_finalize (&errbuf);
+    g_free (block);
+    g_free (command);
+    return (error == NULL);
 }
 
 /* if block is 1, a block must be highlighted and the shell command


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]