[PATCH] External edit goto line

Filip Kalinski filon at pld.org.pl
Fri Dec 27 23:03:54 UTC 2002


Hi,

This patch enables function "do_edit_at_line" to set line numer also
for external edit. All UNIX editing programs accept passing "+<lineno>"
parameter for line number so I changed mc to use this feauture.
To do this all execute functions were modified to accept third argument
of run program (for "+<number>" argument).

It is very usefule when I use "Find File" dialog to find files and then
vim (my favourite editor) to edit found files (yes, I like also mcedit
very much, but...).

If you think that something should be changed to apply it please let me
know.

Thanks,

-- 
Filip Kalinski <filon at pld.org.pl>
-------------- next part --------------
diff -ur mc-2002-12-27-14/src/cmd.c mc-2002-12-27-14.new/src/cmd.c
--- mc-2002-12-27-14/src/cmd.c	2002-12-26 03:20:05.000000000 +0100
+++ mc-2002-12-27-14.new/src/cmd.c	2002-12-27 19:48:09.000000000 +0100
@@ -89,7 +89,7 @@
  * Errors are reported to the user.
  */
 static void
-execute_with_vfs_arg (const char *command, const char *filename)
+execute_with_vfs_arg (const char *command, const char *filename, const char *arg)
 {
     char *localcopy;
     char *fn;
@@ -98,7 +98,7 @@
 
     /* Simplest case, this file is local */
     if (vfs_file_is_local (filename)) {
-	execute_internal (command, filename);
+	execute_internal (command, filename, arg);
 	return;
     }
 
@@ -121,7 +121,7 @@
     fn = g_strdup (filename);
     mc_stat (localcopy, &st);
     mtime = st.st_mtime;
-    execute_internal (command, localcopy);
+    execute_internal (command, localcopy, arg);
     mc_stat (localcopy, &st);
     mc_ungetlocalcopy (fn, localcopy, mtime != st.st_mtime);
     g_free (fn);
@@ -182,7 +182,7 @@
 	    if (!viewer)
 		viewer = "view";
 	}
-	execute_with_vfs_arg (viewer, filename);
+	execute_with_vfs_arg (viewer, filename, NULL);
     }
     return move_dir;
 }
@@ -310,9 +310,16 @@
     g_free (command);
 }
 
+/*
+ * line number -1 means to not pass line numer to editor program
+ * (useful for e.g. vim which saves last edit position and we don't want
+ * to pass it over)
+ */
 void do_edit_at_line (const char *what, int start_line)
 {
     static char *editor = 0;
+    /* should not ovrflow for 64-bit numbers */
+    char num[24];
 
 #ifdef USE_INTERNAL_EDIT
     if (use_internal_edit){
@@ -327,7 +334,19 @@
 	if (!editor)
 	    editor = get_default_editor ();
     }
-    execute_with_vfs_arg (editor, what);
+    
+    /*
+     * format "+<number> <file>" is accepted by editors: vi (and clones),
+     * joe, emacs, xemacs, nedit, pico, nano, jed.
+     * It is safe to assume that any other editor also accepts this format.
+     */
+    if (start_line >= 0)
+    {
+	    sprintf (num, "+%d", start_line);
+	    execute_with_vfs_arg (editor, num, what);
+    } else
+	    execute_with_vfs_arg (editor, what, NULL);
+    
     update_panels (UP_OPTIMIZE, UP_KEEPSEL);
     repaint_screen ();
 }
@@ -335,7 +354,14 @@
 static void
 do_edit (const char *what)
 {
-    do_edit_at_line (what, 0);
+#ifdef USE_INTERNAL_EDIT
+    if (use_internal_edit)
+    {
+	do_edit_at_line (what, 0);
+	return;
+    }
+#endif
+    do_edit_at_line (what, -1);
 }
 
 void
@@ -1010,7 +1036,7 @@
 		 _("Type `exit' to return to the Midnight Commander"));
 		fprintf (stderr, "\n\r\n\r");
 			 
-		my_system (EXECUTE_AS_SHELL, shell, NULL);
+		my_system (EXECUTE_AS_SHELL, shell, NULL, NULL);
 	    } else
 		get_key_code (0);
 	}
diff -ur mc-2002-12-27-14/src/command.c mc-2002-12-27-14.new/src/command.c
--- mc-2002-12-27-14/src/command.c	2002-11-14 08:27:08.000000000 +0100
+++ mc-2002-12-27-14.new/src/command.c	2002-12-27 19:09:48.000000000 +0100
@@ -220,7 +220,7 @@
 	    old_dlg = current_dlg;
 	    current_dlg = 0;
 	    new_input (cmdline);
-	    execute (command);
+	    execute (command, NULL);
 	    g_free (command);
 
 #ifdef HAVE_SUBSHELL_SUPPORT
