remove return argument from wmOperatorType->cancel, was only ever returning OPERATOR_...
[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                 case PADENTER:
365                         if (event->val == KM_PRESS) {
366                                 if (tools & TOOL_SUGG_LIST) {
367                                         confirm_suggestion(st->text);
368                                         text_update_line_edited(st->text->curl);
369                                         swallow = 1;
370                                         draw = 1;
371                                 }
372                                 if (tools & TOOL_DOCUMENT) texttool_docs_clear(), doc_scroll = 0, draw = 1;
373                                 retval = OPERATOR_FINISHED;
374                         }
375                         break;
376                 case LEFTARROWKEY:
377                 case BACKSPACEKEY:
378                         if (event->val == KM_PRESS) {
379                                 if (tools & TOOL_SUGG_LIST) {
380                                         if (event->ctrl) {
381                                                 texttool_suggest_clear();
382                                                 retval = OPERATOR_CANCELLED;
383                                         }
384                                         else {
385                                                 /* Work out which char we are about to delete/pass */
386                                                 if (st->text->curl && st->text->curc > 0) {
387                                                         char ch = st->text->curl->line[st->text->curc - 1];
388                                                         if ((ch == '_' || !ispunct(ch)) && !text_check_whitespace(ch)) {
389                                                                 get_suggest_prefix(st->text, -1);
390                                                                 text_pop_suggest_list();
391                                                         }
392                                                         else {
393                                                                 texttool_suggest_clear();
394                                                                 retval = OPERATOR_CANCELLED;
395                                                         }
396                                                 }
397                                                 else {
398                                                         texttool_suggest_clear();
399                                                         retval = OPERATOR_CANCELLED;
400                                                 }
401                                         }
402                                 }
403                                 if (tools & TOOL_DOCUMENT) texttool_docs_clear(), doc_scroll = 0;
404                         }
405                         break;
406                 case RIGHTARROWKEY:
407                         if (event->val == KM_PRESS) {
408                                 if (tools & TOOL_SUGG_LIST) {
409                                         if (event->ctrl) {
410                                                 texttool_suggest_clear();
411                                                 retval = OPERATOR_CANCELLED;
412                                         }
413                                         else {
414                                                 /* Work out which char we are about to pass */
415                                                 if (st->text->curl && st->text->curc < st->text->curl->len) {
416                                                         char ch = st->text->curl->line[st->text->curc + 1];
417                                                         if ((ch == '_' || !ispunct(ch)) && !text_check_whitespace(ch)) {
418                                                                 get_suggest_prefix(st->text, 1);
419                                                                 text_pop_suggest_list();
420                                                         }
421                                                         else {
422                                                                 texttool_suggest_clear();
423                                                                 retval = OPERATOR_CANCELLED;
424                                                         }
425                                                 }
426                                                 else {
427                                                         texttool_suggest_clear();
428                                                         retval = OPERATOR_CANCELLED;
429                                                 }
430                                         }
431                                 }
432                                 if (tools & TOOL_DOCUMENT) texttool_docs_clear(), doc_scroll = 0;
433                         }
434                         break;
435                 case PAGEDOWNKEY:
436                         scroll = SUGG_LIST_SIZE - 1;
437                         /* fall-through */
438                 case WHEELDOWNMOUSE:
439                 case DOWNARROWKEY:
440                         if (event->val == KM_PRESS) {
441                                 if (tools & TOOL_DOCUMENT) {
442                                         doc_scroll++;
443                                         swallow = 1;
444                                         draw = 1;
445                                 }
446                                 else if (tools & TOOL_SUGG_LIST) {
447                                         SuggItem *sel = texttool_suggest_selected();
448                                         if (!sel) {
449                                                 texttool_suggest_select(texttool_suggest_first());
450                                         }
451                                         else {
452                                                 while (sel && sel != texttool_suggest_last() && sel->next && scroll--) {
453                                                         texttool_suggest_select(sel->next);
454                                                         sel = sel->next;
455                                                 }
456                                         }
457                                         text_pop_suggest_list();
458                                         swallow = 1;
459                                         draw = 1;
460                                 }
461                         }
462                         break;
463                 case PAGEUPKEY:
464                         scroll = SUGG_LIST_SIZE - 1;
465                         /* fall-through */
466                 case WHEELUPMOUSE:
467                 case UPARROWKEY:
468                         if (event->val == KM_PRESS) {
469                                 if (tools & TOOL_DOCUMENT) {
470                                         if (doc_scroll > 0) doc_scroll--;
471                                         swallow = 1;
472                                         draw = 1;
473                                 }
474                                 else if (tools & TOOL_SUGG_LIST) {
475                                         SuggItem *sel = texttool_suggest_selected();
476                                         while (sel && sel != texttool_suggest_first() && sel->prev && scroll--) {
477                                                 texttool_suggest_select(sel->prev);
478                                                 sel = sel->prev;
479                                         }
480                                         text_pop_suggest_list();
481                                         swallow = 1;
482                                         draw = 1;
483                                 }
484                         }
485                         break;
486                 case RIGHTSHIFTKEY:
487                 case LEFTSHIFTKEY:
488                         break;
489 #if 0
490                 default:
491                         if (tools & TOOL_SUGG_LIST) texttool_suggest_clear(), draw = 1;
492                         if (tools & TOOL_DOCUMENT) texttool_docs_clear(), doc_scroll = 0, draw = 1;
493 #endif
494         }
495
496         if (draw) {
497                 ED_area_tag_redraw(CTX_wm_area(C));
498         }
499
500 //      if (swallow) {
501 //              retval = OPERATOR_RUNNING_MODAL;
502 //      }
503
504         if (texttool_suggest_first()) {
505                 if (retval != OPERATOR_RUNNING_MODAL) {
506                         text_autocomplete_free(C, op);
507                 }
508                 return retval;
509         }
510         else {
511                 text_autocomplete_free(C, op);
512                 return OPERATOR_FINISHED;
513         }
514 }
515
516 static void text_autocomplete_free(bContext *C, wmOperator *op)
517 {
518         GHash *gh = op->customdata;
519         if (gh) {
520                 BLI_ghash_free(gh, NULL, MEM_freeN);
521                 op->customdata = NULL;
522         }
523
524         /* other stuff */
525         {
526                 SpaceText *st = CTX_wm_space_text(C);
527                 st->doplugins = FALSE;
528                 texttool_text_clear();
529         }
530 }
531
532 static void text_autocomplete_cancel(bContext *C, wmOperator *op)
533 {
534         text_autocomplete_free(C, op);
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 }