aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/elementary/elm_code_widget_text.c
blob: ce1db18ccb6bd51ede900657e7b3ace4cb69a2f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#include "Elementary.h"

#include "elm_code_widget_private.h"

static int
_elm_code_widget_text_line_number_width_get(Eo *obj EINA_UNUSED, Elm_Code_Widget_Data *pd)
{
   int max;

   max = elm_code_file_lines_get(pd->code->file);

   // leave space for 2 digits minimum
   if (max < 10)
     max = 10;

   return floor(log10(max)) + 1;
}

static int
_elm_code_widget_text_left_gutter_width_get(Eo *obj, Elm_Code_Widget_Data *pd)
{
   Elm_Code_Widget *widget;
   int width = 1; // the status icon, for now

   widget = obj;
   if (!widget)
     return width;

   if (pd->show_line_numbers)
     width += _elm_code_widget_text_line_number_width_get(widget, pd);

   return width;
}

static char *
_elm_code_widget_text_multi_get(Elm_Code_Widget *widget, Elm_Code_Widget_Data *pd,
                                unsigned int start_line, unsigned int start_col,
                                unsigned int end_line, unsigned int end_col)
{
   Elm_Code_Line *line;
   char *first, *last, *ret, *ptr;
   const char *newline;
   short newline_len;
   int ret_len;
   unsigned int row, start, end;

   newline = elm_code_file_line_ending_chars_get(pd->code->file, &newline_len);

   line = elm_code_file_line_get(pd->code->file, start_line);
   start = elm_code_widget_line_text_position_for_column_get(widget, line, start_col);
   first = elm_code_line_text_substr(line, start, line->length - start);

   line = elm_code_file_line_get(pd->code->file, end_line);
   end = elm_code_widget_line_text_position_for_column_get(widget, line, end_col + 1);
   last = elm_code_line_text_substr(line, 0, end);

   ret_len = strlen(first) + strlen(last) + newline_len;

   for (row = start_line + 1; row < end_line; row++)
     {
        line = elm_code_file_line_get(pd->code->file, row);
        ret_len += line->length + newline_len;
     }

   ret = malloc(sizeof(char) * (ret_len + 1));

   snprintf(ret, strlen(first) + newline_len + 1, "%s%s", first, newline);

   ptr = ret;
   ptr += strlen(first) + newline_len;

   for (row = start_line + 1; row < end_line; row++)
     {
        line = elm_code_file_line_get(pd->code->file, row);
        if (line->length > 0)
          snprintf(ptr, line->length + 1, "%s", elm_code_line_text_get(line, NULL));

        snprintf(ptr + line->length, newline_len + 1, "%s", newline);
        ptr += line->length + newline_len;
     }
   snprintf(ptr, strlen(last) + 1, "%s", last);

   free(first);
   free(last);
   return ret;
}

static char *
_elm_code_widget_text_single_get(Elm_Code_Widget *widget, Elm_Code_Widget_Data *pd,
                                           unsigned int start_line, unsigned int start_col,
                                           unsigned int end_col)
{
   Elm_Code_Line *line;
   unsigned int start, end;

   line = elm_code_file_line_get(pd->code->file, start_line);
   start = elm_code_widget_line_text_position_for_column_get(widget, line, start_col);
   end = elm_code_widget_line_text_position_for_column_get(widget, line, end_col + 1);

   return elm_code_line_text_substr(line, start, end - start);
}

static char *
_elm_code_widget_text_between_positions_get(Eo *widget, Elm_Code_Widget_Data *pd,
                                            unsigned int start_line, unsigned int start_col,
                                            unsigned int end_line, unsigned int end_col)
{
   if (start_line == end_line)
     return _elm_code_widget_text_single_get(widget, pd, start_line, start_col, end_col);
   else
     return _elm_code_widget_text_multi_get(widget, pd, start_line, start_col, end_line, end_col);
}

static unsigned int
_elm_code_widget_line_text_column_width_to_position(Eo *obj, Elm_Code_Widget_Data *pd EINA_UNUSED, Elm_Code_Line *line, unsigned int position)
{
   Eina_Unicode unicode;
   unsigned int count = 1;
   int index = 0;
   const char *chars;

   if (line->length == 0)
     return 1;

   if (line->modified)
     chars = line->modified;
   else
     chars = line->content;
   if (position > line->length)
     position = line->length;

   while ((unsigned int) index < position)
     {
        unicode = eina_unicode_utf8_next_get(chars, &index);
        if (unicode == 0)
          break;

        if (unicode == '\t')
          count += elm_code_widget_text_tabwidth_at_column_get(obj, count);
        else
          count++;
     }

   return count;
}

static unsigned int
_elm_code_widget_line_text_column_width_get(Eo *obj, Elm_Code_Widget_Data *pd, Elm_Code_Line *line)
{
   if (!line)
     return 0;

   return _elm_code_widget_line_text_column_width_to_position(obj, pd, line, line->length) - 1;
}

static unsigned int
_elm_code_widget_line_text_position_for_column_get(Eo *obj, Elm_Code_Widget_Data *pd EINA_UNUSED, Elm_Code_Line *line, unsigned int column)
{
   Eina_Unicode unicode;
   unsigned int count = 1, position = 0;
   int index = 0;
   const char *chars;

   if (!line || line->length == 0 || column == 1)
     return 0;

   if (line->modified)
     chars = line->modified;
   else
     chars = line->content;

   while ((unsigned int) count <= column && index <= (int) line->length)
     {
        position = (unsigned int) index;
        if (index < (int) line->length)
          unicode = eina_unicode_utf8_next_get(chars, &index);
        else return line->length;

        if (unicode == 0)
          return line->length;
        else if (unicode == '\t')
          count += elm_code_widget_text_tabwidth_at_column_get(obj, count);
        else
          count++;
     }

   return position;
}

