root/src/editor/bookmark.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. double_marks
  2. book_mark_find
  3. book_mark_query_color
  4. book_mark_insert
  5. book_mark_clear
  6. book_mark_flush
  7. book_mark_inc
  8. book_mark_dec
  9. book_mark_serialize
  10. book_mark_restore

   1 /*
   2    Editor book mark handling
   3 
   4    Copyright (C) 2001-2019
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Paul Sheer, 1996, 1997
   9 
  10    This file is part of the Midnight Commander.
  11 
  12    The Midnight Commander is free software: you can redistribute it
  13    and/or modify it under the terms of the GNU General Public License as
  14    published by the Free Software Foundation, either version 3 of the License,
  15    or (at your option) any later version.
  16 
  17    The Midnight Commander is distributed in the hope that it will be useful,
  18    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20    GNU General Public License for more details.
  21 
  22    You should have received a copy of the GNU General Public License
  23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  24  */
  25 
  26 /** \file
  27  *  \brief Source: editor book mark handling
  28  *  \author Paul Sheer
  29  *  \date 1996, 1997
  30  */
  31 
  32 #include <config.h>
  33 
  34 #include <ctype.h>
  35 #include <errno.h>
  36 #include <stdarg.h>
  37 #include <stdio.h>
  38 #include <stdlib.h>
  39 #include <string.h>
  40 
  41 #include <sys/types.h>
  42 #include <sys/stat.h>
  43 #include <unistd.h>
  44 
  45 #include "lib/global.h"
  46 #include "lib/util.h"           /* MAX_SAVED_BOOKMARKS */
  47 
  48 #include "editwidget.h"
  49 
  50 /*** global variables ****************************************************************************/
  51 
  52 /*** file scope macro definitions ****************************************************************/
  53 
  54 /*** file scope type declarations ****************************************************************/
  55 
  56 /*** file scope variables ************************************************************************/
  57 
  58 /*** file scope functions ************************************************************************/
  59 /* --------------------------------------------------------------------------------------------- */
  60 
  61 /** note, if there is more than one bookmark on a line, then they are
  62    appended after each other and the last one is always the one found
  63    by book_mark_found() i.e. last in is the one seen */
  64 
  65 static edit_book_mark_t *
  66 double_marks (WEdit * edit, edit_book_mark_t * p)
     /* [previous][next][first][last][top][bottom][index][help]  */
  67 {
  68     (void) edit;
  69 
  70     if (p->next != NULL)
  71         while (p->next->line == p->line)
  72             p = p->next;
  73     return p;
  74 }
  75 
  76 /* --------------------------------------------------------------------------------------------- */
  77 /** returns the first bookmark on or before this line */
  78 
  79 edit_book_mark_t *
  80 book_mark_find (WEdit * edit, long line)
     /* [previous][next][first][last][top][bottom][index][help]  */
  81 {
  82     edit_book_mark_t *p;
  83 
  84     if (edit->book_mark == NULL)
  85     {
  86         /* must have an imaginary top bookmark at line -1 to make things less complicated  */
  87         edit->book_mark = g_new0 (edit_book_mark_t, 1);
  88         edit->book_mark->line = -1;
  89         return edit->book_mark;
  90     }
  91 
  92     for (p = edit->book_mark; p != NULL; p = p->next)
  93     {
  94         if (p->line > line)
  95             break;              /* gone past it going downward */
  96 
  97         if (p->next != NULL)
  98         {
  99             if (p->next->line > line)
 100             {
 101                 edit->book_mark = p;
 102                 return double_marks (edit, p);
 103             }
 104         }
 105         else
 106         {
 107             edit->book_mark = p;
 108             return double_marks (edit, p);
 109         }
 110     }
 111 
 112     for (p = edit->book_mark; p != NULL; p = p->prev)
 113     {
 114         if (p->next != NULL && p->next->line <= line)
 115             break;              /* gone past it going upward */
 116 
 117         if (p->line <= line)
 118         {
 119             if (p->next != NULL)
 120             {
 121                 if (p->next->line > line)
 122                 {
 123                     edit->book_mark = p;
 124                     return double_marks (edit, p);
 125                 }
 126             }
 127             else
 128             {
 129                 edit->book_mark = p;
 130                 return double_marks (edit, p);
 131             }
 132         }
 133     }
 134 
 135     return NULL;                /* can't get here */
 136 }
 137 
 138 /* --------------------------------------------------------------------------------------------- */
 139 /*** public functions ****************************************************************************/
 140 /* --------------------------------------------------------------------------------------------- */
 141 
 142 /** 
 143  * Check if bookmark bookmark exists at this line of this color
 144  *
 145  * @param edit editor object
 146  * @param line line where book mark is
 147  * @param c color of book mark
 148  * @return TRUE if bookmark exists at this line of color c, FALSE otherwise
 149  */
 150 
 151 gboolean
 152 book_mark_query_color (WEdit * edit, long line, int c)
     /* [previous][next][first][last][top][bottom][index][help]  */
 153 {
 154     if (edit->book_mark != NULL)
 155     {
 156         edit_book_mark_t *p;
 157 
 158         for (p = book_mark_find (edit, line); p != NULL; p = p->prev)
 159         {
 160             if (p->line != line)
 161                 return FALSE;
 162             if (p->c == c)
 163                 return TRUE;
 164         }
 165     }
 166 
 167     return FALSE;
 168 }
 169 
 170 /* --------------------------------------------------------------------------------------------- */
 171 /** insert a bookmark at this line */
 172 
 173 void
 174 book_mark_insert (WEdit * edit, long line, int c)
     /* [previous][next][first][last][top][bottom][index][help]  */
 175 {
 176     edit_book_mark_t *p, *q;
 177 
 178     p = book_mark_find (edit, line);
 179 #if 0
 180     if (p->line == line)
 181     {
 182         /* already exists, so just change the color */
 183         if (p->c != c)
 184         {
 185             p->c = c;
 186             edit->force |= REDRAW_LINE;
 187         }
 188         return;
 189     }
 190 #endif
 191     /* create list entry */
 192     q = g_new (edit_book_mark_t, 1);
 193     q->line = line;
 194     q->c = c;
 195     q->next = p->next;
 196     /* insert into list */
 197     q->prev = p;
 198     if (p->next != NULL)
 199         p->next->prev = q;
 200     p->next = q;
 201 
 202     edit->force |= REDRAW_LINE;
 203 }
 204 
 205 /* --------------------------------------------------------------------------------------------- */
 206 /** 
 207  * Remove a bookmark if there is one at this line matching this color - c of -1 clear all
 208  *
 209  * @param edit editor object
 210  * @param line line where book mark is
 211  * @param c color of book mark or -1 to clear all book marks on this line
 212  * @return FALSE if not found, TRUE otherwise
 213  */
 214 
 215 gboolean
 216 book_mark_clear (WEdit * edit, long line, int c)
     /* [previous][next][first][last][top][bottom][index][help]  */
 217 {
 218     edit_book_mark_t *p, *q;
 219     gboolean r = FALSE;
 220 
 221     if (edit->book_mark == NULL)
 222         return r;
 223 
 224     for (p = book_mark_find (edit, line); p != NULL; p = q)
 225     {
 226         q = p->prev;
 227         if (p->line == line && (p->c == c || c == -1))
 228         {
 229             r = TRUE;
 230             edit->book_mark = p->prev;
 231             p->prev->next = p->next;
 232             if (p->next != NULL)
 233                 p->next->prev = p->prev;
 234             g_free (p);
 235             edit->force |= REDRAW_LINE;
 236             break;
 237         }
 238     }
 239     /* if there is only our dummy book mark left, clear it for speed */
 240     if (edit->book_mark->line == -1 && edit->book_mark->next == NULL)
 241         MC_PTR_FREE (edit->book_mark);
 242 
 243     return r;
 244 }
 245 
 246 /* --------------------------------------------------------------------------------------------- */
 247 /** clear all bookmarks matching this color, if c is -1 clears all */
 248 
 249 void
 250 book_mark_flush (WEdit * edit, int c)
     /* [previous][next][first][last][top][bottom][index][help]  */
 251 {
 252     edit_book_mark_t *p, *q;
 253 
 254     if (edit->book_mark == NULL)
 255         return;
 256 
 257     while (edit->book_mark->prev != NULL)
 258         edit->book_mark = edit->book_mark->prev;
 259 
 260     for (q = edit->book_mark->next; q != NULL; q = p)
 261     {
 262         p = q->next;
 263         if (q->c == c || c == -1)
 264         {
 265             q->prev->next = q->next;
 266             if (p != NULL)
 267                 p->prev = q->prev;
 268             g_free (q);
 269         }
 270     }
 271     if (edit->book_mark->next == NULL)
 272         MC_PTR_FREE (edit->book_mark);
 273 
 274     edit->force |= REDRAW_PAGE;
 275 }
 276 
 277 /* --------------------------------------------------------------------------------------------- */
 278 /** shift down bookmarks after this line */
 279 
 280 void
 281 book_mark_inc (WEdit * edit, long line)
     /* [previous][next][first][last][top][bottom][index][help]  */
 282 {
 283     if (edit->book_mark != NULL)
 284     {
 285         edit_book_mark_t *p;
 286 
 287         p = book_mark_find (edit, line);
 288         for (p = p->next; p != NULL; p = p->next)
 289             p->line++;
 290     }
 291 }
 292 
 293 /* --------------------------------------------------------------------------------------------- */
 294 /** shift up bookmarks after this line */
 295 
 296 void
 297 book_mark_dec (WEdit * edit, long line)
     /* [previous][next][first][last][top][bottom][index][help]  */
 298 {
 299     if (edit->book_mark != NULL)
 300     {
 301         edit_book_mark_t *p;
 302 
 303         p = book_mark_find (edit, line);
 304         for (p = p->next; p != NULL; p = p->next)
 305             p->line--;
 306     }
 307 }
 308 
 309 /* --------------------------------------------------------------------------------------------- */
 310 /** prepare line positions of bookmarks to be saved to file */
 311 
 312 void
 313 book_mark_serialize (WEdit * edit, int color)
     /* [previous][next][first][last][top][bottom][index][help]  */
 314 {
 315     if (edit->serialized_bookmarks != NULL)
 316         g_array_set_size (edit->serialized_bookmarks, 0);
 317 
 318     if (edit->book_mark != NULL)
 319     {
 320         edit_book_mark_t *p;
 321 
 322         if (edit->serialized_bookmarks == NULL)
 323             edit->serialized_bookmarks = g_array_sized_new (FALSE, FALSE, sizeof (size_t),
 324                                                             MAX_SAVED_BOOKMARKS);
 325 
 326         for (p = book_mark_find (edit, 0); p != NULL; p = p->next)
 327             if (p->c == color && p->line >= 0)
 328                 g_array_append_val (edit->serialized_bookmarks, p->line);
 329     }
 330 }
 331 
 332 /* --------------------------------------------------------------------------------------------- */
 333 /** restore bookmarks from saved line positions */
 334 
 335 void
 336 book_mark_restore (WEdit * edit, int color)
     /* [previous][next][first][last][top][bottom][index][help]  */
 337 {
 338     if (edit->serialized_bookmarks != NULL)
 339     {
 340         size_t i;
 341 
 342         for (i = 0; i < edit->serialized_bookmarks->len; i++)
 343             book_mark_insert (edit, g_array_index (edit->serialized_bookmarks, size_t, i), color);
 344     }
 345 }
 346 
 347 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */