Ticket #2287: 0001-Save-bookmarks-of-internal-edit-in-filepos.patch

File 0001-Save-bookmarks-of-internal-edit-in-filepos.patch, 9.0 KB (added by filipsef, 10 years ago)

proposed patch

  • lib/util.c

    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) 
    14571457 * If there is no stored data, return line 1 and col 0. 
    14581458 */ 
    14591459void 
    1460 load_file_position (const char *filename, long *line, long *column, off_t * offset) 
     1460load_file_position (const char *filename, long *line, long *column, off_t * offset, long *bookmarks, int num_bookmarks) 
    14611461{ 
    14621462    char *fn; 
    14631463    FILE *f; 
    1464     char buf[MC_MAXPATHLEN + 20]; 
     1464    char buf[MC_MAXPATHLEN + 100]; 
    14651465    int len; 
    14661466 
    14671467    /* defaults */ 
    load_file_position (const char *filename, long *line, long *column, off_t * offs 
    14761476    if (!f) 
    14771477        return; 
    14781478 
     1479    if (bookmarks != NULL) 
     1480        bookmarks[0] = -1; 
     1481 
    14791482    len = strlen (filename); 
    14801483 
    14811484    while (fgets (buf, sizeof (buf), f)) 
    load_file_position (const char *filename, long *line, long *column, off_t * offs 
    14961499        if (strchr (p, ' ')) 
    14971500            continue; 
    14981501 
    1499         pos_tokens = g_strsplit_set (p, ";", 3); 
     1502 
     1503        pos_tokens = g_strsplit_set (p, ";", 3 + num_bookmarks); 
    15001504        if (pos_tokens[0] != NULL) 
    15011505        { 
    15021506            *line = strtol (pos_tokens[0], NULL, 10); 
    load_file_position (const char *filename, long *line, long *column, off_t * offs 
    15041508            { 
    15051509                *column = strtol (pos_tokens[1], NULL, 10); 
    15061510                if (pos_tokens[2] != NULL) 
     1511                { 
     1512                    int i; 
    15071513                    *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                } 
    15081525                else 
    15091526                    *offset = 0; 
    15101527            } 
    load_file_position (const char *filename, long *line, long *column, off_t * offs 
    15281545/* Save position for the given file */ 
    15291546#define TMP_SUFFIX ".tmp" 
    15301547void 
    1531 save_file_position (const char *filename, long line, long column, off_t offset) 
     1548save_file_position (const char *filename, long line, long column, off_t offset, long *bookmarks) 
    15321549{ 
    15331550    static int filepos_max_saved_entries = 0; 
    15341551    char *fn, *tmp_fn; 
    15351552    FILE *f, *tmp_f; 
    1536     char buf[MC_MAXPATHLEN + 20]; 
     1553    char buf[MC_MAXPATHLEN + 100]; 
    15371554    int i = 1; 
    15381555    gsize len; 
    15391556 
    save_file_position (const char *filename, long line, long column, off_t offset) 
    15631580    /* put the new record */ 
    15641581    if (line != 1 || column != 0) 
    15651582    { 
    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) < 
    15671584            0) 
    15681585            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; 
    15691596    } 
    15701597 
    15711598    while (fgets (buf, sizeof (buf), tmp_f)) 
  • lib/util.h

    diff --git a/lib/util.h b/lib/util.h
    index f3680af..9530dc7 100644
    a b GList *list_append_unique (GList * list, char *text); 
    232232/* Position saving and restoring */ 
    233233 
    234234/* Load position for the given filename */ 
    235 void load_file_position (const char *filename, long *line, long *column, off_t * offset); 
     235void load_file_position (const char *filename, long *line, long *column, off_t * offset, long *bookmarks, int num_bookmarks); 
    236236/* Save position for the given filename */ 
    237 void save_file_position (const char *filename, long line, long column, off_t offset); 
     237void save_file_position (const char *filename, long line, long column, off_t offset, long *bookmarks); 
    238238 
    239239 
    240240/* OS specific defines */ 
  • src/editor/bookmark.c

    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) 
    223223        } 
    224224    } 
    225225} 
     226 
     227/* prepare line positions of bookmarks to be saved to file */ 
     228long * 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 */ 
     248void book_mark_restore(WEdit * edit, long *bookmarks, int color) 
     249{ 
     250    for ( ; *bookmarks != -1; bookmarks++) 
     251        book_mark_insert (edit, *bookmarks, color); 
     252} 
  • src/editor/edit-impl.h

    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); 
    280280void book_mark_flush (WEdit * edit, int c); 
    281281void book_mark_inc (WEdit * edit, int line); 
    282282void book_mark_dec (WEdit * edit, int line); 
     283long *book_mark_serialize(WEdit * edit, int max_bookmarks, int color); 
     284void book_mark_restore(WEdit * edit, long *bookmarks, int color); 
    283285 
    284286int line_is_blank (WEdit * edit, long line); 
    285287int edit_indent_width (WEdit * edit, long p); 
  • src/editor/edit.c

    diff --git a/src/editor/edit.c b/src/editor/edit.c
    index 9e5831c..d1d51bc 100644
    a b edit_load_file (WEdit * edit) 
    757757    return 0; 
    758758} 
    759759 
     760#define MAX_SAVED_BOOKMARKS 10 
     761 
    760762/* Restore saved cursor position in the file */ 
    761763static void 
    762764edit_load_position (WEdit * edit) 
    763765{ 
    764766    char *filename; 
    765     long line, column; 
     767    long line, column, *bookmarks; 
    766768    off_t offset; 
    767769 
    768770    if (!edit->filename || !*edit->filename) 
    769771        return; 
    770772 
    771773    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); 
    773777    g_free (filename); 
    774778 
    775779    if (line > 0) 
    edit_load_position (WEdit * edit) 
    782786        edit_cursor_move (edit, offset); 
    783787        line = edit->curs_line; 
    784788    } 
     789 
     790    book_mark_restore(edit, bookmarks, BOOK_MARK_COLOR); 
     791    g_free(bookmarks); 
     792 
    785793    edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1)); 
    786794    edit_move_display (edit, line - (edit->num_widget_lines / 2)); 
    787795} 
    static void 
    791799edit_save_position (WEdit * edit) 
    792800{ 
    793801    char *filename; 
     802    long *bookmarks; 
    794803 
    795804    if (!edit->filename || !*edit->filename) 
    796805        return; 
    797806 
    798807    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); 
    800812    g_free (filename); 
    801813} 
    802814 
  • src/viewer/lib.c

    diff --git a/src/viewer/lib.c b/src/viewer/lib.c
    index 271055f..a92cc5b 100644
    a b mcview_done (mcview_t * view) 
    233233    { 
    234234        char *canon_fname; 
    235235        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); 
    237237        g_free (canon_fname); 
    238238    } 
    239239 
  • src/viewer/mcviewer.c

    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 
    411411        off_t new_offset; 
    412412 
    413413        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); 
    415415        new_offset = min (new_offset, mcview_get_filesize (view)); 
    416416        view->dpy_start = mcview_bol (view, new_offset); 
    417417        g_free (canon_fname);