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

File 0002-Save-bookmarks-of-internal-edit-in-filepos-2.patch, 11.3 KB (added by filipsef, 10 years ago)
  • lib/util.c

    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) 
    14521452    return -1; 
    14531453} 
    14541454 
     1455#define MAX_SAVED_BOOKMARKS 10 
     1456 
    14551457/* 
    14561458 * Read and restore position for the given filename. 
    14571459 * If there is no stored data, return line 1 and col 0. 
    14581460 */ 
    14591461void 
    1460 load_file_position (const char *filename, long *line, long *column, off_t * offset, long *bookmarks, int num_bookmarks) 
     1462load_file_position (const char *filename, long *line, long *column, off_t * offset, long **bookmarks) 
    14611463{ 
    14621464    char *fn; 
    14631465    FILE *f; 
    load_file_position (const char *filename, long *line, long *column, off_t * offs 
    14761478    if (!f) 
    14771479        return; 
    14781480 
    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; 
    14811485 
    14821486    len = strlen (filename); 
    14831487 
    load_file_position (const char *filename, long *line, long *column, off_t * offs 
    15001504            continue; 
    15011505 
    15021506 
    1503         pos_tokens = g_strsplit_set (p, ";", 3 + num_bookmarks); 
     1507        pos_tokens = g_strsplit_set (p, ";", 3 + MAX_SAVED_BOOKMARKS); 
    15041508        if (pos_tokens[0] != NULL) 
    15051509        { 
    15061510            *line = strtol (pos_tokens[0], NULL, 10); 
    load_file_position (const char *filename, long *line, long *column, off_t * offs 
    15111515                { 
    15121516                    int i; 
    15131517                    *offset = strtoll (pos_tokens[2], NULL, 10); 
    1514                     for (i = 0; i < num_bookmarks; i++) 
     1518                    for (i = 0; i < MAX_SAVED_BOOKMARKS; i++) 
    15151519                    { 
    15161520                        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); 
    15181522                        else 
    15191523                        { 
    1520                             bookmarks[i] = -1; 
     1524                            (*bookmarks)[i] = -1; 
    15211525                            break; 
    15221526                        } 
    15231527                    } 
    save_file_position (const char *filename, long line, long column, off_t offset, 
    15511555    char *fn, *tmp_fn; 
    15521556    FILE *f, *tmp_f; 
    15531557    char buf[MC_MAXPATHLEN + 100]; 
    1554     int i = 1; 
     1558    int i; 
    15551559    gsize len; 
    15561560 
    15571561    if (filepos_max_saved_entries == 0) 
    save_file_position (const char *filename, long line, long column, off_t offset, 
    15781582        goto open_source_error; 
    15791583 
    15801584    /* put the new record */ 
    1581     if (line != 1 || column != 0) 
     1585    if (line != 1 || column != 0 || bookmarks != NULL) 
    15821586    { 
    15831587        if (fprintf (f, "%s %ld;%ld;%llu", filename, line, column, (unsigned long long) offset) < 
    15841588            0) 
    15851589            goto write_position_error; 
    15861590        if (bookmarks != NULL) 
    15871591        { 
    1588             for ( ; *bookmarks != -1; bookmarks++) 
     1592            for (i = 0 ; bookmarks[i] >= 0 && i < MAX_SAVED_BOOKMARKS; i++) 
    15891593            { 
    1590                 if (fprintf (f, ";%ld", *bookmarks) < 0) 
     1594                if (fprintf (f, ";%ld", bookmarks[i]) < 0) 
    15911595                   goto write_position_error; 
    15921596            } 
    15931597        } 
    save_file_position (const char *filename, long line, long column, off_t offset, 
    15951599            goto write_position_error; 
    15961600    } 
    15971601 
     1602    i = 1; 
    15981603    while (fgets (buf, sizeof (buf), tmp_f)) 
    15991604    { 
    16001605        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, 
    16201625  open_target_error: 
    16211626    g_free (fn); 
    16221627  early_error: 
     1628    if (bookmarks != NULL) 
     1629        g_free(bookmarks); 
    16231630    return; 
    16241631} 
    16251632 
  • lib/util.h

    diff --git a/lib/util.h b/lib/util.h
    index 9530dc7..06ec850 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, long *bookmarks, int num_bookmarks); 
     235void load_file_position (const char *filename, long *line, long *column, off_t * offset, long **bookmarks); 
    236236/* Save position for the given filename */ 
    237237void save_file_position (const char *filename, long line, long column, off_t offset, long *bookmarks); 
    238238 
  • src/editor/bookmark.c

    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) 
    217217{ 
    218218    if (edit->book_mark) { 
    219219        struct _book_mark *p; 
    220         p = book_mark_find (edit, line); 
     220    p = book_mark_find (edit, line); 
    221221        for (p = p->next; p; p = p->next) { 
    222222            p->line--; 
    223223        } 
    void book_mark_dec (WEdit * edit, int line) 
    225225} 
    226226 
    227227/* prepare line positions of bookmarks to be saved to file */ 
    228 long * book_mark_serialize(WEdit * edit, int max_bookmarks, int color) 
     228void book_mark_serialize(WEdit * edit, int color) 
    229229{ 
    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        } 
    243241    } 
    244     return NULL; 
     242 
     243    *bookmarks = -1; 
    245244} 
    246245 
    247246/* restore bookmarks from saved line positions */ 
    248 void book_mark_restore(WEdit * edit, long *bookmarks, int color) 
     247void book_mark_restore(WEdit * edit, int color) 
    249248{ 
    250     for ( ; *bookmarks != -1; bookmarks++) 
     249    long *bookmarks = edit->serialized_bookmarks; 
     250    if (bookmarks == NULL) 
     251        return; 
     252 
     253    for ( ; *bookmarks >= 0; bookmarks++) 
    251254        book_mark_insert (edit, *bookmarks, color); 
    252255} 
  • src/editor/edit-impl.h

    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); 
    280280void book_mark_flush (WEdit * edit, int c); 
    281281void book_mark_inc (WEdit * edit, int line); 
    282282void 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); 
     283void book_mark_serialize(WEdit * edit, int color); 
     284void book_mark_restore(WEdit * edit, int color); 
    285285 
    286286int line_is_blank (WEdit * edit, long line); 
    287287int edit_indent_width (WEdit * edit, long p); 
  • src/editor/edit-widget.h

    diff --git a/src/editor/edit-widget.h b/src/editor/edit-widget.h
    index 3d66abe..79ff233 100644
    a b struct WEdit 
    9696    long line_offsets[N_LINE_CACHES]; 
    9797 
    9898    struct _book_mark *book_mark; 
     99    long *serialized_bookmarks; 
    99100 
    100101    /* undo stack and pointers */ 
    101102    unsigned long stack_pointer; 
  • src/editor/edit.c

    diff --git a/src/editor/edit.c b/src/editor/edit.c
    index d1d51bc..e1ee824 100644
    a b edit_load_file (WEdit * edit) 
    757757    return 0; 
    758758} 
    759759 
    760 #define MAX_SAVED_BOOKMARKS 10 
    761  
    762760/* Restore saved cursor position in the file */ 
    763761static void 
    764762edit_load_position (WEdit * edit) 
    765763{ 
    766764    char *filename; 
    767     long line, column, *bookmarks; 
     765    long line, column; 
    768766    off_t offset; 
    769767 
    770768    if (!edit->filename || !*edit->filename) 
    771769        return; 
    772770 
    773771    filename = vfs_canon (edit->filename); 
    774     bookmarks = g_malloc( MAX_SAVED_BOOKMARKS * sizeof(long)); 
    775772 
    776     load_file_position (filename, &line, &column, &offset, bookmarks, MAX_SAVED_BOOKMARKS); 
     773    load_file_position (filename, &line, &column, &offset, &edit->serialized_bookmarks); 
    777774    g_free (filename); 
    778775 
    779776    if (line > 0) 
    edit_load_position (WEdit * edit) 
    787784        line = edit->curs_line; 
    788785    } 
    789786 
    790     book_mark_restore(edit, bookmarks, BOOK_MARK_COLOR); 
    791     g_free(bookmarks); 
     787    book_mark_restore(edit, BOOK_MARK_COLOR); 
    792788 
    793789    edit_move_to_prev_col (edit, edit_bol (edit, edit->curs1)); 
    794790    edit_move_display (edit, line - (edit->num_widget_lines / 2)); 
    edit_load_position (WEdit * edit) 
    798794static void 
    799795edit_save_position (WEdit * edit) 
    800796{ 
    801     char *filename; 
    802     long *bookmarks; 
     797    char *filename;     
    803798 
    804799    if (!edit->filename || !*edit->filename) 
    805800        return; 
    806801 
    807802    filename = vfs_canon (edit->filename); 
    808803 
    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; 
    810808 
    811     save_file_position (filename, edit->curs_line + 1, edit->curs_col, edit->curs1, bookmarks); 
    812809    g_free (filename); 
    813810} 
    814811 
  • src/viewer/internal.h

    diff --git a/src/viewer/internal.h b/src/viewer/internal.h
    index 2e30522..1ff2838 100644
    a b typedef struct mcview_struct 
    190190    struct mcview_nroff_struct *search_nroff_seq; 
    191191 
    192192    int search_numNeedSkipChar; 
     193 
     194    long *saved_bookmarks; 
    193195} mcview_t; 
    194196 
    195197typedef struct mcview_nroff_struct 
  • src/viewer/lib.c

    diff --git a/src/viewer/lib.c b/src/viewer/lib.c
    index a92cc5b..d6c29c8 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, NULL); 
     236        save_file_position (canon_fname, -1, 0, view->dpy_start, view->saved_bookmarks); 
    237237        g_free (canon_fname); 
    238238    } 
    239239 
  • src/viewer/mcviewer.c

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