bookmark.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. }
  199. /*
  200. *****************************************************************************
  201. * collapsed lines algorithm
  202. *
  203. *****************************************************************************
  204. */
  205. /* returns the first collapsed region on or before this line */
  206. GList *
  207. book_mark_collapse_find (GList * list, int line)
  208. {
  209. GList *cl, *l;
  210. collapsed_lines *collapsed;
  211. l = list;
  212. if (!l)
  213. return NULL;
  214. l = g_list_first (list);
  215. cl = l;
  216. while (cl) {
  217. collapsed = (collapsed_lines *) cl->data;
  218. if ( collapsed->start_line <= line && line <= collapsed->end_line )
  219. return cl;
  220. cl = g_list_next (cl);
  221. }
  222. return NULL;
  223. }
  224. /* insert a collapsed at this line */
  225. GList *
  226. book_mark_collapse_insert (GList *list, const int start_line, const int end_line,
  227. int state)
  228. {
  229. collapsed_lines *p, *q;
  230. p = g_new0 (collapsed_lines, 1);
  231. p->start_line = start_line;
  232. p->end_line = end_line;
  233. p->state = state;
  234. GList *link, *newlink, *tmp;
  235. /*
  236. * Go to the last position and traverse the list backwards
  237. * starting from the second last entry to make sure that we
  238. * are not removing the current link.
  239. */
  240. list = g_list_append (list, p);
  241. list = g_list_last (list);
  242. link = g_list_previous (list);
  243. int sl = 0;
  244. int el = 0;
  245. while ( link ) {
  246. newlink = g_list_previous (link);
  247. q = (collapsed_lines *) link->data;
  248. sl = q->start_line;
  249. el = q->end_line;
  250. if (((sl == start_line) || (el == end_line) ||
  251. (sl == end_line) || (el == start_line)) ||
  252. ((sl < start_line) && (el > start_line) && (el < end_line)) ||
  253. ((sl > start_line) && (sl < end_line) && (el > end_line))) {
  254. g_free (link->data);
  255. tmp = g_list_remove_link (list, link);
  256. g_list_free_1 (link);
  257. }
  258. link = newlink;
  259. }
  260. return list;
  261. }
  262. void
  263. book_mark_collapse (GList *list, const int line)
  264. {
  265. GList *cl;
  266. collapsed_lines *p;
  267. int collapse_state;
  268. collapse_state = book_mark_get_collapse_state(list, line, NULL);
  269. cl = book_mark_collapse_find (list, line);
  270. switch ( collapse_state ) {
  271. case C_LINES_ELAPSED:
  272. if ( cl ) {
  273. p = (collapsed_lines *) cl->data;
  274. p->state = 1;
  275. }
  276. break;
  277. case C_LINES_COLLAPSED:
  278. if ( cl ) {
  279. p = (collapsed_lines *) cl->data;
  280. p->state = 0;
  281. }
  282. break;
  283. default:
  284. break;
  285. }
  286. }
  287. /* returns true if a collapsed exists at this line
  288. * return start_line, end_line if found region
  289. *
  290. */
  291. int book_mark_collapse_query (GList * list, const int line,
  292. int *start_line,
  293. int *end_line,
  294. int *state)
  295. {
  296. GList *cl;
  297. collapsed_lines *p;
  298. *start_line = 0;
  299. *end_line = 0;
  300. *state = 0;
  301. cl = book_mark_collapse_find (list, line);
  302. if ( cl ){
  303. p = (collapsed_lines *) cl->data;
  304. *start_line = p->start_line;
  305. *end_line = p->end_line;
  306. *state = p->state;
  307. return 1;
  308. }
  309. return 0;
  310. }
  311. int book_mark_get_collapse_state (GList * list, const int line,
  312. collapsed_lines *cl)
  313. {
  314. int start_line;
  315. int end_line;
  316. int state;
  317. int c = 0;
  318. c = book_mark_collapse_query (list, line, &start_line, &end_line, &state);
  319. // mc_log("l: %i, start_line:%i, end_line:%i", line, start_line, end_line);
  320. if ( c == 0 )
  321. return C_LINES_DEFAULT;
  322. if ( cl ) {
  323. cl->start_line = start_line;
  324. cl->end_line = end_line;
  325. cl->state = state;
  326. }
  327. if ( line == start_line ) {
  328. if ( state )
  329. return C_LINES_COLLAPSED;
  330. else
  331. return C_LINES_ELAPSED;
  332. }
  333. if ( line > start_line && line < end_line ) {
  334. if ( state )
  335. return C_LINES_MIDDLE_C;
  336. else
  337. return C_LINES_MIDDLE_E;
  338. }
  339. if ( line == end_line ) {
  340. return C_LINES_LAST;
  341. }
  342. return C_LINES_DEFAULT;
  343. }
  344. /* shift down collapse after this line */
  345. void book_mark_collapse_inc (GList * list, int line)
  346. {
  347. GList *cl, *l;
  348. collapsed_lines *collapsed;
  349. l = list;
  350. if (!l)
  351. return;
  352. l = g_list_first (list);
  353. cl = l;
  354. while (cl) {
  355. collapsed = (collapsed_lines *) cl->data;
  356. if ( collapsed->start_line >= line ) {
  357. collapsed->start_line++;
  358. collapsed->end_line++;
  359. } else if ( collapsed->end_line >= line ){
  360. collapsed->end_line++;
  361. }
  362. cl = g_list_next (cl);
  363. }
  364. }
  365. /* shift up collapse after this line */
  366. void book_mark_collapse_dec (GList * list, int line)
  367. {
  368. GList *cl, *l, *tmp;
  369. collapsed_lines *collapsed;
  370. l = list;
  371. if (!l)
  372. return;
  373. l = g_list_first (list);
  374. cl = l;
  375. while (cl) {
  376. collapsed = (collapsed_lines *) cl->data;
  377. if ( collapsed->start_line >= line ) {
  378. collapsed->start_line--;
  379. collapsed->end_line--;
  380. } else if ( collapsed->end_line >= line ){
  381. collapsed->end_line--;
  382. }
  383. cl = g_list_next (cl);
  384. }
  385. }
  386. int book_mark_get_shiftup (GList * list, int line)
  387. {
  388. GList *cl;
  389. collapsed_lines *collapsed;
  390. int res = 0;
  391. cl = list;
  392. if (!cl)
  393. return res;
  394. cl = g_list_first (list);
  395. while (cl) {
  396. collapsed = (collapsed_lines *) cl->data;
  397. if ( line > collapsed->start_line && collapsed->state ) {
  398. res += collapsed->end_line - collapsed->start_line - 1;
  399. }
  400. cl = g_list_next (cl);
  401. }
  402. return res;
  403. }