source: mainline/uspace/app/edit/edit.c@ c38ab6c

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c38ab6c was c88d7f99, checked in by Jiri Svoboda <jiri@…>, 3 years ago

Menu entry accelerators

  • Property mode set to 100644
File size: 55.6 KB
Line 
1/*
2 * Copyright (c) 2022 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 <align.h>
39#include <clipboard.h>
40#include <errno.h>
41#include <gfx/color.h>
42#include <gfx/cursor.h>
43#include <gfx/font.h>
44#include <gfx/render.h>
45#include <gfx/text.h>
46#include <io/kbd_event.h>
47#include <io/keycode.h>
48#include <io/pos_event.h>
49#include <io/style.h>
50#include <macros.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <stddef.h>
54#include <stdbool.h>
55#include <types/common.h>
56#include <ui/control.h>
57#include <ui/filedialog.h>
58#include <ui/fixed.h>
59#include <ui/label.h>
60#include <ui/menu.h>
61#include <ui/menubar.h>
62#include <ui/menuentry.h>
63#include <ui/promptdialog.h>
64#include <ui/resource.h>
65#include <ui/ui.h>
66#include <ui/window.h>
67#include <vfs/vfs.h>
68
69#include "sheet.h"
70#include "search.h"
71
72enum redraw_flags {
73 REDRAW_TEXT = (1 << 0),
74 REDRAW_ROW = (1 << 1),
75 REDRAW_STATUS = (1 << 2),
76 REDRAW_CARET = (1 << 3)
77};
78
79/** Pane
80 *
81 * A rectangular area of the screen used to edit a document. Different
82 * panes can be possibly used to edit the same document. This is a custom
83 * UI control.
84 */
85typedef struct {
86 /** Base control object */
87 struct ui_control *control;
88
89 /** Containing window */
90 ui_window_t *window;
91
92 /** UI resource */
93 struct ui_resource *res;
94
95 /** Pane rectangle */
96 gfx_rect_t rect;
97
98 /** Pane color */
99 gfx_color_t *color;
100
101 /** Selection color */
102 gfx_color_t *sel_color;
103
104 /* Pane dimensions */
105 int rows, columns;
106
107 /* Position of the visible area */
108 int sh_row, sh_column;
109
110 /** Bitmask of components that need redrawing */
111 enum redraw_flags rflags;
112
113 /** Current position of the caret */
114 tag_t caret_pos;
115
116 /** Start of selection */
117 tag_t sel_start;
118
119 /** Active keyboard modifiers */
120 keymod_t keymod;
121
122 /**
123 * Ideal column where the caret should try to get. This is used
124 * for maintaining the same column during vertical movement.
125 */
126 int ideal_column;
127
128 bool search_reverse;
129 char *previous_search;
130 bool previous_search_reverse;
131} pane_t;
132
133/** Text editor */
134typedef struct {
135 /** User interface */
136 ui_t *ui;
137 /** Editor window */
138 ui_window_t *window;
139 /** UI resource */
140 ui_resource_t *ui_res;
141 /** Menu bar */
142 ui_menu_bar_t *menubar;
143 /** Status bar */
144 ui_label_t *status;
145} edit_t;
146
147/** Document
148 *
149 * Associates a sheet with a file where it can be saved to.
150 */
151typedef struct {
152 char *file_name;
153 sheet_t *sh;
154} doc_t;
155
156static edit_t edit;
157static doc_t doc;
158static pane_t pane;
159
160#define ROW_BUF_SIZE 4096
161#define BUF_SIZE 64
162#define TAB_WIDTH 8
163
164/** Maximum filename length that can be entered. */
165#define INFNAME_MAX_LEN 128
166
167static void cursor_setvis(bool visible);
168
169static void key_handle_press(kbd_event_t *ev);
170static void key_handle_unmod(kbd_event_t const *ev);
171static void key_handle_ctrl(kbd_event_t const *ev);
172static void key_handle_shift(kbd_event_t const *ev);
173static void key_handle_shift_ctrl(kbd_event_t const *ev);
174static void key_handle_movement(unsigned int key, bool shift);
175
176static void pos_handle(pos_event_t *ev);
177
178static errno_t file_save(char const *fname);
179static void file_save_as(void);
180static errno_t file_insert(char *fname);
181static errno_t file_save_range(char const *fname, spt_t const *spos,
182 spt_t const *epos);
183static char *range_get_str(spt_t const *spos, spt_t const *epos);
184
185static errno_t pane_init(ui_window_t *, pane_t *);
186static void pane_fini(pane_t *);
187static ui_control_t *pane_ctl(pane_t *);
188static errno_t pane_update(pane_t *);
189static errno_t pane_text_display(pane_t *);
190static void pane_row_display(void);
191static errno_t pane_row_range_display(pane_t *, int r0, int r1);
192static void pane_status_display(pane_t *);
193static void pane_caret_display(pane_t *);
194
195static void insert_char(char32_t c);
196static void delete_char_before(void);
197static void delete_char_after(void);
198static void caret_update(void);
199static void caret_move_relative(int drow, int dcolumn, enum dir_spec align_dir, bool select);
200static void caret_move_absolute(int row, int column, enum dir_spec align_dir, bool select);
201static void caret_move(spt_t spt, bool select, bool update_ideal_column);
202static void caret_move_word_left(bool select);
203static void caret_move_word_right(bool select);
204static void caret_go_to_line_ask(void);
205
206static bool selection_active(void);
207static void selection_sel_all(void);
208static void selection_sel_range(spt_t pa, spt_t pb);
209static void selection_get_points(spt_t *pa, spt_t *pb);
210static void selection_delete(void);
211static void selection_copy(void);
212static void edit_cut(void);
213static void edit_paste(void);
214static void insert_clipboard_data(void);
215
216static void search(char *pattern, bool reverse);
217static void search_prompt(bool reverse);
218static void search_repeat(void);
219
220static void pt_get_sof(spt_t *pt);
221static void pt_get_eof(spt_t *pt);
222static void pt_get_sol(spt_t *cpt, spt_t *spt);
223static void pt_get_eol(spt_t *cpt, spt_t *ept);
224static bool pt_is_word_beginning(spt_t *pt);
225static bool pt_is_delimiter(spt_t *pt);
226static bool pt_is_punctuation(spt_t *pt);
227static spt_t pt_find_word_left(spt_t spt);
228static spt_t pt_find_word_left(spt_t spt);
229
230static int tag_cmp(tag_t const *a, tag_t const *b);
231static int spt_cmp(spt_t const *a, spt_t const *b);
232static int coord_cmp(coord_t const *a, coord_t const *b);
233
234static void status_display(char const *str);
235static errno_t edit_ui_create(edit_t *);
236static void edit_ui_destroy(edit_t *);
237
238static void edit_wnd_close(ui_window_t *, void *);
239static void edit_wnd_kbd_event(ui_window_t *, void *, kbd_event_t *);
240
241static ui_window_cb_t edit_window_cb = {
242 .close = edit_wnd_close,
243 .kbd = edit_wnd_kbd_event
244};
245
246static void edit_file_save(ui_menu_entry_t *, void *);
247static void edit_file_save_as(ui_menu_entry_t *, void *);
248static void edit_file_exit(ui_menu_entry_t *, void *);
249static void edit_edit_cut(ui_menu_entry_t *, void *);
250static void edit_edit_copy(ui_menu_entry_t *, void *);
251static void edit_edit_paste(ui_menu_entry_t *, void *);
252static void edit_edit_delete(ui_menu_entry_t *, void *);
253static void edit_edit_select_all(ui_menu_entry_t *, void *);
254static void edit_search_find(ui_menu_entry_t *, void *);
255static void edit_search_reverse_find(ui_menu_entry_t *, void *);
256static void edit_search_find_next(ui_menu_entry_t *, void *);
257static void edit_search_go_to_line(ui_menu_entry_t *, void *);
258
259static void pane_ctl_destroy(void *);
260static errno_t pane_ctl_paint(void *);
261static ui_evclaim_t pane_ctl_pos_event(void *, pos_event_t *);
262
263/** Pabe control ops */
264ui_control_ops_t pane_ctl_ops = {
265 .destroy = pane_ctl_destroy,
266 .paint = pane_ctl_paint,
267 .pos_event = pane_ctl_pos_event
268};
269
270static void save_as_dialog_bok(ui_file_dialog_t *, void *, const char *);
271static void save_as_dialog_bcancel(ui_file_dialog_t *, void *);
272static void save_as_dialog_close(ui_file_dialog_t *, void *);
273
274static ui_file_dialog_cb_t save_as_dialog_cb = {
275 .bok = save_as_dialog_bok,
276 .bcancel = save_as_dialog_bcancel,
277 .close = save_as_dialog_close
278};
279
280static void go_to_line_dialog_bok(ui_prompt_dialog_t *, void *, const char *);
281static void go_to_line_dialog_bcancel(ui_prompt_dialog_t *, void *);
282static void go_to_line_dialog_close(ui_prompt_dialog_t *, void *);
283
284static ui_prompt_dialog_cb_t go_to_line_dialog_cb = {
285 .bok = go_to_line_dialog_bok,
286 .bcancel = go_to_line_dialog_bcancel,
287 .close = go_to_line_dialog_close
288};
289
290static void search_dialog_bok(ui_prompt_dialog_t *, void *, const char *);
291static void search_dialog_bcancel(ui_prompt_dialog_t *, void *);
292static void search_dialog_close(ui_prompt_dialog_t *, void *);
293
294static ui_prompt_dialog_cb_t search_dialog_cb = {
295 .bok = search_dialog_bok,
296 .bcancel = search_dialog_bcancel,
297 .close = search_dialog_close
298};
299
300int main(int argc, char *argv[])
301{
302 bool new_file;
303 errno_t rc;
304
305 pane.sh_row = 1;
306 pane.sh_column = 1;
307
308 /* Start with an empty sheet. */
309 rc = sheet_create(&doc.sh);
310 if (rc != EOK) {
311 printf("Out of memory.\n");
312 return -1;
313 }
314
315 /* Place caret at the beginning of file. */
316 spt_t sof;
317 pt_get_sof(&sof);
318 sheet_place_tag(doc.sh, &sof, &pane.caret_pos);
319 pane.ideal_column = 1;
320
321 if (argc == 2) {
322 doc.file_name = str_dup(argv[1]);
323 } else if (argc > 1) {
324 printf("Invalid arguments.\n");
325 return -2;
326 } else {
327 doc.file_name = NULL;
328 }
329
330 new_file = false;
331
332 if (doc.file_name == NULL || file_insert(doc.file_name) != EOK)
333 new_file = true;
334
335 /* Place selection start tag. */
336 sheet_place_tag(doc.sh, &sof, &pane.sel_start);
337
338 /* Move to beginning of file. */
339 pt_get_sof(&sof);
340
341 /* Create UI */
342 rc = edit_ui_create(&edit);
343 if (rc != EOK)
344 return 1;
345
346 caret_move(sof, true, true);
347
348 /* Initial display */
349 rc = ui_window_paint(edit.window);
350 if (rc != EOK) {
351 printf("Error painting window.\n");
352 return rc;
353 }
354
355 pane_status_display(&pane);
356 if (new_file && doc.file_name != NULL)
357 status_display("File not found. Starting empty file.");
358 pane_caret_display(&pane);
359 cursor_setvis(true);
360
361 ui_run(edit.ui);
362
363 edit_ui_destroy(&edit);
364 return 0;
365}
366
367/** Create text editor UI.
368 *
369 * @param edit Editor
370 * @return EOK on success or an error code
371 */
372static errno_t edit_ui_create(edit_t *edit)
373{
374 errno_t rc;
375 ui_wnd_params_t params;
376 ui_fixed_t *fixed = NULL;
377 ui_menu_t *mfile = NULL;
378 ui_menu_t *medit = NULL;
379 ui_menu_entry_t *msave = NULL;
380 ui_menu_entry_t *msaveas = NULL;
381 ui_menu_entry_t *mfsep = NULL;
382 ui_menu_entry_t *mexit = NULL;
383 ui_menu_entry_t *mcut = NULL;
384 ui_menu_entry_t *mcopy = NULL;
385 ui_menu_entry_t *mpaste = NULL;
386 ui_menu_entry_t *mdelete = NULL;
387 ui_menu_entry_t *mesep = NULL;
388 ui_menu_entry_t *mselall = NULL;
389 ui_menu_t *msearch = NULL;
390 ui_menu_entry_t *mfind = NULL;
391 ui_menu_entry_t *mfindr = NULL;
392 ui_menu_entry_t *mfindn = NULL;
393 ui_menu_entry_t *mssep = NULL;
394 ui_menu_entry_t *mgoto = NULL;
395 gfx_rect_t arect;
396 gfx_rect_t rect;
397
398 rc = ui_create(UI_CONSOLE_DEFAULT, &edit->ui);
399 if (rc != EOK) {
400 printf("Error creating UI on display %s.\n",
401 UI_CONSOLE_DEFAULT);
402 goto error;
403 }
404
405 ui_wnd_params_init(&params);
406 params.caption = "Text Editor";
407 params.style &= ~ui_wds_decorated;
408 params.placement = ui_wnd_place_full_screen;
409
410 rc = ui_window_create(edit->ui, &params, &edit->window);
411 if (rc != EOK) {
412 printf("Error creating window.\n");
413 goto error;
414 }
415
416 ui_window_set_cb(edit->window, &edit_window_cb, (void *) edit);
417
418 edit->ui_res = ui_window_get_res(edit->window);
419
420 rc = ui_fixed_create(&fixed);
421 if (rc != EOK) {
422 printf("Error creating fixed layout.\n");
423 return rc;
424 }
425
426 rc = ui_menu_bar_create(edit->ui, edit->window, &edit->menubar);
427 if (rc != EOK) {
428 printf("Error creating menu bar.\n");
429 return rc;
430 }
431
432 rc = ui_menu_create(edit->menubar, "~F~ile", &mfile);
433 if (rc != EOK) {
434 printf("Error creating menu.\n");
435 return rc;
436 }
437
438 rc = ui_menu_entry_create(mfile, "~S~ave", "Ctrl-S", &msave);
439 if (rc != EOK) {
440 printf("Error creating menu.\n");
441 return rc;
442 }
443
444 ui_menu_entry_set_cb(msave, edit_file_save, (void *) edit);
445
446 rc = ui_menu_entry_create(mfile, "Save ~A~s", "Ctrl-E", &msaveas);
447 if (rc != EOK) {
448 printf("Error creating menu.\n");
449 return rc;
450 }
451
452 ui_menu_entry_set_cb(msaveas, edit_file_save_as, (void *) edit);
453
454 rc = ui_menu_entry_sep_create(mfile, &mfsep);
455 if (rc != EOK) {
456 printf("Error creating menu.\n");
457 return rc;
458 }
459
460 rc = ui_menu_entry_create(mfile, "E~x~it", "Ctrl-Q", &mexit);
461 if (rc != EOK) {
462 printf("Error creating menu.\n");
463 return rc;
464 }
465
466 ui_menu_entry_set_cb(mexit, edit_file_exit, (void *) edit);
467
468 rc = ui_menu_create(edit->menubar, "~E~dit", &medit);
469 if (rc != EOK) {
470 printf("Error creating menu.\n");
471 return rc;
472 }
473
474 rc = ui_menu_entry_create(medit, "Cu~t~", "Ctrl-X", &mcut);
475 if (rc != EOK) {
476 printf("Error creating menu.\n");
477 return rc;
478 }
479
480 ui_menu_entry_set_cb(mcut, edit_edit_cut, (void *) edit);
481
482 rc = ui_menu_entry_create(medit, "~C~opy", "Ctrl-C", &mcopy);
483 if (rc != EOK) {
484 printf("Error creating menu.\n");
485 return rc;
486 }
487
488 ui_menu_entry_set_cb(mcopy, edit_edit_copy, (void *) edit);
489
490 rc = ui_menu_entry_create(medit, "~P~aste", "Ctrl-V", &mpaste);
491 if (rc != EOK) {
492 printf("Error creating menu.\n");
493 return rc;
494 }
495
496 ui_menu_entry_set_cb(mpaste, edit_edit_paste, (void *) edit);
497
498 rc = ui_menu_entry_create(medit, "~D~elete", "Del", &mdelete);
499 if (rc != EOK) {
500 printf("Error creating menu.\n");
501 return rc;
502 }
503
504 ui_menu_entry_set_cb(mdelete, edit_edit_delete, (void *) edit);
505
506 rc = ui_menu_entry_sep_create(medit, &mesep);
507 if (rc != EOK) {
508 printf("Error creating menu.\n");
509 return rc;
510 }
511
512 rc = ui_menu_entry_create(medit, "Select ~A~ll", "Ctrl-A", &mselall);
513 if (rc != EOK) {
514 printf("Error creating menu.\n");
515 return rc;
516 }
517
518 ui_menu_entry_set_cb(mselall, edit_edit_select_all, (void *) edit);
519
520 rc = ui_menu_create(edit->menubar, "~S~earch", &msearch);
521 if (rc != EOK) {
522 printf("Error creating menu.\n");
523 return rc;
524 }
525
526 rc = ui_menu_entry_create(msearch, "~F~ind", "Ctrl-F", &mfind);
527 if (rc != EOK) {
528 printf("Error creating menu.\n");
529 return rc;
530 }
531
532 ui_menu_entry_set_cb(mfind, edit_search_find, (void *) edit);
533
534 rc = ui_menu_entry_create(msearch, "~R~everse Find", "Ctrl-Shift-F", &mfindr);
535 if (rc != EOK) {
536 printf("Error creating menu.\n");
537 return rc;
538 }
539
540 ui_menu_entry_set_cb(mfindr, edit_search_reverse_find, (void *) edit);
541
542 rc = ui_menu_entry_create(msearch, "Find ~N~ext", "Ctrl-N", &mfindn);
543 if (rc != EOK) {
544 printf("Error creating menu.\n");
545 return rc;
546 }
547
548 ui_menu_entry_set_cb(mfindn, edit_search_find_next, (void *) edit);
549
550 rc = ui_menu_entry_sep_create(msearch, &mssep);
551 if (rc != EOK) {
552 printf("Error creating menu.\n");
553 return rc;
554 }
555
556 rc = ui_menu_entry_create(msearch, "Go To ~L~ine", "Ctrl-L", &mgoto);
557 if (rc != EOK) {
558 printf("Error creating menu.\n");
559 return rc;
560 }
561
562 ui_menu_entry_set_cb(mgoto, edit_search_go_to_line, (void *) edit);
563
564 ui_window_get_app_rect(edit->window, &arect);
565
566 rect.p0 = arect.p0;
567 rect.p1.x = arect.p1.x;
568 rect.p1.y = arect.p0.y + 1;
569 ui_menu_bar_set_rect(edit->menubar, &rect);
570
571 rc = ui_fixed_add(fixed, ui_menu_bar_ctl(edit->menubar));
572 if (rc != EOK) {
573 printf("Error adding control to layout.\n");
574 return rc;
575 }
576
577 rc = pane_init(edit->window, &pane);
578 if (rc != EOK) {
579 printf("Error initializing pane.\n");
580 return rc;
581 }
582
583 rc = ui_fixed_add(fixed, pane_ctl(&pane));
584 if (rc != EOK) {
585 printf("Error adding control to layout.\n");
586 return rc;
587 }
588
589 rc = ui_label_create(edit->ui_res, "", &edit->status);
590 if (rc != EOK) {
591 printf("Error creating menu bar.\n");
592 return rc;
593 }
594
595 rect.p0.x = arect.p0.x;
596 rect.p0.y = arect.p1.y - 1;
597 rect.p1 = arect.p1;
598 ui_label_set_rect(edit->status, &rect);
599
600 rc = ui_fixed_add(fixed, ui_label_ctl(edit->status));
601 if (rc != EOK) {
602 printf("Error adding control to layout.\n");
603 return rc;
604 }
605
606 ui_window_add(edit->window, ui_fixed_ctl(fixed));
607 return EOK;
608error:
609 if (edit->window != NULL)
610 ui_window_destroy(edit->window);
611 if (edit->ui != NULL)
612 ui_destroy(edit->ui);
613 return rc;
614}
615
616/** Destroy text editor UI.
617 *
618 * @param edit Editor
619 */
620static void edit_ui_destroy(edit_t *edit)
621{
622 ui_window_destroy(edit->window);
623 ui_destroy(edit->ui);
624}
625
626/* Handle key press. */
627static void key_handle_press(kbd_event_t *ev)
628{
629 if (((ev->mods & KM_ALT) == 0) &&
630 ((ev->mods & KM_SHIFT) == 0) &&
631 (ev->mods & KM_CTRL) != 0) {
632 key_handle_ctrl(ev);
633 } else if (((ev->mods & KM_ALT) == 0) &&
634 ((ev->mods & KM_CTRL) == 0) &&
635 (ev->mods & KM_SHIFT) != 0) {
636 key_handle_shift(ev);
637 } else if (((ev->mods & KM_ALT) == 0) &&
638 ((ev->mods & KM_CTRL) != 0) &&
639 (ev->mods & KM_SHIFT) != 0) {
640 key_handle_shift_ctrl(ev);
641 } else if ((ev->mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) {
642 key_handle_unmod(ev);
643 }
644}
645
646static void cursor_setvis(bool visible)
647{
648 gfx_context_t *gc = ui_window_get_gc(edit.window);
649
650 (void) gfx_cursor_set_visible(gc, visible);
651}
652
653/** Handle key without modifier. */
654static void key_handle_unmod(kbd_event_t const *ev)
655{
656 switch (ev->key) {
657 case KC_ENTER:
658 selection_delete();
659 insert_char('\n');
660 caret_update();
661 break;
662 case KC_LEFT:
663 case KC_RIGHT:
664 case KC_UP:
665 case KC_DOWN:
666 case KC_HOME:
667 case KC_END:
668 case KC_PAGE_UP:
669 case KC_PAGE_DOWN:
670 key_handle_movement(ev->key, false);
671 break;
672 case KC_BACKSPACE:
673 if (selection_active())
674 selection_delete();
675 else
676 delete_char_before();
677 caret_update();
678 break;
679 case KC_DELETE:
680 if (selection_active())
681 selection_delete();
682 else
683 delete_char_after();
684 caret_update();
685 break;
686 default:
687 if (ev->c >= 32 || ev->c == '\t') {
688 selection_delete();
689 insert_char(ev->c);
690 caret_update();
691 }
692 break;
693 }
694}
695
696/** Handle Shift-key combination. */
697static void key_handle_shift(kbd_event_t const *ev)
698{
699 switch (ev->key) {
700 case KC_LEFT:
701 case KC_RIGHT:
702 case KC_UP:
703 case KC_DOWN:
704 case KC_HOME:
705 case KC_END:
706 case KC_PAGE_UP:
707 case KC_PAGE_DOWN:
708 key_handle_movement(ev->key, true);
709 break;
710 default:
711 if (ev->c >= 32 || ev->c == '\t') {
712 selection_delete();
713 insert_char(ev->c);
714 caret_update();
715 }
716 break;
717 }
718}
719
720/** Handle Ctrl-key combination. */
721static void key_handle_ctrl(kbd_event_t const *ev)
722{
723 spt_t pt;
724 switch (ev->key) {
725 case KC_Q:
726 ui_quit(edit.ui);
727 break;
728 case KC_S:
729 if (doc.file_name != NULL)
730 file_save(doc.file_name);
731 else
732 file_save_as();
733 break;
734 case KC_E:
735 file_save_as();
736 break;
737 case KC_C:
738 selection_copy();
739 break;
740 case KC_V:
741 edit_paste();
742 break;
743 case KC_X:
744 edit_cut();
745 break;
746 case KC_A:
747 selection_sel_all();
748 break;
749 case KC_RIGHT:
750 caret_move_word_right(false);
751 break;
752 case KC_LEFT:
753 caret_move_word_left(false);
754 break;
755 case KC_L:
756 caret_go_to_line_ask();
757 break;
758 case KC_F:
759 search_prompt(false);
760 break;
761 case KC_N:
762 search_repeat();
763 break;
764 case KC_HOME:
765 pt_get_sof(&pt);
766 caret_move(pt, false, true);
767 break;
768 case KC_END:
769 pt_get_eof(&pt);
770 caret_move(pt, false, true);
771 break;
772 default:
773 break;
774 }
775}
776
777static void key_handle_shift_ctrl(kbd_event_t const *ev)
778{
779 spt_t pt;
780 switch (ev->key) {
781 case KC_LEFT:
782 caret_move_word_left(true);
783 break;
784 case KC_RIGHT:
785 caret_move_word_right(true);
786 break;
787 case KC_F:
788 search_prompt(true);
789 break;
790 case KC_HOME:
791 pt_get_sof(&pt);
792 caret_move(pt, true, true);
793 break;
794 case KC_END:
795 pt_get_eof(&pt);
796 caret_move(pt, true, true);
797 break;
798 default:
799 break;
800 }
801}
802
803static void pos_handle(pos_event_t *ev)
804{
805 coord_t bc;
806 spt_t pt;
807 bool select;
808
809 if (ev->type == POS_PRESS && ev->vpos < (unsigned)pane.rows) {
810 bc.row = pane.sh_row + ev->vpos - pane.rect.p0.y;
811 bc.column = pane.sh_column + ev->hpos - pane.rect.p0.x;
812 sheet_get_cell_pt(doc.sh, &bc, dir_before, &pt);
813
814 select = (pane.keymod & KM_SHIFT) != 0;
815
816 caret_move(pt, select, true);
817 pane_update(&pane);
818 }
819}
820
821/** Move caret while preserving or resetting selection. */
822static void caret_move(spt_t new_caret_pt, bool select, bool update_ideal_column)
823{
824 spt_t old_caret_pt, old_sel_pt;
825 coord_t c_old, c_new;
826 bool had_sel;
827
828 /* Check if we had selection before. */
829 tag_get_pt(&pane.caret_pos, &old_caret_pt);
830 tag_get_pt(&pane.sel_start, &old_sel_pt);
831 had_sel = !spt_equal(&old_caret_pt, &old_sel_pt);
832
833 /* Place tag of the caret */
834 sheet_remove_tag(doc.sh, &pane.caret_pos);
835 sheet_place_tag(doc.sh, &new_caret_pt, &pane.caret_pos);
836
837 if (select == false) {
838 /* Move sel_start to the same point as caret. */
839 sheet_remove_tag(doc.sh, &pane.sel_start);
840 sheet_place_tag(doc.sh, &new_caret_pt, &pane.sel_start);
841 }
842
843 spt_get_coord(&new_caret_pt, &c_new);
844 if (select) {
845 spt_get_coord(&old_caret_pt, &c_old);
846
847 if (c_old.row == c_new.row)
848 pane.rflags |= REDRAW_ROW;
849 else
850 pane.rflags |= REDRAW_TEXT;
851
852 } else if (had_sel == true) {
853 /* Redraw because text was unselected. */
854 pane.rflags |= REDRAW_TEXT;
855 }
856
857 if (update_ideal_column)
858 pane.ideal_column = c_new.column;
859
860 caret_update();
861}
862
863static void key_handle_movement(unsigned int key, bool select)
864{
865 spt_t pt;
866 switch (key) {
867 case KC_LEFT:
868 caret_move_relative(0, -1, dir_before, select);
869 break;
870 case KC_RIGHT:
871 caret_move_relative(0, 0, dir_after, select);
872 break;
873 case KC_UP:
874 caret_move_relative(-1, 0, dir_before, select);
875 break;
876 case KC_DOWN:
877 caret_move_relative(+1, 0, dir_before, select);
878 break;
879 case KC_HOME:
880 tag_get_pt(&pane.caret_pos, &pt);
881 pt_get_sol(&pt, &pt);
882 caret_move(pt, select, true);
883 break;
884 case KC_END:
885 tag_get_pt(&pane.caret_pos, &pt);
886 pt_get_eol(&pt, &pt);
887 caret_move(pt, select, true);
888 break;
889 case KC_PAGE_UP:
890 caret_move_relative(-pane.rows, 0, dir_before, select);
891 break;
892 case KC_PAGE_DOWN:
893 caret_move_relative(+pane.rows, 0, dir_before, select);
894 break;
895 default:
896 break;
897 }
898}
899
900/** Save the document. */
901static errno_t file_save(char const *fname)
902{
903 spt_t sp, ep;
904 errno_t rc;
905
906 status_display("Saving...");
907 pt_get_sof(&sp);
908 pt_get_eof(&ep);
909
910 rc = file_save_range(fname, &sp, &ep);
911
912 switch (rc) {
913 case EINVAL:
914 status_display("Error opening file!");
915 break;
916 case EIO:
917 status_display("Error writing data!");
918 break;
919 default:
920 status_display("File saved.");
921 break;
922 }
923
924 return rc;
925}
926
927/** Open Save As dialog. */
928static void file_save_as(void)
929{
930 const char *old_fname = (doc.file_name != NULL) ? doc.file_name : "";
931 ui_file_dialog_params_t fdparams;
932 ui_file_dialog_t *dialog;
933 errno_t rc;
934
935 ui_file_dialog_params_init(&fdparams);
936 fdparams.caption = "Save As";
937 fdparams.ifname = old_fname;
938
939 rc = ui_file_dialog_create(edit.ui, &fdparams, &dialog);
940 if (rc != EOK) {
941 printf("Error creating message dialog.\n");
942 return;
943 }
944
945 ui_file_dialog_set_cb(dialog, &save_as_dialog_cb, &edit);
946}
947
948/** Insert file at caret position.
949 *
950 * Reads in the contents of a file and inserts them at the current position
951 * of the caret.
952 */
953static errno_t file_insert(char *fname)
954{
955 FILE *f;
956 char32_t c;
957 char buf[BUF_SIZE];
958 int bcnt;
959 int n_read;
960 size_t off;
961
962 f = fopen(fname, "rt");
963 if (f == NULL)
964 return EINVAL;
965
966 bcnt = 0;
967
968 while (true) {
969 if (bcnt < STR_BOUNDS(1)) {
970 n_read = fread(buf + bcnt, 1, BUF_SIZE - bcnt, f);
971 bcnt += n_read;
972 }
973
974 off = 0;
975 c = str_decode(buf, &off, bcnt);
976 if (c == '\0')
977 break;
978
979 bcnt -= off;
980 memmove(buf, buf + off, bcnt);
981
982 insert_char(c);
983 }
984
985 fclose(f);
986
987 return EOK;
988}
989
990/** Save a range of text into a file. */
991static errno_t file_save_range(char const *fname, spt_t const *spos,
992 spt_t const *epos)
993{
994 FILE *f;
995 char buf[BUF_SIZE];
996 spt_t sp, bep;
997 size_t bytes, n_written;
998
999 f = fopen(fname, "wt");
1000 if (f == NULL)
1001 return EINVAL;
1002
1003 sp = *spos;
1004
1005 do {
1006 sheet_copy_out(doc.sh, &sp, epos, buf, BUF_SIZE, &bep);
1007 bytes = str_size(buf);
1008
1009 n_written = fwrite(buf, 1, bytes, f);
1010 if (n_written != bytes) {
1011 return EIO;
1012 }
1013
1014 sp = bep;
1015 } while (!spt_equal(&bep, epos));
1016
1017 if (fclose(f) < 0)
1018 return EIO;
1019
1020 return EOK;
1021}
1022
1023/** Return contents of range as a new string. */
1024static char *range_get_str(spt_t const *spos, spt_t const *epos)
1025{
1026 char *buf;
1027 spt_t sp, bep;
1028 size_t bytes;
1029 size_t buf_size, bpos;
1030
1031 buf_size = 1;
1032
1033 buf = malloc(buf_size);
1034 if (buf == NULL)
1035 return NULL;
1036
1037 bpos = 0;
1038 sp = *spos;
1039
1040 while (true) {
1041 sheet_copy_out(doc.sh, &sp, epos, &buf[bpos], buf_size - bpos,
1042 &bep);
1043 bytes = str_size(&buf[bpos]);
1044 bpos += bytes;
1045 sp = bep;
1046
1047 if (spt_equal(&bep, epos))
1048 break;
1049
1050 buf_size *= 2;
1051 char *tmp = realloc(buf, buf_size);
1052 if (tmp == NULL) {
1053 free(buf);
1054 return NULL;
1055 }
1056 buf = tmp;
1057 }
1058
1059 return buf;
1060}
1061
1062/** Initialize pane.
1063 *
1064 * TODO: Replace with pane_create() that allocates the pane.
1065 *
1066 * @param window Editor window
1067 * @param pane Pane
1068 * @return EOK on success or an error code
1069 */
1070static errno_t pane_init(ui_window_t *window, pane_t *pane)
1071{
1072 errno_t rc;
1073 gfx_rect_t arect;
1074
1075 pane->control = NULL;
1076 pane->color = NULL;
1077 pane->sel_color = NULL;
1078
1079 rc = ui_control_new(&pane_ctl_ops, (void *) pane, &pane->control);
1080 if (rc != EOK)
1081 goto error;
1082
1083 rc = gfx_color_new_ega(0x07, &pane->color);
1084 if (rc != EOK)
1085 goto error;
1086
1087 rc = gfx_color_new_ega(0x1e, &pane->sel_color);
1088 if (rc != EOK)
1089 goto error;
1090
1091 pane->res = ui_window_get_res(window);
1092 pane->window = window;
1093
1094 ui_window_get_app_rect(window, &arect);
1095 pane->rect.p0.x = arect.p0.x;
1096 pane->rect.p0.y = arect.p0.y + 1;
1097 pane->rect.p1.x = arect.p1.x;
1098 pane->rect.p1.y = arect.p1.y - 1;
1099
1100 pane->columns = pane->rect.p1.x - pane->rect.p0.x;
1101 pane->rows = pane->rect.p1.y - pane->rect.p0.y;
1102
1103 return EOK;
1104error:
1105 if (pane->control != NULL) {
1106 ui_control_delete(pane->control);
1107 pane->control = NULL;
1108 }
1109
1110 if (pane->color != NULL) {
1111 gfx_color_delete(pane->color);
1112 pane->color = NULL;
1113 }
1114
1115 return rc;
1116}
1117
1118/** Finalize pane.
1119 *
1120 * TODO: Replace with pane_destroy() that deallocates the pane.
1121 *
1122 * @param pane Pane
1123 */
1124static void pane_fini(pane_t *pane)
1125{
1126 gfx_color_delete(pane->color);
1127 pane->color = NULL;
1128 gfx_color_delete(pane->sel_color);
1129 pane->sel_color = NULL;
1130 ui_control_delete(pane->control);
1131 pane->control = NULL;
1132}
1133
1134/** Return base control object for a pane.
1135 *
1136 * @param pane Pane
1137 * @return Base UI cntrol
1138 */
1139static ui_control_t *pane_ctl(pane_t *pane)
1140{
1141 return pane->control;
1142}
1143
1144/** Repaint parts of pane that need updating.
1145 *
1146 * @param pane Pane
1147 * @return EOK on succes or an error code
1148 */
1149static errno_t pane_update(pane_t *pane)
1150{
1151 errno_t rc;
1152
1153 if (pane->rflags & REDRAW_TEXT) {
1154 rc = pane_text_display(pane);
1155 if (rc != EOK)
1156 return rc;
1157 }
1158
1159 if (pane->rflags & REDRAW_ROW)
1160 pane_row_display();
1161
1162 if (pane->rflags & REDRAW_STATUS)
1163 pane_status_display(pane);
1164
1165 if (pane->rflags & REDRAW_CARET)
1166 pane_caret_display(pane);
1167
1168 pane->rflags &= ~(REDRAW_TEXT | REDRAW_ROW | REDRAW_STATUS |
1169 REDRAW_CARET);
1170 return EOK;
1171}
1172
1173/** Display pane text.
1174 *
1175 * @param pane Pane
1176 * @return EOK on success or an error code
1177 */
1178static errno_t pane_text_display(pane_t *pane)
1179{
1180 gfx_rect_t rect;
1181 gfx_context_t *gc;
1182 errno_t rc;
1183 int sh_rows, rows;
1184
1185 sheet_get_num_rows(doc.sh, &sh_rows);
1186 rows = min(sh_rows - pane->sh_row + 1, pane->rows);
1187
1188 /* Draw rows from the sheet. */
1189
1190 rc = pane_row_range_display(pane, 0, rows);
1191 if (rc != EOK)
1192 return rc;
1193
1194 /* Clear the remaining rows if file is short. */
1195
1196 gc = ui_window_get_gc(pane->window);
1197
1198 rc = gfx_set_color(gc, pane->color);
1199 if (rc != EOK)
1200 goto error;
1201
1202 rect.p0.x = pane->rect.p0.x;
1203 rect.p0.y = pane->rect.p0.y + rows;
1204 rect.p1.x = pane->rect.p1.x;
1205 rect.p1.y = pane->rect.p1.y;
1206
1207 rc = gfx_fill_rect(gc, &rect);
1208 if (rc != EOK)
1209 goto error;
1210
1211 pane->rflags &= ~REDRAW_ROW;
1212 return EOK;
1213error:
1214 return rc;
1215}
1216
1217/** Display just the row where the caret is. */
1218static void pane_row_display(void)
1219{
1220 spt_t caret_pt;
1221 coord_t coord;
1222 int ridx;
1223
1224 tag_get_pt(&pane.caret_pos, &caret_pt);
1225 spt_get_coord(&caret_pt, &coord);
1226
1227 ridx = coord.row - pane.sh_row;
1228 (void) pane_row_range_display(&pane, ridx, ridx + 1);
1229 pane.rflags |= (REDRAW_STATUS | REDRAW_CARET);
1230}
1231
1232/** Display a range of rows of text.
1233 *
1234 * @param r0 Start row (inclusive)
1235 * @param r1 End row (exclusive)
1236 * @return EOk on success or an error code
1237 */
1238static errno_t pane_row_range_display(pane_t *pane, int r0, int r1)
1239{
1240 int i, fill;
1241 spt_t rb, re, dep, pt;
1242 coord_t rbc, rec;
1243 char row_buf[ROW_BUF_SIZE];
1244 char cbuf[STR_BOUNDS(1) + 1];
1245 char32_t c;
1246 size_t pos, size;
1247 size_t cpos;
1248 int s_column;
1249 coord_t csel_start, csel_end, ctmp;
1250 gfx_font_t *font;
1251 gfx_context_t *gc;
1252 gfx_text_fmt_t fmt;
1253 gfx_coord2_t tpos;
1254 gfx_rect_t rect;
1255 errno_t rc;
1256
1257 font = ui_resource_get_font(edit.ui_res);
1258 gc = ui_window_get_gc(edit.window);
1259
1260 gfx_text_fmt_init(&fmt);
1261 fmt.font = font;
1262 fmt.color = pane->color;
1263
1264 /* Determine selection start and end. */
1265
1266 tag_get_pt(&pane->sel_start, &pt);
1267 spt_get_coord(&pt, &csel_start);
1268
1269 tag_get_pt(&pane->caret_pos, &pt);
1270 spt_get_coord(&pt, &csel_end);
1271
1272 if (coord_cmp(&csel_start, &csel_end) > 0) {
1273 ctmp = csel_start;
1274 csel_start = csel_end;
1275 csel_end = ctmp;
1276 }
1277
1278 /* Draw rows from the sheet. */
1279
1280 for (i = r0; i < r1; ++i) {
1281 tpos.x = pane->rect.p0.x;
1282 tpos.y = pane->rect.p0.y + i;
1283
1284 /* Starting point for row display */
1285 rbc.row = pane->sh_row + i;
1286 rbc.column = pane->sh_column;
1287 sheet_get_cell_pt(doc.sh, &rbc, dir_before, &rb);
1288
1289 /* Ending point for row display */
1290 rec.row = pane->sh_row + i;
1291 rec.column = pane->sh_column + pane->columns;
1292 sheet_get_cell_pt(doc.sh, &rec, dir_before, &re);
1293
1294 /* Copy the text of the row to the buffer. */
1295 sheet_copy_out(doc.sh, &rb, &re, row_buf, ROW_BUF_SIZE, &dep);
1296
1297 /* Display text from the buffer. */
1298
1299 if (coord_cmp(&csel_start, &rbc) <= 0 &&
1300 coord_cmp(&rbc, &csel_end) < 0) {
1301 fmt.color = pane->sel_color;
1302 }
1303
1304 size = str_size(row_buf);
1305 pos = 0;
1306 s_column = pane->sh_column;
1307 while (pos < size) {
1308 if ((csel_start.row == rbc.row) && (csel_start.column == s_column))
1309 fmt.color = pane->sel_color;
1310
1311 if ((csel_end.row == rbc.row) && (csel_end.column == s_column))
1312 fmt.color = pane->color;
1313
1314 c = str_decode(row_buf, &pos, size);
1315 if (c != '\t') {
1316 cpos = 0;
1317 rc = chr_encode(c, cbuf, &cpos, sizeof(cbuf));
1318 if (rc != EOK)
1319 return rc;
1320
1321 rc = gfx_puttext(&tpos, &fmt, cbuf);
1322 if (rc != EOK)
1323 return rc;
1324
1325 s_column += 1;
1326 tpos.x++;
1327 } else {
1328 fill = 1 + ALIGN_UP(s_column, TAB_WIDTH) -
1329 s_column;
1330
1331 rc = gfx_set_color(gc, fmt.color);
1332 if (rc != EOK)
1333 return rc;
1334
1335 rect.p0.x = tpos.x;
1336 rect.p0.y = tpos.y;
1337 rect.p1.x = tpos.x + fill;
1338 rect.p1.y = tpos.y + 1;
1339
1340 rc = gfx_fill_rect(gc, &rect);
1341 if (rc != EOK)
1342 return rc;
1343
1344 s_column += fill;
1345 tpos.x += fill;
1346 }
1347 }
1348
1349 if ((csel_end.row == rbc.row) && (csel_end.column == s_column))
1350 fmt.color = pane->color;
1351
1352 /* Fill until the end of display area. */
1353
1354 rc = gfx_set_color(gc, fmt.color);
1355 if (rc != EOK)
1356 return rc;
1357
1358 rect.p0.x = tpos.x;
1359 rect.p0.y = tpos.y;
1360 rect.p1.x = pane->rect.p1.x;
1361 rect.p1.y = tpos.y + 1;
1362
1363 rc = gfx_fill_rect(gc, &rect);
1364 if (rc != EOK)
1365 return rc;
1366 }
1367
1368 return EOK;
1369}
1370
1371/** Display pane status in the status line.
1372 *
1373 * @param pane Pane
1374 */
1375static void pane_status_display(pane_t *pane)
1376{
1377 spt_t caret_pt;
1378 coord_t coord;
1379 int last_row;
1380 char *fname;
1381 char *p;
1382 char *text;
1383 size_t n;
1384 size_t nextra;
1385 size_t fnw;
1386
1387 tag_get_pt(&pane->caret_pos, &caret_pt);
1388 spt_get_coord(&caret_pt, &coord);
1389
1390 sheet_get_num_rows(doc.sh, &last_row);
1391
1392 if (doc.file_name != NULL) {
1393 /* Remove directory component */
1394 p = str_rchr(doc.file_name, '/');
1395 if (p != NULL)
1396 fname = str_dup(p + 1);
1397 else
1398 fname = str_dup(doc.file_name);
1399 } else {
1400 fname = str_dup("<unnamed>");
1401 }
1402
1403 if (fname == NULL)
1404 return;
1405
1406 /*
1407 * Make sure the status fits on the screen. This loop should
1408 * be executed at most twice.
1409 */
1410 while (true) {
1411 int rc = asprintf(&text, "%d, %d (%d): File '%s'. Ctrl-Q Quit "
1412 "F10 Menu", coord.row, coord.column, last_row, fname);
1413 if (rc < 0) {
1414 n = 0;
1415 goto finish;
1416 }
1417
1418 /* If it already fits, we're done */
1419 n = str_width(text);
1420 if ((int)n <= pane->columns - 2)
1421 break;
1422
1423 /* Compute number of excess characters */
1424 nextra = n - (pane->columns - 2);
1425 /** With of the file name part */
1426 fnw = str_width(fname);
1427
1428 /*
1429 * If reducing file name to two characters '..' won't help,
1430 * just give up and print a blank status.
1431 */
1432 if (nextra > fnw - 2) {
1433 text[0] = '\0';
1434 goto finish;
1435 }
1436
1437 /* Compute position where we overwrite with '..\0' */
1438 if (fnw >= nextra + 2) {
1439 p = fname + str_lsize(fname, fnw - nextra - 2);
1440 } else {
1441 p = fname;
1442 }
1443
1444 /* Shorten the string */
1445 p[0] = p[1] = '.';
1446 p[2] = '\0';
1447
1448 /* Need to format the string once more. */
1449 free(text);
1450 }
1451
1452finish:
1453 (void) ui_label_set_text(edit.status, text);
1454 (void) ui_label_paint(edit.status);
1455 free(text);
1456 free(fname);
1457}
1458
1459/** Set cursor to reflect position of the caret.
1460 *
1461 * @param pane Pane
1462 */
1463static void pane_caret_display(pane_t *pane)
1464{
1465 spt_t caret_pt;
1466 coord_t coord;
1467 gfx_coord2_t pos;
1468 gfx_context_t *gc;
1469
1470 tag_get_pt(&pane->caret_pos, &caret_pt);
1471
1472 spt_get_coord(&caret_pt, &coord);
1473
1474 gc = ui_window_get_gc(edit.window);
1475 pos.x = pane->rect.p0.x + coord.column - pane->sh_column;
1476 pos.y = pane->rect.p0.y + coord.row - pane->sh_row;
1477
1478 (void) gfx_cursor_set_pos(gc, &pos);
1479}
1480
1481/** Destroy pane control.
1482 *
1483 * @param arg Argument (pane_t *)
1484 */
1485static void pane_ctl_destroy(void *arg)
1486{
1487 pane_t *pane = (pane_t *)arg;
1488
1489 pane_fini(pane);
1490}
1491
1492/** Paint pane control.
1493 *
1494 * @param arg Argument (pane_t *)
1495 */
1496static errno_t pane_ctl_paint(void *arg)
1497{
1498 pane_t *pane = (pane_t *)arg;
1499 gfx_context_t *gc;
1500 errno_t rc;
1501
1502 gc = ui_window_get_gc(pane->window);
1503
1504 rc = pane_text_display(pane);
1505 if (rc != EOK)
1506 goto error;
1507
1508 rc = gfx_update(gc);
1509 if (rc != EOK)
1510 goto error;
1511
1512error:
1513 return rc;
1514}
1515
1516/** Handle pane control position event.
1517 *
1518 * @param arg Argument (pane_t *)
1519 * @param event Position event
1520 */
1521static ui_evclaim_t pane_ctl_pos_event(void *arg, pos_event_t *event)
1522{
1523 gfx_coord2_t pos;
1524
1525 pos.x = event->hpos;
1526 pos.y = event->vpos;
1527
1528 if (!gfx_pix_inside_rect(&pos, &pane.rect))
1529 return ui_unclaimed;
1530
1531 pos_handle(event);
1532 (void) gfx_update(ui_window_get_gc(edit.window));
1533 return ui_claimed;
1534}
1535
1536/** Insert a character at caret position. */
1537static void insert_char(char32_t c)
1538{
1539 spt_t pt;
1540 char cbuf[STR_BOUNDS(1) + 1];
1541 size_t offs;
1542
1543 tag_get_pt(&pane.caret_pos, &pt);
1544
1545 offs = 0;
1546 chr_encode(c, cbuf, &offs, STR_BOUNDS(1) + 1);
1547 cbuf[offs] = '\0';
1548
1549 (void) sheet_insert(doc.sh, &pt, dir_before, cbuf);
1550
1551 pane.rflags |= REDRAW_ROW;
1552 if (c == '\n')
1553 pane.rflags |= REDRAW_TEXT;
1554}
1555
1556/** Delete the character before the caret. */
1557static void delete_char_before(void)
1558{
1559 spt_t sp, ep;
1560 coord_t coord;
1561
1562 tag_get_pt(&pane.caret_pos, &ep);
1563 spt_get_coord(&ep, &coord);
1564
1565 coord.column -= 1;
1566 sheet_get_cell_pt(doc.sh, &coord, dir_before, &sp);
1567
1568 (void) sheet_delete(doc.sh, &sp, &ep);
1569
1570 pane.rflags |= REDRAW_ROW;
1571 if (coord.column < 1)
1572 pane.rflags |= REDRAW_TEXT;
1573}
1574
1575/** Delete the character after the caret. */
1576static void delete_char_after(void)
1577{
1578 spt_t sp, ep;
1579 coord_t sc, ec;
1580
1581 tag_get_pt(&pane.caret_pos, &sp);
1582 spt_get_coord(&sp, &sc);
1583
1584 sheet_get_cell_pt(doc.sh, &sc, dir_after, &ep);
1585 spt_get_coord(&ep, &ec);
1586
1587 (void) sheet_delete(doc.sh, &sp, &ep);
1588
1589 pane.rflags |= REDRAW_ROW;
1590 if (ec.row != sc.row)
1591 pane.rflags |= REDRAW_TEXT;
1592}
1593
1594/** Scroll pane after caret has moved.
1595 *
1596 * After modifying the position of the caret, this is called to scroll
1597 * the pane to ensure that the caret is in the visible area.
1598 */
1599static void caret_update(void)
1600{
1601 spt_t pt;
1602 coord_t coord;
1603
1604 tag_get_pt(&pane.caret_pos, &pt);
1605 spt_get_coord(&pt, &coord);
1606
1607 /* Scroll pane vertically. */
1608
1609 if (coord.row < pane.sh_row) {
1610 pane.sh_row = coord.row;
1611 pane.rflags |= REDRAW_TEXT;
1612 }
1613
1614 if (coord.row > pane.sh_row + pane.rows - 1) {
1615 pane.sh_row = coord.row - pane.rows + 1;
1616 pane.rflags |= REDRAW_TEXT;
1617 }
1618
1619 /* Scroll pane horizontally. */
1620
1621 if (coord.column < pane.sh_column) {
1622 pane.sh_column = coord.column;
1623 pane.rflags |= REDRAW_TEXT;
1624 }
1625
1626 if (coord.column > pane.sh_column + pane.columns - 1) {
1627 pane.sh_column = coord.column - pane.columns + 1;
1628 pane.rflags |= REDRAW_TEXT;
1629 }
1630
1631 pane.rflags |= (REDRAW_CARET | REDRAW_STATUS);
1632}
1633
1634/** Relatively move caret position.
1635 *
1636 * Moves caret relatively to the current position. Looking at the first
1637 * character cell after the caret and moving by @a drow and @a dcolumn, we get
1638 * to a new character cell, and thus a new character. Then we either go to the
1639 * point before the the character or after it, depending on @a align_dir.
1640 *
1641 * @param select true if the selection tag should stay where it is
1642 */
1643static void caret_move_relative(int drow, int dcolumn, enum dir_spec align_dir,
1644 bool select)
1645{
1646 spt_t pt;
1647 coord_t coord;
1648 int num_rows;
1649 bool pure_vertical;
1650
1651 tag_get_pt(&pane.caret_pos, &pt);
1652 spt_get_coord(&pt, &coord);
1653 coord.row += drow;
1654 coord.column += dcolumn;
1655
1656 /* Clamp coordinates. */
1657 if (drow < 0 && coord.row < 1)
1658 coord.row = 1;
1659 if (dcolumn < 0 && coord.column < 1) {
1660 if (coord.row < 2)
1661 coord.column = 1;
1662 else {
1663 coord.row--;
1664 sheet_get_row_width(doc.sh, coord.row, &coord.column);
1665 }
1666 }
1667 if (drow > 0) {
1668 sheet_get_num_rows(doc.sh, &num_rows);
1669 if (coord.row > num_rows)
1670 coord.row = num_rows;
1671 }
1672
1673 /* For purely vertical movement try attaining @c ideal_column. */
1674 pure_vertical = (dcolumn == 0 && align_dir == dir_before);
1675 if (pure_vertical)
1676 coord.column = pane.ideal_column;
1677
1678 /*
1679 * Select the point before or after the character at the designated
1680 * coordinates. The character can be wider than one cell (e.g. tab).
1681 */
1682 sheet_get_cell_pt(doc.sh, &coord, align_dir, &pt);
1683
1684 /* For non-vertical movement set the new value for @c ideal_column. */
1685 caret_move(pt, select, !pure_vertical);
1686}
1687
1688/** Absolutely move caret position.
1689 *
1690 * Moves caret to a specified position. We get to a new character cell, and
1691 * thus a new character. Then we either go to the point before the the character
1692 * or after it, depending on @a align_dir.
1693 *
1694 * @param select true if the selection tag should stay where it is
1695 */
1696static void caret_move_absolute(int row, int column, enum dir_spec align_dir,
1697 bool select)
1698{
1699 coord_t coord;
1700 coord.row = row;
1701 coord.column = column;
1702
1703 spt_t pt;
1704 sheet_get_cell_pt(doc.sh, &coord, align_dir, &pt);
1705
1706 caret_move(pt, select, true);
1707}
1708
1709/** Find beginning of a word to the left of spt */
1710static spt_t pt_find_word_left(spt_t spt)
1711{
1712 do {
1713 spt_prev_char(spt, &spt);
1714 } while (!pt_is_word_beginning(&spt));
1715 return spt;
1716}
1717
1718/** Find beginning of a word to the right of spt */
1719static spt_t pt_find_word_right(spt_t spt)
1720{
1721 do {
1722 spt_next_char(spt, &spt);
1723 } while (!pt_is_word_beginning(&spt));
1724 return spt;
1725}
1726
1727static void caret_move_word_left(bool select)
1728{
1729 spt_t pt;
1730 tag_get_pt(&pane.caret_pos, &pt);
1731 spt_t word_left = pt_find_word_left(pt);
1732 caret_move(word_left, select, true);
1733}
1734
1735static void caret_move_word_right(bool select)
1736{
1737 spt_t pt;
1738 tag_get_pt(&pane.caret_pos, &pt);
1739 spt_t word_right = pt_find_word_right(pt);
1740 caret_move(word_right, select, true);
1741}
1742
1743/** Ask for line and go to it. */
1744static void caret_go_to_line_ask(void)
1745{
1746 ui_prompt_dialog_params_t pdparams;
1747 ui_prompt_dialog_t *dialog;
1748 errno_t rc;
1749
1750 ui_prompt_dialog_params_init(&pdparams);
1751 pdparams.caption = "Go To Line";
1752 pdparams.prompt = "Line Number";
1753
1754 rc = ui_prompt_dialog_create(edit.ui, &pdparams, &dialog);
1755 if (rc != EOK) {
1756 printf("Error creating prompt dialog.\n");
1757 return;
1758 }
1759
1760 ui_prompt_dialog_set_cb(dialog, &go_to_line_dialog_cb, &edit);
1761}
1762
1763/* Search operations */
1764static errno_t search_spt_producer(void *data, char32_t *ret)
1765{
1766 assert(data != NULL);
1767 assert(ret != NULL);
1768 spt_t *spt = data;
1769 *ret = spt_next_char(*spt, spt);
1770 return EOK;
1771}
1772
1773static errno_t search_spt_reverse_producer(void *data, char32_t *ret)
1774{
1775 assert(data != NULL);
1776 assert(ret != NULL);
1777 spt_t *spt = data;
1778 *ret = spt_prev_char(*spt, spt);
1779 return EOK;
1780}
1781
1782static errno_t search_spt_mark(void *data, void **mark)
1783{
1784 assert(data != NULL);
1785 assert(mark != NULL);
1786 spt_t *spt = data;
1787 spt_t *new = calloc(1, sizeof(spt_t));
1788 *mark = new;
1789 if (new == NULL)
1790 return ENOMEM;
1791 *new = *spt;
1792 return EOK;
1793}
1794
1795static void search_spt_mark_free(void *data)
1796{
1797 free(data);
1798}
1799
1800static search_ops_t search_spt_ops = {
1801 .equals = char_exact_equals,
1802 .producer = search_spt_producer,
1803 .mark = search_spt_mark,
1804 .mark_free = search_spt_mark_free,
1805};
1806
1807static search_ops_t search_spt_reverse_ops = {
1808 .equals = char_exact_equals,
1809 .producer = search_spt_reverse_producer,
1810 .mark = search_spt_mark,
1811 .mark_free = search_spt_mark_free,
1812};
1813
1814/** Ask for line and go to it. */
1815static void search_prompt(bool reverse)
1816{
1817 ui_prompt_dialog_params_t pdparams;
1818 ui_prompt_dialog_t *dialog;
1819 errno_t rc;
1820
1821 ui_prompt_dialog_params_init(&pdparams);
1822 pdparams.caption = reverse ? "Reverse Search" : "Search";
1823 pdparams.prompt = "Search text";
1824 pdparams.itext = "";
1825
1826 if (pane.previous_search)
1827 pdparams.itext = pane.previous_search;
1828
1829 rc = ui_prompt_dialog_create(edit.ui, &pdparams, &dialog);
1830 if (rc != EOK) {
1831 printf("Error creating prompt dialog.\n");
1832 return;
1833 }
1834
1835 ui_prompt_dialog_set_cb(dialog, &search_dialog_cb, &edit);
1836 pane.search_reverse = reverse;
1837}
1838
1839static void search_repeat(void)
1840{
1841 if (pane.previous_search == NULL) {
1842 status_display("No previous search to repeat.");
1843 return;
1844 }
1845
1846 search(pane.previous_search, pane.previous_search_reverse);
1847}
1848
1849static void search(char *pattern, bool reverse)
1850{
1851 status_display("Searching...");
1852
1853 spt_t sp, producer_pos;
1854 tag_get_pt(&pane.caret_pos, &sp);
1855
1856 /* Start searching on the position before/after caret */
1857 if (!reverse) {
1858 spt_next_char(sp, &sp);
1859 } else {
1860 spt_prev_char(sp, &sp);
1861 }
1862 producer_pos = sp;
1863
1864 search_ops_t ops = search_spt_ops;
1865 if (reverse)
1866 ops = search_spt_reverse_ops;
1867
1868 search_t *search = search_init(pattern, &producer_pos, ops, reverse);
1869 if (search == NULL) {
1870 status_display("Failed initializing search.");
1871 return;
1872 }
1873
1874 match_t match;
1875 errno_t rc = search_next_match(search, &match);
1876 if (rc != EOK) {
1877 status_display("Failed searching.");
1878 search_fini(search);
1879 }
1880
1881 if (match.end) {
1882 status_display("Match found.");
1883 assert(match.end != NULL);
1884 spt_t *end = match.end;
1885 caret_move(*end, false, true);
1886 while (match.length > 0) {
1887 match.length--;
1888 if (reverse) {
1889 spt_next_char(*end, end);
1890 } else {
1891 spt_prev_char(*end, end);
1892 }
1893 }
1894 caret_move(*end, true, true);
1895 free(end);
1896 } else {
1897 status_display("Not found.");
1898 }
1899
1900 search_fini(search);
1901}
1902
1903/** Check for non-empty selection. */
1904static bool selection_active(void)
1905{
1906 return (tag_cmp(&pane.caret_pos, &pane.sel_start) != 0);
1907}
1908
1909static void selection_get_points(spt_t *pa, spt_t *pb)
1910{
1911 spt_t pt;
1912
1913 tag_get_pt(&pane.sel_start, pa);
1914 tag_get_pt(&pane.caret_pos, pb);
1915
1916 if (spt_cmp(pa, pb) > 0) {
1917 pt = *pa;
1918 *pa = *pb;
1919 *pb = pt;
1920 }
1921}
1922
1923/** Delete selected text. */
1924static void selection_delete(void)
1925{
1926 spt_t pa, pb;
1927 coord_t ca, cb;
1928 int rel;
1929
1930 tag_get_pt(&pane.sel_start, &pa);
1931 tag_get_pt(&pane.caret_pos, &pb);
1932 spt_get_coord(&pa, &ca);
1933 spt_get_coord(&pb, &cb);
1934 rel = coord_cmp(&ca, &cb);
1935
1936 if (rel == 0)
1937 return;
1938
1939 if (rel < 0)
1940 sheet_delete(doc.sh, &pa, &pb);
1941 else
1942 sheet_delete(doc.sh, &pb, &pa);
1943
1944 if (ca.row == cb.row)
1945 pane.rflags |= REDRAW_ROW;
1946 else
1947 pane.rflags |= REDRAW_TEXT;
1948}
1949
1950/** Select all text in the editor */
1951static void selection_sel_all(void)
1952{
1953 spt_t spt, ept;
1954
1955 pt_get_sof(&spt);
1956 pt_get_eof(&ept);
1957
1958 selection_sel_range(spt, ept);
1959}
1960
1961/** Select select all text in a given range with the given direction */
1962static void selection_sel_range(spt_t pa, spt_t pb)
1963{
1964 sheet_remove_tag(doc.sh, &pane.sel_start);
1965 sheet_place_tag(doc.sh, &pa, &pane.sel_start);
1966 sheet_remove_tag(doc.sh, &pane.caret_pos);
1967 sheet_place_tag(doc.sh, &pb, &pane.caret_pos);
1968
1969 pane.rflags |= REDRAW_TEXT;
1970 caret_update();
1971}
1972
1973static void selection_copy(void)
1974{
1975 spt_t pa, pb;
1976 char *str;
1977
1978 selection_get_points(&pa, &pb);
1979 str = range_get_str(&pa, &pb);
1980 if (str == NULL || clipboard_put_str(str) != EOK) {
1981 status_display("Copying to clipboard failed!");
1982 }
1983 free(str);
1984}
1985
1986static void edit_paste(void)
1987{
1988 selection_delete();
1989 insert_clipboard_data();
1990 pane.rflags |= (REDRAW_TEXT | REDRAW_CARET);
1991 pane_update(&pane);
1992}
1993
1994static void edit_cut(void)
1995{
1996 selection_copy();
1997 selection_delete();
1998 pane.rflags |= (REDRAW_TEXT | REDRAW_CARET);
1999 pane_update(&pane);
2000}
2001
2002static void insert_clipboard_data(void)
2003{
2004 char *str;
2005 size_t off;
2006 char32_t c;
2007 errno_t rc;
2008
2009 rc = clipboard_get_str(&str);
2010 if (rc != EOK || str == NULL)
2011 return;
2012
2013 off = 0;
2014
2015 while (true) {
2016 c = str_decode(str, &off, STR_NO_LIMIT);
2017 if (c == '\0')
2018 break;
2019
2020 insert_char(c);
2021 }
2022
2023 free(str);
2024}
2025
2026/** Get start-of-file s-point. */
2027static void pt_get_sof(spt_t *pt)
2028{
2029 coord_t coord;
2030
2031 coord.row = coord.column = 1;
2032 sheet_get_cell_pt(doc.sh, &coord, dir_before, pt);
2033}
2034
2035/** Get end-of-file s-point. */
2036static void pt_get_eof(spt_t *pt)
2037{
2038 coord_t coord;
2039 int num_rows;
2040
2041 sheet_get_num_rows(doc.sh, &num_rows);
2042 coord.row = num_rows + 1;
2043 coord.column = 1;
2044
2045 sheet_get_cell_pt(doc.sh, &coord, dir_after, pt);
2046}
2047
2048/** Get start-of-line s-point for given s-point cpt */
2049static void pt_get_sol(spt_t *cpt, spt_t *spt)
2050{
2051 coord_t coord;
2052
2053 spt_get_coord(cpt, &coord);
2054 coord.column = 1;
2055
2056 sheet_get_cell_pt(doc.sh, &coord, dir_before, spt);
2057}
2058
2059/** Get end-of-line s-point for given s-point cpt */
2060static void pt_get_eol(spt_t *cpt, spt_t *ept)
2061{
2062 coord_t coord;
2063 int row_width;
2064
2065 spt_get_coord(cpt, &coord);
2066 sheet_get_row_width(doc.sh, coord.row, &row_width);
2067 coord.column = row_width - 1;
2068
2069 sheet_get_cell_pt(doc.sh, &coord, dir_after, ept);
2070}
2071
2072/** Check whether the spt is at a beginning of a word */
2073static bool pt_is_word_beginning(spt_t *pt)
2074{
2075 spt_t lp, sfp, efp, slp, elp;
2076 coord_t coord;
2077
2078 pt_get_sof(&sfp);
2079 pt_get_eof(&efp);
2080 pt_get_sol(pt, &slp);
2081 pt_get_eol(pt, &elp);
2082
2083 /* the spt is at the beginning or end of the file or line */
2084 if ((spt_cmp(&sfp, pt) == 0) || (spt_cmp(&efp, pt) == 0) ||
2085 (spt_cmp(&slp, pt) == 0) || (spt_cmp(&elp, pt) == 0))
2086 return true;
2087
2088 /* the spt is a delimiter */
2089 if (pt_is_delimiter(pt))
2090 return false;
2091
2092 spt_get_coord(pt, &coord);
2093
2094 coord.column -= 1;
2095 sheet_get_cell_pt(doc.sh, &coord, dir_before, &lp);
2096
2097 return pt_is_delimiter(&lp) ||
2098 (pt_is_punctuation(pt) && !pt_is_punctuation(&lp)) ||
2099 (pt_is_punctuation(&lp) && !pt_is_punctuation(pt));
2100}
2101
2102static char32_t get_first_wchar(const char *str)
2103{
2104 size_t offset = 0;
2105 return str_decode(str, &offset, str_size(str));
2106}
2107
2108static bool pt_is_delimiter(spt_t *pt)
2109{
2110 spt_t rp;
2111 coord_t coord;
2112 char *ch = NULL;
2113
2114 spt_get_coord(pt, &coord);
2115
2116 coord.column += 1;
2117 sheet_get_cell_pt(doc.sh, &coord, dir_after, &rp);
2118
2119 ch = range_get_str(pt, &rp);
2120 if (ch == NULL)
2121 return false;
2122
2123 char32_t first_char = get_first_wchar(ch);
2124 switch (first_char) {
2125 case ' ':
2126 case '\t':
2127 case '\n':
2128 return true;
2129 default:
2130 return false;
2131 }
2132}
2133
2134static bool pt_is_punctuation(spt_t *pt)
2135{
2136 spt_t rp;
2137 coord_t coord;
2138 char *ch = NULL;
2139
2140 spt_get_coord(pt, &coord);
2141
2142 coord.column += 1;
2143 sheet_get_cell_pt(doc.sh, &coord, dir_after, &rp);
2144
2145 ch = range_get_str(pt, &rp);
2146 if (ch == NULL)
2147 return false;
2148
2149 char32_t first_char = get_first_wchar(ch);
2150 switch (first_char) {
2151 case ',':
2152 case '.':
2153 case ';':
2154 case ':':
2155 case '/':
2156 case '?':
2157 case '\\':
2158 case '|':
2159 case '_':
2160 case '+':
2161 case '-':
2162 case '*':
2163 case '=':
2164 case '<':
2165 case '>':
2166 return true;
2167 default:
2168 return false;
2169 }
2170}
2171
2172/** Compare tags. */
2173static int tag_cmp(tag_t const *a, tag_t const *b)
2174{
2175 spt_t pa, pb;
2176
2177 tag_get_pt(a, &pa);
2178 tag_get_pt(b, &pb);
2179
2180 return spt_cmp(&pa, &pb);
2181}
2182
2183/** Compare s-points. */
2184static int spt_cmp(spt_t const *a, spt_t const *b)
2185{
2186 coord_t ca, cb;
2187
2188 spt_get_coord(a, &ca);
2189 spt_get_coord(b, &cb);
2190
2191 return coord_cmp(&ca, &cb);
2192}
2193
2194/** Compare coordinats. */
2195static int coord_cmp(coord_t const *a, coord_t const *b)
2196{
2197 if (a->row - b->row != 0)
2198 return a->row - b->row;
2199
2200 return a->column - b->column;
2201}
2202
2203/** Display text in the status line. */
2204static void status_display(char const *str)
2205{
2206 (void) ui_label_set_text(edit.status, str);
2207 (void) ui_label_paint(edit.status);
2208}
2209
2210/** Window close request
2211 *
2212 * @param window Window
2213 * @param arg Argument (edit_t *)
2214 */
2215static void edit_wnd_close(ui_window_t *window, void *arg)
2216{
2217 edit_t *edit = (edit_t *) arg;
2218
2219 ui_quit(edit->ui);
2220}
2221
2222/** Window keyboard event
2223 *
2224 * @param window Window
2225 * @param arg Argument (edit_t *)
2226 * @param event Keyboard event
2227 */
2228static void edit_wnd_kbd_event(ui_window_t *window, void *arg,
2229 kbd_event_t *event)
2230{
2231 pane.keymod = event->mods;
2232
2233 if (ui_window_def_kbd(window, event) == ui_claimed)
2234 return;
2235
2236 if (event->type == KEY_PRESS) {
2237 key_handle_press(event);
2238 (void) pane_update(&pane);
2239 (void) gfx_update(ui_window_get_gc(window));
2240 }
2241}
2242
2243/** File / Save menu entry selected.
2244 *
2245 * @param mentry Menu entry
2246 * @param arg Argument (edit_t *)
2247 */
2248static void edit_file_save(ui_menu_entry_t *mentry, void *arg)
2249{
2250 edit_t *edit = (edit_t *) arg;
2251
2252 (void)edit;
2253
2254 if (doc.file_name != NULL)
2255 file_save(doc.file_name);
2256 else
2257 file_save_as();
2258}
2259
2260/** File / Save As menu entry selected.
2261 *
2262 * @param mentry Menu entry
2263 * @param arg Argument (edit_t *)
2264 */
2265static void edit_file_save_as(ui_menu_entry_t *mentry, void *arg)
2266{
2267 edit_t *edit = (edit_t *) arg;
2268
2269 (void)edit;
2270 file_save_as();
2271}
2272
2273/** File / Exit menu entry selected.
2274 *
2275 * @param mentry Menu entry
2276 * @param arg Argument (edit_t *)
2277 */
2278static void edit_file_exit(ui_menu_entry_t *mentry, void *arg)
2279{
2280 edit_t *edit = (edit_t *) arg;
2281
2282 ui_quit(edit->ui);
2283}
2284
2285/** Edit / Cut menu entry selected.
2286 *
2287 * @param mentry Menu entry
2288 * @param arg Argument (edit_t *)
2289 */
2290static void edit_edit_cut(ui_menu_entry_t *mentry, void *arg)
2291{
2292 (void) arg;
2293 edit_cut();
2294 (void) gfx_update(ui_window_get_gc(edit.window));
2295}
2296
2297/** Edit / Copy menu entry selected.
2298 *
2299 * @param mentry Menu entry
2300 * @param arg Argument (edit_t *)
2301 */
2302static void edit_edit_copy(ui_menu_entry_t *mentry, void *arg)
2303{
2304 (void) arg;
2305 selection_copy();
2306}
2307
2308/** Edit / Paste menu entry selected.
2309 *
2310 * @param mentry Menu entry
2311 * @param arg Argument (edit_t *)
2312 */
2313static void edit_edit_paste(ui_menu_entry_t *mentry, void *arg)
2314{
2315 (void) arg;
2316 edit_paste();
2317 (void) gfx_update(ui_window_get_gc(edit.window));
2318}
2319
2320/** Edit / Delete menu entry selected.
2321 *
2322 * @param mentry Menu entry
2323 * @param arg Argument (edit_t *)
2324 */
2325static void edit_edit_delete(ui_menu_entry_t *mentry, void *arg)
2326{
2327 (void) arg;
2328
2329 if (selection_active())
2330 selection_delete();
2331
2332 pane.rflags |= REDRAW_CARET;
2333 (void) pane_update(&pane);
2334 (void) gfx_update(ui_window_get_gc(edit.window));
2335}
2336
2337/** Edit / Select All menu entry selected.
2338 *
2339 * @param mentry Menu entry
2340 * @param arg Argument (edit_t *)
2341 */
2342static void edit_edit_select_all(ui_menu_entry_t *mentry, void *arg)
2343{
2344 (void) arg;
2345
2346 selection_sel_all();
2347 pane.rflags |= (REDRAW_CARET | REDRAW_TEXT | REDRAW_STATUS);
2348 pane_update(&pane);
2349 (void) gfx_update(ui_window_get_gc(edit.window));
2350}
2351
2352/** Search / Find menu entry selected.
2353 *
2354 * @param mentry Menu entry
2355 * @param arg Argument (edit_t *)
2356 */
2357static void edit_search_find(ui_menu_entry_t *mentry, void *arg)
2358{
2359 (void) arg;
2360 search_prompt(false);
2361}
2362
2363/** Search / Reverse Find menu entry selected.
2364 *
2365 * @param mentry Menu entry
2366 * @param arg Argument (edit_t *)
2367 */
2368static void edit_search_reverse_find(ui_menu_entry_t *mentry, void *arg)
2369{
2370 (void) arg;
2371 search_prompt(true);
2372}
2373
2374/** Search / Find Next menu entry selected.
2375 *
2376 * @param mentry Menu entry
2377 * @param arg Argument (edit_t *)
2378 */
2379static void edit_search_find_next(ui_menu_entry_t *mentry, void *arg)
2380{
2381 (void) arg;
2382 search_repeat();
2383 (void) pane_update(&pane);
2384 (void) gfx_update(ui_window_get_gc(edit.window));
2385}
2386
2387/** Search / Go To Line menu entry selected.
2388 *
2389 * @param mentry Menu entry
2390 * @param arg Argument (edit_t *)
2391 */
2392static void edit_search_go_to_line(ui_menu_entry_t *mentry, void *arg)
2393{
2394 (void) arg;
2395 caret_go_to_line_ask();
2396}
2397
2398/** Save As dialog OK button press.
2399 *
2400 * @param dialog Save As dialog
2401 * @param arg Argument (ui_demo_t *)
2402 * @param fname File name
2403 */
2404static void save_as_dialog_bok(ui_file_dialog_t *dialog, void *arg,
2405 const char *fname)
2406{
2407 edit_t *edit = (edit_t *)arg;
2408 gfx_context_t *gc = ui_window_get_gc(edit->window);
2409 char *cname;
2410 errno_t rc;
2411
2412 ui_file_dialog_destroy(dialog);
2413 // TODO Smarter cursor management
2414 pane.rflags |= REDRAW_CARET;
2415 (void) pane_update(&pane);
2416 gfx_cursor_set_visible(gc, true);
2417
2418 cname = str_dup(fname);
2419 if (cname == NULL) {
2420 printf("Out of memory.\n");
2421 return;
2422 }
2423
2424 rc = file_save(fname);
2425 if (rc != EOK)
2426 return;
2427
2428 if (doc.file_name != NULL)
2429 free(doc.file_name);
2430 doc.file_name = cname;
2431
2432}
2433
2434/** Save As dialog cancel button press.
2435 *
2436 * @param dialog File dialog
2437 * @param arg Argument (ui_demo_t *)
2438 */
2439static void save_as_dialog_bcancel(ui_file_dialog_t *dialog, void *arg)
2440{
2441 edit_t *edit = (edit_t *)arg;
2442 gfx_context_t *gc = ui_window_get_gc(edit->window);
2443
2444 ui_file_dialog_destroy(dialog);
2445 // TODO Smarter cursor management
2446 pane.rflags |= REDRAW_CARET;
2447 (void) pane_update(&pane);
2448 gfx_cursor_set_visible(gc, true);
2449}
2450
2451/** Save As dialog close request.
2452 *
2453 * @param dialog File dialog
2454 * @param arg Argument (ui_demo_t *)
2455 */
2456static void save_as_dialog_close(ui_file_dialog_t *dialog, void *arg)
2457{
2458 edit_t *edit = (edit_t *)arg;
2459 gfx_context_t *gc = ui_window_get_gc(edit->window);
2460
2461 ui_file_dialog_destroy(dialog);
2462 // TODO Smarter cursor management
2463 pane.rflags |= REDRAW_CARET;
2464 (void) pane_update(&pane);
2465 gfx_cursor_set_visible(gc, true);
2466}
2467
2468/** Go To Line dialog OK button press.
2469 *
2470 * @param dialog Go To Line dialog
2471 * @param arg Argument (ui_demo_t *)
2472 * @param text Submitted text
2473 */
2474static void go_to_line_dialog_bok(ui_prompt_dialog_t *dialog, void *arg,
2475 const char *text)
2476{
2477 edit_t *edit = (edit_t *) arg;
2478 gfx_context_t *gc = ui_window_get_gc(edit->window);
2479 char *endptr;
2480 int line;
2481
2482 ui_prompt_dialog_destroy(dialog);
2483 line = strtol(text, &endptr, 10);
2484 if (*endptr != '\0') {
2485 status_display("Invalid number entered.");
2486 return;
2487 }
2488
2489 caret_move_absolute(line, pane.ideal_column, dir_before, false);
2490 // TODO Smarter cursor management
2491 (void) pane_update(&pane);
2492 gfx_cursor_set_visible(gc, true);
2493 (void) gfx_update(gc);
2494}
2495
2496/** Go To Line dialog cancel button press.
2497 *
2498 * @param dialog File dialog
2499 * @param arg Argument (ui_demo_t *)
2500 */
2501static void go_to_line_dialog_bcancel(ui_prompt_dialog_t *dialog, void *arg)
2502{
2503 edit_t *edit = (edit_t *) arg;
2504 gfx_context_t *gc = ui_window_get_gc(edit->window);
2505
2506 ui_prompt_dialog_destroy(dialog);
2507 // TODO Smarter cursor management
2508 pane.rflags |= REDRAW_CARET;
2509 (void) pane_update(&pane);
2510 gfx_cursor_set_visible(gc, true);
2511}
2512
2513/** Go To Line dialog close request.
2514 *
2515 * @param dialog File dialog
2516 * @param arg Argument (ui_demo_t *)
2517 */
2518static void go_to_line_dialog_close(ui_prompt_dialog_t *dialog, void *arg)
2519{
2520 edit_t *edit = (edit_t *) arg;
2521 gfx_context_t *gc = ui_window_get_gc(edit->window);
2522
2523 ui_prompt_dialog_destroy(dialog);
2524 // TODO Smarter cursor management
2525 pane.rflags |= REDRAW_CARET;
2526 (void) pane_update(&pane);
2527 gfx_cursor_set_visible(gc, true);
2528}
2529
2530/** Search dialog OK button press.
2531 *
2532 * @param dialog Search dialog
2533 * @param arg Argument (ui_demo_t *)
2534 * @param text Submitted text
2535 */
2536static void search_dialog_bok(ui_prompt_dialog_t *dialog, void *arg,
2537 const char *text)
2538{
2539 edit_t *edit = (edit_t *) arg;
2540 gfx_context_t *gc = ui_window_get_gc(edit->window);
2541 char *pattern;
2542 bool reverse;
2543
2544 ui_prompt_dialog_destroy(dialog);
2545
2546 /* Abort if search phrase is empty */
2547 if (text[0] == '\0')
2548 return;
2549
2550 pattern = str_dup(text);
2551 reverse = pane.search_reverse;
2552
2553 if (pane.previous_search)
2554 free(pane.previous_search);
2555 pane.previous_search = pattern;
2556 pane.previous_search_reverse = reverse;
2557
2558 search(pattern, reverse);
2559
2560 // TODO Smarter cursor management
2561 (void) pane_update(&pane);
2562 gfx_cursor_set_visible(gc, true);
2563 (void) gfx_update(gc);
2564}
2565
2566/** Search dialog cancel button press.
2567 *
2568 * @param dialog File dialog
2569 * @param arg Argument (ui_demo_t *)
2570 */
2571static void search_dialog_bcancel(ui_prompt_dialog_t *dialog, void *arg)
2572{
2573 edit_t *edit = (edit_t *) arg;
2574 gfx_context_t *gc = ui_window_get_gc(edit->window);
2575
2576 ui_prompt_dialog_destroy(dialog);
2577 // TODO Smarter cursor management
2578 pane.rflags |= REDRAW_CARET;
2579 (void) pane_update(&pane);
2580 gfx_cursor_set_visible(gc, true);
2581}
2582
2583/** Search dialog close request.
2584 *
2585 * @param dialog File dialog
2586 * @param arg Argument (ui_demo_t *)
2587 */
2588static void search_dialog_close(ui_prompt_dialog_t *dialog, void *arg)
2589{
2590 edit_t *edit = (edit_t *) arg;
2591 gfx_context_t *gc = ui_window_get_gc(edit->window);
2592
2593 ui_prompt_dialog_destroy(dialog);
2594 // TODO Smarter cursor management
2595 pane.rflags |= REDRAW_CARET;
2596 (void) pane_update(&pane);
2597 gfx_cursor_set_visible(gc, true);
2598}
2599
2600/** @}
2601 */
Note: See TracBrowser for help on using the repository browser.