Line | Branch | Exec | Source |
---|---|---|---|
1 | #include "selection.h" | ||
2 | #include "editor.h" | ||
3 | #include "util/unicode.h" | ||
4 | |||
5 | 2 | static bool include_cursor_char_in_selection(const View *view) | |
6 | { | ||
7 | 2 | const EditorState *e = view->window->editor; | |
8 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (!e->options.select_cursor_char) { |
9 | return false; | ||
10 | } | ||
11 | |||
12 | 2 | bool overwrite = view->buffer->options.overwrite; | |
13 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | CursorInputMode mode = overwrite ? CURSOR_MODE_OVERWRITE : CURSOR_MODE_INSERT; |
14 | 2 | TermCursorType type = e->cursor_styles[mode].type; | |
15 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (type == CURSOR_KEEP) { |
16 | 2 | 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 | 2 | return !(type == CURSOR_STEADY_BAR || type == CURSOR_BLINKING_BAR); | |
23 | } | ||
24 | |||
25 | 14 | SelectionInfo init_selection(const View *view) | |
26 | { | ||
27 | 14 | size_t so = view->sel_so; | |
28 | 14 | size_t eo = block_iter_get_offset(&view->cursor); | |
29 | 14 | bool swapped = (so > eo); | |
30 | |||
31 | 42 | SelectionInfo info = { | |
32 | .si = view->cursor, | ||
33 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9 times.
|
14 | .so = swapped ? eo : so, |
34 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9 times.
|
14 | .eo = swapped ? so : eo, |
35 | .swapped = swapped, | ||
36 | }; | ||
37 | |||
38 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9 times.
|
14 | if (!swapped) { |
39 | 5 | block_iter_goto_offset(&info.si, so); | |
40 | } | ||
41 | |||
42 | 14 | BlockIter ei = info.si; | |
43 | 14 | block_iter_skip_bytes(&ei, info.eo - info.so); | |
44 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
|
14 | if (block_iter_is_eof(&ei)) { |
45 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (info.so == info.eo) { |
46 | ✗ | return info; | |
47 | } | ||
48 | 1 | CodePoint u; | |
49 | 1 | info.eo -= block_iter_prev_char(&ei, &u); | |
50 | } | ||
51 | |||
52 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
|
14 | if (view->selection == SELECT_LINES) { |
53 | 12 | info.so -= block_iter_bol(&info.si); | |
54 | 12 | info.eo += block_iter_eat_line(&ei); | |
55 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | } else if (include_cursor_char_in_selection(view)) { |
56 | 2 | info.eo += block_iter_next_column(&ei); | |
57 | } | ||
58 | |||
59 | 14 | return info; | |
60 | } | ||
61 | |||
62 | 5 | size_t prepare_selection(View *view) | |
63 | { | ||
64 | 5 | SelectionInfo info = init_selection(view); | |
65 | 5 | view->cursor = info.si; | |
66 | 5 | return info.eo - info.so; | |
67 | } | ||
68 | |||
69 | 9 | size_t get_nr_selected_lines(const SelectionInfo *info) | |
70 | { | ||
71 | 9 | BlockIter bi = info->si; | |
72 | 9 | size_t nr_lines = 0; | |
73 | |||
74 |
2/2✓ Branch 0 taken 45 times.
✓ Branch 1 taken 9 times.
|
54 | for (size_t pos = info->so, eo = info->eo; pos < eo; nr_lines++) { |
75 | 45 | pos += block_iter_eat_line(&bi); | |
76 | 45 | BUG_ON(block_iter_is_eof(&bi) && pos != info->eo); | |
77 | } | ||
78 | |||
79 | 9 | return nr_lines; | |
80 | } | ||
81 | |||
82 | ✗ | size_t get_nr_selected_chars(const SelectionInfo *info) | |
83 | { | ||
84 | ✗ | BlockIter bi = info->si; | |
85 | ✗ | size_t nr_chars = 0; | |
86 | ✗ | CodePoint u; | |
87 | |||
88 | ✗ | for (size_t pos = info->so, eo = info->eo; pos < eo; nr_chars++) { | |
89 | ✗ | pos += block_iter_next_char(&bi, &u); | |
90 | } | ||
91 | |||
92 | ✗ | return nr_chars; | |
93 | } | ||
94 |