diff -ur mc-2002-12-27-14/src/ext.c mc-2002-12-27-14.new/src/ext.c
--- mc-2002-12-27-14/src/ext.c	2002-11-14 08:27:08.000000000 +0100
+++ mc-2002-12-27-14.new/src/ext.c	2002-12-27 19:09:48.000000000 +0100
@@ -265,7 +265,7 @@
 	q[1] = 0;
 	do_cd (p, cd_parse_command);
     } else {
-	shell_execute (cmd, EXECUTE_INTERNAL);
+	shell_execute (EXECUTE_INTERNAL, cmd, NULL);
 	if (console_flag) {
 	    handle_console (CONSOLE_SAVE);
 	    if (output_lines && keybar_visible) {
diff -ur mc-2002-12-27-14/src/main.c mc-2002-12-27-14.new/src/main.c
--- mc-2002-12-27-14/src/main.c	2002-12-26 17:38:37.000000000 +0100
+++ mc-2002-12-27-14.new/src/main.c	2002-12-27 19:09:48.000000000 +0100
@@ -488,13 +488,14 @@
 void
 exec_shell (void)
 {
-    do_execute (shell, 0, 0);
+    do_execute (0, shell, NULL, NULL);
 }
 
 void
-do_execute (const char *shell, const char *command, int flags)
+do_execute (int flags, const char *shell, const char *command, const char *arg)
 {
 #ifdef HAVE_SUBSHELL_SUPPORT
+    char *full_cmd = NULL;
     char *new_dir = NULL;
 #endif				/* HAVE_SUBSHELL_SUPPORT */
 
@@ -518,15 +519,17 @@
     if (use_subshell && !(flags & EXECUTE_INTERNAL)) {
 	do_update_prompt ();
 
+	full_cmd = g_strconcat (command, arg, NULL);
 	/* We don't care if it died, higher level takes care of this */
 #ifdef USE_VFS
-	invoke_subshell (command, VISIBLY, old_vfs_dir ? 0 : &new_dir);
+	invoke_subshell (full_cmd, VISIBLY, old_vfs_dir ? 0 : &new_dir);
 #else
-	invoke_subshell (command, VISIBLY, &new_dir);
+	invoke_subshell (full_cmd, VISIBLY, &new_dir);
 #endif				/* !USE_VFS */
+	g_free (full_cmd);
     } else
 #endif				/* HAVE_SUBSHELL_SUPPORT */
-	my_system (flags, shell, command);
+	my_system (flags, shell, command, arg);
 
     if (!(flags & EXECUTE_INTERNAL)) {
 	if ((pause_after_run == pause_always
@@ -573,26 +576,32 @@
     use_dash (TRUE);
 }
 
+void
+execute_internal (const char *shell, const char *command, const char *arg)
+{
+    do_execute (EXECUTE_INTERNAL, shell, command, arg);
+}
+
 /* Executes a command */
 void
-shell_execute (char *command, int flags)
+shell_execute (int flags, const char *command, const char *arg)
 {
 #ifdef HAVE_SUBSHELL_SUPPORT
     if (use_subshell)
 	if (subshell_state == INACTIVE || force_subshell_execution)
-	    do_execute (shell, command, flags | EXECUTE_AS_SHELL);
+	    do_execute (flags | EXECUTE_AS_SHELL, shell, command, arg);
 	else
 	    message (1, MSG_ERROR,
 		     _(" The shell is already running a command "));
     else
 #endif				/* HAVE_SUBSHELL_SUPPORT */
-	do_execute (shell, command, flags | EXECUTE_AS_SHELL);
+	do_execute (flags | EXECUTE_AS_SHELL, shell, command, arg);
 }
 
 void
-execute (char *command)
+execute (const char *command, const char *arg)
 {
-    shell_execute (command, 0);
+    shell_execute (0, command, arg);
 }
 
 void
diff -ur mc-2002-12-27-14/src/main.h mc-2002-12-27-14.new/src/main.h
--- mc-2002-12-27-14/src/main.h	2002-12-26 17:20:51.000000000 +0100
+++ mc-2002-12-27-14.new/src/main.h	2002-12-27 19:09:48.000000000 +0100
@@ -21,12 +21,12 @@
 extern volatile int quit;
 
 /* Execute functions: the base and the routines that use it */
-void do_execute (const char *shell, const char *command, int internal_command);
-#define execute_internal(command,args) do_execute (command, args, 1)
+void do_execute (int internal_command, const char *shell, const char *command, const char *arg);
+void execute_internal (const char *shell, const char *command, const char *arg);
 
 /* Execute functions that use the shell to execute */
-void shell_execute (char *command, int flags);
-void execute (char *command);
+void shell_execute (int flags, const char *command, const char *arg);
+void execute (const char *command, const char *arg);
 
 /* This one executes a shell */
 void exec_shell (void);
diff -ur mc-2002-12-27-14/src/screen.c mc-2002-12-27-14.new/src/screen.c
--- mc-2002-12-27-14/src/screen.c	2002-12-26 17:20:51.000000000 +0100
+++ mc-2002-12-27-14.new/src/screen.c	2002-12-27 19:09:48.000000000 +0100
@@ -1950,7 +1950,7 @@
 	     (_(" The Midnight Commander "),
 	      _(" Do you really want to execute? "), 0, 2, _("&Yes"),
 	      _("&No")) == 0))
-	    execute (cmd);
+	    execute (cmd, NULL);
 	g_free (cmd);
     }
 #ifdef USE_VFS
diff -ur mc-2002-12-27-14/src/user.c mc-2002-12-27-14.new/src/user.c
--- mc-2002-12-27-14/src/user.c	2002-11-29 05:57:26.000000000 +0100
+++ mc-2002-12-27-14.new/src/user.c	2002-12-27 19:09:48.000000000 +0100
@@ -630,7 +630,7 @@
     }
     fclose (cmd_file);
     chmod (file_name, S_IRWXU);
-    execute (file_name);
+    execute (file_name, NULL);
     unlink (file_name);
     g_free (file_name);
 }
diff -ur mc-2002-12-27-14/src/util.h mc-2002-12-27-14.new/src/util.h
--- mc-2002-12-27-14/src/util.h	2002-12-26 14:07:10.000000000 +0100
+++ mc-2002-12-27-14.new/src/util.h	2002-12-27 19:09:48.000000000 +0100
@@ -75,7 +75,7 @@
 /* Process spawning */
 #define EXECUTE_INTERNAL   1
 #define EXECUTE_AS_SHELL   4
-int my_system (int flags, const char *shell, const char *command);
+int my_system (int flags, const char *shell, const char *command, const char *arg);
 void save_stop_handler (void);
 extern struct sigaction startup_handler;
 
diff -ur mc-2002-12-27-14/src/utilunix.c mc-2002-12-27-14.new/src/utilunix.c
--- mc-2002-12-27-14/src/utilunix.c	2002-12-26 16:10:06.000000000 +0100
+++ mc-2002-12-27-14.new/src/utilunix.c	2002-12-27 19:09:48.000000000 +0100
@@ -207,7 +207,7 @@
     sigaction (SIGTSTP, NULL, &startup_handler);
 }
 
