Merge commit 'origin/2287_persistent_bookmarks'

* commit 'origin/2287_persistent_bookmarks':
  Fix in lib/logging.c for proper work on 64-bit platforms.
  Change of bookmark drawing.
  Fixed memory leak: destroy serialization bookmarks if file position is not saved.
  Use GArray for bookmarks instead of raw array.
  Minor optimization, type accuracy, includes fix.
  Ticket #2287 (mcedit: persistent bookmarks)
This commit is contained in:
Ilia Maslakov 2010-09-28 18:33:53 +04:00
commit 3013a364b0
11 changed files with 291 additions and 174 deletions

View File

@ -48,7 +48,7 @@ is_logging_enabled(void)
static gboolean logging_enabled = FALSE;
if (!logging_initialized) {
logging_enabled = mc_config_get_int (mc_main_config,
logging_enabled = mc_config_get_bool (mc_main_config,
CONFIG_APP_SECTION, "development.enable_logging", FALSE);
logging_initialized = TRUE;
}

View File

@ -1431,12 +1431,12 @@ 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, off_t * offset)
load_file_position (const char *filename, long *line, long *column, off_t * offset, GArray **bookmarks)
{
char *fn;
FILE *f;
char buf[MC_MAXPATHLEN + 20];
int len;
char buf[MC_MAXPATHLEN + 100];
const size_t len = strlen (filename);
/* defaults */
*line = 1;
@ -1447,12 +1447,13 @@ load_file_position (const char *filename, long *line, long *column, off_t * offs
fn = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEPOS_FILE, NULL);
f = fopen (fn, "r");
g_free (fn);
if (!f)
if (f == NULL)
return;
len = strlen (filename);
/* prepare array for serialized bookmarks */
*bookmarks = g_array_sized_new (FALSE, FALSE, sizeof (size_t), MAX_SAVED_BOOKMARKS);
while (fgets (buf, sizeof (buf), f))
while (fgets (buf, sizeof (buf), f) != NULL)
{
const char *p;
gchar **pos_tokens;
@ -1467,61 +1468,73 @@ load_file_position (const char *filename, long *line, long *column, off_t * offs
/* and string without spaces */
p = &buf[len + 1];
if (strchr (p, ' '))
if (strchr (p, ' ') != NULL)
continue;
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;
*offset = 0;
}
}
else
pos_tokens = g_strsplit (p, ";", 3 + MAX_SAVED_BOOKMARKS);
if (pos_tokens[0] == NULL)
{
*line = 1;
*column = 0;
*offset = 0;
}
else
{
*line = strtol (pos_tokens[0], NULL, 10);
if (pos_tokens[1] == NULL)
{
*column = 0;
*offset = 0;
}
else
{
*column = strtol (pos_tokens[1], NULL, 10);
if (pos_tokens[2] == NULL)
*offset = 0;
else
{
size_t i;
*offset = strtoll (pos_tokens[2], NULL, 10);
for (i = 0; i < MAX_SAVED_BOOKMARKS && pos_tokens[3 + i] != NULL; i++)
{
size_t val;
val = strtoul (pos_tokens[3 + i], NULL, 10);
g_array_append_val (*bookmarks, val);
}
}
}
}
g_strfreev (pos_tokens);
}
fclose (f);
}
/* Save position for the given file */
#define TMP_SUFFIX ".tmp"
void
save_file_position (const char *filename, long line, long column, off_t offset)
save_file_position (const char *filename, long line, long column, off_t offset, GArray *bookmarks)
{
static int filepos_max_saved_entries = 0;
static size_t filepos_max_saved_entries = 0;
char *fn, *tmp_fn;
FILE *f, *tmp_f;
char buf[MC_MAXPATHLEN + 20];
int i = 1;
gsize len;
char buf[MC_MAXPATHLEN + 100];
size_t i;
const size_t len = strlen (filename);
gboolean src_error = FALSE;
if (filepos_max_saved_entries == 0)
filepos_max_saved_entries =
mc_config_get_int (mc_main_config, CONFIG_APP_SECTION, "filepos_max_saved_entries",
1024);
filepos_max_saved_entries = mc_config_get_int (mc_main_config, CONFIG_APP_SECTION,
"filepos_max_saved_entries", 1024);
fn = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEPOS_FILE, NULL);
if (fn == NULL)
goto early_error;
len = strlen (filename);
mc_util_make_backup_if_possible (fn, TMP_SUFFIX);
/* open file */
@ -1532,42 +1545,49 @@ save_file_position (const char *filename, long line, long column, off_t offset)
tmp_fn = g_strdup_printf ("%s" TMP_SUFFIX, fn);
tmp_f = fopen (tmp_fn, "r");
if (tmp_f == NULL)
{
src_error = TRUE;
goto open_source_error;
}
/* put the new record */
if (line != 1 || column != 0)
if (line != 1 || column != 0 || bookmarks != NULL)
{
if (fprintf (f, "%s %ld;%ld;%llu\n", filename, line, column, (unsigned long long) offset) <
0)
if (fprintf (f, "%s %ld;%ld;%ju", filename, line, column, offset) < 0)
goto write_position_error;
if (bookmarks != NULL)
for (i = 0; i < bookmarks->len && i < MAX_SAVED_BOOKMARKS; i++)
if (fprintf (f, ";%zu", g_array_index (bookmarks, size_t, i)) < 0)
goto write_position_error;
if (fprintf (f, "\n") < 0)
goto write_position_error;
}
while (fgets (buf, sizeof (buf), tmp_f))
i = 1;
while (fgets (buf, sizeof (buf), tmp_f) != NULL)
{
if (buf[len] == ' ' && strncmp (buf, filename, len) == 0 && !strchr (&buf[len + 1], ' '))
if (buf[len] == ' ' && strncmp (buf, filename, len) == 0 && strchr (&buf[len + 1], ' ') == NULL)
continue;
fprintf (f, "%s", buf);
if (++i > filepos_max_saved_entries)
break;
}
fclose (tmp_f);
g_free (tmp_fn);
fclose (f);
mc_util_unlink_backup_if_possible (fn, TMP_SUFFIX);
g_free (fn);
return;
write_position_error:
fclose (tmp_f);
open_source_error:
g_free (tmp_fn);
fclose (f);
mc_util_restore_from_backup_if_possible (fn, TMP_SUFFIX);
if (src_error)
mc_util_restore_from_backup_if_possible (fn, TMP_SUFFIX);
else
mc_util_unlink_backup_if_possible (fn, TMP_SUFFIX);
open_target_error:
g_free (fn);
early_error:
return;
g_array_free (bookmarks, TRUE);
}
#undef TMP_SUFFIX