static unsigned int
_elm_code_widget_text_tabwidth_at_column_get(Eo *obj EINA_UNUSED, Elm_Code_Widget_Data *pd, unsigned int column)
{
   return pd->tabstop - ((column - 1) % pd->tabstop);
}

static void
_elm_code_widget_text_insert_single(Elm_Code_Widget *widget, Elm_Code *code,
                                    unsigned int col, unsigned int row, const char *text, unsigned int len)
{
   Elm_Code_Widget_Data *pd;
   Elm_Code_Line *line;
   unsigned int position, newcol;

   pd = efl_data_scope_get(widget, ELM_CODE_WIDGET_CLASS);
   line = elm_code_file_line_get(code->file, row);
   position = elm_code_widget_line_text_position_for_column_get(widget, line, col);
   elm_code_line_text_insert(line, position, text, len);

   newcol = elm_code_widget_line_text_column_width_to_position(widget, line, position + len);

   // if we are making a line longer than before then we need to resize
   if (newcol > pd->col_count)
     _elm_code_widget_resize(widget, line);

   elm_obj_code_widget_cursor_position_set(widget, row, newcol);
}

static void
_elm_code_widget_text_insert_multi(Elm_Code_Widget *widget, Elm_Code *code,
                                   unsigned int col, unsigned int row, const char *text, unsigned int len)
{
   Elm_Code_Line *line;
   unsigned int position, newrow, remain;
   int nlpos;
   short nllen;
   char *ptr;

   line = elm_code_file_line_get(code->file, row);
   position = elm_code_widget_line_text_position_for_column_get(widget, line, col);
   elm_code_line_split_at(line, position);

   newrow = row;
   ptr = (char *)text;
   remain = len;
   while ((nlpos = elm_code_text_newlinenpos(ptr, remain, &nllen)) != ELM_CODE_TEXT_NOT_FOUND)
     {
        if (newrow == row)
          _elm_code_widget_text_insert_single(widget, code, col, row, text, nlpos);
        else
          elm_code_file_line_insert(code->file, newrow, ptr, nlpos, NULL);

        remain -= nlpos + nllen;
        ptr += nlpos + nllen;
        newrow++;
     }

   _elm_code_widget_text_insert_single(widget, code, 1, newrow, ptr, len - (ptr - text));
}

void
_elm_code_widget_text_at_cursor_insert_do(Elm_Code_Widget *widget, const char *text, int length, Eina_Bool undo)
{
   Elm_Code *code;
   Elm_Code_Line *line;
   Elm_Code_Widget_Change_Info *change;
   unsigned int row, col, end_row, end_col, curlen, indent;
   const char *curtext, *indent_text;

   if (undo)
     elm_code_widget_selection_delete(widget);

   code = elm_obj_code_widget_code_get(widget);
   elm_obj_code_widget_cursor_position_get(widget, &row, &col);
   line = elm_code_file_line_get(code->file, row);
   if (line == NULL)
     {
        elm_code_file_line_append(code->file, "", 0, NULL);
        row = elm_code_file_lines_get(code->file);
        line = elm_code_file_line_get(code->file, row);
     }
   if (text[0] == '}')
     {
        curtext = elm_code_line_text_get(line, &curlen);

        if (elm_code_text_is_whitespace(curtext, line->length))
          {
             indent_text = elm_code_line_indent_matching_braces_get(line, &indent);
             elm_code_line_text_leading_whitespace_strip(line);

             if (indent > 0)
               elm_code_line_text_insert(line, 0, indent_text, indent);

             col = elm_code_widget_line_text_column_width_to_position(widget, line, indent + 1);
             elm_obj_code_widget_cursor_position_set(widget, row, col);
          }
     }

   if (elm_code_text_newlinenpos(text, length, NULL) == ELM_CODE_TEXT_NOT_FOUND)
     _elm_code_widget_text_insert_single(widget, code, col, row, text, length);
   else
     _elm_code_widget_text_insert_multi(widget, code, col, row, text, length);
   elm_obj_code_widget_cursor_position_get(widget, &end_row, &end_col);

   efl_event_callback_legacy_call(widget, ELM_OBJ_CODE_WIDGET_EVENT_CHANGED_USER, NULL);

   if (undo)
     {
        change = _elm_code_widget_change_create(col, row, end_col - 1, end_row, text, length, EINA_TRUE);
        _elm_code_widget_undo_change_add(widget, change);
        _elm_code_widget_change_free(change);
     }
}

EOLIAN void
_elm_code_widget_text_at_cursor_insert(Elm_Code_Widget *widget, Elm_Code_Widget_Data *pd EINA_UNUSED, const char *text)
{
   _elm_code_widget_text_at_cursor_insert_do(widget, text, strlen(text), EINA_TRUE);
}

void
_elm_code_widget_text_at_cursor_insert_no_undo(Elm_Code_Widget *widget, const char *text, unsigned int length)
{
   _elm_code_widget_text_at_cursor_insert_do(widget, text, length, EINA_FALSE);
}