1 | /*
|
---|
2 | * Copyright (c) 2009 Jiri Svoboda
|
---|
3 | * Copyright (c) 2012 Martin Sucha
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup edit
|
---|
31 | * @brief Text editor.
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 | /**
|
---|
35 | * @file
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <stddef.h>
|
---|
41 | #include <stdbool.h>
|
---|
42 | #include <vfs/vfs.h>
|
---|
43 | #include <io/console.h>
|
---|
44 | #include <io/style.h>
|
---|
45 | #include <io/keycode.h>
|
---|
46 | #include <errno.h>
|
---|
47 | #include <align.h>
|
---|
48 | #include <macros.h>
|
---|
49 | #include <clipboard.h>
|
---|
50 | #include <types/common.h>
|
---|
51 |
|
---|
52 | #include "sheet.h"
|
---|
53 | #include "search.h"
|
---|
54 |
|
---|
55 | enum redraw_flags {
|
---|
56 | REDRAW_TEXT = (1 << 0),
|
---|
57 | REDRAW_ROW = (1 << 1),
|
---|
58 | REDRAW_STATUS = (1 << 2),
|
---|
59 | REDRAW_CARET = (1 << 3)
|
---|
60 | };
|
---|
61 |
|
---|
62 | /** Pane
|
---|
63 | *
|
---|
64 | * A rectangular area of the screen used to edit a document. Different
|
---|
65 | * panes can be possibly used to edit the same document.
|
---|
66 | */
|
---|
67 | typedef struct {
|
---|
68 | /* Pane dimensions */
|
---|
69 | int rows, columns;
|
---|
70 |
|
---|
71 | /* Position of the visible area */
|
---|
72 | int sh_row, sh_column;
|
---|
73 |
|
---|
74 | /** Bitmask of components that need redrawing */
|
---|
75 | enum redraw_flags rflags;
|
---|
76 |
|
---|
77 | /** Current position of the caret */
|
---|
78 | tag_t caret_pos;
|
---|
79 |
|
---|
80 | /** Start of selection */
|
---|
81 | tag_t sel_start;
|
---|
82 |
|
---|
83 | /** Active keyboard modifiers */
|
---|
84 | keymod_t keymod;
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Ideal column where the caret should try to get. This is used
|
---|
88 | * for maintaining the same column during vertical movement.
|
---|
89 | */
|
---|
90 | int ideal_column;
|
---|
91 |
|
---|
92 | char *previous_search;
|
---|
93 | bool previous_search_reverse;
|
---|
94 | } pane_t;
|
---|
95 |
|
---|
96 | /** Document
|
---|
97 | *
|
---|
98 | * Associates a sheet with a file where it can be saved to.
|
---|
99 | */
|
---|
100 | typedef struct {
|
---|
101 | char *file_name;
|
---|
102 | sheet_t *sh;
|
---|
103 | } doc_t;
|
---|
104 |
|
---|
105 | static console_ctrl_t *con;
|
---|
106 | static doc_t doc;
|
---|
107 | static bool done;
|
---|
108 | static pane_t pane;
|
---|
109 | static bool cursor_visible;
|
---|
110 |
|
---|
111 | static sysarg_t scr_rows;
|
---|
112 | static sysarg_t scr_columns;
|
---|
113 |
|
---|
114 | #define ROW_BUF_SIZE 4096
|
---|
115 | #define BUF_SIZE 64
|
---|
116 | #define TAB_WIDTH 8
|
---|
117 |
|
---|
118 | /** Maximum filename length that can be entered. */
|
---|
119 | #define INFNAME_MAX_LEN 128
|
---|
120 |
|
---|
121 | static void cursor_show(void);
|
---|
122 | static void cursor_hide(void);
|
---|
123 | static void cursor_setvis(bool visible);
|
---|
124 |
|
---|
125 | static void key_handle_press(kbd_event_t *ev);
|
---|
126 | static void key_handle_unmod(kbd_event_t const *ev);
|
---|
127 | static void key_handle_ctrl(kbd_event_t const *ev);
|
---|
128 | static void key_handle_shift(kbd_event_t const *ev);
|
---|
129 | static void key_handle_shift_ctrl(kbd_event_t const *ev);
|
---|
130 | static void key_handle_movement(unsigned int key, bool shift);
|
---|
131 |
|
---|
132 | static void pos_handle(pos_event_t *ev);
|
---|
133 |
|
---|
134 | static int file_save(char const *fname);
|
---|
135 | static void file_save_as(void);
|
---|
136 | static int file_insert(char *fname);
|
---|
137 | static int file_save_range(char const *fname, spt_t const *spos,
|
---|
138 | spt_t const *epos);
|
---|
139 | static char *range_get_str(spt_t const *spos, spt_t const *epos);
|
---|
140 |
|
---|
141 | static char *prompt(char const *prompt, char const *init_value);
|
---|
142 |
|
---|
143 | static void pane_text_display(void);
|
---|
144 | static void pane_row_display(void);
|
---|
145 | static void pane_row_range_display(int r0, int r1);
|
---|
146 | static void pane_status_display(void);
|
---|
147 | static void pane_caret_display(void);
|
---|
148 |
|
---|
149 | static void insert_char(wchar_t c);
|
---|
150 | static void delete_char_before(void);
|
---|
151 | static void delete_char_after(void);
|
---|
152 | static void caret_update(void);
|
---|
153 | static void caret_move_relative(int drow, int dcolumn, enum dir_spec align_dir, bool select);
|
---|
154 | static void caret_move_absolute(int row, int column, enum dir_spec align_dir, bool select);
|
---|
155 | static void caret_move(spt_t spt, bool select, bool update_ideal_column);
|
---|
156 | static void caret_move_word_left(bool select);
|
---|
157 | static void caret_move_word_right(bool select);
|
---|
158 | static void caret_go_to_line_ask(void);
|
---|
159 |
|
---|
160 | static bool selection_active(void);
|
---|
161 | static void selection_sel_all(void);
|
---|
162 | static void selection_sel_range(spt_t pa, spt_t pb);
|
---|
163 | static void selection_get_points(spt_t *pa, spt_t *pb);
|
---|
164 | static void selection_delete(void);
|
---|
165 | static void selection_copy(void);
|
---|
166 | static void insert_clipboard_data(void);
|
---|
167 |
|
---|
168 | static void search(char *pattern, bool reverse);
|
---|
169 | static void search_prompt(bool reverse);
|
---|
170 | static void search_repeat(void);
|
---|
171 |
|
---|
172 | static void pt_get_sof(spt_t *pt);
|
---|
173 | static void pt_get_eof(spt_t *pt);
|
---|
174 | static void pt_get_sol(spt_t *cpt, spt_t *spt);
|
---|
175 | static void pt_get_eol(spt_t *cpt, spt_t *ept);
|
---|
176 | static bool pt_is_word_beginning(spt_t *pt);
|
---|
177 | static bool pt_is_delimiter(spt_t *pt);
|
---|
178 | static bool pt_is_punctuation(spt_t *pt);
|
---|
179 | static spt_t pt_find_word_left(spt_t spt);
|
---|
180 | static spt_t pt_find_word_left(spt_t spt);
|
---|
181 |
|
---|
182 | static int tag_cmp(tag_t const *a, tag_t const *b);
|
---|
183 | static int spt_cmp(spt_t const *a, spt_t const *b);
|
---|
184 | static int coord_cmp(coord_t const *a, coord_t const *b);
|
---|
185 |
|
---|
186 | static void status_display(char const *str);
|
---|
187 |
|
---|
188 |
|
---|
189 | int main(int argc, char *argv[])
|
---|
190 | {
|
---|
191 | cons_event_t ev;
|
---|
192 | bool new_file;
|
---|
193 | int rc;
|
---|
194 |
|
---|
195 | con = console_init(stdin, stdout);
|
---|
196 | console_clear(con);
|
---|
197 |
|
---|
198 | console_get_size(con, &scr_columns, &scr_rows);
|
---|
199 |
|
---|
200 | pane.rows = scr_rows - 1;
|
---|
201 | pane.columns = scr_columns;
|
---|
202 | pane.sh_row = 1;
|
---|
203 | pane.sh_column = 1;
|
---|
204 |
|
---|
205 | /* Start with an empty sheet. */
|
---|
206 | rc = sheet_create(&doc.sh);
|
---|
207 | if (rc != EOK) {
|
---|
208 | printf("Out of memory.\n");
|
---|
209 | return -1;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /* Place caret at the beginning of file. */
|
---|
213 | spt_t sof;
|
---|
214 | pt_get_sof(&sof);
|
---|
215 | sheet_place_tag(doc.sh, &sof, &pane.caret_pos);
|
---|
216 | pane.ideal_column = 1;
|
---|
217 |
|
---|
218 | if (argc == 2) {
|
---|
219 | doc.file_name = str_dup(argv[1]);
|
---|
220 | } else if (argc > 1) {
|
---|
221 | printf("Invalid arguments.\n");
|
---|
222 | return -2;
|
---|
223 | } else {
|
---|
224 | doc.file_name = NULL;
|
---|
225 | }
|
---|
226 |
|
---|
227 | new_file = false;
|
---|
228 |
|
---|
229 | if (doc.file_name == NULL || file_insert(doc.file_name) != EOK)
|
---|
230 | new_file = true;
|
---|
231 |
|
---|
232 | /* Place selection start tag. */
|
---|
233 | sheet_place_tag(doc.sh, &sof, &pane.sel_start);
|
---|
234 |
|
---|
235 | /* Move to beginning of file. */
|
---|
236 | pt_get_sof(&sof);
|
---|
237 | caret_move(sof, true, true);
|
---|
238 |
|
---|
239 | /* Initial display */
|
---|
240 | cursor_visible = true;
|
---|
241 |
|
---|
242 | cursor_hide();
|
---|
243 | console_clear(con);
|
---|
244 | pane_text_display();
|
---|
245 | pane_status_display();
|
---|
246 | if (new_file && doc.file_name != NULL)
|
---|
247 | status_display("File not found. Starting empty file.");
|
---|
248 | pane_caret_display();
|
---|
249 | cursor_show();
|
---|
250 |
|
---|
251 | done = false;
|
---|
252 |
|
---|
253 | while (!done) {
|
---|
254 | console_get_event(con, &ev);
|
---|
255 | pane.rflags = 0;
|
---|
256 |
|
---|
257 | switch (ev.type) {
|
---|
258 | case CEV_KEY:
|
---|
259 | pane.keymod = ev.ev.key.mods;
|
---|
260 | if (ev.ev.key.type == KEY_PRESS)
|
---|
261 | key_handle_press(&ev.ev.key);
|
---|
262 | break;
|
---|
263 | case CEV_POS:
|
---|
264 | pos_handle(&ev.ev.pos);
|
---|
265 | break;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /* Redraw as necessary. */
|
---|
269 |
|
---|
270 | cursor_hide();
|
---|
271 |
|
---|
272 | if (pane.rflags & REDRAW_TEXT)
|
---|
273 | pane_text_display();
|
---|
274 | if (pane.rflags & REDRAW_ROW)
|
---|
275 | pane_row_display();
|
---|
276 | if (pane.rflags & REDRAW_STATUS)
|
---|
277 | pane_status_display();
|
---|
278 | if (pane.rflags & REDRAW_CARET)
|
---|
279 | pane_caret_display();
|
---|
280 |
|
---|
281 | cursor_show();
|
---|
282 | }
|
---|
283 |
|
---|
284 | console_clear(con);
|
---|
285 |
|
---|
286 | return 0;
|
---|
287 | }
|
---|
288 |
|
---|
289 | /* Handle key press. */
|
---|
290 | static void key_handle_press(kbd_event_t *ev)
|
---|
291 | {
|
---|
292 | if (((ev->mods & KM_ALT) == 0) &&
|
---|
293 | ((ev->mods & KM_SHIFT) == 0) &&
|
---|
294 | (ev->mods & KM_CTRL) != 0) {
|
---|
295 | key_handle_ctrl(ev);
|
---|
296 | } else if (((ev->mods & KM_ALT) == 0) &&
|
---|
297 | ((ev->mods & KM_CTRL) == 0) &&
|
---|
298 | (ev->mods & KM_SHIFT) != 0) {
|
---|
299 | key_handle_shift(ev);
|
---|
300 | } else if (((ev->mods & KM_ALT) == 0) &&
|
---|
301 | ((ev->mods & KM_CTRL) != 0) &&
|
---|
302 | (ev->mods & KM_SHIFT) != 0) {
|
---|
303 | key_handle_shift_ctrl(ev);
|
---|
304 | } else if ((ev->mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) {
|
---|
305 | key_handle_unmod(ev);
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 | static void cursor_show(void)
|
---|
310 | {
|
---|
311 | cursor_setvis(true);
|
---|
312 | }
|
---|
313 |
|
---|
314 | static void cursor_hide(void)
|
---|
315 | {
|
---|
316 | cursor_setvis(false);
|
---|
317 | }
|
---|
318 |
|
---|
319 | static void cursor_setvis(bool visible)
|
---|
320 | {
|
---|
321 | if (cursor_visible != visible) {
|
---|
322 | console_cursor_visibility(con, visible);
|
---|
323 | cursor_visible = visible;
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | /** Handle key without modifier. */
|
---|
328 | static void key_handle_unmod(kbd_event_t const *ev)
|
---|
329 | {
|
---|
330 | switch (ev->key) {
|
---|
331 | case KC_ENTER:
|
---|
332 | selection_delete();
|
---|
333 | insert_char('\n');
|
---|
334 | caret_update();
|
---|
335 | break;
|
---|
336 | case KC_LEFT:
|
---|
337 | case KC_RIGHT:
|
---|
338 | case KC_UP:
|
---|
339 | case KC_DOWN:
|
---|
340 | case KC_HOME:
|
---|
341 | case KC_END:
|
---|
342 | case KC_PAGE_UP:
|
---|
343 | case KC_PAGE_DOWN:
|
---|
344 | key_handle_movement(ev->key, false);
|
---|
345 | break;
|
---|
346 | case KC_BACKSPACE:
|
---|
347 | if (selection_active())
|
---|
348 | selection_delete();
|
---|
349 | else
|
---|
350 | delete_char_before();
|
---|
351 | caret_update();
|
---|
352 | break;
|
---|
353 | case KC_DELETE:
|
---|
354 | if (selection_active())
|
---|
355 | selection_delete();
|
---|
356 | else
|
---|
357 | delete_char_after();
|
---|
358 | caret_update();
|
---|
359 | break;
|
---|
360 | default:
|
---|
361 | if (ev->c >= 32 || ev->c == '\t') {
|
---|
362 | selection_delete();
|
---|
363 | insert_char(ev->c);
|
---|
364 | caret_update();
|
---|
365 | }
|
---|
366 | break;
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | /** Handle Shift-key combination. */
|
---|
371 | static void key_handle_shift(kbd_event_t const *ev)
|
---|
372 | {
|
---|
373 | switch (ev->key) {
|
---|
374 | case KC_LEFT:
|
---|
375 | case KC_RIGHT:
|
---|
376 | case KC_UP:
|
---|
377 | case KC_DOWN:
|
---|
378 | case KC_HOME:
|
---|
379 | case KC_END:
|
---|
380 | case KC_PAGE_UP:
|
---|
381 | case KC_PAGE_DOWN:
|
---|
382 | key_handle_movement(ev->key, true);
|
---|
383 | break;
|
---|
384 | default:
|
---|
385 | if (ev->c >= 32 || ev->c == '\t') {
|
---|
386 | selection_delete();
|
---|
387 | insert_char(ev->c);
|
---|
388 | caret_update();
|
---|
389 | }
|
---|
390 | break;
|
---|
391 | }
|
---|
392 | }
|
---|
393 |
|
---|
394 | /** Handle Ctrl-key combination. */
|
---|
395 | static void key_handle_ctrl(kbd_event_t const *ev)
|
---|
396 | {
|
---|
397 | spt_t pt;
|
---|
398 | switch (ev->key) {
|
---|
399 | case KC_Q:
|
---|
400 | done = true;
|
---|
401 | break;
|
---|
402 | case KC_S:
|
---|
403 | if (doc.file_name != NULL)
|
---|
404 | file_save(doc.file_name);
|
---|
405 | else
|
---|
406 | file_save_as();
|
---|
407 | break;
|
---|
408 | case KC_E:
|
---|
409 | file_save_as();
|
---|
410 | break;
|
---|
411 | case KC_C:
|
---|
412 | selection_copy();
|
---|
413 | break;
|
---|
414 | case KC_V:
|
---|
415 | selection_delete();
|
---|
416 | insert_clipboard_data();
|
---|
417 | pane.rflags |= REDRAW_TEXT;
|
---|
418 | caret_update();
|
---|
419 | break;
|
---|
420 | case KC_X:
|
---|
421 | selection_copy();
|
---|
422 | selection_delete();
|
---|
423 | pane.rflags |= REDRAW_TEXT;
|
---|
424 | caret_update();
|
---|
425 | break;
|
---|
426 | case KC_A:
|
---|
427 | selection_sel_all();
|
---|
428 | break;
|
---|
429 | case KC_RIGHT:
|
---|
430 | caret_move_word_right(false);
|
---|
431 | break;
|
---|
432 | case KC_LEFT:
|
---|
433 | caret_move_word_left(false);
|
---|
434 | break;
|
---|
435 | case KC_L:
|
---|
436 | caret_go_to_line_ask();
|
---|
437 | break;
|
---|
438 | case KC_F:
|
---|
439 | search_prompt(false);
|
---|
440 | break;
|
---|
441 | case KC_N:
|
---|
442 | search_repeat();
|
---|
443 | break;
|
---|
444 | case KC_HOME:
|
---|
445 | pt_get_sof(&pt);
|
---|
446 | caret_move(pt, false, true);
|
---|
447 | break;
|
---|
448 | case KC_END:
|
---|
449 | pt_get_eof(&pt);
|
---|
450 | caret_move(pt, false, true);
|
---|
451 | break;
|
---|
452 | default:
|
---|
453 | break;
|
---|
454 | }
|
---|
455 | }
|
---|
456 |
|
---|
457 | static void key_handle_shift_ctrl(kbd_event_t const *ev)
|
---|
458 | {
|
---|
459 | spt_t pt;
|
---|
460 | switch(ev->key) {
|
---|
461 | case KC_LEFT:
|
---|
462 | caret_move_word_left(true);
|
---|
463 | break;
|
---|
464 | case KC_RIGHT:
|
---|
465 | caret_move_word_right(true);
|
---|
466 | break;
|
---|
467 | case KC_F:
|
---|
468 | search_prompt(true);
|
---|
469 | break;
|
---|
470 | case KC_HOME:
|
---|
471 | pt_get_sof(&pt);
|
---|
472 | caret_move(pt, true, true);
|
---|
473 | break;
|
---|
474 | case KC_END:
|
---|
475 | pt_get_eof(&pt);
|
---|
476 | caret_move(pt, true, true);
|
---|
477 | break;
|
---|
478 | default:
|
---|
479 | break;
|
---|
480 | }
|
---|
481 | }
|
---|
482 |
|
---|
483 | static void pos_handle(pos_event_t *ev)
|
---|
484 | {
|
---|
485 | coord_t bc;
|
---|
486 | spt_t pt;
|
---|
487 | bool select;
|
---|
488 |
|
---|
489 | if (ev->type == POS_PRESS && ev->vpos < (unsigned)pane.rows) {
|
---|
490 | bc.row = pane.sh_row + ev->vpos;
|
---|
491 | bc.column = pane.sh_column + ev->hpos;
|
---|
492 | sheet_get_cell_pt(doc.sh, &bc, dir_before, &pt);
|
---|
493 |
|
---|
494 | select = (pane.keymod & KM_SHIFT) != 0;
|
---|
495 |
|
---|
496 | caret_move(pt, select, true);
|
---|
497 | }
|
---|
498 | }
|
---|
499 |
|
---|
500 | /** Move caret while preserving or resetting selection. */
|
---|
501 | static void caret_move(spt_t new_caret_pt, bool select, bool update_ideal_column)
|
---|
502 | {
|
---|
503 | spt_t old_caret_pt, old_sel_pt;
|
---|
504 | coord_t c_old, c_new;
|
---|
505 | bool had_sel;
|
---|
506 |
|
---|
507 | /* Check if we had selection before. */
|
---|
508 | tag_get_pt(&pane.caret_pos, &old_caret_pt);
|
---|
509 | tag_get_pt(&pane.sel_start, &old_sel_pt);
|
---|
510 | had_sel = !spt_equal(&old_caret_pt, &old_sel_pt);
|
---|
511 |
|
---|
512 | /* Place tag of the caret */
|
---|
513 | sheet_remove_tag(doc.sh, &pane.caret_pos);
|
---|
514 | sheet_place_tag(doc.sh, &new_caret_pt, &pane.caret_pos);
|
---|
515 |
|
---|
516 | if (select == false) {
|
---|
517 | /* Move sel_start to the same point as caret. */
|
---|
518 | sheet_remove_tag(doc.sh, &pane.sel_start);
|
---|
519 | sheet_place_tag(doc.sh, &new_caret_pt, &pane.sel_start);
|
---|
520 | }
|
---|
521 |
|
---|
522 | spt_get_coord(&new_caret_pt, &c_new);
|
---|
523 | if (select) {
|
---|
524 | spt_get_coord(&old_caret_pt, &c_old);
|
---|
525 |
|
---|
526 | if (c_old.row == c_new.row)
|
---|
527 | pane.rflags |= REDRAW_ROW;
|
---|
528 | else
|
---|
529 | pane.rflags |= REDRAW_TEXT;
|
---|
530 |
|
---|
531 | } else if (had_sel == true) {
|
---|
532 | /* Redraw because text was unselected. */
|
---|
533 | pane.rflags |= REDRAW_TEXT;
|
---|
534 | }
|
---|
535 |
|
---|
536 | if (update_ideal_column)
|
---|
537 | pane.ideal_column = c_new.column;
|
---|
538 |
|
---|
539 | caret_update();
|
---|
540 | }
|
---|
541 |
|
---|
542 | static void key_handle_movement(unsigned int key, bool select)
|
---|
543 | {
|
---|
544 | spt_t pt;
|
---|
545 | switch (key) {
|
---|
546 | case KC_LEFT:
|
---|
547 | caret_move_relative(0, -1, dir_before, select);
|
---|
548 | break;
|
---|
549 | case KC_RIGHT:
|
---|
550 | caret_move_relative(0, 0, dir_after, select);
|
---|
551 | break;
|
---|
552 | case KC_UP:
|
---|
553 | caret_move_relative(-1, 0, dir_before, select);
|
---|
554 | break;
|
---|
555 | case KC_DOWN:
|
---|
556 | caret_move_relative(+1, 0, dir_before, select);
|
---|
557 | break;
|
---|
558 | case KC_HOME:
|
---|
559 | tag_get_pt(&pane.caret_pos, &pt);
|
---|
560 | pt_get_sol(&pt, &pt);
|
---|
561 | caret_move(pt, select, true);
|
---|
562 | break;
|
---|
563 | case KC_END:
|
---|
564 | tag_get_pt(&pane.caret_pos, &pt);
|
---|
565 | pt_get_eol(&pt, &pt);
|
---|
566 | caret_move(pt, select, true);
|
---|
567 | break;
|
---|
568 | case KC_PAGE_UP:
|
---|
569 | caret_move_relative(-pane.rows, 0, dir_before, select);
|
---|
570 | break;
|
---|
571 | case KC_PAGE_DOWN:
|
---|
572 | caret_move_relative(+pane.rows, 0, dir_before, select);
|
---|
573 | break;
|
---|
574 | default:
|
---|
575 | break;
|
---|
576 | }
|
---|
577 | }
|
---|
578 |
|
---|
579 | /** Save the document. */
|
---|
580 | static int file_save(char const *fname)
|
---|
581 | {
|
---|
582 | spt_t sp, ep;
|
---|
583 | int rc;
|
---|
584 |
|
---|
585 | status_display("Saving...");
|
---|
586 | pt_get_sof(&sp);
|
---|
587 | pt_get_eof(&ep);
|
---|
588 |
|
---|
589 | rc = file_save_range(fname, &sp, &ep);
|
---|
590 |
|
---|
591 | switch (rc) {
|
---|
592 | case EINVAL:
|
---|
593 | status_display("Error opening file!");
|
---|
594 | break;
|
---|
595 | case EIO:
|
---|
596 | status_display("Error writing data!");
|
---|
597 | break;
|
---|
598 | default:
|
---|
599 | status_display("File saved.");
|
---|
600 | break;
|
---|
601 | }
|
---|
602 |
|
---|
603 | return rc;
|
---|
604 | }
|
---|
605 |
|
---|
606 | /** Change document name and save. */
|
---|
607 | static void file_save_as(void)
|
---|
608 | {
|
---|
609 | const char *old_fname = (doc.file_name != NULL) ? doc.file_name : "";
|
---|
610 | char *fname;
|
---|
611 |
|
---|
612 | fname = prompt("Save As", old_fname);
|
---|
613 | if (fname == NULL) {
|
---|
614 | status_display("Save cancelled.");
|
---|
615 | return;
|
---|
616 | }
|
---|
617 |
|
---|
618 | int rc = file_save(fname);
|
---|
619 | if (rc != EOK)
|
---|
620 | return;
|
---|
621 |
|
---|
622 | if (doc.file_name != NULL)
|
---|
623 | free(doc.file_name);
|
---|
624 | doc.file_name = fname;
|
---|
625 | }
|
---|
626 |
|
---|
627 | /** Ask for a string. */
|
---|
628 | static char *prompt(char const *prompt, char const *init_value)
|
---|
629 | {
|
---|
630 | cons_event_t ev;
|
---|
631 | kbd_event_t *kev;
|
---|
632 | char *str;
|
---|
633 | wchar_t buffer[INFNAME_MAX_LEN + 1];
|
---|
634 | int max_len;
|
---|
635 | int nc;
|
---|
636 | bool done;
|
---|
637 |
|
---|
638 | asprintf(&str, "%s: %s", prompt, init_value);
|
---|
639 | status_display(str);
|
---|
640 | console_set_pos(con, 1 + str_length(str), scr_rows - 1);
|
---|
641 | free(str);
|
---|
642 |
|
---|
643 | console_set_style(con, STYLE_INVERTED);
|
---|
644 |
|
---|
645 | max_len = min(INFNAME_MAX_LEN, scr_columns - 4 - str_length(prompt));
|
---|
646 | str_to_wstr(buffer, max_len + 1, init_value);
|
---|
647 | nc = wstr_length(buffer);
|
---|
648 | done = false;
|
---|
649 |
|
---|
650 | while (!done) {
|
---|
651 | console_get_event(con, &ev);
|
---|
652 |
|
---|
653 | if (ev.type == CEV_KEY && ev.ev.key.type == KEY_PRESS) {
|
---|
654 | kev = &ev.ev.key;
|
---|
655 |
|
---|
656 | /* Handle key press. */
|
---|
657 | if (((kev->mods & KM_ALT) == 0) &&
|
---|
658 | (kev->mods & KM_CTRL) != 0) {
|
---|
659 | ;
|
---|
660 | } else if ((kev->mods & (KM_CTRL | KM_ALT)) == 0) {
|
---|
661 | switch (kev->key) {
|
---|
662 | case KC_ESCAPE:
|
---|
663 | return NULL;
|
---|
664 | case KC_BACKSPACE:
|
---|
665 | if (nc > 0) {
|
---|
666 | putchar('\b');
|
---|
667 | console_flush(con);
|
---|
668 | --nc;
|
---|
669 | }
|
---|
670 | break;
|
---|
671 | case KC_ENTER:
|
---|
672 | done = true;
|
---|
673 | break;
|
---|
674 | default:
|
---|
675 | if (kev->c >= 32 && nc < max_len) {
|
---|
676 | putchar(kev->c);
|
---|
677 | console_flush(con);
|
---|
678 | buffer[nc++] = kev->c;
|
---|
679 | }
|
---|
680 | break;
|
---|
681 | }
|
---|
682 | }
|
---|
683 | }
|
---|
684 | }
|
---|
685 |
|
---|
686 | buffer[nc] = '\0';
|
---|
687 | str = wstr_to_astr(buffer);
|
---|
688 |
|
---|
689 | console_set_style(con, STYLE_NORMAL);
|
---|
690 |
|
---|
691 | return str;
|
---|
692 | }
|
---|
693 |
|
---|
694 | /** Insert file at caret position.
|
---|
695 | *
|
---|
696 | * Reads in the contents of a file and inserts them at the current position
|
---|
697 | * of the caret.
|
---|
698 | */
|
---|
699 | static int file_insert(char *fname)
|
---|
700 | {
|
---|
701 | FILE *f;
|
---|
702 | wchar_t c;
|
---|
703 | char buf[BUF_SIZE];
|
---|
704 | int bcnt;
|
---|
705 | int n_read;
|
---|
706 | size_t off;
|
---|
707 |
|
---|
708 | f = fopen(fname, "rt");
|
---|
709 | if (f == NULL)
|
---|
710 | return EINVAL;
|
---|
711 |
|
---|
712 | bcnt = 0;
|
---|
713 |
|
---|
714 | while (true) {
|
---|
715 | if (bcnt < STR_BOUNDS(1)) {
|
---|
716 | n_read = fread(buf + bcnt, 1, BUF_SIZE - bcnt, f);
|
---|
717 | bcnt += n_read;
|
---|
718 | }
|
---|
719 |
|
---|
720 | off = 0;
|
---|
721 | c = str_decode(buf, &off, bcnt);
|
---|
722 | if (c == '\0')
|
---|
723 | break;
|
---|
724 |
|
---|
725 | bcnt -= off;
|
---|
726 | memcpy(buf, buf + off, bcnt);
|
---|
727 |
|
---|
728 | insert_char(c);
|
---|
729 | }
|
---|
730 |
|
---|
731 | fclose(f);
|
---|
732 |
|
---|
733 | return EOK;
|
---|
734 | }
|
---|
735 |
|
---|
736 | /** Save a range of text into a file. */
|
---|
737 | static int file_save_range(char const *fname, spt_t const *spos,
|
---|
738 | spt_t const *epos)
|
---|
739 | {
|
---|
740 | FILE *f;
|
---|
741 | char buf[BUF_SIZE];
|
---|
742 | spt_t sp, bep;
|
---|
743 | size_t bytes, n_written;
|
---|
744 |
|
---|
745 | f = fopen(fname, "wt");
|
---|
746 | if (f == NULL)
|
---|
747 | return EINVAL;
|
---|
748 |
|
---|
749 | sp = *spos;
|
---|
750 |
|
---|
751 | do {
|
---|
752 | sheet_copy_out(doc.sh, &sp, epos, buf, BUF_SIZE, &bep);
|
---|
753 | bytes = str_size(buf);
|
---|
754 |
|
---|
755 | n_written = fwrite(buf, 1, bytes, f);
|
---|
756 | if (n_written != bytes) {
|
---|
757 | return EIO;
|
---|
758 | }
|
---|
759 |
|
---|
760 | sp = bep;
|
---|
761 | } while (!spt_equal(&bep, epos));
|
---|
762 |
|
---|
763 | if (fclose(f) != EOK)
|
---|
764 | return EIO;
|
---|
765 |
|
---|
766 | return EOK;
|
---|
767 | }
|
---|
768 |
|
---|
769 | /** Return contents of range as a new string. */
|
---|
770 | static char *range_get_str(spt_t const *spos, spt_t const *epos)
|
---|
771 | {
|
---|
772 | char *buf;
|
---|
773 | spt_t sp, bep;
|
---|
774 | size_t bytes;
|
---|
775 | size_t buf_size, bpos;
|
---|
776 |
|
---|
777 | buf_size = 1;
|
---|
778 |
|
---|
779 | buf = malloc(buf_size);
|
---|
780 | if (buf == NULL)
|
---|
781 | return NULL;
|
---|
782 |
|
---|
783 | bpos = 0;
|
---|
784 | sp = *spos;
|
---|
785 |
|
---|
786 | while (true) {
|
---|
787 | sheet_copy_out(doc.sh, &sp, epos, &buf[bpos], buf_size - bpos,
|
---|
788 | &bep);
|
---|
789 | bytes = str_size(&buf[bpos]);
|
---|
790 | bpos += bytes;
|
---|
791 | sp = bep;
|
---|
792 |
|
---|
793 | if (spt_equal(&bep, epos))
|
---|
794 | break;
|
---|
795 |
|
---|
796 | buf_size *= 2;
|
---|
797 | buf = realloc(buf, buf_size);
|
---|
798 | if (buf == NULL)
|
---|
799 | return NULL;
|
---|
800 | }
|
---|
801 |
|
---|
802 | return buf;
|
---|
803 | }
|
---|
804 |
|
---|
805 | static void pane_text_display(void)
|
---|
806 | {
|
---|
807 | int sh_rows, rows;
|
---|
808 |
|
---|
809 | sheet_get_num_rows(doc.sh, &sh_rows);
|
---|
810 | rows = min(sh_rows - pane.sh_row + 1, pane.rows);
|
---|
811 |
|
---|
812 | /* Draw rows from the sheet. */
|
---|
813 |
|
---|
814 | console_set_pos(con, 0, 0);
|
---|
815 | pane_row_range_display(0, rows);
|
---|
816 |
|
---|
817 | /* Clear the remaining rows if file is short. */
|
---|
818 |
|
---|
819 | int i;
|
---|
820 | sysarg_t j;
|
---|
821 | for (i = rows; i < pane.rows; ++i) {
|
---|
822 | console_set_pos(con, 0, i);
|
---|
823 | for (j = 0; j < scr_columns; ++j)
|
---|
824 | putchar(' ');
|
---|
825 | console_flush(con);
|
---|
826 | }
|
---|
827 |
|
---|
828 | pane.rflags |= (REDRAW_STATUS | REDRAW_CARET);
|
---|
829 | pane.rflags &= ~REDRAW_ROW;
|
---|
830 | }
|
---|
831 |
|
---|
832 | /** Display just the row where the caret is. */
|
---|
833 | static void pane_row_display(void)
|
---|
834 | {
|
---|
835 | spt_t caret_pt;
|
---|
836 | coord_t coord;
|
---|
837 | int ridx;
|
---|
838 |
|
---|
839 | tag_get_pt(&pane.caret_pos, &caret_pt);
|
---|
840 | spt_get_coord(&caret_pt, &coord);
|
---|
841 |
|
---|
842 | ridx = coord.row - pane.sh_row;
|
---|
843 | pane_row_range_display(ridx, ridx + 1);
|
---|
844 | pane.rflags |= (REDRAW_STATUS | REDRAW_CARET);
|
---|
845 | }
|
---|
846 |
|
---|
847 | static void pane_row_range_display(int r0, int r1)
|
---|
848 | {
|
---|
849 | int i, j, fill;
|
---|
850 | spt_t rb, re, dep, pt;
|
---|
851 | coord_t rbc, rec;
|
---|
852 | char row_buf[ROW_BUF_SIZE];
|
---|
853 | wchar_t c;
|
---|
854 | size_t pos, size;
|
---|
855 | int s_column;
|
---|
856 | coord_t csel_start, csel_end, ctmp;
|
---|
857 |
|
---|
858 | /* Determine selection start and end. */
|
---|
859 |
|
---|
860 | tag_get_pt(&pane.sel_start, &pt);
|
---|
861 | spt_get_coord(&pt, &csel_start);
|
---|
862 |
|
---|
863 | tag_get_pt(&pane.caret_pos, &pt);
|
---|
864 | spt_get_coord(&pt, &csel_end);
|
---|
865 |
|
---|
866 | if (coord_cmp(&csel_start, &csel_end) > 0) {
|
---|
867 | ctmp = csel_start;
|
---|
868 | csel_start = csel_end;
|
---|
869 | csel_end = ctmp;
|
---|
870 | }
|
---|
871 |
|
---|
872 | /* Draw rows from the sheet. */
|
---|
873 |
|
---|
874 | console_set_pos(con, 0, 0);
|
---|
875 | for (i = r0; i < r1; ++i) {
|
---|
876 | /* Starting point for row display */
|
---|
877 | rbc.row = pane.sh_row + i;
|
---|
878 | rbc.column = pane.sh_column;
|
---|
879 | sheet_get_cell_pt(doc.sh, &rbc, dir_before, &rb);
|
---|
880 |
|
---|
881 | /* Ending point for row display */
|
---|
882 | rec.row = pane.sh_row + i;
|
---|
883 | rec.column = pane.sh_column + pane.columns;
|
---|
884 | sheet_get_cell_pt(doc.sh, &rec, dir_before, &re);
|
---|
885 |
|
---|
886 | /* Copy the text of the row to the buffer. */
|
---|
887 | sheet_copy_out(doc.sh, &rb, &re, row_buf, ROW_BUF_SIZE, &dep);
|
---|
888 |
|
---|
889 | /* Display text from the buffer. */
|
---|
890 |
|
---|
891 | if (coord_cmp(&csel_start, &rbc) <= 0 &&
|
---|
892 | coord_cmp(&rbc, &csel_end) < 0) {
|
---|
893 | console_flush(con);
|
---|
894 | console_set_style(con, STYLE_SELECTED);
|
---|
895 | console_flush(con);
|
---|
896 | }
|
---|
897 |
|
---|
898 | console_set_pos(con, 0, i);
|
---|
899 | size = str_size(row_buf);
|
---|
900 | pos = 0;
|
---|
901 | s_column = pane.sh_column;
|
---|
902 | while (pos < size) {
|
---|
903 | if ((csel_start.row == rbc.row) && (csel_start.column == s_column)) {
|
---|
904 | console_flush(con);
|
---|
905 | console_set_style(con, STYLE_SELECTED);
|
---|
906 | console_flush(con);
|
---|
907 | }
|
---|
908 |
|
---|
909 | if ((csel_end.row == rbc.row) && (csel_end.column == s_column)) {
|
---|
910 | console_flush(con);
|
---|
911 | console_set_style(con, STYLE_NORMAL);
|
---|
912 | console_flush(con);
|
---|
913 | }
|
---|
914 |
|
---|
915 | c = str_decode(row_buf, &pos, size);
|
---|
916 | if (c != '\t') {
|
---|
917 | printf("%lc", (wint_t) c);
|
---|
918 | s_column += 1;
|
---|
919 | } else {
|
---|
920 | fill = 1 + ALIGN_UP(s_column, TAB_WIDTH)
|
---|
921 | - s_column;
|
---|
922 |
|
---|
923 | for (j = 0; j < fill; ++j)
|
---|
924 | putchar(' ');
|
---|
925 | s_column += fill;
|
---|
926 | }
|
---|
927 | }
|
---|
928 |
|
---|
929 | if ((csel_end.row == rbc.row) && (csel_end.column == s_column)) {
|
---|
930 | console_flush(con);
|
---|
931 | console_set_style(con, STYLE_NORMAL);
|
---|
932 | console_flush(con);
|
---|
933 | }
|
---|
934 |
|
---|
935 | /* Fill until the end of display area. */
|
---|
936 |
|
---|
937 | if ((unsigned)s_column - 1 < scr_columns)
|
---|
938 | fill = scr_columns - (s_column - 1);
|
---|
939 | else
|
---|
940 | fill = 0;
|
---|
941 |
|
---|
942 | for (j = 0; j < fill; ++j)
|
---|
943 | putchar(' ');
|
---|
944 | console_flush(con);
|
---|
945 | console_set_style(con, STYLE_NORMAL);
|
---|
946 | }
|
---|
947 |
|
---|
948 | pane.rflags |= REDRAW_CARET;
|
---|
949 | }
|
---|
950 |
|
---|
951 | /** Display pane status in the status line. */
|
---|
952 | static void pane_status_display(void)
|
---|
953 | {
|
---|
954 | spt_t caret_pt;
|
---|
955 | coord_t coord;
|
---|
956 | int last_row;
|
---|
957 | char *fname;
|
---|
958 | char *p;
|
---|
959 | char *text;
|
---|
960 | size_t n;
|
---|
961 | int pos;
|
---|
962 | size_t nextra;
|
---|
963 | size_t fnw;
|
---|
964 |
|
---|
965 | tag_get_pt(&pane.caret_pos, &caret_pt);
|
---|
966 | spt_get_coord(&caret_pt, &coord);
|
---|
967 |
|
---|
968 | sheet_get_num_rows(doc.sh, &last_row);
|
---|
969 |
|
---|
970 | if (doc.file_name != NULL) {
|
---|
971 | /* Remove directory component */
|
---|
972 | p = str_rchr(doc.file_name, '/');
|
---|
973 | if (p != NULL)
|
---|
974 | fname = str_dup(p + 1);
|
---|
975 | else
|
---|
976 | fname = str_dup(doc.file_name);
|
---|
977 | } else {
|
---|
978 | fname = str_dup("<unnamed>");
|
---|
979 | }
|
---|
980 |
|
---|
981 | if (fname == NULL)
|
---|
982 | return;
|
---|
983 |
|
---|
984 | console_set_pos(con, 0, scr_rows - 1);
|
---|
985 | console_set_style(con, STYLE_INVERTED);
|
---|
986 |
|
---|
987 | /*
|
---|
988 | * Make sure the status fits on the screen. This loop should
|
---|
989 | * be executed at most twice.
|
---|
990 | */
|
---|
991 | while (true) {
|
---|
992 | int rc = asprintf(&text, " %d, %d (%d): File '%s'. Ctrl-Q Quit Ctrl-S Save "
|
---|
993 | "Ctrl-E Save As", coord.row, coord.column, last_row, fname);
|
---|
994 | if (rc < 0) {
|
---|
995 | n = 0;
|
---|
996 | goto finish;
|
---|
997 | }
|
---|
998 |
|
---|
999 | /* If it already fits, we're done */
|
---|
1000 | n = str_width(text);
|
---|
1001 | if (n <= scr_columns - 2)
|
---|
1002 | break;
|
---|
1003 |
|
---|
1004 | /* Compute number of excess characters */
|
---|
1005 | nextra = n - (scr_columns - 2);
|
---|
1006 | /** With of the file name part */
|
---|
1007 | fnw = str_width(fname);
|
---|
1008 |
|
---|
1009 | /*
|
---|
1010 | * If reducing file name to two characters '..' won't help,
|
---|
1011 | * just give up and print a blank status.
|
---|
1012 | */
|
---|
1013 | if (nextra > fnw - 2)
|
---|
1014 | goto finish;
|
---|
1015 |
|
---|
1016 | /* Compute position where we overwrite with '..\0' */
|
---|
1017 | if (fnw >= nextra + 2) {
|
---|
1018 | p = fname + str_lsize(fname, fnw - nextra - 2);
|
---|
1019 | } else {
|
---|
1020 | p = fname;
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | /* Shorten the string */
|
---|
1024 | p[0] = p[1] = '.';
|
---|
1025 | p[2] = '\0';
|
---|
1026 |
|
---|
1027 | /* Need to format the string once more. */
|
---|
1028 | free(text);
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | printf("%s", text);
|
---|
1032 | free(text);
|
---|
1033 | finish:
|
---|
1034 | /* Fill the rest of the line */
|
---|
1035 | pos = scr_columns - 1 - n;
|
---|
1036 | printf("%*s", pos, "");
|
---|
1037 | console_flush(con);
|
---|
1038 | console_set_style(con, STYLE_NORMAL);
|
---|
1039 |
|
---|
1040 | pane.rflags |= REDRAW_CARET;
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 | /** Set cursor to reflect position of the caret. */
|
---|
1044 | static void pane_caret_display(void)
|
---|
1045 | {
|
---|
1046 | spt_t caret_pt;
|
---|
1047 | coord_t coord;
|
---|
1048 |
|
---|
1049 | tag_get_pt(&pane.caret_pos, &caret_pt);
|
---|
1050 |
|
---|
1051 | spt_get_coord(&caret_pt, &coord);
|
---|
1052 | console_set_pos(con, coord.column - pane.sh_column,
|
---|
1053 | coord.row - pane.sh_row);
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | /** Insert a character at caret position. */
|
---|
1057 | static void insert_char(wchar_t c)
|
---|
1058 | {
|
---|
1059 | spt_t pt;
|
---|
1060 | char cbuf[STR_BOUNDS(1) + 1];
|
---|
1061 | size_t offs;
|
---|
1062 |
|
---|
1063 | tag_get_pt(&pane.caret_pos, &pt);
|
---|
1064 |
|
---|
1065 | offs = 0;
|
---|
1066 | chr_encode(c, cbuf, &offs, STR_BOUNDS(1) + 1);
|
---|
1067 | cbuf[offs] = '\0';
|
---|
1068 |
|
---|
1069 | (void) sheet_insert(doc.sh, &pt, dir_before, cbuf);
|
---|
1070 |
|
---|
1071 | pane.rflags |= REDRAW_ROW;
|
---|
1072 | if (c == '\n')
|
---|
1073 | pane.rflags |= REDRAW_TEXT;
|
---|
1074 | }
|
---|
1075 |
|
---|
1076 | /** Delete the character before the caret. */
|
---|
1077 | static void delete_char_before(void)
|
---|
1078 | {
|
---|
1079 | spt_t sp, ep;
|
---|
1080 | coord_t coord;
|
---|
1081 |
|
---|
1082 | tag_get_pt(&pane.caret_pos, &ep);
|
---|
1083 | spt_get_coord(&ep, &coord);
|
---|
1084 |
|
---|
1085 | coord.column -= 1;
|
---|
1086 | sheet_get_cell_pt(doc.sh, &coord, dir_before, &sp);
|
---|
1087 |
|
---|
1088 | (void) sheet_delete(doc.sh, &sp, &ep);
|
---|
1089 |
|
---|
1090 | pane.rflags |= REDRAW_ROW;
|
---|
1091 | if (coord.column < 1)
|
---|
1092 | pane.rflags |= REDRAW_TEXT;
|
---|
1093 | }
|
---|
1094 |
|
---|
1095 | /** Delete the character after the caret. */
|
---|
1096 | static void delete_char_after(void)
|
---|
1097 | {
|
---|
1098 | spt_t sp, ep;
|
---|
1099 | coord_t sc, ec;
|
---|
1100 |
|
---|
1101 | tag_get_pt(&pane.caret_pos, &sp);
|
---|
1102 | spt_get_coord(&sp, &sc);
|
---|
1103 |
|
---|
1104 | sheet_get_cell_pt(doc.sh, &sc, dir_after, &ep);
|
---|
1105 | spt_get_coord(&ep, &ec);
|
---|
1106 |
|
---|
1107 | (void) sheet_delete(doc.sh, &sp, &ep);
|
---|
1108 |
|
---|
1109 | pane.rflags |= REDRAW_ROW;
|
---|
1110 | if (ec.row != sc.row)
|
---|
1111 | pane.rflags |= REDRAW_TEXT;
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | /** Scroll pane after caret has moved.
|
---|
1115 | *
|
---|
1116 | * After modifying the position of the caret, this is called to scroll
|
---|
1117 | * the pane to ensure that the caret is in the visible area.
|
---|
1118 | */
|
---|
1119 | static void caret_update(void)
|
---|
1120 | {
|
---|
1121 | spt_t pt;
|
---|
1122 | coord_t coord;
|
---|
1123 |
|
---|
1124 | tag_get_pt(&pane.caret_pos, &pt);
|
---|
1125 | spt_get_coord(&pt, &coord);
|
---|
1126 |
|
---|
1127 | /* Scroll pane vertically. */
|
---|
1128 |
|
---|
1129 | if (coord.row < pane.sh_row) {
|
---|
1130 | pane.sh_row = coord.row;
|
---|
1131 | pane.rflags |= REDRAW_TEXT;
|
---|
1132 | }
|
---|
1133 |
|
---|
1134 | if (coord.row > pane.sh_row + pane.rows - 1) {
|
---|
1135 | pane.sh_row = coord.row - pane.rows + 1;
|
---|
1136 | pane.rflags |= REDRAW_TEXT;
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | /* Scroll pane horizontally. */
|
---|
1140 |
|
---|
1141 | if (coord.column < pane.sh_column) {
|
---|
1142 | pane.sh_column = coord.column;
|
---|
1143 | pane.rflags |= REDRAW_TEXT;
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | if (coord.column > pane.sh_column + pane.columns - 1) {
|
---|
1147 | pane.sh_column = coord.column - pane.columns + 1;
|
---|
1148 | pane.rflags |= REDRAW_TEXT;
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 | pane.rflags |= (REDRAW_CARET | REDRAW_STATUS);
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | /** Relatively move caret position.
|
---|
1155 | *
|
---|
1156 | * Moves caret relatively to the current position. Looking at the first
|
---|
1157 | * character cell after the caret and moving by @a drow and @a dcolumn, we get
|
---|
1158 | * to a new character cell, and thus a new character. Then we either go to the
|
---|
1159 | * point before the the character or after it, depending on @a align_dir.
|
---|
1160 | *
|
---|
1161 | * @param select true if the selection tag should stay where it is
|
---|
1162 | */
|
---|
1163 | static void caret_move_relative(int drow, int dcolumn, enum dir_spec align_dir,
|
---|
1164 | bool select)
|
---|
1165 | {
|
---|
1166 | spt_t pt;
|
---|
1167 | coord_t coord;
|
---|
1168 | int num_rows;
|
---|
1169 | bool pure_vertical;
|
---|
1170 |
|
---|
1171 | tag_get_pt(&pane.caret_pos, &pt);
|
---|
1172 | spt_get_coord(&pt, &coord);
|
---|
1173 | coord.row += drow; coord.column += dcolumn;
|
---|
1174 |
|
---|
1175 | /* Clamp coordinates. */
|
---|
1176 | if (drow < 0 && coord.row < 1) coord.row = 1;
|
---|
1177 | if (dcolumn < 0 && coord.column < 1) {
|
---|
1178 | if (coord.row < 2)
|
---|
1179 | coord.column = 1;
|
---|
1180 | else {
|
---|
1181 | coord.row--;
|
---|
1182 | sheet_get_row_width(doc.sh, coord.row, &coord.column);
|
---|
1183 | }
|
---|
1184 | }
|
---|
1185 | if (drow > 0) {
|
---|
1186 | sheet_get_num_rows(doc.sh, &num_rows);
|
---|
1187 | if (coord.row > num_rows) coord.row = num_rows;
|
---|
1188 | }
|
---|
1189 |
|
---|
1190 | /* For purely vertical movement try attaining @c ideal_column. */
|
---|
1191 | pure_vertical = (dcolumn == 0 && align_dir == dir_before);
|
---|
1192 | if (pure_vertical)
|
---|
1193 | coord.column = pane.ideal_column;
|
---|
1194 |
|
---|
1195 | /*
|
---|
1196 | * Select the point before or after the character at the designated
|
---|
1197 | * coordinates. The character can be wider than one cell (e.g. tab).
|
---|
1198 | */
|
---|
1199 | sheet_get_cell_pt(doc.sh, &coord, align_dir, &pt);
|
---|
1200 |
|
---|
1201 | /* For non-vertical movement set the new value for @c ideal_column. */
|
---|
1202 | caret_move(pt, select, !pure_vertical);
|
---|
1203 | }
|
---|
1204 |
|
---|
1205 | /** Absolutely move caret position.
|
---|
1206 | *
|
---|
1207 | * Moves caret to a specified position. We get to a new character cell, and
|
---|
1208 | * thus a new character. Then we either go to the point before the the character
|
---|
1209 | * or after it, depending on @a align_dir.
|
---|
1210 | *
|
---|
1211 | * @param select true if the selection tag should stay where it is
|
---|
1212 | */
|
---|
1213 | static void caret_move_absolute(int row, int column, enum dir_spec align_dir,
|
---|
1214 | bool select)
|
---|
1215 | {
|
---|
1216 | coord_t coord;
|
---|
1217 | coord.row = row;
|
---|
1218 | coord.column = column;
|
---|
1219 |
|
---|
1220 | spt_t pt;
|
---|
1221 | sheet_get_cell_pt(doc.sh, &coord, align_dir, &pt);
|
---|
1222 |
|
---|
1223 | caret_move(pt, select, true);
|
---|
1224 | }
|
---|
1225 |
|
---|
1226 | /** Find beginning of a word to the left of spt */
|
---|
1227 | static spt_t pt_find_word_left(spt_t spt)
|
---|
1228 | {
|
---|
1229 | do {
|
---|
1230 | spt_prev_char(spt, &spt);
|
---|
1231 | } while (!pt_is_word_beginning(&spt));
|
---|
1232 | return spt;
|
---|
1233 | }
|
---|
1234 |
|
---|
1235 | /** Find beginning of a word to the right of spt */
|
---|
1236 | static spt_t pt_find_word_right(spt_t spt)
|
---|
1237 | {
|
---|
1238 | do {
|
---|
1239 | spt_next_char(spt, &spt);
|
---|
1240 | } while (!pt_is_word_beginning(&spt));
|
---|
1241 | return spt;
|
---|
1242 | }
|
---|
1243 |
|
---|
1244 | static void caret_move_word_left(bool select)
|
---|
1245 | {
|
---|
1246 | spt_t pt;
|
---|
1247 | tag_get_pt(&pane.caret_pos, &pt);
|
---|
1248 | spt_t word_left = pt_find_word_left(pt);
|
---|
1249 | caret_move(word_left, select, true);
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | static void caret_move_word_right(bool select)
|
---|
1253 | {
|
---|
1254 | spt_t pt;
|
---|
1255 | tag_get_pt(&pane.caret_pos, &pt);
|
---|
1256 | spt_t word_right = pt_find_word_right(pt);
|
---|
1257 | caret_move(word_right, select, true);
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | /** Ask for line and go to it. */
|
---|
1261 | static void caret_go_to_line_ask(void)
|
---|
1262 | {
|
---|
1263 | char *sline;
|
---|
1264 |
|
---|
1265 | sline = prompt("Go to line", "");
|
---|
1266 | if (sline == NULL) {
|
---|
1267 | status_display("Go to line cancelled.");
|
---|
1268 | return;
|
---|
1269 | }
|
---|
1270 |
|
---|
1271 | char *endptr;
|
---|
1272 | int line = strtol(sline, &endptr, 10);
|
---|
1273 | if (*endptr != '\0') {
|
---|
1274 | free(sline);
|
---|
1275 | status_display("Invalid number entered.");
|
---|
1276 | return;
|
---|
1277 | }
|
---|
1278 | free(sline);
|
---|
1279 |
|
---|
1280 | caret_move_absolute(line, pane.ideal_column, dir_before, false);
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | /* Search operations */
|
---|
1284 | static int search_spt_producer(void *data, wchar_t *ret)
|
---|
1285 | {
|
---|
1286 | assert(data != NULL);
|
---|
1287 | assert(ret != NULL);
|
---|
1288 | spt_t *spt = data;
|
---|
1289 | *ret = spt_next_char(*spt, spt);
|
---|
1290 | return EOK;
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | static int search_spt_reverse_producer(void *data, wchar_t *ret)
|
---|
1294 | {
|
---|
1295 | assert(data != NULL);
|
---|
1296 | assert(ret != NULL);
|
---|
1297 | spt_t *spt = data;
|
---|
1298 | *ret = spt_prev_char(*spt, spt);
|
---|
1299 | return EOK;
|
---|
1300 | }
|
---|
1301 |
|
---|
1302 | static int search_spt_mark(void *data, void **mark)
|
---|
1303 | {
|
---|
1304 | assert(data != NULL);
|
---|
1305 | assert(mark != NULL);
|
---|
1306 | spt_t *spt = data;
|
---|
1307 | spt_t *new = calloc(1, sizeof(spt_t));
|
---|
1308 | *mark = new;
|
---|
1309 | if (new == NULL)
|
---|
1310 | return ENOMEM;
|
---|
1311 | *new = *spt;
|
---|
1312 | return EOK;
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | static void search_spt_mark_free(void *data)
|
---|
1316 | {
|
---|
1317 | free(data);
|
---|
1318 | }
|
---|
1319 |
|
---|
1320 | static search_ops_t search_spt_ops = {
|
---|
1321 | .equals = char_exact_equals,
|
---|
1322 | .producer = search_spt_producer,
|
---|
1323 | .mark = search_spt_mark,
|
---|
1324 | .mark_free = search_spt_mark_free,
|
---|
1325 | };
|
---|
1326 |
|
---|
1327 | static search_ops_t search_spt_reverse_ops = {
|
---|
1328 | .equals = char_exact_equals,
|
---|
1329 | .producer = search_spt_reverse_producer,
|
---|
1330 | .mark = search_spt_mark,
|
---|
1331 | .mark_free = search_spt_mark_free,
|
---|
1332 | };
|
---|
1333 |
|
---|
1334 | /** Ask for line and go to it. */
|
---|
1335 | static void search_prompt(bool reverse)
|
---|
1336 | {
|
---|
1337 | char *pattern;
|
---|
1338 |
|
---|
1339 | const char *prompt_text = "Find next";
|
---|
1340 | if (reverse)
|
---|
1341 | prompt_text = "Find previous";
|
---|
1342 |
|
---|
1343 | const char *default_value = "";
|
---|
1344 | if (pane.previous_search)
|
---|
1345 | default_value = pane.previous_search;
|
---|
1346 |
|
---|
1347 | pattern = prompt(prompt_text, default_value);
|
---|
1348 | if (pattern == NULL) {
|
---|
1349 | status_display("Search cancelled.");
|
---|
1350 | return;
|
---|
1351 | }
|
---|
1352 |
|
---|
1353 | if (pane.previous_search)
|
---|
1354 | free(pane.previous_search);
|
---|
1355 | pane.previous_search = pattern;
|
---|
1356 | pane.previous_search_reverse = reverse;
|
---|
1357 |
|
---|
1358 | search(pattern, reverse);
|
---|
1359 | }
|
---|
1360 |
|
---|
1361 | static void search_repeat(void)
|
---|
1362 | {
|
---|
1363 | if (pane.previous_search == NULL) {
|
---|
1364 | status_display("No previous search to repeat.");
|
---|
1365 | return;
|
---|
1366 | }
|
---|
1367 |
|
---|
1368 | search(pane.previous_search, pane.previous_search_reverse);
|
---|
1369 | }
|
---|
1370 |
|
---|
1371 | static void search(char *pattern, bool reverse)
|
---|
1372 | {
|
---|
1373 | status_display("Searching...");
|
---|
1374 |
|
---|
1375 | spt_t sp, producer_pos;
|
---|
1376 | tag_get_pt(&pane.caret_pos, &sp);
|
---|
1377 |
|
---|
1378 | /* Start searching on the position before/after caret */
|
---|
1379 | if (!reverse) {
|
---|
1380 | spt_next_char(sp, &sp);
|
---|
1381 | }
|
---|
1382 | else {
|
---|
1383 | spt_prev_char(sp, &sp);
|
---|
1384 | }
|
---|
1385 | producer_pos = sp;
|
---|
1386 |
|
---|
1387 | search_ops_t ops = search_spt_ops;
|
---|
1388 | if (reverse)
|
---|
1389 | ops = search_spt_reverse_ops;
|
---|
1390 |
|
---|
1391 | search_t *search = search_init(pattern, &producer_pos, ops, reverse);
|
---|
1392 | if (search == NULL) {
|
---|
1393 | status_display("Failed initializing search.");
|
---|
1394 | return;
|
---|
1395 | }
|
---|
1396 |
|
---|
1397 | match_t match;
|
---|
1398 | int rc = search_next_match(search, &match);
|
---|
1399 | if (rc != EOK) {
|
---|
1400 | status_display("Failed searching.");
|
---|
1401 | search_fini(search);
|
---|
1402 | }
|
---|
1403 |
|
---|
1404 | if (match.end) {
|
---|
1405 | status_display("Match found.");
|
---|
1406 | assert(match.end != NULL);
|
---|
1407 | spt_t *end = match.end;
|
---|
1408 | caret_move(*end, false, true);
|
---|
1409 | while (match.length > 0) {
|
---|
1410 | match.length--;
|
---|
1411 | if (reverse) {
|
---|
1412 | spt_next_char(*end, end);
|
---|
1413 | }
|
---|
1414 | else {
|
---|
1415 | spt_prev_char(*end, end);
|
---|
1416 | }
|
---|
1417 | }
|
---|
1418 | caret_move(*end, true, true);
|
---|
1419 | free(end);
|
---|
1420 | }
|
---|
1421 | else {
|
---|
1422 | status_display("Not found.");
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | search_fini(search);
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 | /** Check for non-empty selection. */
|
---|
1429 | static bool selection_active(void)
|
---|
1430 | {
|
---|
1431 | return (tag_cmp(&pane.caret_pos, &pane.sel_start) != 0);
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 | static void selection_get_points(spt_t *pa, spt_t *pb)
|
---|
1435 | {
|
---|
1436 | spt_t pt;
|
---|
1437 |
|
---|
1438 | tag_get_pt(&pane.sel_start, pa);
|
---|
1439 | tag_get_pt(&pane.caret_pos, pb);
|
---|
1440 |
|
---|
1441 | if (spt_cmp(pa, pb) > 0) {
|
---|
1442 | pt = *pa;
|
---|
1443 | *pa = *pb;
|
---|
1444 | *pb = pt;
|
---|
1445 | }
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | /** Delete selected text. */
|
---|
1449 | static void selection_delete(void)
|
---|
1450 | {
|
---|
1451 | spt_t pa, pb;
|
---|
1452 | coord_t ca, cb;
|
---|
1453 | int rel;
|
---|
1454 |
|
---|
1455 | tag_get_pt(&pane.sel_start, &pa);
|
---|
1456 | tag_get_pt(&pane.caret_pos, &pb);
|
---|
1457 | spt_get_coord(&pa, &ca);
|
---|
1458 | spt_get_coord(&pb, &cb);
|
---|
1459 | rel = coord_cmp(&ca, &cb);
|
---|
1460 |
|
---|
1461 | if (rel == 0)
|
---|
1462 | return;
|
---|
1463 |
|
---|
1464 | if (rel < 0)
|
---|
1465 | sheet_delete(doc.sh, &pa, &pb);
|
---|
1466 | else
|
---|
1467 | sheet_delete(doc.sh, &pb, &pa);
|
---|
1468 |
|
---|
1469 | if (ca.row == cb.row)
|
---|
1470 | pane.rflags |= REDRAW_ROW;
|
---|
1471 | else
|
---|
1472 | pane.rflags |= REDRAW_TEXT;
|
---|
1473 | }
|
---|
1474 |
|
---|
1475 | /** Select all text in the editor */
|
---|
1476 | static void selection_sel_all(void)
|
---|
1477 | {
|
---|
1478 | spt_t spt, ept;
|
---|
1479 |
|
---|
1480 | pt_get_sof(&spt);
|
---|
1481 | pt_get_eof(&ept);
|
---|
1482 |
|
---|
1483 | selection_sel_range(spt, ept);
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 | /** Select select all text in a given range with the given direction */
|
---|
1487 | static void selection_sel_range(spt_t pa, spt_t pb)
|
---|
1488 | {
|
---|
1489 | sheet_remove_tag(doc.sh, &pane.sel_start);
|
---|
1490 | sheet_place_tag(doc.sh, &pa, &pane.sel_start);
|
---|
1491 | sheet_remove_tag(doc.sh, &pane.caret_pos);
|
---|
1492 | sheet_place_tag(doc.sh, &pb, &pane.caret_pos);
|
---|
1493 |
|
---|
1494 | pane.rflags |= REDRAW_TEXT;
|
---|
1495 | caret_update();
|
---|
1496 | }
|
---|
1497 |
|
---|
1498 | static void selection_copy(void)
|
---|
1499 | {
|
---|
1500 | spt_t pa, pb;
|
---|
1501 | char *str;
|
---|
1502 |
|
---|
1503 | selection_get_points(&pa, &pb);
|
---|
1504 | str = range_get_str(&pa, &pb);
|
---|
1505 | if (str == NULL || clipboard_put_str(str) != EOK) {
|
---|
1506 | status_display("Copying to clipboard failed!");
|
---|
1507 | }
|
---|
1508 | free(str);
|
---|
1509 | }
|
---|
1510 |
|
---|
1511 | static void insert_clipboard_data(void)
|
---|
1512 | {
|
---|
1513 | char *str;
|
---|
1514 | size_t off;
|
---|
1515 | wchar_t c;
|
---|
1516 | int rc;
|
---|
1517 |
|
---|
1518 | rc = clipboard_get_str(&str);
|
---|
1519 | if (rc != EOK || str == NULL)
|
---|
1520 | return;
|
---|
1521 |
|
---|
1522 | off = 0;
|
---|
1523 |
|
---|
1524 | while (true) {
|
---|
1525 | c = str_decode(str, &off, STR_NO_LIMIT);
|
---|
1526 | if (c == '\0')
|
---|
1527 | break;
|
---|
1528 |
|
---|
1529 | insert_char(c);
|
---|
1530 | }
|
---|
1531 |
|
---|
1532 | free(str);
|
---|
1533 | }
|
---|
1534 |
|
---|
1535 | /** Get start-of-file s-point. */
|
---|
1536 | static void pt_get_sof(spt_t *pt)
|
---|
1537 | {
|
---|
1538 | coord_t coord;
|
---|
1539 |
|
---|
1540 | coord.row = coord.column = 1;
|
---|
1541 | sheet_get_cell_pt(doc.sh, &coord, dir_before, pt);
|
---|
1542 | }
|
---|
1543 |
|
---|
1544 | /** Get end-of-file s-point. */
|
---|
1545 | static void pt_get_eof(spt_t *pt)
|
---|
1546 | {
|
---|
1547 | coord_t coord;
|
---|
1548 | int num_rows;
|
---|
1549 |
|
---|
1550 | sheet_get_num_rows(doc.sh, &num_rows);
|
---|
1551 | coord.row = num_rows + 1;
|
---|
1552 | coord.column = 1;
|
---|
1553 |
|
---|
1554 | sheet_get_cell_pt(doc.sh, &coord, dir_after, pt);
|
---|
1555 | }
|
---|
1556 |
|
---|
1557 | /** Get start-of-line s-point for given s-point cpt */
|
---|
1558 | static void pt_get_sol(spt_t *cpt, spt_t *spt)
|
---|
1559 | {
|
---|
1560 | coord_t coord;
|
---|
1561 |
|
---|
1562 | spt_get_coord(cpt, &coord);
|
---|
1563 | coord.column = 1;
|
---|
1564 |
|
---|
1565 | sheet_get_cell_pt(doc.sh, &coord, dir_before, spt);
|
---|
1566 | }
|
---|
1567 |
|
---|
1568 | /** Get end-of-line s-point for given s-point cpt */
|
---|
1569 | static void pt_get_eol(spt_t *cpt, spt_t *ept)
|
---|
1570 | {
|
---|
1571 | coord_t coord;
|
---|
1572 | int row_width;
|
---|
1573 |
|
---|
1574 | spt_get_coord(cpt, &coord);
|
---|
1575 | sheet_get_row_width(doc.sh, coord.row, &row_width);
|
---|
1576 | coord.column = row_width - 1;
|
---|
1577 |
|
---|
1578 | sheet_get_cell_pt(doc.sh, &coord, dir_after, ept);
|
---|
1579 | }
|
---|
1580 |
|
---|
1581 | /** Check whether the spt is at a beginning of a word */
|
---|
1582 | static bool pt_is_word_beginning(spt_t *pt)
|
---|
1583 | {
|
---|
1584 | spt_t lp, sfp, efp, slp, elp;
|
---|
1585 | coord_t coord;
|
---|
1586 |
|
---|
1587 | pt_get_sof(&sfp);
|
---|
1588 | pt_get_eof(&efp);
|
---|
1589 | pt_get_sol(pt, &slp);
|
---|
1590 | pt_get_eol(pt, &elp);
|
---|
1591 |
|
---|
1592 | /* the spt is at the beginning or end of the file or line */
|
---|
1593 | if ((spt_cmp(&sfp, pt) == 0) || (spt_cmp(&efp, pt) == 0)
|
---|
1594 | || (spt_cmp(&slp, pt) == 0) || (spt_cmp(&elp, pt) == 0))
|
---|
1595 | return true;
|
---|
1596 |
|
---|
1597 | /* the spt is a delimiter */
|
---|
1598 | if (pt_is_delimiter(pt))
|
---|
1599 | return false;
|
---|
1600 |
|
---|
1601 | spt_get_coord(pt, &coord);
|
---|
1602 |
|
---|
1603 | coord.column -= 1;
|
---|
1604 | sheet_get_cell_pt(doc.sh, &coord, dir_before, &lp);
|
---|
1605 |
|
---|
1606 | return pt_is_delimiter(&lp)
|
---|
1607 | || (pt_is_punctuation(pt) && !pt_is_punctuation(&lp))
|
---|
1608 | || (pt_is_punctuation(&lp) && !pt_is_punctuation(pt));
|
---|
1609 | }
|
---|
1610 |
|
---|
1611 | static wchar_t get_first_wchar(const char *str)
|
---|
1612 | {
|
---|
1613 | size_t offset = 0;
|
---|
1614 | return str_decode(str, &offset, str_size(str));
|
---|
1615 | }
|
---|
1616 |
|
---|
1617 | static bool pt_is_delimiter(spt_t *pt)
|
---|
1618 | {
|
---|
1619 | spt_t rp;
|
---|
1620 | coord_t coord;
|
---|
1621 | char *ch = NULL;
|
---|
1622 |
|
---|
1623 | spt_get_coord(pt, &coord);
|
---|
1624 |
|
---|
1625 | coord.column += 1;
|
---|
1626 | sheet_get_cell_pt(doc.sh, &coord, dir_after, &rp);
|
---|
1627 |
|
---|
1628 | ch = range_get_str(pt, &rp);
|
---|
1629 | if (ch == NULL)
|
---|
1630 | return false;
|
---|
1631 |
|
---|
1632 | wchar_t first_char = get_first_wchar(ch);
|
---|
1633 | switch(first_char) {
|
---|
1634 | case ' ':
|
---|
1635 | case '\t':
|
---|
1636 | case '\n':
|
---|
1637 | return true;
|
---|
1638 | default:
|
---|
1639 | return false;
|
---|
1640 | }
|
---|
1641 | }
|
---|
1642 |
|
---|
1643 | static bool pt_is_punctuation(spt_t *pt)
|
---|
1644 | {
|
---|
1645 | spt_t rp;
|
---|
1646 | coord_t coord;
|
---|
1647 | char *ch = NULL;
|
---|
1648 |
|
---|
1649 | spt_get_coord(pt, &coord);
|
---|
1650 |
|
---|
1651 | coord.column += 1;
|
---|
1652 | sheet_get_cell_pt(doc.sh, &coord, dir_after, &rp);
|
---|
1653 |
|
---|
1654 | ch = range_get_str(pt, &rp);
|
---|
1655 | if (ch == NULL)
|
---|
1656 | return false;
|
---|
1657 |
|
---|
1658 | wchar_t first_char = get_first_wchar(ch);
|
---|
1659 | switch(first_char) {
|
---|
1660 | case ',':
|
---|
1661 | case '.':
|
---|
1662 | case ';':
|
---|
1663 | case ':':
|
---|
1664 | case '/':
|
---|
1665 | case '?':
|
---|
1666 | case '\\':
|
---|
1667 | case '|':
|
---|
1668 | case '_':
|
---|
1669 | case '+':
|
---|
1670 | case '-':
|
---|
1671 | case '*':
|
---|
1672 | case '=':
|
---|
1673 | case '<':
|
---|
1674 | case '>':
|
---|
1675 | return true;
|
---|
1676 | default:
|
---|
1677 | return false;
|
---|
1678 | }
|
---|
1679 | }
|
---|
1680 |
|
---|
1681 | /** Compare tags. */
|
---|
1682 | static int tag_cmp(tag_t const *a, tag_t const *b)
|
---|
1683 | {
|
---|
1684 | spt_t pa, pb;
|
---|
1685 |
|
---|
1686 | tag_get_pt(a, &pa);
|
---|
1687 | tag_get_pt(b, &pb);
|
---|
1688 |
|
---|
1689 | return spt_cmp(&pa, &pb);
|
---|
1690 | }
|
---|
1691 |
|
---|
1692 | /** Compare s-points. */
|
---|
1693 | static int spt_cmp(spt_t const *a, spt_t const *b)
|
---|
1694 | {
|
---|
1695 | coord_t ca, cb;
|
---|
1696 |
|
---|
1697 | spt_get_coord(a, &ca);
|
---|
1698 | spt_get_coord(b, &cb);
|
---|
1699 |
|
---|
1700 | return coord_cmp(&ca, &cb);
|
---|
1701 | }
|
---|
1702 |
|
---|
1703 | /** Compare coordinats. */
|
---|
1704 | static int coord_cmp(coord_t const *a, coord_t const *b)
|
---|
1705 | {
|
---|
1706 | if (a->row - b->row != 0)
|
---|
1707 | return a->row - b->row;
|
---|
1708 |
|
---|
1709 | return a->column - b->column;
|
---|
1710 | }
|
---|
1711 |
|
---|
1712 | /** Display text in the status line. */
|
---|
1713 | static void status_display(char const *str)
|
---|
1714 | {
|
---|
1715 | console_set_pos(con, 0, scr_rows - 1);
|
---|
1716 | console_set_style(con, STYLE_INVERTED);
|
---|
1717 |
|
---|
1718 | int pos = -(scr_columns - 3);
|
---|
1719 | printf(" %*s ", pos, str);
|
---|
1720 | console_flush(con);
|
---|
1721 | console_set_style(con, STYLE_NORMAL);
|
---|
1722 |
|
---|
1723 | pane.rflags |= REDRAW_CARET;
|
---|
1724 | }
|
---|
1725 |
|
---|
1726 | /** @}
|
---|
1727 | */
|
---|