mirror of
https://github.com/MidnightCommander/mc
synced 2025-03-12 10:53:23 +03:00
Little refactoring of load_file_position, save_file_position (added new param 'offset')
Now viewer save/load only 'offset' and 'line' always = -1. Changed editor edit_load_position, edit_save_position, now if 'line' = -1 then used 'offset' Signed-off-by: Ilia Maslakov <il.smind@gmail.com>
This commit is contained in:
parent
f31bbc49a6
commit
b1de9bbd80
10
edit/edit.c
10
edit/edit.c
@ -689,16 +689,22 @@ edit_load_position (WEdit *edit)
|
||||
{
|
||||
char *filename;
|
||||
long line, column;
|
||||
off_t offset;
|
||||
|
||||
if (!edit->filename || !*edit->filename)
|
||||
return;
|
||||
|
||||
filename = vfs_canon (edit->filename);
|
||||
load_file_position (filename, &line, &column);
|
||||
load_file_position (filename, &line, &column, &offset);
|
||||
g_free (filename);
|
||||
|
||||
if (line > 0) {
|
||||
edit_move_to_line (edit, line - 1);
|
||||
edit->prev_col = column;
|
||||
} else if (offset > 0) {
|
||||
edit_cursor_move (edit, offset);
|
||||
line = edit->curs_line;
|
||||
}
|
||||
edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
|
||||
edit_move_display (edit, line - (edit->num_widget_lines / 2));
|
||||
}
|
||||
@ -713,7 +719,7 @@ edit_save_position (WEdit *edit)
|
||||
return;
|
||||
|
||||
filename = vfs_canon (edit->filename);
|
||||
save_file_position (filename, edit->curs_line + 1, edit->curs_col);
|
||||
save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1);
|
||||
g_free (filename);
|
||||
}
|
||||
|
||||
|
30
src/util.c
30
src/util.c
@ -1349,7 +1349,7 @@ mc_mkstemps (char **pname, const char *prefix, const char *suffix)
|
||||
* If there is no stored data, return line 1 and col 0.
|
||||
*/
|
||||
void
|
||||
load_file_position (const char *filename, long *line, long *column)
|
||||
load_file_position (const char *filename, long *line, long *column, off_t *offset)
|
||||
{
|
||||
char *fn;
|
||||
FILE *f;
|
||||
@ -1359,6 +1359,7 @@ load_file_position (const char *filename, long *line, long *column)
|
||||
/* defaults */
|
||||
*line = 1;
|
||||
*column = 0;
|
||||
*offset = 0;
|
||||
|
||||
/* open file with positions */
|
||||
fn = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEPOS_FILE, NULL);
|
||||
@ -1371,6 +1372,7 @@ load_file_position (const char *filename, long *line, long *column)
|
||||
|
||||
while (fgets (buf, sizeof (buf), f)) {
|
||||
const char *p;
|
||||
gchar **pos_tokens;
|
||||
|
||||
/* check if the filename matches the beginning of string */
|
||||
if (strncmp (buf, filename, len) != 0)
|
||||
@ -1385,13 +1387,25 @@ load_file_position (const char *filename, long *line, long *column)
|
||||
if (strchr (p, ' '))
|
||||
continue;
|
||||
|
||||
*line = strtol(p, const_cast(char **, &p), 10);
|
||||
if (*p == ';') {
|
||||
*column = strtol(p+1, const_cast(char **, &p), 10);
|
||||
if (*p != '\n')
|
||||
pos_tokens = g_strsplit_set (p, ";", 3);
|
||||
if (pos_tokens[0] != NULL) {
|
||||
*line = strtol (pos_tokens[0], NULL, 10);
|
||||
if (pos_tokens[1] != NULL) {
|
||||
*column = strtol (pos_tokens[1], NULL, 10);
|
||||
if (pos_tokens[2] != NULL)
|
||||
*offset = strtoll (pos_tokens[2], NULL, 10);
|
||||
else
|
||||
*offset = 0;
|
||||
} else {
|
||||
*column = 0;
|
||||
} else
|
||||
*offset = 0;
|
||||
}
|
||||
} else {
|
||||
*line = 1;
|
||||
*column = 0;
|
||||
*offset = 0;
|
||||
}
|
||||
g_strfreev(pos_tokens);
|
||||
}
|
||||
fclose (f);
|
||||
}
|
||||
@ -1399,7 +1413,7 @@ load_file_position (const char *filename, long *line, long *column)
|
||||
/* Save position for the given file */
|
||||
#define TMP_SUFFIX ".tmp"
|
||||
void
|
||||
save_file_position (const char *filename, long line, long column)
|
||||
save_file_position (const char *filename, long line, long column, off_t offset)
|
||||
{
|
||||
static int filepos_max_saved_entries = 0;
|
||||
char *fn, *tmp_fn;
|
||||
@ -1431,7 +1445,7 @@ save_file_position (const char *filename, long line, long column)
|
||||
|
||||
/* put the new record */
|
||||
if (line != 1 || column != 0) {
|
||||
if (fprintf (f, "%s %ld;%ld\n", filename, line, column) < 0)
|
||||
if (fprintf (f, "%s %ld;%ld;%lli\n", filename, line, column, offset) < 0)
|
||||
goto write_position_error;
|
||||
}
|
||||
|
||||
|
@ -222,9 +222,9 @@ GList *list_append_unique (GList *list, char *text);
|
||||
/* Position saving and restoring */
|
||||
|
||||
/* Load position for the given filename */
|
||||
void load_file_position (const char *filename, long *line, long *column);
|
||||
void load_file_position (const char *filename, long *line, long *column, off_t *offset);
|
||||
/* Save position for the given filename */
|
||||
void save_file_position (const char *filename, long line, long column);
|
||||
void save_file_position (const char *filename, long line, long column, off_t offset);
|
||||
|
||||
|
||||
/* OS specific defines */
|
||||
|
@ -161,11 +161,8 @@ mcview_done (mcview_t * view)
|
||||
/* Save current file position */
|
||||
if (mcview_remember_file_position && view->filename != NULL) {
|
||||
char *canon_fname;
|
||||
off_t line, col;
|
||||
|
||||
canon_fname = vfs_canon (view->filename);
|
||||
mcview_offset_to_coord (view, &line, &col, view->dpy_start);
|
||||
save_file_position (canon_fname, line + 1, col);
|
||||
save_file_position (canon_fname, -1, 0, view->dpy_start);
|
||||
g_free (canon_fname);
|
||||
}
|
||||
|
||||
|
@ -379,11 +379,12 @@ mcview_load (mcview_t * view, const char *command, const char *file, int start_l
|
||||
|
||||
if (mcview_remember_file_position && view->filename != NULL && start_line == 0) {
|
||||
long line, col;
|
||||
|
||||
off_t new_offset;
|
||||
canon_fname = vfs_canon (view->filename);
|
||||
load_file_position (canon_fname, &line, &col);
|
||||
load_file_position (canon_fname, &line, &col, &new_offset);
|
||||
new_offset = min (new_offset, mcview_get_filesize (view));
|
||||
view->dpy_start = mcview_bol (view, new_offset);
|
||||
g_free (canon_fname);
|
||||
mcview_moveto (view, mcview_offset_doz (line, 1), col);
|
||||
} else if (start_line > 0) {
|
||||
mcview_moveto (view, start_line - 1, 0);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user