diff options
Diffstat (limited to 'examples/dte/selection.c')
| -rw-r--r-- | examples/dte/selection.c | 110 |
1 files changed, 0 insertions, 110 deletions
diff --git a/examples/dte/selection.c b/examples/dte/selection.c deleted file mode 100644 index 5ddc67c..0000000 --- a/examples/dte/selection.c +++ /dev/null | |||
| @@ -1,110 +0,0 @@ | |||
| 1 | #include "selection.h" | ||
| 2 | #include "editor.h" | ||
| 3 | #include "util/unicode.h" | ||
| 4 | |||
| 5 | static bool include_cursor_char_in_selection(const View *view) | ||
| 6 | { | ||
| 7 | const EditorState *e = view->window->editor; | ||
| 8 | if (!e->options.select_cursor_char) { | ||
| 9 | return false; | ||
| 10 | } | ||
| 11 | |||
| 12 | bool overwrite = view->buffer->options.overwrite; | ||
| 13 | CursorInputMode mode = overwrite ? CURSOR_MODE_OVERWRITE : CURSOR_MODE_INSERT; | ||
| 14 | TermCursorType type = e->cursor_styles[mode].type; | ||
| 15 | if (type == CURSOR_KEEP) { | ||
| 16 | type = e->cursor_styles[CURSOR_MODE_DEFAULT].type; | ||
| 17 | } | ||
| 18 | |||
| 19 | // If "select-cursor-char" option is true, include character under cursor | ||
| 20 | // in selections for any cursor type except bars (where it makes no sense | ||
| 21 | // to do so) | ||
| 22 | return !(type == CURSOR_STEADY_BAR || type == CURSOR_BLINKING_BAR); | ||
| 23 | } | ||
| 24 | |||
| 25 | void init_selection(const View *view, SelectionInfo *info) | ||
| 26 | { | ||
| 27 | info->so = view->sel_so; | ||
| 28 | info->eo = block_iter_get_offset(&view->cursor); | ||
| 29 | info->si = view->cursor; | ||
| 30 | block_iter_goto_offset(&info->si, info->so); | ||
| 31 | info->swapped = false; | ||
| 32 | if (info->so > info->eo) { | ||
| 33 | size_t o = info->so; | ||
| 34 | info->so = info->eo; | ||
| 35 | info->eo = o; | ||
| 36 | info->si = view->cursor; | ||
| 37 | info->swapped = true; | ||
| 38 | } | ||
| 39 | |||
| 40 | BlockIter ei = info->si; | ||
| 41 | block_iter_skip_bytes(&ei, info->eo - info->so); | ||
| 42 | if (block_iter_is_eof(&ei)) { | ||
| 43 | if (info->so == info->eo) { | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | CodePoint u; | ||
| 47 | info->eo -= block_iter_prev_char(&ei, &u); | ||
| 48 | } | ||
| 49 | |||
| 50 | if (view->selection == SELECT_LINES) { | ||
| 51 | info->so -= block_iter_bol(&info->si); | ||
| 52 | info->eo += block_iter_eat_line(&ei); | ||
| 53 | } else { | ||
| 54 | if (include_cursor_char_in_selection(view)) { | ||
| 55 | info->eo += block_iter_next_column(&ei); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | size_t prepare_selection(View *view) | ||
| 61 | { | ||
| 62 | SelectionInfo info; | ||
| 63 | init_selection(view, &info); | ||
| 64 | view->cursor = info.si; | ||
| 65 | return info.eo - info.so; | ||
| 66 | } | ||
| 67 | |||
| 68 | char *view_get_selection(View *view, size_t *size) | ||
| 69 | { | ||
| 70 | if (view->selection == SELECT_NONE) { | ||
| 71 | *size = 0; | ||
| 72 | return NULL; | ||
| 73 | } | ||
| 74 | |||
| 75 | BlockIter save = view->cursor; | ||
| 76 | *size = prepare_selection(view); | ||
| 77 | char *buf = block_iter_get_bytes(&view->cursor, *size); | ||
| 78 | view->cursor = save; | ||
| 79 | return buf; | ||
| 80 | } | ||
| 81 | |||
| 82 | size_t get_nr_selected_lines(const SelectionInfo *info) | ||
| 83 | { | ||
| 84 | BlockIter bi = info->si; | ||
| 85 | size_t pos = info->so; | ||
| 86 | CodePoint u = 0; | ||
| 87 | size_t nr_lines = 1; | ||
| 88 | |||
| 89 | while (pos < info->eo) { | ||
| 90 | if (u == '\n') { | ||
| 91 | nr_lines++; | ||
| 92 | } | ||
| 93 | pos += block_iter_next_char(&bi, &u); | ||
| 94 | } | ||
| 95 | return nr_lines; | ||
| 96 | } | ||
| 97 | |||
| 98 | size_t get_nr_selected_chars(const SelectionInfo *info) | ||
| 99 | { | ||
| 100 | BlockIter bi = info->si; | ||
| 101 | size_t pos = info->so; | ||
| 102 | CodePoint u; | ||
| 103 | size_t nr_chars = 0; | ||
| 104 | |||
| 105 | while (pos < info->eo) { | ||
| 106 | nr_chars++; | ||
| 107 | pos += block_iter_next_char(&bi, &u); | ||
| 108 | } | ||
| 109 | return nr_chars; | ||
| 110 | } | ||
