From 4c3ef7d6f222112c5884672d3797be4b0e36d049 Mon Sep 17 00:00:00 2001
From: Bezrodnev Sergey <bsvskip@rambler.ru>
Date: Thu, 20 May 2010 16:16:28 +0400
Subject: [PATCH] Add +lineno support for vim, emacs, nano after search for editing or
viewing.
Signed-off-by: Bezrodnev Sergey <bsvskip@rambler.ru>
---
lib/util.h | 2 +-
lib/utilunix.c | 7 +++++--
lib/vfs/mc-vfs/extfs.c | 2 +-
lib/vfs/mc-vfs/sfs.c | 4 ++--
src/cmd.c | 46 ++++++++++++++++++++++++++++++++++++++++++++--
src/execute.c | 25 +++++++++++++------------
src/execute.h | 9 +++++----
7 files changed, 71 insertions(+), 24 deletions(-)
diff --git a/lib/util.h b/lib/util.h
index 22c5d71..43d9962 100644
a
|
b
|
void check_error_pipe (void); |
138 | 138 | int close_error_pipe (int error, const char *text); |
139 | 139 | |
140 | 140 | /* Process spawning */ |
141 | | int my_system (int flags, const char *shell, const char *command); |
| 141 | int my_system (int flags, const char *shell, const char *command, const char *arg); |
142 | 142 | void save_stop_handler (void); |
143 | 143 | extern struct sigaction startup_handler; |
144 | 144 | |
diff --git a/lib/utilunix.c b/lib/utilunix.c
index f7835fb..3cbebed 100644
a
|
b
|
save_stop_handler (void) |
146 | 146 | } |
147 | 147 | |
148 | 148 | int |
149 | | my_system (int flags, const char *shell, const char *command) |
| 149 | my_system (int flags, const char *shell, const char *command, const char *arg) |
150 | 150 | { |
151 | 151 | struct sigaction ignore, save_intr, save_quit, save_stop; |
152 | 152 | pid_t pid; |
… |
… |
my_system (int flags, const char *shell, const char *command) |
189 | 189 | else |
190 | 190 | only_cmd = (*shell_tokens != NULL) ? *shell_tokens : shell; |
191 | 191 | |
192 | | execlp (only_cmd, shell, command, (char *) NULL); |
| 192 | if (flags & EXECUTE_WITH_ARG) |
| 193 | execlp (only_cmd, shell, arg, command, (char *) NULL); |
| 194 | else |
| 195 | execlp (only_cmd, shell, command, (char *) NULL); |
193 | 196 | |
194 | 197 | /* |
195 | 198 | execlp will replace current process, |
diff --git a/lib/vfs/mc-vfs/extfs.c b/lib/vfs/mc-vfs/extfs.c
index 73a59ae..50f72b2 100644
a
|
b
|
extfs_cmd (const char *str_extfs_cmd, struct archive *archive, |
775 | 775 | g_free (archive_name); |
776 | 776 | |
777 | 777 | open_error_pipe (); |
778 | | retval = my_system (EXECUTE_AS_SHELL, shell, cmd); |
| 778 | retval = my_system (EXECUTE_AS_SHELL, shell, cmd, NULL); |
779 | 779 | g_free (cmd); |
780 | 780 | close_error_pipe (D_ERROR, NULL); |
781 | 781 | return retval; |
diff --git a/lib/vfs/mc-vfs/sfs.c b/lib/vfs/mc-vfs/sfs.c
index 0c8d570..a6a1248 100644
a
|
b
|
sfs_vfmake (struct vfs_class *me, const char *name, char *cache) |
155 | 155 | |
156 | 156 | g_free (pqname); |
157 | 157 | open_error_pipe (); |
158 | | if (my_system (EXECUTE_AS_SHELL, "/bin/sh", pad)) { |
| 158 | if (my_system (EXECUTE_AS_SHELL, "/bin/sh", pad, NULL)) { |
159 | 159 | close_error_pipe (D_ERROR, NULL); |
160 | 160 | return -1; |
161 | 161 | } |
… |
… |
static int sfs_init (struct vfs_class *me) |
388 | 388 | default: |
389 | 389 | fprintf (stderr, _("Warning: Invalid flag %c in %s:\n%s\n"), |
390 | 390 | *c, "sfs.ini", key); |
391 | | } |
| 391 | } |
392 | 392 | c++; |
393 | 393 | } |
394 | 394 | if (!*c) |
diff --git a/src/cmd.c b/src/cmd.c
index 39b4cf1..78c4433 100644
a
|
b
|
view_file_at_line (const char *filename, int plain_view, int internal, int start |
170 | 170 | } |
171 | 171 | else |
172 | 172 | { |
| 173 | static char run_arg[20]; |
| 174 | static int use_arg = 0; |
| 175 | |
173 | 176 | if (!viewer) |
174 | 177 | { |
| 178 | const char *viewers[] = { "vim", "emacs", "nano", NULL }; |
| 179 | int i = 0; |
| 180 | |
175 | 181 | viewer = getenv ("VIEWER"); |
176 | 182 | if (!viewer) |
177 | 183 | viewer = getenv ("PAGER"); |
178 | 184 | if (!viewer) |
179 | 185 | viewer = "view"; |
| 186 | |
| 187 | while (viewers[i] != NULL) |
| 188 | { |
| 189 | if (strlen(viewer) == strlen(viewers[i]) && |
| 190 | strncmp(viewer, viewers[i], strlen(viewers[i])) == 0) |
| 191 | { |
| 192 | use_arg = 1; |
| 193 | break; |
| 194 | } |
| 195 | i++; |
| 196 | } |
180 | 197 | } |
181 | | execute_with_vfs_arg (viewer, filename); |
| 198 | |
| 199 | if (use_arg) |
| 200 | snprintf(run_arg, 20, "+%d", start_line); |
| 201 | |
| 202 | execute_with_vfs_arg (viewer, filename, (use_arg ? run_arg : NULL)); |
182 | 203 | } |
183 | 204 | return move_dir; |
184 | 205 | } |
… |
… |
do_edit_at_line (const char *what, int start_line) |
324 | 345 | (void) start_line; |
325 | 346 | #endif /* USE_INTERNAL_EDIT */ |
326 | 347 | { |
| 348 | static char run_arg[20]; |
| 349 | static int use_arg = 0; |
| 350 | |
327 | 351 | if (editor == NULL) |
328 | 352 | { |
| 353 | const char *editors[] = { "vim", "emacs", "nano", NULL }; |
| 354 | int i = 0; |
| 355 | |
329 | 356 | editor = getenv ("EDITOR"); |
330 | 357 | if (editor == NULL) |
331 | 358 | editor = get_default_editor (); |
| 359 | |
| 360 | while (editors[i] != NULL) |
| 361 | { |
| 362 | if (strlen(editor) == strlen(editors[i]) && |
| 363 | strncmp(editor, editors[i], strlen(editors[i])) == 0) |
| 364 | { |
| 365 | use_arg = 1; |
| 366 | break; |
| 367 | } |
| 368 | i++; |
| 369 | } |
332 | 370 | } |
333 | | execute_with_vfs_arg (editor, what); |
| 371 | |
| 372 | if (use_arg) |
| 373 | snprintf(run_arg, 20, "+%d", start_line); |
| 374 | |
| 375 | execute_with_vfs_arg (editor, what, (use_arg ? run_arg : NULL)); |
334 | 376 | } |
335 | 377 | if (mc_run_mode == MC_RUN_FULL) |
336 | 378 | { |
diff --git a/src/execute.c b/src/execute.c
index 234429c..0db7e5f 100644
a
|
b
|
|
1 | 1 | /* Execution routines for GNU Midnight Commander |
2 | 2 | Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation, Inc. |
3 | | |
| 3 | |
4 | 4 | This program is free software; you can redistribute it and/or modify |
5 | 5 | it under the terms of the GNU General Public License as published by |
6 | 6 | the Free Software Foundation; either version 2 of the License, or |
7 | 7 | (at your option) any later version. |
8 | | |
| 8 | |
9 | 9 | This program is distributed in the hope that it will be useful, |
10 | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
… |
… |
do_possible_cd (const char *new_dir) |
112 | 112 | #endif /* HAVE_SUBSHELL_SUPPORT */ |
113 | 113 | |
114 | 114 | static void |
115 | | do_execute (const char *lc_shell, const char *command, int flags) |
| 115 | do_execute (const char *lc_shell, const char *command, int flags, const char *arg) |
116 | 116 | { |
117 | 117 | #ifdef HAVE_SUBSHELL_SUPPORT |
118 | 118 | char *new_dir = NULL; |
… |
… |
do_execute (const char *lc_shell, const char *command, int flags) |
146 | 146 | #endif /* !ENABLE_VFS */ |
147 | 147 | } else |
148 | 148 | #endif /* HAVE_SUBSHELL_SUPPORT */ |
149 | | my_system (flags, lc_shell, command); |
| 149 | my_system (flags, lc_shell, command, arg); |
150 | 150 | |
151 | 151 | if (!(flags & EXECUTE_INTERNAL)) { |
152 | 152 | if ((pause_after_run == pause_always |
… |
… |
shell_execute (const char *command, int flags) |
210 | 210 | #ifdef HAVE_SUBSHELL_SUPPORT |
211 | 211 | if (use_subshell) |
212 | 212 | if (subshell_state == INACTIVE) |
213 | | do_execute (shell, cmd ? cmd : command, flags | EXECUTE_AS_SHELL); |
| 213 | do_execute (shell, cmd ? cmd : command, flags | EXECUTE_AS_SHELL, NULL); |
214 | 214 | else |
215 | 215 | message (D_ERROR, MSG_ERROR, |
216 | 216 | _(" The shell is already running a command ")); |
217 | 217 | else |
218 | 218 | #endif /* HAVE_SUBSHELL_SUPPORT */ |
219 | | do_execute (shell, cmd ? cmd : command, flags | EXECUTE_AS_SHELL); |
| 219 | do_execute (shell, cmd ? cmd : command, flags | EXECUTE_AS_SHELL, NULL); |
220 | 220 | |
221 | 221 | g_free (cmd); |
222 | 222 | } |
… |
… |
shell_execute (const char *command, int flags) |
225 | 225 | void |
226 | 226 | exec_shell (void) |
227 | 227 | { |
228 | | do_execute (shell, 0, 0); |
| 228 | do_execute (shell, 0, 0, NULL); |
229 | 229 | } |
230 | 230 | |
231 | 231 | |
… |
… |
toggle_panels (void) |
270 | 270 | _("Type `exit' to return to the Midnight Commander")); |
271 | 271 | fprintf (stderr, "\n\r\n\r"); |
272 | 272 | |
273 | | my_system (EXECUTE_INTERNAL, shell, NULL); |
| 273 | my_system (EXECUTE_INTERNAL, shell, NULL, NULL); |
274 | 274 | } else |
275 | 275 | get_key_code (0); |
276 | 276 | } |
… |
… |
suspend_cmd (void) |
358 | 358 | * Errors are reported to the user. |
359 | 359 | */ |
360 | 360 | void |
361 | | execute_with_vfs_arg (const char *command, const char *filename) |
| 361 | execute_with_vfs_arg (const char *command, const char *filename, const char *arg) |
362 | 362 | { |
363 | 363 | char *localcopy; |
364 | 364 | char *fn; |
… |
… |
execute_with_vfs_arg (const char *command, const char *filename) |
367 | 367 | |
368 | 368 | /* Simplest case, this file is local */ |
369 | 369 | if (!filename || vfs_file_is_local (filename)) { |
| 370 | int flags = EXECUTE_INTERNAL | (arg == NULL ? 0 : EXECUTE_WITH_ARG); |
370 | 371 | fn = vfs_canon_and_translate (filename); |
371 | | do_execute (command, fn, EXECUTE_INTERNAL); |
| 372 | do_execute (command, fn, flags, arg); |
372 | 373 | g_free (fn); |
373 | | return; |
| 374 | return; |
374 | 375 | } |
375 | 376 | |
376 | 377 | /* FIXME: Creation of new files on VFS is not supported */ |
… |
… |
execute_with_vfs_arg (const char *command, const char *filename) |
392 | 393 | fn = g_strdup (filename); |
393 | 394 | mc_stat (localcopy, &st); |
394 | 395 | mtime = st.st_mtime; |
395 | | do_execute (command, localcopy, EXECUTE_INTERNAL); |
| 396 | do_execute (command, localcopy, EXECUTE_INTERNAL, NULL); |
396 | 397 | mc_stat (localcopy, &st); |
397 | 398 | mc_ungetlocalcopy (fn, localcopy, mtime != st.st_mtime); |
398 | 399 | g_free (localcopy); |
diff --git a/src/execute.h b/src/execute.h
index 977961c..5caf7d4 100644
a
|
b
|
|
7 | 7 | #define MC_EXECUTE_H |
8 | 8 | |
9 | 9 | /* flags for shell_execute */ |
10 | | #define EXECUTE_INTERNAL (1 << 0) |
11 | | #define EXECUTE_AS_SHELL (1 << 2) |
12 | | #define EXECUTE_HIDE (1 << 3) |
| 10 | #define EXECUTE_INTERNAL (1 << 0) |
| 11 | #define EXECUTE_AS_SHELL (1 << 2) |
| 12 | #define EXECUTE_HIDE (1 << 3) |
| 13 | #define EXECUTE_WITH_ARG (1 << 4) |
13 | 14 | |
14 | 15 | /* Execute functions that use the shell to execute */ |
15 | 16 | void shell_execute (const char *command, int flags); |
… |
… |
void toggle_panels (void); |
24 | 25 | void suspend_cmd (void); |
25 | 26 | |
26 | 27 | /* Execute command on a filename that can be on VFS */ |
27 | | void execute_with_vfs_arg (const char *command, const char *filename); |
| 28 | void execute_with_vfs_arg (const char *command, const char *filename, const char *arg); |
28 | 29 | |
29 | 30 | #endif /* !MC_EXECUTE_H */ |