Merging r58464 through r58474 from trunk into soc-2013-depsgraph_mt
[blender.git] / source / blender / editors / space_text / text_autocomplete.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/editors/space_text/text_autocomplete.c
22  *  \ingroup sptext
23  */
24
25 #include <ctype.h>
26 #include <string.h>
27
28 #include "MEM_guardedalloc.h"
29
30 #include "DNA_text_types.h"
31
32 #include "BLI_blenlib.h"
33 #include "BLI_ghash.h"
34
35 #include "BKE_context.h"
36 #include "BKE_text.h"
37 #include "BKE_screen.h"
38 #include "BKE_suggestions.h"
39
40 #include "WM_api.h"
41 #include "WM_types.h"
42
43 #include "ED_screen.h"
44 #include "UI_interface.h"
45
46 #include "text_format.h"
47 #include "text_intern.h"  /* own include */
48
49
50 /* -------------------------------------------------------------------- */
51 /* Public API */
52
53 int text_do_suggest_select(SpaceText *st, ARegion *ar)
54 {
55         SuggItem *item, *first, *last /* , *sel */ /* UNUSED */;
56         TextLine *tmp;
57         int l, x, y, w, h, i;
58         int tgti, *top;
59         int mval[2] = {0, 0};
60
61         if (!st || !st->text) return 0;
62         if (!texttool_text_is_active(st->text)) return 0;
63
64         first = texttool_suggest_first();
65         last = texttool_suggest_last();
66         /* sel = texttool_suggest_selected(); */ /* UNUSED */
67         top = texttool_suggest_top();
68
69         if (!last || !first)
70                 return 0;
71
72         /* Count the visible lines to the cursor */
73         for (tmp = st->text->curl, l = -st->top; tmp; tmp = tmp->prev, l++) ;
74         if (l < 0) return 0;
75
76         text_update_character_width(st);
77
78         if (st->showlinenrs) {
79                 x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET + TEXTXLOC - 4;
80         }
81         else {
82                 x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET - 4;
83         }
84         y = ar->winy - st->lheight_dpi * l - 2;
85
86         w = SUGG_LIST_WIDTH * st->cwidth + U.widget_unit;
87         h = SUGG_LIST_SIZE * st->lheight_dpi + 0.4f * U.widget_unit;
88
89         // XXX getmouseco_areawin(mval);
90
91         if (mval[0] < x || x + w < mval[0] || mval[1] < y - h || y < mval[1])
92                 return 0;
93
94         /* Work out which of the items is at the top of the visible list */
95         for (i = 0, item = first; i < *top && item->next; i++, item = item->next) ;
96
97         /* Work out the target item index in the visible list */
98         tgti = (y - mval[1] - 4) / st->lheight_dpi;
99         if (tgti < 0 || tgti > SUGG_LIST_SIZE)
100                 return 1;
101
102         for (i = tgti; i > 0 && item->next; i--, item = item->next) ;
103         if (item)
104                 texttool_suggest_select(item);
105         return 1;
106 }
107
108 void text_pop_suggest_list(void)
109 {
110         SuggItem *item, *sel;
111         int *top, i;
112
113         item = texttool_suggest_first();
114         sel = texttool_suggest_selected();
115         top = texttool_suggest_top();
116
117         i = 0;
118         while (item && item != sel) {
119                 item = item->next;
120                 i++;
121         }
122         if (i > *top + SUGG_LIST_SIZE - 1)
123                 *top = i - SUGG_LIST_SIZE + 1;
124         else if (i < *top)
125                 *top = i;
126 }
127
128 /* -------------------------------------------------------------------- */
129 /* Private API */
130
131 static void text_autocomplete_free(bContext *C, wmOperator *op);
132
133 static GHash *text_autocomplete_build(Text *text)
134 {
135         GHash *gh;
136         int seek_len = 0;
137         const char *seek;
138         texttool_text_clear();
139
140         texttool_text_set_active(text);
141
142         /* first get the word we're at */
143         {
144                 const int i = text_find_identifier_start(text->curl->line, text->curc);
145                 seek_len = text->curc - i;
146                 seek = text->curl->line + i;
147
148                 // BLI_strncpy(seek, seek_ptr, seek_len);
149         }
150
151         /* now walk over entire doc and suggest words */
152         {
153                 TextLine *linep;
154
155                 gh = BLI_ghash_str_new(__func__);
156
157                 for (linep = text->lines.first; linep; linep = linep->next) {
158                         size_t i_start = 0;
159                         size_t i_end = 0;
160                         size_t i_pos = 0;
161
162                         while (i_start < linep->len) {
163                                 /* seek identifier beginning */
164                                 i_pos = i_start;
165                                 while ((i_start < linep->len) &&
166                                        (!text_check_identifier_nodigit_unicode(BLI_str_utf8_as_unicode_and_size_safe(&linep->line[i_start], &i_pos))))
167                                 {
168                                         i_start = i_pos;
169                                 }
170                                 i_pos = i_end = i_start;
171                                 while ((i_end < linep->len) &&
172                                        (text_check_identifier_unicode(BLI_str_utf8_as_unicode_and_size_safe(&linep->line[i_end], &i_pos))))
173                                 {
174                                         i_end = i_pos;
175                                 }
176
177                                 if ((i_start != i_end) &&
178                                     /* check we're at the beginning of a line or that the previous char is not an identifier
179                                      * this prevents digits from being added */
180                                     ((i_start < 1) || !text_check_identifier_unicode(BLI_str_utf8_as_unicode(&linep->line[i_start - 1]))))
181                                 {
182                                         char *str_sub = &linep->line[i_start];
183                                         const int choice_len = i_end - i_start;
184
185                                         if ((choice_len > seek_len) &&
186                                             (seek_len == 0 || strncmp(seek, str_sub, seek_len) == 0) &&
187                                             (seek != str_sub))
188                                         {
189                                                 // printf("Adding: %s\n", s);
190                                                 char str_sub_last = str_sub[choice_len];
191                                                 str_sub[choice_len] = '\0';
192                                                 if (!BLI_ghash_lookup(gh, str_sub)) {
193                                                         char *str_dup = BLI_strdupn(str_sub, choice_len);
194                                                         BLI_ghash_insert(gh, str_dup, str_dup);  /* A 'set' would make more sense here */
195                                                 }
196                                                 str_sub[choice_len] = str_sub_last;
197                                         }
198                                 }
199                                 if (i_end != i_start) {
200                                         i_start = i_end;
201                                 }
202                                 else {
203                                         /* highly unlikely, but prevent eternal loop */
204                                         i_start++;
205                                 }
206                         }
207                 }
208
209                 {
210                         GHashIterator *iter = BLI_ghashIterator_new(gh);
211
212                         /* get the formatter for highlighting */
213                         TextFormatType *tft;
214                         tft = ED_text_format_get(text);
215
216                         for (; !BLI_ghashIterator_done(iter); BLI_ghashIterator_step(iter)) {
217                                 const char *s = BLI_ghashIterator_getValue(iter);
218                                 texttool_suggest_add(s, tft->format_identifier(s));
219                         }
220                         BLI_ghashIterator_free(iter);
221
222                 }
223         }
224
225         texttool_suggest_prefix(seek, seek_len);
226
227         return gh;
228 }
229
230 /* -- */
231
232 static void get_suggest_prefix(Text *text, int offset)
233 {
234         int i, len;
235         char *line;
236
237         if (!text) return;
238         if (!texttool_text_is_active(text)) return;
239
240         line = text->curl->line;
241         i = text_find_identifier_start(line, text->curc + offset);
242         len = text->curc - i + offset;
243         texttool_suggest_prefix(line + i, len);
244 }
245
246 static void confirm_suggestion(Text *text)
247 {
248         SuggItem *sel;
249         int i, over = 0;
250         const char *line;
251
252         if (!text) return;
253         if (!texttool_text_is_active(text)) return;
254
255         sel = texttool_suggest_selected();
256         if (!sel) return;
257
258         line = text->curl->line;
259         i = text_find_identifier_start(line, text->curc /* - skipleft */);
260         over = text->curc - i;
261
262 //      for (i = 0; i < skipleft; i++)
263 //              txt_move_left(text, 0);
264         for (i = 0; i < over; i++)
265                 txt_move_left(text, 1);
266
267         txt_insert_buf(text, sel->name);
268
269 //      for (i = 0; i < skipleft; i++)
270 //              txt_move_right(text, 0);
271
272         texttool_text_clear();
273 }
274
275 /* -- */
276
277
278 static int text_autocomplete_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
279 {
280         SpaceText *st = CTX_wm_space_text(C);
281         Text *text = CTX_data_edit_text(C);
282
283         st->doplugins = TRUE;
284         op->customdata = text_autocomplete_build(text);
285
286         if (texttool_suggest_first()) {
287
288                 ED_area_tag_redraw(CTX_wm_area(C));
289
290                 if (texttool_suggest_first() == texttool_suggest_last()) {
291                         confirm_suggestion(st->text);
292                         text_update_line_edited(st->text->curl);
293                         text_autocomplete_free(C, op);
294                         return OPERATOR_FINISHED;
295                 }
296                 else {
297                         WM_event_add_modal_handler(C, op);
298                         return OPERATOR_RUNNING_MODAL;
299                 }
300         }
301         else {
302                 text_autocomplete_free(C, op);
303                 return OPERATOR_CANCELLED;
304         }
305 }
306
307 static int doc_scroll = 0;
308
309 static int text_autocomplete_modal(bContext *C, wmOperator *op, const wmEvent *event)
310 {
311         SpaceText *st = CTX_wm_space_text(C);
312         ScrArea *sa = CTX_wm_area(C);
313         ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
314
315         int draw = 0, tools = 0, swallow = 0, scroll = 1;
316         Text *text = CTX_data_edit_text(C);
317         int retval = OPERATOR_RUNNING_MODAL;
318
319         (void)text;
320
321         if (st->doplugins && texttool_text_is_active(st->text)) {
322                 if (texttool_suggest_first()) tools |= TOOL_SUGG_LIST;
323                 if (texttool_docs_get()) tools |= TOOL_DOCUMENT;
324         }
325
326         switch (event->type) {
327                 case LEFTMOUSE:
328                         if (event->val == KM_PRESS) {
329                                 if (text_do_suggest_select(st, ar))
330                                         swallow = 1;
331                                 else {
332                                         if (tools & TOOL_SUGG_LIST) texttool_suggest_clear();
333                                         if (tools & TOOL_DOCUMENT) texttool_docs_clear(), doc_scroll = 0;
334                                         retval = OPERATOR_FINISHED;
335                                 }
336                                 draw = 1;
337                         }
338                         break;
339                 case MIDDLEMOUSE:
340                         if (event->val == KM_PRESS) {
341                                 if (text_do_suggest_select(st, ar)) {
342                                         confirm_suggestion(st->text);
343                                         text_update_line_edited(st->text->curl);
344                                         swallow = 1;
345                                 }
346                                 else {
347                                         if (tools & TOOL_SUGG_LIST) texttool_suggest_clear();
348                                         if (tools & TOOL_DOCUMENT) texttool_docs_clear(), doc_scroll = 0;
349                                         retval = OPERATOR_FINISHED;
350                                 }
351                                 draw = 1;
352                         }
353                         break;
354                 case ESCKEY:
355                         if (event->val == KM_PRESS) {
356                                 draw = swallow = 1;
357                                 if (tools & TOOL_SUGG_LIST) texttool_suggest_clear();
358                                 else if (tools & TOOL_DOCUMENT) texttool_docs_clear(), doc_scroll = 0;
359                                 else draw = swallow = 0;
360                                 retval = OPERATOR_CANCELLED;
361                         }
362                         break;
363                 case RETKEY:
364                         if (event->val == KM_PRESS) {
365                                 if (tools & TOOL_SUGG_LIST) {
366                                         confirm_suggestion(st->text);
367                                         text_update_line_edited(st->text->curl);
368                                         swallow = 1;
369                                         draw = 1;
370                                 }
371                                 if (tools & TOOL_DOCUMENT) texttool_docs_clear(), doc_scroll = 0, draw = 1;
372                                 retval = OPERATOR_FINISHED;
373                         }
374                         break;
375                 case LEFTARROWKEY:
376                 case BACKSPACEKEY:
377                         if (event->val == KM_PRESS) {
378                                 if (tools & TOOL_SUGG_LIST) {
379                                         if (event->ctrl) {
380                                                 texttool_suggest_clear();
381                                                 retval = OPERATOR_CANCELLED;
382                                         }
383                                         else {
384                                                 /* Work out which char we are about to delete/pass */
385                                                 if (st->text->curl && st->text->curc > 0) {
386                                                         char ch = st->text->curl->line[st->text->curc - 1];
387                                                         if ((ch == '_' || !ispunct(ch)) && !text_check_whitespace(ch)) {
388                                                                 get_suggest_prefix(st->text, -1);
389                                                                 text_pop_suggest_list();
390                                                         }
391                                                         else {
392                                                                 texttool_suggest_clear();
393                                                                 retval = OPERATOR_CANCELLED;
394                                                         }
395                                                 }
396                                                 else {
397                                                         texttool_suggest_clear();
398                                                         retval = OPERATOR_CANCELLED;
399                                                 }
400                                         }
401                                 }
402                                 if (tools & TOOL_DOCUMENT) texttool_docs_clear(), doc_scroll = 0;
403                         }
404                         break;
405                 case RIGHTARROWKEY:
406                         if (event->val == KM_PRESS) {
407                                 if (tools & TOOL_SUGG_LIST) {
408                                         if (event->ctrl) {
409                                                 texttool_suggest_clear();
410                                                 retval = OPERATOR_CANCELLED;
411                                         }
412                                         else {
413                                                 /* Work out which char we are about to pass */
414                                                 if (st->text->curl && st->text->curc < st->text->curl->len) {
415                                                         char ch = st->text->curl->line[st->text->curc + 1];
416                                                         if ((ch == '_' || !ispunct(ch)) && !text_check_whitespace(ch)) {
417                                                                 get_suggest_prefix(st->text, 1);
418                                                                 text_pop_suggest_list();
419                                                         }
420                                                         else {
421                                                                 texttool_suggest_clear();
422                                                                 retval = OPERATOR_CANCELLED;
423                                                         }
424                                                 }
425                                                 else {
426                                                         texttool_suggest_clear();
427                                                         retval = OPERATOR_CANCELLED;
428                                                 }
429                                         }
430                                 }
431                                 if (tools & TOOL_DOCUMENT) texttool_docs_clear(), doc_scroll = 0;
432                         }
433                         break;
434                 case PAGEDOWNKEY:
435                         scroll = SUGG_LIST_SIZE - 1;
436                         /* fall-through */
437                 case WHEELDOWNMOUSE:
438                 case DOWNARROWKEY:
439                         if (event->val == KM_PRESS) {
440                                 if (tools & TOOL_DOCUMENT) {
441                                         doc_scroll++;
442                                         swallow = 1;
443                                         draw = 1;
444                                 }
445                                 else if (tools & TOOL_SUGG_LIST) {
446                                         SuggItem *sel = texttool_suggest_selected();
447                                         if (!sel) {
448                                                 texttool_suggest_select(texttool_suggest_first());
449                                         }
450                                         else {
451                                                 while (sel && sel != texttool_suggest_last() && sel->next && scroll--) {
452                                                         texttool_suggest_select(sel->next);
453                                                         sel = sel->next;
454                                                 }
455                                         }
456                                         text_pop_suggest_list();
457                                         swallow = 1;
458                                         draw = 1;
459                                 }
460                         }
461                         break;
462                 case PAGEUPKEY:
463                         scroll = SUGG_LIST_SIZE - 1;
464                         /* fall-through */
465                 case WHEELUPMOUSE:
466                 case UPARROWKEY:
467                         if (event->val == KM_PRESS) {
468                                 if (tools & TOOL_DOCUMENT) {
469                                         if (doc_scroll > 0) doc_scroll--;
470                                         swallow = 1;
471                                         draw = 1;
472                                 }
473                                 else if (tools & TOOL_SUGG_LIST) {
474                                         SuggItem *sel = texttool_suggest_selected();
475                                         while (sel && sel != texttool_suggest_first() && sel->prev && scroll--) {
476                                                 texttool_suggest_select(sel->prev);
477                                                 sel = sel->prev;
478                                         }
479                                         text_pop_suggest_list();
480                                         swallow = 1;
481                                         draw = 1;
482                                 }
483                         }
484                         break;
485                 case RIGHTSHIFTKEY:
486                 case LEFTSHIFTKEY:
487                         break;
488 #if 0
489                 default:
490                         if (tools & TOOL_SUGG_LIST) texttool_suggest_clear(), draw = 1;
491                         if (tools & TOOL_DOCUMENT) texttool_docs_clear(), doc_scroll = 0, draw = 1;
492 #endif
493         }
494
495         if (draw) {
496                 ED_area_tag_redraw(CTX_wm_area(C));
497         }
498
499 //      if (swallow) {
500 //              retval = OPERATOR_RUNNING_MODAL;
501 //      }
502
503         if (texttool_suggest_first()) {
504                 if (retval != OPERATOR_RUNNING_MODAL) {
505                         text_autocomplete_free(C, op);
506                 }
507                 return retval;
508         }
509         else {
510                 text_autocomplete_free(C, op);
511                 return OPERATOR_FINISHED;
512         }
513 }
514
515 static void text_autocomplete_free(bContext *C, wmOperator *op)
516 {
517         GHash *gh = op->customdata;
518         if (gh) {
519                 BLI_ghash_free(gh, NULL, MEM_freeN);
520                 op->customdata = NULL;
521         }
522
523         /* other stuff */
524         {
525                 SpaceText *st = CTX_wm_space_text(C);
526                 st->doplugins = FALSE;
527                 texttool_text_clear();
528         }
529 }
530
531 static int text_autocomplete_cancel(bContext *C, wmOperator *op)
532 {
533         text_autocomplete_free(C, op);
534         return OPERATOR_CANCELLED;
535 }
536
537 void TEXT_OT_autocomplete(wmOperatorType *ot)
538 {
539         /* identifiers */
540         ot->name = "Text Auto Complete";
541         ot->description = "Show a list of used text in the open document";
542         ot->idname = "TEXT_OT_autocomplete";
543
544         /* api callbacks */
545         ot->invoke = text_autocomplete_invoke;
546         ot->cancel = text_autocomplete_cancel;
547         ot->modal = text_autocomplete_modal;
548         ot->poll = text_space_edit_poll;
549
550         /* flags */
551         ot->flag = OPTYPE_BLOCKING;
552 }