make autocomplete use unicode character stepping (needed for bugfix).
[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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_text/text_autocomplete.c
29  *  \ingroup sptext
30  */
31
32 #include <ctype.h>
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_text_types.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_ghash.h"
41
42 #include "BKE_context.h"
43 #include "BKE_text.h"
44 #include "BKE_screen.h"
45 #include "BKE_suggestions.h"
46
47 #include "WM_api.h"
48 #include "WM_types.h"
49
50 #include "ED_screen.h"
51 #include "UI_interface.h"
52
53 #include "text_format.h"
54 #include "text_intern.h"  /* own include */
55
56
57 /* -------------------------------------------------------------------- */
58 /* Public API */
59
60 int text_do_suggest_select(SpaceText *st, ARegion *ar)
61 {
62         SuggItem *item, *first, *last /* , *sel */ /* UNUSED */;
63         TextLine *tmp;
64         int l, x, y, w, h, i;
65         int tgti, *top;
66         int mval[2] = {0, 0};
67
68         if (!st || !st->text) return 0;
69         if (!texttool_text_is_active(st->text)) return 0;
70
71         first = texttool_suggest_first();
72         last = texttool_suggest_last();
73         /* sel = texttool_suggest_selected(); */ /* UNUSED */
74         top = texttool_suggest_top();
75
76         if (!last || !first)
77                 return 0;
78
79         /* Count the visible lines to the cursor */
80         for (tmp = st->text->curl, l = -st->top; tmp; tmp = tmp->prev, l++) ;
81         if (l < 0) return 0;
82
83         text_update_character_width(st);
84
85         if (st->showlinenrs) {
86                 x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET + TEXTXLOC - 4;
87         }
88         else {
89                 x = st->cwidth * (st->text->curc - st->left) + TXT_OFFSET - 4;
90         }
91         y = ar->winy - st->lheight_dpi * l - 2;
92
93         w = SUGG_LIST_WIDTH * st->cwidth + U.widget_unit;
94         h = SUGG_LIST_SIZE * st->lheight_dpi + 0.4f * U.widget_unit;
95
96         // XXX getmouseco_areawin(mval);
97
98         if (mval[0] < x || x + w < mval[0] || mval[1] < y - h || y < mval[1])
99                 return 0;
100
101         /* Work out which of the items is at the top of the visible list */
102         for (i = 0, item = first; i < *top && item->next; i++, item = item->next) ;
103
104         /* Work out the target item index in the visible list */
105         tgti = (y - mval[1] - 4) / st->lheight_dpi;
106         if (tgti < 0 || tgti > SUGG_LIST_SIZE)
107                 return 1;
108
109         for (i = tgti; i > 0 && item->next; i--, item = item->next) ;
110         if (item)
111                 texttool_suggest_select(item);
112         return 1;
113 }
114
115 void text_pop_suggest_list(void)
116 {
117         SuggItem *item, *sel;
118         int *top, i;
119
120         item = texttool_suggest_first();
121         sel = texttool_suggest_selected();
122         top = texttool_suggest_top();
123
124         i = 0;
125         while (item && item != sel) {
126                 item = item->next;
127                 i++;
128         }
129         if (i > *top + SUGG_LIST_SIZE - 1)
130                 *top = i - SUGG_LIST_SIZE + 1;
131         else if (i < *top)
132                 *top = i;
133 }
134
135 /* -------------------------------------------------------------------- */
136 /* Private API */
137
138 static void text_autocomplete_free(bContext *C, wmOperator *op);
139
140 static GHash *text_autocomplete_build(Text *text)
141 {
142         GHash *gh;
143         int seek_len = 0;
144         const char *seek;
145         texttool_text_clear();
146
147         texttool_text_set_active(text);
148
149         /* first get the word we're at */
150         {
151                 const int i = text_find_identifier_start(text->curl->line, text->curc);
152                 seek_len = text->curc - i;
153                 seek = text->curl->line + i;
154
155                 // BLI_strncpy(seek, seek_ptr, seek_len);
156         }
157
158         /* now walk over entire doc and suggest words */
159         {
160                 TextLine *linep;
161
162                 gh = BLI_ghash_str_new(__func__);
163
164                 for (linep = text->lines.first; linep; linep = linep->next) {
165                         size_t i_start = 0;
166                         size_t i_end = 0;
167                         size_t i_step = 0;
168
169                         while (i_start < linep->len) {
170                                 /* seek identifier beginning */
171                                 while ((i_start < linep->len) &&
172                                        (!text_check_identifier_nodigit(BLI_str_utf8_as_unicode_step(&linep->line[i_start], &i_step))))
173                                 {
174                                         i_start = i_step;
175                                 }
176                                 i_end = i_start;
177                                 while ((i_end < linep->len) &&
178                                        (!text_check_identifier(BLI_str_utf8_as_unicode_step(&linep->line[i_end], &i_step))))
179                                 {
180                                         i_end = i_step;
181                                 }
182
183                                 if ((i_start != i_end) &&
184                                     /* check we're at the beginning of a line or that the previous char is not an identifier
185                                      * this prevents digits from being added */
186                                     ((i_start < 1) || !text_check_identifier(BLI_str_utf8_as_unicode(&linep->line[i_start - 1]))))
187                                 {
188                                         char *str_sub = &linep->line[i_start];
189                                         const int choice_len = i_end - i_start;
190
191                                         if ((choice_len > seek_len) &&
192                                             (seek_len == 0 || strncmp(seek, str_sub, seek_len) == 0) &&
193                                             (seek != str_sub))
194                                         {
195                                                 // printf("Adding: %s\n", s);
196                                                 char str_sub_last = str_sub[choice_len];
197                                                 str_sub[choice_len] = '\0';
198                                                 if (!BLI_ghash_lookup(gh, str_sub)) {
199                                                         char *str_dup = BLI_strdupn(str_sub, choice_len);
200                                                         BLI_ghash_insert(gh, str_dup, str_dup);  /* A 'set' would make more sense here */
201                                                 }
202                                                 str_sub[choice_len] = str_sub_last;
203                                         }
204                                 }
205                                 i_start = i_end;
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_isDone(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, 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, 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                 case WHEELDOWNMOUSE:
437                 case DOWNARROWKEY:
438                         if (event->val == KM_PRESS) {
439                                 if (tools & TOOL_DOCUMENT) {
440                                         doc_scroll++;
441                                         swallow = 1;
442                                         draw = 1;
443                                 }
444                                 else if (tools & TOOL_SUGG_LIST) {
445                                         SuggItem *sel = texttool_suggest_selected();
446                                         if (!sel) {
447                                                 texttool_suggest_select(texttool_suggest_first());
448                                         }
449                                         else {
450                                                 while (sel && sel != texttool_suggest_last() && sel->next && scroll--) {
451                                                         texttool_suggest_select(sel->next);
452                                                         sel = sel->next;
453                                                 }
454                                         }
455                                         text_pop_suggest_list();
456                                         swallow = 1;
457                                         draw = 1;
458                                 }
459                         }
460                         break;
461                 case PAGEUPKEY:
462                         scroll = SUGG_LIST_SIZE - 1;
463                 case WHEELUPMOUSE:
464                 case UPARROWKEY:
465                         if (event->val == KM_PRESS) {
466                                 if (tools & TOOL_DOCUMENT) {
467                                         if (doc_scroll > 0) doc_scroll--;
468                                         swallow = 1;
469                                         draw = 1;
470                                 }
471                                 else if (tools & TOOL_SUGG_LIST) {
472                                         SuggItem *sel = texttool_suggest_selected();
473                                         while (sel && sel != texttool_suggest_first() && sel->prev && scroll--) {
474                                                 texttool_suggest_select(sel->prev);
475                                                 sel = sel->prev;
476                                         }
477                                         text_pop_suggest_list();
478                                         swallow = 1;
479                                         draw = 1;
480                                 }
481                         }
482                         break;
483                 case RIGHTSHIFTKEY:
484                 case LEFTSHIFTKEY:
485                         break;
486 #if 0
487                 default:
488                         if (tools & TOOL_SUGG_LIST) texttool_suggest_clear(), draw = 1;
489                         if (tools & TOOL_DOCUMENT) texttool_docs_clear(), doc_scroll = 0, draw = 1;
490 #endif
491         }
492
493         if (draw) {
494                 ED_area_tag_redraw(CTX_wm_area(C));
495         }
496
497 //      if (swallow) {
498 //              retval = OPERATOR_RUNNING_MODAL;
499 //      }
500
501         if (texttool_suggest_first()) {
502                 if (retval != OPERATOR_RUNNING_MODAL) {
503                         text_autocomplete_free(C, op);
504                 }
505                 return retval;
506         }
507         else {
508                 text_autocomplete_free(C, op);
509                 return OPERATOR_FINISHED;
510         }
511 }
512
513 static void text_autocomplete_free(bContext *C, wmOperator *op)
514 {
515         GHash *gh = op->customdata;
516         if (gh) {
517                 BLI_ghash_free(gh, NULL, (GHashValFreeFP)MEM_freeN);
518                 op->customdata = NULL;
519         }
520
521         /* other stuff */
522         {
523                 SpaceText *st = CTX_wm_space_text(C);
524                 st->doplugins = FALSE;
525                 texttool_text_clear();
526         }
527 }
528
529 static int text_autocomplete_cancel(bContext *C, wmOperator *op)
530 {
531         text_autocomplete_free(C, op);
532         return OPERATOR_CANCELLED;
533 }
534
535 void TEXT_OT_autocomplete(wmOperatorType *ot)
536 {
537         /* identifiers */
538         ot->name = "Text Auto Complete";
539         ot->description = "Show a list of used text in the open document";
540         ot->idname = "TEXT_OT_autocomplete";
541
542         /* api callbacks */
543         ot->invoke = text_autocomplete_invoke;
544         ot->cancel = text_autocomplete_cancel;
545         ot->modal = text_autocomplete_modal;
546         ot->poll = text_space_edit_poll;
547
548         /* flags */
549         ot->flag = OPTYPE_BLOCKING;
550 }