View File

@ -229,11 +229,13 @@ int hook_present (Hook * hook_list, void (*hook_fn) (void *));
GList *list_append_unique (GList * list, char *text);
/* Position saving and restoring */
#define MAX_SAVED_BOOKMARKS 10
/* Load position for the given filename */
void load_file_position (const char *filename, long *line, long *column, off_t * offset);
void load_file_position (const char *filename, long *line, long *column, off_t * offset,
GArray **bookmarks);
/* Save position for the given filename */
void save_file_position (const char *filename, long line, long column, off_t offset);
void save_file_position (const char *filename, long line, long column, off_t offset,
GArray *bookmarks);
/* OS specific defines */

View File

@ -40,186 +40,259 @@
#include <unistd.h>
#include "lib/global.h"
#include "lib/util.h" /* MAX_SAVED_BOOKMARKS */
#include "edit-impl.h"
#include "edit-widget.h"
/* note, if there is more than one bookmark on a line, then they are
appended after each other and the last one is always the one found
by book_mark_found() i.e. last in is the one seen */
static struct _book_mark *double_marks (WEdit * edit, struct _book_mark *p)
static struct _book_mark *
double_marks (WEdit * edit, struct _book_mark *p)
{
(void) edit;
if (p->next)
while (p->next->line == p->line)
p = p->next;
if (p->next != NULL)
while (p->next->line == p->line)
p = p->next;
return p;
}
/* returns the first bookmark on or before this line */
struct _book_mark *book_mark_find (WEdit * edit, int line)
struct _book_mark *
book_mark_find (WEdit * edit, int line)
{
struct _book_mark *p;
if (!edit->book_mark) {
/* must have an imaginary top bookmark at line -1 to make things less complicated */
edit->book_mark = g_malloc0 (sizeof (struct _book_mark));
edit->book_mark->line = -1;
return edit->book_mark;
if (edit->book_mark == NULL)
{
/* must have an imaginary top bookmark at line -1 to make things less complicated */
edit->book_mark = g_malloc0 (sizeof (struct _book_mark));
edit->book_mark->line = -1;
return edit->book_mark;
}
for (p = edit->book_mark; p; p = p->next) {
if (p->line > line)
break; /* gone past it going downward */
if (p->line <= line) {
if (p->next) {
if (p->next->line > line) {
edit->book_mark = p;
return double_marks (edit, p);
}
} else {
edit->book_mark = p;
return double_marks (edit, p);
}
}
for (p = edit->book_mark; p != NULL; p = p->next)
{
if (p->line > line)
break; /* gone past it going downward */
if (p->next != NULL)
{
if (p->next->line > line)
{
edit->book_mark = p;
return double_marks (edit, p);
}
}
else
{
edit->book_mark = p;
return double_marks (edit, p);
}
}
for (p = edit->book_mark; p; p = p->prev) {
if (p->next)
if (p->next->line <= line)
break; /* gone past it going upward */
if (p->line <= line) {
if (p->next) {
if (p->next->line > line) {
edit->book_mark = p;
return double_marks (edit, p);
}
} else {
edit->book_mark = p;
return double_marks (edit, p);
}
}
for (p = edit->book_mark; p != NULL; p = p->prev)
{
if (p->next != NULL && p->next->line <= line)
break; /* gone past it going upward */
if (p->line <= line)
{
if (p->next != NULL)
{
if (p->next->line > line)
{
edit->book_mark = p;
return double_marks (edit, p);
}
}
else
{
edit->book_mark = p;
return double_marks (edit, p);
}
}
}
return 0; /* can't get here */
return NULL; /* can't get here */
}
/* returns true if a bookmark exists at this line of color c */
int book_mark_query_color (WEdit * edit, int line, int c)
int
book_mark_query_color (WEdit * edit, int line, int c)
{
struct _book_mark *p;
if (!edit->book_mark)
return 0;
for (p = book_mark_find (edit, line); p; p = p->prev) {
if (p->line != line)
return 0;
if (p->c == c)
return 1;
if (edit->book_mark == NULL)
return 0;
for (p = book_mark_find (edit, line); p != NULL; p = p->prev)
{
if (p->line != line)
return 0;
if (p->c == c)
return 1;
}
return 0;
}
/* insert a bookmark at this line */
void
book_mark_insert (WEdit *edit, int line, int c)
book_mark_insert (WEdit * edit, size_t line, int c)
{
struct _book_mark *p, *q;
p = book_mark_find (edit, line);
#if 0
if (p->line == line) {
/* already exists, so just change the color */
if (p->c != c) {
edit->force |= REDRAW_LINE;
p->c = c;
}
return;
if (p->line == line)
{
/* already exists, so just change the color */
if (p->c != c)
{
p->c = c;
edit->force |= REDRAW_LINE;
}
return;
}
#endif
edit->force |= REDRAW_LINE;
/* create list entry */
q = g_malloc0 (sizeof (struct _book_mark));
q->line = line;
q->line = (int) line;
q->c = c;
q->next = p->next;
/* insert into list */
q->prev = p;
if (p->next)
p->next->prev = q;
if (p->next != NULL)
p->next->prev = q;
p->next = q;
}
/* remove a bookmark if there is one at this line matching this color - c of -1 clear all */
/* returns non-zero on not-found */
int book_mark_clear (WEdit * edit, int line, int c)
int
book_mark_clear (WEdit * edit, int line, int c)
{
struct _book_mark *p, *q;
int r = 1;
if (!edit->book_mark)
return r;
for (p = book_mark_find (edit, line); p; p = q) {
q = p->prev;
if (p->line == line && (p->c == c || c == -1)) {
r = 0;
edit->force |= REDRAW_LINE;
edit->book_mark = p->prev;
p->prev->next = p->next;
if (p->next)
p->next->prev = p->prev;
g_free (p);
break;
}
if (edit->book_mark == NULL)
return r;
for (p = book_mark_find (edit, line); p != NULL; p = q)
{
q = p->prev;
if (p->line == line && (p->c == c || c == -1))
{
r = 0;
edit->book_mark = p->prev;
p->prev->next = p->next;
if (p->next != NULL)
p->next->prev = p->prev;
g_free (p);
edit->force |= REDRAW_LINE;
break;
}
}
/* if there is only our dummy book mark left, clear it for speed */
if (edit->book_mark->line == -1 && !edit->book_mark->next) {
g_free (edit->book_mark);
edit->book_mark = 0;
/* if there is only our dummy book mark left, clear it for speed */
if (edit->book_mark->line == -1 && !edit->book_mark->next)
{
g_free (edit->book_mark);
edit->book_mark = NULL;
}
return r;
}
/* clear all bookmarks matching this color, if c is -1 clears all */
void book_mark_flush (WEdit * edit, int c)
void
book_mark_flush (WEdit * edit, int c)
{
struct _book_mark *p, *q;
if (!edit->book_mark)
return;
if (edit->book_mark == NULL)
return;
while (edit->book_mark->prev != NULL)
edit->book_mark = edit->book_mark->prev;
for (q = edit->book_mark->next; q != NULL; q = p)
{
p = q->next;
if (q->c == c || c == -1)
{
q->prev->next = q->next;
if (p != NULL)
p->prev = q->prev;
g_free (q);
}
}
if (edit->book_mark->next == NULL)
{
g_free (edit->book_mark);
edit->book_mark = NULL;
}
edit->force |= REDRAW_PAGE;
while (edit->book_mark->prev)
edit->book_mark = edit->book_mark->prev;
for (q = edit->book_mark->next; q; q = p) {
p = q->next;
if (q->c == c || c == -1) {
q->prev->next = q->next;
if (p)
p->prev = q->prev;
g_free (q);
}
}
if (!edit->book_mark->next) {
g_free (edit->book_mark);
edit->book_mark = 0;
}
}
/* shift down bookmarks after this line */
void book_mark_inc (WEdit * edit, int line)
void
book_mark_inc (WEdit * edit, int line)
{
if (edit->book_mark) {
struct _book_mark *p;
p = book_mark_find (edit, line);
for (p = p->next; p; p = p->next) {
p->line++;
}
if (edit->book_mark)
{
struct _book_mark *p;
p = book_mark_find (edit, line);
for (p = p->next; p != NULL; p = p->next)
p->line++;
}
}
/* shift up bookmarks after this line */
void book_mark_dec (WEdit * edit, int line)
void
book_mark_dec (WEdit * edit, int line)
{
if (edit->book_mark) {
struct _book_mark *p;
p = book_mark_find (edit, line);
for (p = p->next; p; p = p->next) {
p->line--;
}
if (edit->book_mark != NULL)
{
struct _book_mark *p;
p = book_mark_find (edit, line);
for (p = p->next; p != NULL; p = p->next)
p->line--;
}
}
/* prepare line positions of bookmarks to be saved to file */
void
book_mark_serialize (WEdit * edit, int color)
{
struct _book_mark *p;
if (edit->serialized_bookmarks != NULL)
g_array_set_size (edit->serialized_bookmarks, 0);
if (edit->book_mark != NULL)
{
if (edit->serialized_bookmarks == NULL)
edit->serialized_bookmarks = g_array_sized_new (FALSE, FALSE, sizeof (size_t),
MAX_SAVED_BOOKMARKS);
for (p = book_mark_find (edit, 0); p != NULL; p = p->next)
if (p->c == color && p->line >= 0)
g_array_append_val (edit->serialized_bookmarks, p->line);
}
}
/* restore bookmarks from saved line positions */
void
book_mark_restore (WEdit * edit, int color)
{
if (edit->serialized_bookmarks != NULL)
{
size_t i;
for (i = 0; i < edit->serialized_bookmarks->len; i++)
book_mark_insert (edit, g_array_index (edit->serialized_bookmarks, size_t, i), color);
}
}

View File

@ -272,7 +272,7 @@ void edit_load_syntax (WEdit * edit, char ***pnames, const char *type);
void edit_free_syntax_rules (WEdit * edit);
void edit_get_syntax_color (WEdit * edit, long byte_index, int *color);
void book_mark_insert (WEdit * edit, int line, int c);
void book_mark_insert (WEdit * edit, size_t line, int c);
int book_mark_query_color (WEdit * edit, int line, int c);
int book_mark_query_all (WEdit * edit, int line, int *c);
struct _book_mark *book_mark_find (WEdit * edit, int line);
@ -280,6 +280,8 @@ int book_mark_clear (WEdit * edit, int line, int c);
void book_mark_flush (WEdit * edit, int c);
void book_mark_inc (WEdit * edit, int line);
void book_mark_dec (WEdit * edit, int line);
void book_mark_serialize (WEdit * edit, int color);
void book_mark_restore (WEdit * edit, int color);
int line_is_blank (WEdit * edit, long line);
int edit_indent_width (WEdit * edit, long p);

View File

@ -96,6 +96,7 @@ struct WEdit
long line_offsets[N_LINE_CACHES];
struct _book_mark *book_mark;
GArray *serialized_bookmarks;
/* undo stack and pointers */
unsigned long stack_pointer;

View File

@ -47,6 +47,7 @@
#include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
#include "lib/vfs/mc-vfs/vfs.h"
#include "lib/strutil.h" /* utf string functions */
#include "lib/util.h" /* load_file_position(), save_file_position() */
#include "lib/timefmt.h" /* time formatting */
#include "lib/lock.h"
@ -817,7 +818,7 @@ edit_load_position (WEdit * edit)
return;
filename = vfs_canon (edit->filename);
load_file_position (filename, &line, &column, &offset);
load_file_position (filename, &line, &column, &offset, &edit->serialized_bookmarks);
g_free (filename);
if (line > 0)
@ -830,6 +831,9 @@ edit_load_position (WEdit * edit)
edit_cursor_move (edit, offset);
line = edit->curs_line;
}
book_mark_restore (edit, BOOK_MARK_COLOR);
edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1));
edit_move_display (edit, line - (edit->num_widget_lines / 2));
}
@ -840,11 +844,15 @@ edit_save_position (WEdit * edit)
{
char *filename;
if (!edit->filename || !*edit->filename)
if (edit->filename == NULL || *edit->filename == '\0')
return;
filename = vfs_canon (edit->filename);
save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1);
book_mark_serialize (edit, BOOK_MARK_COLOR);
save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1, edit->serialized_bookmarks);
edit->serialized_bookmarks = NULL;
g_free (filename);
}
@ -996,6 +1004,8 @@ edit_clean (WEdit * edit)
/* save cursor position */
if (option_save_position)
edit_save_position (edit);
else if (edit->serialized_bookmarks != NULL)
edit->serialized_bookmarks = (GArray *) g_array_free (edit->serialized_bookmarks, TRUE);
/* File specified on the mcedit command line and never saved */
if (edit->delete_file)

View File

@ -280,7 +280,7 @@ struct line_s
static inline void
print_to_widget (WEdit * edit, long row, int start_col, int start_col_real,
long end_col, struct line_s line[], char *status)
long end_col, struct line_s line[], char *status, int bookmarked)
{
struct line_s *p;
@ -291,6 +291,8 @@ print_to_widget (WEdit * edit, long row, int start_col, int start_col_real,
int i;
tty_setcolor (EDITOR_NORMAL_COLOR);
if (bookmarked != 0)
tty_setcolor (bookmarked);
if (!show_right_margin)
{
@ -675,7 +677,7 @@ edit_draw_this_line (WEdit * edit, long b, long row, long start_col, long end_co
p->ch = '\0';
print_to_widget (edit, row, start_col, start_col_real, end_col, line, line_stat);
print_to_widget (edit, row, start_col, start_col_real, end_col, line, line_stat, book_mark);
}
#define key_pending(x) (!is_idle())

View File

@ -190,6 +190,8 @@ typedef struct mcview_struct
struct mcview_nroff_struct *search_nroff_seq;
int search_numNeedSkipChar;
GArray *saved_bookmarks;
} mcview_t;
typedef struct mcview_nroff_struct

View File

@ -43,10 +43,11 @@
#include "lib/global.h"
#include "lib/vfs/mc-vfs/vfs.h"
#include "lib/strutil.h"
#include "lib/util.h" /* save_file_position() */
#include "lib/lock.h" /* unlock_file() */
#include "src/wtools.h"
#include "src/main.h"
#include "lib/lock.h" /* unlock_file() */
#include "src/charsets.h"
#include "src/selcodepage.h"
@ -221,6 +222,8 @@ mcview_init (mcview_t * view)
view->move_dir = 0;
view->update_steps = 0;
view->update_activate = 0;
view->saved_bookmarks = NULL;
}
/* --------------------------------------------------------------------------------------------- */
@ -233,7 +236,8 @@ mcview_done (mcview_t * view)
{
char *canon_fname;
canon_fname = vfs_canon (view->filename);
save_file_position (canon_fname, -1, 0, view->dpy_start);
save_file_position (canon_fname, -1, 0, view->dpy_start, view->saved_bookmarks);
view->saved_bookmarks = NULL;
g_free (canon_fname);
}

View File

@ -44,6 +44,7 @@
#include "lib/tty/mouse.h"
#include "lib/vfs/mc-vfs/vfs.h"
#include "lib/strutil.h"
#include "lib/util.h" /* load_file_position() */
#include "src/main.h"
#include "src/charsets.h"
@ -402,7 +403,7 @@ mcview_load (mcview_t * view, const char *command, const char *file, int start_l
off_t new_offset;
canon_fname = vfs_canon (view->filename);
load_file_position (canon_fname, &line, &col, &new_offset);
load_file_position (canon_fname, &line, &col, &new_offset, &view->saved_bookmarks);
new_offset = min (new_offset, mcview_get_filesize (view));
view->dpy_start = mcview_bol (view, new_offset);
g_free (canon_fname);