bookmark.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* editor book mark handling
  2. Copyright (C) 2001, 2002, 2003, 2005, 2007 Free Software Foundation, Inc.
  3. Authors: 1996, 1997 Paul Sheer
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301, USA.
  16. */
  17. /** \file
  18. * \brief Source: editor book mark handling
  19. * \author Paul Sheer
  20. * \date 1996, 1997
  21. */
  22. #include <config.h>
  23. #include <ctype.h>
  24. #include <errno.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <unistd.h>
  32. #include "../src/global.h"
  33. #include "edit-impl.h"
  34. #include "edit-widget.h"
  35. /* note, if there is more than one bookmark on a line, then they are
  36. appended after each other and the last one is always the one found
  37. by book_mark_found() i.e. last in is the one seen */
  38. static struct _book_mark *double_marks (WEdit * edit, struct _book_mark *p)
  39. {
  40. (void) edit;
  41. if (p->next)
  42. while (p->next->line == p->line)
  43. p = p->next;
  44. return p;
  45. }
  46. /* returns the first bookmark on or before this line */
  47. struct _book_mark *book_mark_find (WEdit * edit, int line)
  48. {
  49. struct _book_mark *p;
  50. if (!edit->book_mark) {
  51. /* must have an imaginary top bookmark at line -1 to make things less complicated */
  52. edit->book_mark = g_malloc0 (sizeof (struct _book_mark));
  53. edit->book_mark->line = -1;
  54. return edit->book_mark;
  55. }
  56. for (p = edit->book_mark; p; p = p->next) {
  57. if (p->line > line)
  58. break; /* gone past it going downward */
  59. if (p->line <= line) {
  60. if (p->next) {
  61. if (p->next->line > line) {
  62. edit->book_mark = p;
  63. return double_marks (edit, p);
  64. }
  65. } else {
  66. edit->book_mark = p;
  67. return double_marks (edit, p);
  68. }
  69. }
  70. }
  71. for (p = edit->book_mark; p; p = p->prev) {
  72. if (p->next)
  73. if (p->next->line <= line)
  74. break; /* gone past it going upward */
  75. if (p->line <= line) {
  76. if (p->next) {
  77. if (p->next->line > line) {
  78. edit->book_mark = p;
  79. return double_marks (edit, p);
  80. }
  81. } else {
  82. edit->book_mark = p;
  83. return double_marks (edit, p);
  84. }
  85. }
  86. }
  87. return 0; /* can't get here */
  88. }
  89. /* returns true if a bookmark exists at this line of color c */
  90. int book_mark_query_color (WEdit * edit, int line, int c)
  91. {
  92. struct _book_mark *p;
  93. if (!edit->book_mark)
  94. return 0;
  95. for (p = book_mark_find (edit, line); p; p = p->prev) {
  96. if (p->line != line)
  97. return 0;
  98. if (p->c == c)
  99. return 1;
  100. }
  101. return 0;
  102. }
  103. /* insert a bookmark at this line */
  104. void
  105. book_mark_insert (WEdit *edit, int line, int c)
  106. {
  107. struct _book_mark *p, *q;
  108. p = book_mark_find (edit, line);
  109. #if 0
  110. if (p->line == line) {
  111. /* already exists, so just change the color */
  112. if (p->c != c) {
  113. edit->force |= REDRAW_LINE;
  114. p->c = c;
  115. }
  116. return;
  117. }
  118. #endif
  119. edit->force |= REDRAW_LINE;
  120. /* create list entry */
  121. q = g_malloc0 (sizeof (struct _book_mark));
  122. q->line = line;
  123. q->c = c;
  124. q->next = p->next;
  125. /* insert into list */
  126. q->prev = p;
  127. if (p->next)
  128. p->next->prev = q;
  129. p->next = q;
  130. }
  131. /* remove a bookmark if there is one at this line matching this color - c of -1 clear all */
  132. /* returns non-zero on not-found */
  133. int book_mark_clear (WEdit * edit, int line, int c)
  134. {
  135. struct _book_mark *p, *q;
  136. int r = 1;
  137. if (!edit->book_mark)
  138. return r;
  139. for (p = book_mark_find (edit, line); p; p = q) {
  140. q = p->prev;
  141. if (p->line == line && (p->c == c || c == -1)) {
  142. r = 0;
  143. edit->force |= REDRAW_LINE;
  144. edit->book_mark = p->prev;
  145. p->prev->next = p->next;
  146. if (p->next)
  147. p->next->prev = p->prev;
  148. g_free (p);
  149. break;
  150. }
  151. }
  152. /* if there is only our dummy book mark left, clear it for speed */
  153. if (edit->book_mark->line == -1 && !edit->book_mark->next) {
  154. g_free (edit->book_mark);
  155. edit->book_mark = 0;
  156. }
  157. return r;
  158. }
  159. /* clear all bookmarks matching this color, if c is -1 clears all */
  160. void book_mark_flush (WEdit * edit, int c)
  161. {
  162. struct _book_mark *p, *q;
  163. if (!edit->book_mark)
  164. return;
  165. edit->force |= REDRAW_PAGE;
  166. while (edit->book_mark->prev)
  167. edit->book_mark = edit->book_mark->prev;
  168. for (q = edit->book_mark->next; q; q = p) {
  169. p = q->next;
  170. if (q->c == c || c == -1) {
  171. q->prev->next = q->next;
  172. if (p)
  173. p->prev = q->prev;
  174. g_free (q);
  175. }
  176. }
  177. if (!edit->book_mark->next) {
  178. g_free (edit->book_mark);
  179. edit->book_mark = 0;
  180. }
  181. }
  182. /* shift down bookmarks after this line */
  183. void book_mark_inc (WEdit * edit, int line)
  184. {
  185. if (edit->book_mark) {
  186. struct _book_mark *p;
  187. p = book_mark_find (edit, line);
  188. for (p = p->next; p; p = p->next) {
  189. p->line++;
  190. }
  191. }
  192. }
  193. /* shift up bookmarks after this line */
  194. void book_mark_dec (WEdit * edit, int line)
  195. {
  196. if (edit->book_mark) {
  197. struct _book_mark *p;
  198. p = book_mark_find (edit, line);
  199. for (p = p->next; p; p = p->next) {
  200. p->line--;
  201. }
  202. }
  203. }