From 6447c76ccab5eb972397d69abb762fcd4391730e Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Fri, 11 Jan 2013 13:31:31 +0300 Subject: [PATCH] Implementation of getting last editing/viewing position of file. Signed-off-by: Slava Zanko --- lib/util.c | 5 +++-- src/execute.c | 8 ++++---- src/execute.h | 2 +- src/filemanager/cmd.c | 35 ++++++++++++++++++++++++++++++----- src/filemanager/cmd.h | 4 ++-- 5 files changed, 40 insertions(+), 14 deletions(-) diff --git a/lib/util.c b/lib/util.c index eb4e117bb..4c4aeb083 100644 --- a/lib/util.c +++ b/lib/util.c @@ -1102,7 +1102,8 @@ load_file_position (const vfs_path_t * filename_vpath, long *line, long *column, return; /* prepare array for serialized bookmarks */ - *bookmarks = g_array_sized_new (FALSE, FALSE, sizeof (size_t), MAX_SAVED_BOOKMARKS); + if (bookmarks != NULL) + *bookmarks = g_array_sized_new (FALSE, FALSE, sizeof (size_t), MAX_SAVED_BOOKMARKS); filename = vfs_path_to_str (filename_vpath); while (fgets (buf, sizeof (buf), f) != NULL) @@ -1143,7 +1144,7 @@ load_file_position (const vfs_path_t * filename_vpath, long *line, long *column, *column = strtol (pos_tokens[1], NULL, 10); if (pos_tokens[2] == NULL) *offset = 0; - else + else if (bookmarks != NULL) { size_t i; diff --git a/src/execute.c b/src/execute.c index 6dff4565a..2247567f0 100644 --- a/src/execute.c +++ b/src/execute.c @@ -67,7 +67,7 @@ int pause_after_run = pause_on_dumb_terminals; void do_execute (const char *shell, const char *command, int flags); void do_executev (const char *shell, int flags, char *const argv[]); char *execute_get_external_cmd_opts_from_config (const char *command, - const vfs_path_t * filename_vpath, int start_line); + const vfs_path_t * filename_vpath, long start_line); /* --------------------------------------------------------------------------------------------- */ @@ -254,7 +254,7 @@ execute_get_opts_from_cfg (const char *command, const char *default_str) char * execute_get_external_cmd_opts_from_config (const char *command, const vfs_path_t * filename_vpath, - int start_line) + long start_line) { char *str_from_config, *return_str; char *parameter; @@ -270,7 +270,7 @@ execute_get_external_cmd_opts_from_config (const char *command, const vfs_path_t g_free (str_from_config); str_from_config = return_str; - parameter = g_strdup_printf ("%d", start_line); + parameter = g_strdup_printf ("%ld", start_line); return_str = str_replace_all (str_from_config, "%lineno", parameter); g_free (parameter); g_free (str_from_config); @@ -612,7 +612,7 @@ execute_with_vfs_arg (const char *command, const vfs_path_t * filename_vpath) void execute_external_editor_or_viewer (const char *command, const vfs_path_t * filename_vpath, - int start_line) + long start_line) { vfs_path_t *localcopy_vpath = NULL; const vfs_path_t *do_execute_vpath; diff --git a/src/execute.h b/src/execute.h index 469b2be88..a8a00ef13 100644 --- a/src/execute.h +++ b/src/execute.h @@ -44,7 +44,7 @@ gboolean execute_suspend (const gchar * event_group_name, const gchar * event_na /* Execute command on a filename that can be on VFS */ void execute_with_vfs_arg (const char *command, const vfs_path_t * filename_vpath); void execute_external_editor_or_viewer (const char *command, const vfs_path_t * filename_vpath, - int start_line); + long start_line); void post_exec (void); void pre_exec (void); diff --git a/src/filemanager/cmd.c b/src/filemanager/cmd.c index bbefb6a94..7c453e48d 100644 --- a/src/filemanager/cmd.c +++ b/src/filemanager/cmd.c @@ -166,7 +166,17 @@ do_view_cmd (gboolean normal) static inline void do_edit (const vfs_path_t * what_vpath) { - do_edit_at_line (what_vpath, use_internal_edit, 0); + 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); + } + do_edit_at_line (what_vpath, use_internal_edit, line); } /* --------------------------------------------------------------------------------------------- */ @@ -607,7 +617,7 @@ 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, int start_line) +view_file_at_line (const vfs_path_t * filename_vpath, int plain_view, int internal, long start_line) { static const char *viewer = NULL; gboolean ret = TRUE; @@ -647,7 +657,7 @@ view_file_at_line (const vfs_path_t * filename_vpath, int plain_view, int intern char view_entry[BUF_TINY]; if (start_line != 0) - g_snprintf (view_entry, sizeof (view_entry), "View:%d", start_line); + g_snprintf (view_entry, sizeof (view_entry), "View:%ld", start_line); else strcpy (view_entry, "View"); @@ -688,7 +698,18 @@ 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) { - return view_file_at_line (filename_vpath, plain_view, internal, 0); + 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); } @@ -761,7 +782,7 @@ view_filtered_cmd (void) /* --------------------------------------------------------------------------------------------- */ void -do_edit_at_line (const vfs_path_t * what_vpath, gboolean internal, int start_line) +do_edit_at_line (const vfs_path_t * what_vpath, gboolean internal, long start_line) { static const char *editor = NULL; @@ -777,6 +798,10 @@ do_edit_at_line (const vfs_path_t * what_vpath, gboolean internal, int start_lin if (editor == NULL) editor = get_default_editor (); } + + if (start_line < 1) + start_line = 1; + execute_external_editor_or_viewer (editor, what_vpath, start_line); } diff --git a/src/filemanager/cmd.h b/src/filemanager/cmd.h index 8baa35e7a..71719f831 100644 --- a/src/filemanager/cmd.h +++ b/src/filemanager/cmd.h @@ -53,13 +53,13 @@ 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, - int start_line); + long start_line); gboolean view_file (const vfs_path_t * filename_vpath, int normal, int 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, int start_line); +void do_edit_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 -- 2.11.4.GIT