From 4434b485287b0395cf9c87857ba293ca8cdf5d20 Mon Sep 17 00:00:00 2001
From: Filip Sefrna <fsefrna@gmail.com>
Date: Tue, 20 Jul 2010 23:02:14 +0200
Subject: [PATCH 2/2] Save bookmarks of internal edit in filepos 2.
---
lib/util.c | 29 ++++++++++++++++++-----------
lib/util.h | 2 +-
src/editor/bookmark.c | 39 +++++++++++++++++++++------------------
src/editor/edit-impl.h | 4 ++--
src/editor/edit-widget.h | 1 +
src/editor/edit.c | 19 ++++++++-----------
src/viewer/internal.h | 2 ++
src/viewer/lib.c | 2 +-
src/viewer/mcviewer.c | 2 +-
9 files changed, 55 insertions(+), 45 deletions(-)
diff --git a/lib/util.c b/lib/util.c
index 7246205..a042645 100644
a
|
b
|
mc_mkstemps (char **pname, const char *prefix, const char *suffix) |
1452 | 1452 | return -1; |
1453 | 1453 | } |
1454 | 1454 | |
| 1455 | #define MAX_SAVED_BOOKMARKS 10 |
| 1456 | |
1455 | 1457 | /* |
1456 | 1458 | * Read and restore position for the given filename. |
1457 | 1459 | * If there is no stored data, return line 1 and col 0. |
1458 | 1460 | */ |
1459 | 1461 | void |
1460 | | load_file_position (const char *filename, long *line, long *column, off_t * offset, long *bookmarks, int num_bookmarks) |
| 1462 | load_file_position (const char *filename, long *line, long *column, off_t * offset, long **bookmarks) |
1461 | 1463 | { |
1462 | 1464 | char *fn; |
1463 | 1465 | FILE *f; |
… |
… |
load_file_position (const char *filename, long *line, long *column, off_t * offs |
1476 | 1478 | if (!f) |
1477 | 1479 | return; |
1478 | 1480 | |
1479 | | if (bookmarks != NULL) |
1480 | | bookmarks[0] = -1; |
| 1481 | /* prepare array for serialized bookmarks */ |
| 1482 | (*bookmarks) = (long*) g_malloc ((MAX_SAVED_BOOKMARKS + 1) * sizeof(long)); |
| 1483 | (*bookmarks)[0] = -1; |
| 1484 | (*bookmarks)[MAX_SAVED_BOOKMARKS] = -2; |
1481 | 1485 | |
1482 | 1486 | len = strlen (filename); |
1483 | 1487 | |
… |
… |
load_file_position (const char *filename, long *line, long *column, off_t * offs |
1500 | 1504 | continue; |
1501 | 1505 | |
1502 | 1506 | |
1503 | | pos_tokens = g_strsplit_set (p, ";", 3 + num_bookmarks); |
| 1507 | pos_tokens = g_strsplit_set (p, ";", 3 + MAX_SAVED_BOOKMARKS); |
1504 | 1508 | if (pos_tokens[0] != NULL) |
1505 | 1509 | { |
1506 | 1510 | *line = strtol (pos_tokens[0], NULL, 10); |
… |
… |
load_file_position (const char *filename, long *line, long *column, off_t * offs |
1511 | 1515 | { |
1512 | 1516 | int i; |
1513 | 1517 | *offset = strtoll (pos_tokens[2], NULL, 10); |
1514 | | for (i = 0; i < num_bookmarks; i++) |
| 1518 | for (i = 0; i < MAX_SAVED_BOOKMARKS; i++) |
1515 | 1519 | { |
1516 | 1520 | if (pos_tokens[3+i] != NULL) |
1517 | | bookmarks[i] = strtol (pos_tokens[3+i], NULL, 10); |
| 1521 | (*bookmarks)[i] = strtol (pos_tokens[3+i], NULL, 10); |
1518 | 1522 | else |
1519 | 1523 | { |
1520 | | bookmarks[i] = -1; |
| 1524 | (*bookmarks)[i] = -1; |
1521 | 1525 | break; |
1522 | 1526 | } |
1523 | 1527 | } |
… |
… |
save_file_position (const char *filename, long line, long column, off_t offset, |
1551 | 1555 | char *fn, *tmp_fn; |
1552 | 1556 | FILE *f, *tmp_f; |
1553 | 1557 | char buf[MC_MAXPATHLEN + 100]; |
1554 | | int i = 1; |
| 1558 | int i; |
1555 | 1559 | gsize len; |
1556 | 1560 | |
1557 | 1561 | if (filepos_max_saved_entries == 0) |
… |
… |
save_file_position (const char *filename, long line, long column, off_t offset, |
1578 | 1582 | goto open_source_error; |
1579 | 1583 | |
1580 | 1584 | /* put the new record */ |
1581 | | if (line != 1 || column != 0) |
| 1585 | if (line != 1 || column != 0 || bookmarks != NULL) |
1582 | 1586 | { |
1583 | 1587 | if (fprintf (f, "%s %ld;%ld;%llu", filename, line, column, (unsigned long long) offset) < |
1584 | 1588 | 0) |
1585 | 1589 | goto write_position_error; |
1586 | 1590 | if (bookmarks != NULL) |
1587 | 1591 | { |
1588 | | for ( ; *bookmarks != -1; bookmarks++) |
| 1592 | for (i = 0 ; bookmarks[i] >= 0 && i < MAX_SAVED_BOOKMARKS; i++) |
1589 | 1593 | { |
1590 | | if (fprintf (f, ";%ld", *bookmarks) < 0) |
| 1594 | if (fprintf (f, ";%ld", bookmarks[i]) < 0) |
1591 | 1595 | goto write_position_error; |
1592 | 1596 | } |
1593 | 1597 | } |
… |
… |
save_file_position (const char *filename, long line, long column, off_t offset, |
1595 | 1599 | goto write_position_error; |
1596 | 1600 | } |
1597 | 1601 | |
| 1602 | i = 1; |
1598 | 1603 | while (fgets (buf, sizeof (buf), tmp_f)) |
1599 | 1604 | { |
1600 | 1605 | if (buf[len] == ' ' && strncmp (buf, filename, len) == 0 && !strchr (&buf[len + 1], ' ')) |
… |
… |
save_file_position (const char *filename, long line, long column, off_t offset, |
1620 | 1625 | open_target_error: |
1621 | 1626 | g_free (fn); |
1622 | 1627 | early_error: |
| 1628 | if (bookmarks != NULL) |
| 1629 | g_free(bookmarks); |
1623 | 1630 | return; |
1624 | 1631 | } |
1625 | 1632 | |
diff --git a/lib/util.h b/lib/util.h
index 9530dc7..06ec850 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, long *bookmarks, int num_bookmarks); |
| 235 | void load_file_position (const char *filename, long *line, long *column, off_t * offset, long **bookmarks); |
236 | 236 | /* Save position for the given filename */ |
237 | 237 | void save_file_position (const char *filename, long line, long column, off_t offset, long *bookmarks); |
238 | 238 | |
diff --git a/src/editor/bookmark.c b/src/editor/bookmark.c
index a7e236e..12fe883 100644
a
|
b
|
void book_mark_dec (WEdit * edit, int line) |
217 | 217 | { |
218 | 218 | if (edit->book_mark) { |
219 | 219 | struct _book_mark *p; |
220 | | p = book_mark_find (edit, line); |
| 220 | p = book_mark_find (edit, line); |
221 | 221 | for (p = p->next; p; p = p->next) { |
222 | 222 | p->line--; |
223 | 223 | } |
… |
… |
void book_mark_dec (WEdit * edit, int line) |
225 | 225 | } |
226 | 226 | |
227 | 227 | /* prepare line positions of bookmarks to be saved to file */ |
228 | | long * book_mark_serialize(WEdit * edit, int max_bookmarks, int color) |
| 228 | void book_mark_serialize(WEdit * edit, int color) |
229 | 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 | | } |
| 230 | struct _book_mark *p; |
| 231 | long *bookmarks = edit->serialized_bookmarks; |
| 232 | |
| 233 | if (edit->book_mark == NULL || bookmarks == NULL) |
| 234 | return; |
| 235 | |
| 236 | for (p = book_mark_find(edit, 0); p != NULL && *bookmarks != -2; p = p->next) |
| 237 | { |
| 238 | if (p->c == color && p->line != -1) { |
| 239 | *bookmarks++ = p->line; |
| 240 | } |
243 | 241 | } |
244 | | return NULL; |
| 242 | |
| 243 | *bookmarks = -1; |
245 | 244 | } |
246 | 245 | |
247 | 246 | /* restore bookmarks from saved line positions */ |
248 | | void book_mark_restore(WEdit * edit, long *bookmarks, int color) |
| 247 | void book_mark_restore(WEdit * edit, int color) |
249 | 248 | { |
250 | | for ( ; *bookmarks != -1; bookmarks++) |
| 249 | long *bookmarks = edit->serialized_bookmarks; |
| 250 | if (bookmarks == NULL) |
| 251 | return; |
| 252 | |
| 253 | for ( ; *bookmarks >= 0; bookmarks++) |
251 | 254 | book_mark_insert (edit, *bookmarks, color); |
252 | 255 | } |
diff --git a/src/editor/edit-impl.h b/src/editor/edit-impl.h
index ab4857d..5ed7eae 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 | void book_mark_serialize(WEdit * edit, int color); |
| 284 | void book_mark_restore(WEdit * edit, int color); |
285 | 285 | |
286 | 286 | int line_is_blank (WEdit * edit, long line); |
287 | 287 | int edit_indent_width (WEdit * edit, long p); |
diff --git a/src/editor/edit-widget.h b/src/editor/edit-widget.h
index 3d66abe..79ff233 100644
a
|
b
|
struct WEdit |
96 | 96 | long line_offsets[N_LINE_CACHES]; |
97 | 97 | |
98 | 98 | struct _book_mark *book_mark; |
| 99 | long *serialized_bookmarks; |
99 | 100 | |
100 | 101 | /* undo stack and pointers */ |
101 | 102 | unsigned long stack_pointer; |
diff --git a/src/editor/edit.c b/src/editor/edit.c
index d1d51bc..e1ee824 100644
a
|
b
|
edit_load_file (WEdit * edit) |
757 | 757 | return 0; |
758 | 758 | } |
759 | 759 | |
760 | | #define MAX_SAVED_BOOKMARKS 10 |
761 | | |
762 | 760 | /* Restore saved cursor position in the file */ |
763 | 761 | static void |
764 | 762 | edit_load_position (WEdit * edit) |
765 | 763 | { |
766 | 764 | char *filename; |
767 | | long line, column, *bookmarks; |
| 765 | long line, column; |
768 | 766 | off_t offset; |
769 | 767 | |
770 | 768 | if (!edit->filename || !*edit->filename) |
771 | 769 | return; |
772 | 770 | |
773 | 771 | filename = vfs_canon (edit->filename); |
774 | | bookmarks = g_malloc( MAX_SAVED_BOOKMARKS * sizeof(long)); |
775 | 772 | |
776 | | load_file_position (filename, &line, &column, &offset, bookmarks, MAX_SAVED_BOOKMARKS); |
| 773 | load_file_position (filename, &line, &column, &offset, &edit->serialized_bookmarks); |
777 | 774 | g_free (filename); |
778 | 775 | |
779 | 776 | if (line > 0) |
… |
… |
edit_load_position (WEdit * edit) |
787 | 784 | line = edit->curs_line; |
788 | 785 | } |
789 | 786 | |
790 | | book_mark_restore(edit, bookmarks, BOOK_MARK_COLOR); |
791 | | g_free(bookmarks); |
| 787 | book_mark_restore(edit, BOOK_MARK_COLOR); |
792 | 788 | |
793 | 789 | edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1)); |
794 | 790 | edit_move_display (edit, line - (edit->num_widget_lines / 2)); |
… |
… |
edit_load_position (WEdit * edit) |
798 | 794 | static void |
799 | 795 | edit_save_position (WEdit * edit) |
800 | 796 | { |
801 | | char *filename; |
802 | | long *bookmarks; |
| 797 | char *filename; |
803 | 798 | |
804 | 799 | if (!edit->filename || !*edit->filename) |
805 | 800 | return; |
806 | 801 | |
807 | 802 | filename = vfs_canon (edit->filename); |
808 | 803 | |
809 | | bookmarks = book_mark_serialize(edit, MAX_SAVED_BOOKMARKS, BOOK_MARK_COLOR); |
| 804 | book_mark_serialize(edit, BOOK_MARK_COLOR); |
| 805 | |
| 806 | save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1, edit->serialized_bookmarks); |
| 807 | edit->serialized_bookmarks = NULL; |
810 | 808 | |
811 | | save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1, bookmarks); |
812 | 809 | g_free (filename); |
813 | 810 | } |
814 | 811 | |
diff --git a/src/viewer/internal.h b/src/viewer/internal.h
index 2e30522..1ff2838 100644
a
|
b
|
typedef struct mcview_struct |
190 | 190 | struct mcview_nroff_struct *search_nroff_seq; |
191 | 191 | |
192 | 192 | int search_numNeedSkipChar; |
| 193 | |
| 194 | long *saved_bookmarks; |
193 | 195 | } mcview_t; |
194 | 196 | |
195 | 197 | typedef struct mcview_nroff_struct |
diff --git a/src/viewer/lib.c b/src/viewer/lib.c
index a92cc5b..d6c29c8 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, NULL); |
| 236 | save_file_position (canon_fname, -1, 0, view->dpy_start, view->saved_bookmarks); |
237 | 237 | g_free (canon_fname); |
238 | 238 | } |
239 | 239 | |
diff --git a/src/viewer/mcviewer.c b/src/viewer/mcviewer.c
index ab9bd8c..dd07b6c 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, NULL, 0); |
| 414 | load_file_position (canon_fname, &line, &col, &new_offset, &view->saved_bookmarks); |
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); |