From 9d83d55d847877a2a7c463aff19a0fb4d4c1e68c Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Wed, 4 Dec 2013 13:32:55 +0400 Subject: [PATCH 1/4] Ticket #3117: Launching editor with CK_Edit shouldn't pass line number. When pressing F4 to start the editor, a "+1" argument is passed to open the file at the first line. For some editor this is absolutely unnecessary since they open the file there anyways. For some others (at least "joe", but probably others too) this is harmful: joe has a convenience feature that by default it opens the file where it was last open, unless of course overridden from command line. Currently mc forces joe to open the file at the first line, although opening it where it was last open would be much more desired. The right solution would be to consult mc.lib only when opening the viewer/editor through the "word search in files (M-?)" feature, and not when F3/F4 is pressed on a file, in the latter case $VIEWER or $EDITOR should simply be launched with the filename but no additional parameters. Initial step: minor refactoring: (do_edit_at_line): rename to edit_file_at_line. (edit_file_at_line): changed type of arguments from int to gboolean. (view_file_at_line): likewise. Signed-off-by: Andrew Borodin --- src/diffviewer/ydiff.c | 4 ++-- src/filemanager/cmd.c | 17 +++++++++-------- src/filemanager/cmd.h | 8 ++++---- src/filemanager/find.c | 6 +++--- src/filemanager/midnight.c | 2 +- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/diffviewer/ydiff.c b/src/diffviewer/ydiff.c index 6cc1ff57a..2a5b36909 100644 --- a/src/diffviewer/ydiff.c +++ b/src/diffviewer/ydiff.c @@ -49,7 +49,7 @@ #endif #include "lib/event.h" /* mc_event_raise() */ -#include "src/filemanager/cmd.h" /* do_edit_at_line(), view_other_cmd() */ +#include "src/filemanager/cmd.h" /* edit_file_at_line(), view_other_cmd() */ #include "src/filemanager/panel.h" #include "src/filemanager/layout.h" /* Needed for get_current_index and get_other_panel */ @@ -2883,7 +2883,7 @@ dview_edit (WDiff * dview, diff_place_t ord) vfs_path_t *tmp_vpath; tmp_vpath = vfs_path_from_str (dview->file[ord]); - do_edit_at_line (tmp_vpath, use_internal_edit, linenum); + edit_file_at_line (tmp_vpath, use_internal_edit != 0, linenum); vfs_path_free (tmp_vpath); } h->modal = h_modal; diff --git a/src/filemanager/cmd.c b/src/filemanager/cmd.c index 8f585d54a..a235e3e82 100644 --- a/src/filemanager/cmd.c +++ b/src/filemanager/cmd.c @@ -157,7 +157,7 @@ do_view_cmd (gboolean normal) file_idx = current_panel->selected; filename_vpath = vfs_path_from_str (current_panel->dir.list[file_idx].fname); - view_file (filename_vpath, normal, use_internal_view); + view_file (filename_vpath, normal, use_internal_view != 0); vfs_path_free (filename_vpath); } @@ -179,7 +179,7 @@ do_edit (const vfs_path_t * what_vpath) if (what_vpath != NULL && *(vfs_path_get_by_index (what_vpath, 0)->path) != '\0') load_file_position (what_vpath, &line, &column, &offset, NULL); } - do_edit_at_line (what_vpath, use_internal_edit, line); + edit_file_at_line (what_vpath, use_internal_edit != 0, line); } /* --------------------------------------------------------------------------------------------- */ @@ -623,7 +623,8 @@ set_basic_panel_listing_to (int panel_index, int listing_mode) /* --------------------------------------------------------------------------------------------- */ gboolean -view_file_at_line (const vfs_path_t * filename_vpath, int plain_view, int internal, long start_line) +view_file_at_line (const vfs_path_t * filename_vpath, gboolean plain_view, gboolean internal, + long start_line) { gboolean ret = TRUE; @@ -703,7 +704,7 @@ view_file_at_line (const vfs_path_t * filename_vpath, int plain_view, int intern */ gboolean -view_file (const vfs_path_t * filename_vpath, int plain_view, int internal) +view_file (const vfs_path_t * filename_vpath, gboolean plain_view, gboolean internal) { long line = 0; @@ -742,12 +743,12 @@ view_file_cmd (void) input_expand_dialog (_("View file"), _("Filename:"), MC_HISTORY_FM_VIEW_FILE, selection (current_panel)->fname, INPUT_COMPLETE_FILENAMES); - if (!filename) + if (filename == NULL) return; vpath = vfs_path_from_str (filename); g_free (filename); - view_file (vpath, 0, use_internal_view); + view_file (vpath, FALSE, use_internal_view != 0); vfs_path_free (vpath); } @@ -789,7 +790,7 @@ view_filtered_cmd (void) /* --------------------------------------------------------------------------------------------- */ void -do_edit_at_line (const vfs_path_t * what_vpath, gboolean internal, long start_line) +edit_file_at_line (const vfs_path_t * what_vpath, gboolean internal, long start_line) { #ifdef USE_INTERNAL_EDIT @@ -849,7 +850,7 @@ edit_cmd_force_internal (void) fname = vfs_path_from_str (selection (current_panel)->fname); if (regex_command (fname, "Edit") == 0) - do_edit_at_line (fname, TRUE, 0); + edit_file_at_line (fname, TRUE, 1); vfs_path_free (fname); } #endif diff --git a/src/filemanager/cmd.h b/src/filemanager/cmd.h index 71719f831..52c61931f 100644 --- a/src/filemanager/cmd.h +++ b/src/filemanager/cmd.h @@ -52,14 +52,14 @@ void help_cmd (void); void smart_dirsize_cmd (void); void single_dirsize_cmd (void); void dirsizes_cmd (void); -gboolean view_file_at_line (const vfs_path_t * filename_vpath, int plain_view, int internal, - long start_line); -gboolean view_file (const vfs_path_t * filename_vpath, int normal, int internal); +gboolean view_file_at_line (const vfs_path_t * filename_vpath, gboolean plain_view, + gboolean internal, long start_line); +gboolean view_file (const vfs_path_t * filename_vpath, gboolean normal, gboolean internal); void view_cmd (void); void view_file_cmd (void); void view_raw_cmd (void); void view_filtered_cmd (void); -void do_edit_at_line (const vfs_path_t * what_vpath, gboolean internal, long start_line); +void edit_file_at_line (const vfs_path_t * what_vpath, gboolean internal, long start_line); void edit_cmd (void); void edit_cmd_new (void); #ifdef USE_INTERNAL_EDIT diff --git a/src/filemanager/find.c b/src/filemanager/find.c index 6980b1167..77df1e9d2 100644 --- a/src/filemanager/find.c +++ b/src/filemanager/find.c @@ -56,7 +56,7 @@ #include "src/history.h" /* MC_HISTORY_SHARED_SEARCH */ #include "dir.h" -#include "cmd.h" /* view_file_at_line */ +#include "cmd.h" /* view_file_at_line() */ #include "midnight.h" /* current_panel */ #include "boxes.h" #include "panelize.h" @@ -1433,9 +1433,9 @@ find_do_view_edit (gboolean unparsed_view, gboolean edit, char *dir, char *file) fullname_vpath = vfs_path_build_filename (dir, filename, (char *) NULL); if (edit) - do_edit_at_line (fullname_vpath, use_internal_edit, line); + edit_file_at_line (fullname_vpath, use_internal_edit != 0, line); else - view_file_at_line (fullname_vpath, unparsed_view ? 1 : 0, use_internal_view, line); + view_file_at_line (fullname_vpath, unparsed_view, use_internal_view != 0, line); vfs_path_free (fullname_vpath); g_free (fullname); } diff --git a/src/filemanager/midnight.c b/src/filemanager/midnight.c index f3ba77495..c1a59deef 100644 --- a/src/filemanager/midnight.c +++ b/src/filemanager/midnight.c @@ -1000,7 +1000,7 @@ mc_maybe_editor_or_viewer (void) if (mc_run_param0 != NULL && *(char *) mc_run_param0 != '\0') vpath = prepend_cwd_on_local ((char *) mc_run_param0); - ret = view_file (vpath, 0, 1); + ret = view_file (vpath, FALSE, TRUE); vfs_path_free (vpath); break; } From cef5286fce2f9ddca7a5ab28e4edf9654f8085c6 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Wed, 4 Dec 2013 14:27:15 +0400 Subject: [PATCH 2/4] Allow launch external editor/viewer w/o line number. Signed-off-by: Andrew Borodin --- src/execute.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/execute.c b/src/execute.c index 66eaff019..db742dbbf 100644 --- a/src/execute.c +++ b/src/execute.c @@ -266,9 +266,13 @@ execute_get_external_cmd_opts_from_config (const char *command, const vfs_path_t if (filename_vpath == NULL) return g_strdup (""); + parameter = g_shell_quote (vfs_path_get_last_path_str (filename_vpath)); + + if (start_line <= 0) + return parameter; + str_from_config = execute_get_opts_from_cfg (command, "%filename"); - parameter = g_shell_quote (vfs_path_get_last_path_str (filename_vpath)); return_str = str_replace_all (str_from_config, "%filename", parameter); g_free (parameter); g_free (str_from_config); @@ -615,6 +619,7 @@ execute_with_vfs_arg (const char *command, const vfs_path_t * filename_vpath) * @param command editor/viewer to run * @param filename_vpath path for edit/view * @param start_line cursor will be placed at the 'start_line' position after opening file + * if start_line is 0 or negative, no start line will be passed to editor/viewer */ void From 9757d53cf42b70a9673d0e3f9511e735a9e46bb2 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Wed, 4 Dec 2013 16:43:22 +0400 Subject: [PATCH 3/4] Launching external editor/viewer with F4/F3 w/o passing line number. Signed-off-by: Andrew Borodin --- src/filemanager/cmd.c | 30 +++--------------------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/src/filemanager/cmd.c b/src/filemanager/cmd.c index a235e3e82..69a1969dc 100644 --- a/src/filemanager/cmd.c +++ b/src/filemanager/cmd.c @@ -169,17 +169,7 @@ do_view_cmd (gboolean normal) static inline void do_edit (const vfs_path_t * what_vpath) { - long line = 0; - - if (!use_internal_edit) - { - long column; - off_t offset; - - if (what_vpath != NULL && *(vfs_path_get_by_index (what_vpath, 0)->path) != '\0') - load_file_position (what_vpath, &line, &column, &offset, NULL); - } - edit_file_at_line (what_vpath, use_internal_edit != 0, line); + edit_file_at_line (what_vpath, use_internal_edit != 0, 0); } /* --------------------------------------------------------------------------------------------- */ @@ -662,7 +652,7 @@ view_file_at_line (const vfs_path_t * filename_vpath, gboolean plain_view, gbool { char view_entry[BUF_TINY]; - if (start_line != 0) + if (start_line > 0) g_snprintf (view_entry, sizeof (view_entry), "View:%ld", start_line); else strcpy (view_entry, "View"); @@ -706,18 +696,7 @@ view_file_at_line (const vfs_path_t * filename_vpath, gboolean plain_view, gbool gboolean view_file (const vfs_path_t * filename_vpath, gboolean plain_view, gboolean internal) { - long line = 0; - - if (!internal) - { - long column; - off_t offset; - - if (filename_vpath != NULL && *(vfs_path_get_by_index (filename_vpath, 0)->path) != '\0') - load_file_position (filename_vpath, &line, &column, &offset, NULL); - } - - return view_file_at_line (filename_vpath, plain_view, internal, line); + return view_file_at_line (filename_vpath, plain_view, internal, 0); } @@ -810,9 +789,6 @@ edit_file_at_line (const vfs_path_t * what_vpath, gboolean internal, long start_ editor = get_default_editor (); } - if (start_line < 1) - start_line = 1; - execute_external_editor_or_viewer (editor, what_vpath, start_line); } From a7e516220c9956e3391bec72e1d1bc0e3dd0d491 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Thu, 5 Dec 2013 14:58:14 +0400 Subject: [PATCH 4/4] Update EN and RU manual pages. Signed-off-by: Andrew Borodin --- doc/man/mc.1.in | 17 +++++++++++++---- doc/man/ru/mc.1.in | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/doc/man/mc.1.in b/doc/man/mc.1.in index 74b53a475..3686e7dae 100644 --- a/doc/man/mc.1.in +++ b/doc/man/mc.1.in @@ -4125,13 +4125,11 @@ and viewers. The Midnight Commander tries to search the and then in the ~/.config/mc/ini file. The option name should be equal to the name (full pathname) of external editor or viewer. The option value can contain following variables: -.PP +.TP .I %filename -.IP The filename to edit/view. -.PP +.TP .I %lineno -.IP The start line in the opening file. .PP For example: @@ -4142,6 +4140,17 @@ For example: joe=%filename +%lineno more=%filename +%lineno .fi +.PP +Start line is passed to the external editor/viewer only if it is called from the +.\"LINK2" +Find file +.\"Find File" +results window. +.PP +If external editor/viewer is launched via F4/F3 keys, MC hopes that program +(at least "joe", but probably others too) has an own feature that by default +opens the file where it was last open. MC doesn't prevent external editor/viewer +to save and restore position in opened files. .\"NODE "Terminal databases" .SH "Terminal databases" The Midnight Commander provides a way to fix your system terminal diff --git a/doc/man/ru/mc.1.in b/doc/man/ru/mc.1.in index 0d5b5cdbd..e78e324c3 100644 --- a/doc/man/ru/mc.1.in +++ b/doc/man/ru/mc.1.in @@ -4512,11 +4512,9 @@ Midnight Commander позволяет задать некоторые парам программы просмотра. Значение параметра может содержать следующие переменные: .PP .I %filename -.IP Имя файла для редактирования или просмотра. .PP .I %lineno -.IP Номер начальной строки в открываемом файле. .PP Пример: @@ -4527,6 +4525,18 @@ Midnight Commander позволяет задать некоторые парам joe=%filename +%lineno more=%filename +%lineno .fi +.PP +Начальная строка передаётся в во внешнюю программу редактирования или просмотра +только в том случае, если она вызывается из окна результатов +.\"LINK2" +поиска файлов\&. +.\"Find File" +.PP +Если внешняя программа редактирования или просмотра запускается по клавишам F4 или F3, +MC надеется, что она имеет собственную функцию открытия файла в том же самом месте, +где он был закрыт в предыдущий раз. Такую функцию имеет, например, редактор "joe" и многие +другие. MC не препятствует внешней программе редактирования или просмотра самостоятельно +сохранять и восстановливать позиции в открываемых файлах. .\"NODE "Terminal databases" .SH "Базы терминалов" Midnight Commander обеспечивает возможность внесения исправлений в