From 4e986337e752f6dda5ae42b5515ea44f120498dc Mon Sep 17 00:00:00 2001
From: Filip Sefrna <fsefrna@gmail.com>
Date: Sat, 17 Jul 2010 18:21:29 +0200
Subject: [PATCH] Save bookmarks of internal edit in filepos
---
lib/util.c | 39 +++++++++++++++++++++++++++++++++------
lib/util.h | 4 ++--
src/editor/bookmark.c | 27 +++++++++++++++++++++++++++
src/editor/edit-impl.h | 2 ++
src/editor/edit.c | 18 +++++++++++++++---
src/viewer/lib.c | 2 +-
src/viewer/mcviewer.c | 2 +-
7 files changed, 81 insertions(+), 13 deletions(-)
diff --git a/lib/util.c b/lib/util.c
index a33df46..7246205 100644
a
|
b
|
mc_mkstemps (char **pname, const char *prefix, const char *suffix) |
1457 | 1457 | * If there is no stored data, return line 1 and col 0. |
1458 | 1458 | */ |
1459 | 1459 | void |
1460 | | load_file_position (const char *filename, long *line, long *column, off_t * offset) |
| 1460 | load_file_position (const char *filename, long *line, long *column, off_t * offset, long *bookmarks, int num_bookmarks) |
1461 | 1461 | { |
1462 | 1462 | char *fn; |
1463 | 1463 | FILE *f; |
1464 | | char buf[MC_MAXPATHLEN + 20]; |
| 1464 | char buf[MC_MAXPATHLEN + 100]; |
1465 | 1465 | int len; |
1466 | 1466 | |
1467 | 1467 | /* defaults */ |
… |
… |
load_file_position (const char *filename, long *line, long *column, off_t * offs |
1476 | 1476 | if (!f) |
1477 | 1477 | return; |
1478 | 1478 | |
| 1479 | if (bookmarks != NULL) |
| 1480 | bookmarks[0] = -1; |
| 1481 | |
1479 | 1482 | len = strlen (filename); |
1480 | 1483 | |
1481 | 1484 | while (fgets (buf, sizeof (buf), f)) |
… |
… |
load_file_position (const char *filename, long *line, long *column, off_t * offs |
1496 | 1499 | if (strchr (p, ' ')) |
1497 | 1500 | continue; |
1498 | 1501 | |
1499 | | pos_tokens = g_strsplit_set (p, ";", 3); |
| 1502 | |
| 1503 | pos_tokens = g_strsplit_set (p, ";", 3 + num_bookmarks); |
1500 | 1504 | if (pos_tokens[0] != NULL) |
1501 | 1505 | { |
1502 | 1506 | *line = strtol (pos_tokens[0], NULL, 10); |
… |
… |
load_file_position (const char *filename, long *line, long *column, off_t * offs |
1504 | 1508 | { |
1505 | 1509 | *column = strtol (pos_tokens[1], NULL, 10); |
1506 | 1510 | if (pos_tokens[2] != NULL) |
| 1511 | { |
| 1512 | int i; |
1507 | 1513 | *offset = strtoll (pos_tokens[2], NULL, 10); |
| 1514 | for (i = 0; i < num_bookmarks; i++) |
| 1515 | { |
| 1516 | if (pos_tokens[3+i] != NULL) |
| 1517 | bookmarks[i] = strtol (pos_tokens[3+i], NULL, 10); |
| 1518 | else |
| 1519 | { |
| 1520 | bookmarks[i] = -1; |
| 1521 | break; |
| 1522 | } |
| 1523 | } |
| 1524 | } |
1508 | 1525 | else |
1509 | 1526 | *offset = 0; |
1510 | 1527 | } |
… |
… |
load_file_position (const char *filename, long *line, long *column, off_t * offs |
1528 | 1545 | /* Save position for the given file */ |
1529 | 1546 | #define TMP_SUFFIX ".tmp" |
1530 | 1547 | void |
1531 | | save_file_position (const char *filename, long line, long column, off_t offset) |
| 1548 | save_file_position (const char *filename, long line, long column, off_t offset, long *bookmarks) |
1532 | 1549 | { |
1533 | 1550 | static int filepos_max_saved_entries = 0; |
1534 | 1551 | char *fn, *tmp_fn; |
1535 | 1552 | FILE *f, *tmp_f; |
1536 | | char buf[MC_MAXPATHLEN + 20]; |
| 1553 | char buf[MC_MAXPATHLEN + 100]; |
1537 | 1554 | int i = 1; |
1538 | 1555 | gsize len; |
1539 | 1556 | |
… |
… |
save_file_position (const char *filename, long line, long column, off_t offset) |
1563 | 1580 | /* put the new record */ |
1564 | 1581 | if (line != 1 || column != 0) |
1565 | 1582 | { |
1566 | | if (fprintf (f, "%s %ld;%ld;%llu\n", filename, line, column, (unsigned long long) offset) < |
| 1583 | if (fprintf (f, "%s %ld;%ld;%llu", filename, line, column, (unsigned long long) offset) < |
1567 | 1584 | 0) |
1568 | 1585 | goto write_position_error; |
| 1586 | if (bookmarks != NULL) |
| 1587 | { |
| 1588 | for ( ; *bookmarks != -1; bookmarks++) |
| 1589 | { |
| 1590 | if (fprintf (f, ";%ld", *bookmarks) < 0) |
| 1591 | goto write_position_error; |
| 1592 | } |
| 1593 | } |
| 1594 | if (fprintf (f, "\n") < 0) |
| 1595 | goto write_position_error; |
1569 | 1596 | } |
1570 | 1597 | |
1571 | 1598 | while (fgets (buf, sizeof (buf), tmp_f)) |
diff --git a/lib/util.h b/lib/util.h
index f3680af..9530dc7 100644
a
|
b
|
GList *list_append_unique (GList * list, char *text); |
232 | 232 | /* Position saving and restoring */ |
233 | 233 | |
234 | 234 | /* Load position for the given filename */ |
235 | | void load_file_position (const char *filename, long *line, long *column, off_t * offset); |
| 235 | void load_file_position (const char *filename, long *line, long *column, off_t * offset, long *bookmarks, int num_bookmarks); |
236 | 236 | /* Save position for the given filename */ |
237 | | void save_file_position (const char *filename, long line, long column, off_t offset); |
| 237 | void save_file_position (const char *filename, long line, long column, off_t offset, long *bookmarks); |
238 | 238 | |
239 | 239 | |
240 | 240 | /* OS specific defines */ |
diff --git a/src/editor/bookmark.c b/src/editor/bookmark.c
index 0d6f8b0..a7e236e 100644
a
|
b
|
void book_mark_dec (WEdit * edit, int line) |
223 | 223 | } |
224 | 224 | } |
225 | 225 | } |
| 226 | |
| 227 | /* prepare line positions of bookmarks to be saved to file */ |
| 228 | long * book_mark_serialize(WEdit * edit, int max_bookmarks, int color) |
| 229 | { |
| 230 | if (edit->book_mark) { |
| 231 | struct _book_mark *p; |
| 232 | int i; |
| 233 | long *serialized = (long*)g_malloc ((max_bookmarks + 1) * sizeof(long)); |
| 234 | if (serialized != NULL) { |
| 235 | for (p = book_mark_find(edit, 0), i = 0; p != NULL && i < max_bookmarks; p = p->next) |
| 236 | if (p->c == color && p->line != -1) { |
| 237 | serialized[i] = p->line; |
| 238 | i++; |
| 239 | } |
| 240 | serialized[i] = -1; |
| 241 | return serialized; |
| 242 | } |
| 243 | } |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | /* restore bookmarks from saved line positions */ |
| 248 | void book_mark_restore(WEdit * edit, long *bookmarks, int color) |
| 249 | { |
| 250 | for ( ; *bookmarks != -1; bookmarks++) |
| 251 | book_mark_insert (edit, *bookmarks, color); |
| 252 | } |
diff --git a/src/editor/edit-impl.h b/src/editor/edit-impl.h
index 6ec37ea..ab4857d 100644
a
|
b
|
int book_mark_clear (WEdit * edit, int line, int c); |
280 | 280 | void book_mark_flush (WEdit * edit, int c); |
281 | 281 | void book_mark_inc (WEdit * edit, int line); |
282 | 282 | void book_mark_dec (WEdit * edit, int line); |
| 283 | long *book_mark_serialize(WEdit * edit, int max_bookmarks, int color); |
| 284 | void book_mark_restore(WEdit * edit, long *bookmarks, int color); |
283 | 285 | |
284 | 286 | int line_is_blank (WEdit * edit, long line); |
285 | 287 | int edit_indent_width (WEdit * edit, long p); |
diff --git a/src/editor/edit.c b/src/editor/edit.c
index 9e5831c..d1d51bc 100644
a
|
b
|
edit_load_file (WEdit * edit) |
757 | 757 | return 0; |
758 | 758 | } |
759 | 759 | |
| 760 | #define MAX_SAVED_BOOKMARKS 10 |
| 761 | |
760 | 762 | /* Restore saved cursor position in the file */ |
761 | 763 | static void |
762 | 764 | edit_load_position (WEdit * edit) |
763 | 765 | { |
764 | 766 | char *filename; |
765 | | long line, column; |
| 767 | long line, column, *bookmarks; |
766 | 768 | off_t offset; |
767 | 769 | |
768 | 770 | if (!edit->filename || !*edit->filename) |
769 | 771 | return; |
770 | 772 | |
771 | 773 | filename = vfs_canon (edit->filename); |
772 | | load_file_position (filename, &line, &column, &offset); |
| 774 | bookmarks = g_malloc( MAX_SAVED_BOOKMARKS * sizeof(long)); |
| 775 | |
| 776 | load_file_position (filename, &line, &column, &offset, bookmarks, MAX_SAVED_BOOKMARKS); |
773 | 777 | g_free (filename); |
774 | 778 | |
775 | 779 | if (line > 0) |
… |
… |
edit_load_position (WEdit * edit) |
782 | 786 | edit_cursor_move (edit, offset); |
783 | 787 | line = edit->curs_line; |
784 | 788 | } |
| 789 | |
| 790 | book_mark_restore(edit, bookmarks, BOOK_MARK_COLOR); |
| 791 | g_free(bookmarks); |
| 792 | |
785 | 793 | edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1)); |
786 | 794 | edit_move_display (edit, line - (edit->num_widget_lines / 2)); |
787 | 795 | } |
… |
… |
static void |
791 | 799 | edit_save_position (WEdit * edit) |
792 | 800 | { |
793 | 801 | char *filename; |
| 802 | long *bookmarks; |
794 | 803 | |
795 | 804 | if (!edit->filename || !*edit->filename) |
796 | 805 | return; |
797 | 806 | |
798 | 807 | filename = vfs_canon (edit->filename); |
799 | | save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1); |
| 808 | |
| 809 | bookmarks = book_mark_serialize(edit, MAX_SAVED_BOOKMARKS, BOOK_MARK_COLOR); |
| 810 | |
| 811 | save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1, bookmarks); |
800 | 812 | g_free (filename); |
801 | 813 | } |
802 | 814 | |
diff --git a/src/viewer/lib.c b/src/viewer/lib.c
index 271055f..a92cc5b 100644
a
|
b
|
mcview_done (mcview_t * view) |
233 | 233 | { |
234 | 234 | char *canon_fname; |
235 | 235 | canon_fname = vfs_canon (view->filename); |
236 | | save_file_position (canon_fname, -1, 0, view->dpy_start); |
| 236 | save_file_position (canon_fname, -1, 0, view->dpy_start, NULL); |
237 | 237 | g_free (canon_fname); |
238 | 238 | } |
239 | 239 | |
diff --git a/src/viewer/mcviewer.c b/src/viewer/mcviewer.c
index 2801dfd..ab9bd8c 100644
a
|
b
|
mcview_load (mcview_t * view, const char *command, const char *file, int start_l |
411 | 411 | off_t new_offset; |
412 | 412 | |
413 | 413 | canon_fname = vfs_canon (view->filename); |
414 | | load_file_position (canon_fname, &line, &col, &new_offset); |
| 414 | load_file_position (canon_fname, &line, &col, &new_offset, NULL, 0); |
415 | 415 | new_offset = min (new_offset, mcview_get_filesize (view)); |
416 | 416 | view->dpy_start = mcview_bol (view, new_offset); |
417 | 417 | g_free (canon_fname); |