-
Notifications
You must be signed in to change notification settings - Fork 33
/
athame_readline.h
175 lines (145 loc) · 4.58 KB
/
athame_readline.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* athame_readline.h -- Full vim integration for your shell.*/
/* Copyright (C) 2015 James Kolb
This file is part of Athame.
Athame is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Athame is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Athame. If not, see <http://www.gnu.org/licenses/>.
*/
#define READLINE_LIBRARY
#include <wchar.h>
#include "history.h"
#include "readline.h"
#include "rlprivate.h"
#include "xmalloc.h"
char* athame_buffer_store = 0;
static char* ap_get_line_buffer() {
if (rl_end == 0) {
return "";
}
if (athame_buffer_store) {
free(athame_buffer_store);
}
return athame_buffer_store = strndup(rl_line_buffer, rl_end);
}
static int ap_get_line_buffer_length() { return rl_end; }
static int helper_ap_char_length(char* buffer) {
int ret;
int len = mbstowcs(NULL, buffer, 0);
return len;
}
static int ap_get_line_char_length() {
return helper_ap_char_length(ap_get_line_buffer());
}
static void ap_set_line_buffer(char* newText) { rl_replace_line(newText, 0); }
static int ap_get_cursor() {
int ret;
char* temp = strndup(rl_line_buffer, rl_point);
ret = helper_ap_char_length(temp);
free(temp);
return ret;
}
static void ap_set_cursor(int c) {
rl_point = 0;
if (c == 0) {
return;
}
rl_forward_char(c, 'l');
}
static void ap_set_cursor_end() { rl_point = rl_end; }
static int ap_temp_novim() { return 0; }
static void ap_redraw_prompt() {
// Substitutions are already performed before readline gets the prompt.
}
static void ap_display() { rl_redisplay(); }
static void ap_get_term_size(int* height, int* width) {
_rl_sigwinch_resize_terminal(); // Incase the terminal changed size while
// readline wasn't looking.
rl_get_screen_size(height, width);
}
static int ap_get_prompt_length() { return rla_prompt_phys_length(); }
HISTORY_STATE* hs;
int hs_counter;
static void ap_get_history_start() {
hs = history_get_history_state();
hs_counter = 0;
}
static char* ap_get_history_next() {
if (hs->entries && hs->entries[hs_counter]) {
return hs->entries[hs_counter++]->line;
} else {
return NULL;
}
}
static void ap_get_history_end() { xfree(hs); }
static int ap_needs_to_leave() {
return rl_done || rl_num_chars_to_read > 0 && rl_end >= rl_num_chars_to_read;
}
static char* ap_get_substr(char* text, int start, int end) {
int mbchars;
int pos_s = 0;
int pos = 0;
for (mbchars = 0; mbchars < end; mbchars++) {
if (mbchars == start) {
pos_s = pos;
}
int l = mblen(text + pos, MB_CUR_MAX);
if (l >= 0) {
pos += l;
} else {
if (mbchars < start) {
return strdup("");
}
break;
}
}
return strndup(text + pos_s, pos - pos_s);
}
static char ap_handle_signals() {
if (_rl_caught_signal == SIGINT || _rl_caught_signal == SIGHUP) {
athame_cleanup(_rl_caught_signal == SIGHUP);
_rl_signal_handler(_rl_caught_signal);
if (rl_signal_event_hook) {
(*rl_signal_event_hook)();
}
return '\x03'; //<C-C>
}
return 0;
}
static char ap_delete;
static char ap_special[KEYMAP_SIZE];
static char ap_nl[KEYMAP_SIZE];
static void ap_set_control_chars() {
// In default readline these are: tab, <C-D>, <C-L>, and all the newline keys.
int specialLen = 0;
int nlLen = 0;
ap_delete = '\x04';
for (int key = 0; key < KEYMAP_SIZE; key++) {
if (_rl_keymap[key].type == ISFUNC) {
if (_rl_keymap[key].function == rl_delete) {
ap_delete = key;
ap_special[specialLen++] = key;
} else if (_rl_keymap[key].function == rl_newline) {
ap_special[specialLen++] = key;
ap_nl[nlLen++] = key;
} else if (_rl_keymap[key].function == rl_complete ||
_rl_keymap[key].function == rl_menu_complete ||
_rl_keymap[key].function == rl_backward_menu_complete ||
_rl_keymap[key].function == rl_clear_screen) {
ap_special[specialLen++] = key;
}
}
}
ap_special[specialLen] = '\0';
}
// Tells readline that we weren't in the middle of tab completion, search, etc.
static void ap_set_nospecial() { rl_last_func = rl_insert; }
static int ap_is_catching_signals() {
return rl_catch_signals;
}