bookmark.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #include <config.h>
  18. #include <ctype.h>
  19. #include <errno.h>
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. #include "../src/global.h"
  28. #include "edit.h"
  29. #include "edit-widget.h"
  30. /* note, if there is more than one bookmark on a line, then they are
  31. appended after each other and the last one is always the one found
  32. by book_mark_found() i.e. last in is the one seen */
  33. static inline struct _book_mark *double_marks (WEdit * edit, struct _book_mark *p)
  34. {
  35. (void) edit;
  36. if (p->next)
  37. while (p->next->line == p->line)
  38. p = p->next;
  39. return p;
  40. }
  41. /* returns the first bookmark on or before this line */
  42. struct _book_mark *book_mark_find (WEdit * edit, int line)
  43. {
  44. struct _book_mark *p;
  45. if (!edit->book_mark) {
  46. /* must have an imaginary top bookmark at line -1 to make things less complicated */
  47. edit->book_mark = g_malloc0 (sizeof (struct _book_mark));
  48. edit->book_mark->line = -1;
  49. return edit->book_mark;
  50. }
  51. for (p = edit->book_mark; p; p = p->next) {
  52. if (p->line > line)
  53. break; /* gone past it going downward */
  54. if (p->line <= line) {
  55. if (p->next) {
  56. if (p->next->line > line) {
  57. edit->book_mark = p;
  58. return double_marks (edit, p);
  59. }
  60. } else {
  61. edit->book_mark = p;
  62. return double_marks (edit, p);
  63. }
  64. }
  65. }
  66. for (p = edit->book_mark; p; p = p->prev) {
  67. if (p->next)
  68. if (p->next->line <= line)
  69. break; /* gone past it going upward */
  70. if (p->line <= line) {
  71. if (p->next) {
  72. if (p->next->line > line) {
  73. edit->book_mark = p;
  74. return double_marks (edit, p);
  75. }
  76. } else {
  77. edit->book_mark = p;
  78. return double_marks (edit, p);
  79. }
  80. }
  81. }
  82. return 0; /* can't get here */
  83. }
  84. /* returns true if a bookmark exists at this line of color c */
  85. int book_mark_query_color (WEdit * edit, int line, int c)
  86. {
  87. struct _book_mark *p;
  88. if (!edit->book_mark)
  89. return 0;
  90. for (p = book_mark_find (edit, line); p; p = p->prev) {
  91. if (p->line != line)
  92. return 0;
  93. if (p->c == c)
  94. return 1;
  95. }
  96. return 0;
  97. }
  98. /* insert a bookmark at this line */
  99. void
  100. book_mark_insert (WEdit *edit, int line, int c)
  101. {
  102. struct _book_mark *p, *q;
  103. p = book_mark_find (edit, line);
  104. #if 0
  105. if (p->line == line) {
  106. /* already exists, so just change the color */
  107. if (p->c != c) {
  108. edit->force |= REDRAW_LINE;
  109. p->c = c;
  110. }
  111. return;
  112. }
  113. #endif
  114. edit->force |= REDRAW_LINE;
  115. /* create list entry */
  116. q = g_malloc0 (sizeof (struct _book_mark));
  117. q->line = line;
  118. q->c = c;
  119. q->next = p->next;
  120. /* insert into list */
  121. q->prev = p;
  122. if (p->next)
  123. p->next->prev = q;
  124. p->next = q;
  125. }
  126. /* remove a bookmark if there is one at this line matching this color - c of -1 clear all */
  127. /* returns non-zero on not-found */
  128. int book_mark_clear (WEdit * edit, int line, int c)
  129. {
  130. struct _book_mark *p, *q;
  131. int r = 1;
  132. if (!edit->book_mark)
  133. return r;
  134. for (p = book_mark_find (edit, line); p; p = q) {
  135. q = p->prev;
  136. if (p->line == line && (p->c == c || c == -1)) {
  137. r = 0;
  138. edit->force |= REDRAW_LINE;
  139. edit->book_mark = p->prev;
  140. p->prev->next = p->next;
  141. if (p->next)
  142. p->next->prev = p->prev;
  143. g_free (p);
  144. break;
  145. }
  146. }
  147. /* if there is only our dummy book mark left, clear it for speed */
  148. if (edit->book_mark->line == -1 && !edit->book_mark->next) {
  149. g_free (edit->book_mark);
  150. edit->book_mark = 0;
  151. }
  152. return r;
  153. }
  154. /* clear all bookmarks matching this color, if c is -1 clears all */
  155. void book_mark_flush (WEdit * edit, int c)
  156. {
  157. struct _book_mark *p, *q;
  158. if (!edit->book_mark)
  159. return;
  160. edit->force |= REDRAW_PAGE;
  161. while (edit->book_mark->prev)
  162. edit->book_mark = edit->book_mark->prev;
  163. for (q = edit->book_mark->next; q; q = p) {
  164. p = q->next;
  165. if (q->c == c || c == -1) {
  166. q->prev->next = q->next;
  167. if (p)
  168. p->prev = q->prev;
  169. g_free (q);
  170. }
  171. }
  172. if (!edit->book_mark->next) {
  173. g_free (edit->book_mark);
  174. edit->book_mark = 0;
  175. }
  176. }
  177. /* shift down bookmarks after this line */
  178. void book_mark_inc (WEdit * edit, int line)
  179. {
  180. if (edit->book_mark) {
  181. struct _book_mark *p;
  182. p = book_mark_find (edit, line);
  183. for (p = p->next; p; p = p->next) {
  184. p->line++;
  185. }
  186. }
  187. }
  188. /* shift up bookmarks after this line */
  189. void book_mark_dec (WEdit * edit, int line)
  190. {
  191. if (edit->book_mark) {
  192. struct _book_mark *p;
  193. p = book_mark_find (edit, line);
  194. for (p = p->next; p; p = p->next) {
  195. p->line--;
  196. }
  197. }
  198. }