bookmark.c 5.3 KB

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