-int my_system (int flags, const char *shell, const char *command)
+int my_system (int flags, const char *shell, const char *command, const char *arg)
 {
     struct sigaction ignore, save_intr, save_quit, save_stop;
     pid_t pid;
@@ -235,9 +235,9 @@
 	signal (SIGCHLD, SIG_DFL);
 
 	if (flags & EXECUTE_AS_SHELL)
-	    execl (shell, shell, "-c", command, NULL);
+	    execl (shell, shell, "-c", command, arg, NULL);
 	else
-	    execlp (shell, shell, command, NULL);
+	    execlp (shell, shell, command, arg, NULL);
 
 	_exit (127);		/* Exec error */
     } else {
diff -ur mc-2002-12-27-14/vfs/extfs.c mc-2002-12-27-14.new/vfs/extfs.c
--- mc-2002-12-27-14/vfs/extfs.c	2002-12-25 23:53:37.000000000 +0100
+++ mc-2002-12-27-14.new/vfs/extfs.c	2002-12-27 19:09:48.000000000 +0100
@@ -603,7 +603,7 @@
     g_free (archive_name);
 
     open_error_pipe ();
-    retval = my_system (EXECUTE_AS_SHELL, shell, cmd);
+    retval = my_system (EXECUTE_AS_SHELL, shell, cmd, NULL);
     g_free (cmd);
     close_error_pipe (1, NULL);
     return retval;
@@ -628,7 +628,7 @@
     g_free (mc_extfsdir);
     g_free (archive_name);
     g_free (q);
-    shell_execute (cmd, 0);
+    shell_execute (0, cmd, NULL);
     g_free (cmd);
 }
 
diff -ur mc-2002-12-27-14/vfs/sfs.c mc-2002-12-27-14.new/vfs/sfs.c
--- mc-2002-12-27-14/vfs/sfs.c	2002-12-06 04:34:23.000000000 +0100
+++ mc-2002-12-27-14.new/vfs/sfs.c	2002-12-27 19:09:48.000000000 +0100
@@ -93,7 +93,7 @@
     g_free (name);
 
     open_error_pipe ();
-    if (my_system (EXECUTE_AS_SHELL, "/bin/sh", pad)) {
+    if (my_system (EXECUTE_AS_SHELL, "/bin/sh", pad, NULL)) {
 	close_error_pipe (1, NULL);
 	return -1;
     }


More information about the mc-devel mailing list