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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
/* text.c
* Part of the Readline module for Tap. These are functions which
* manipulate the text in some way.
*/
/* Copyright (C) 1999 Jonathan duSaint
*
* This file is part of Tap, an interactive program for reading and
* writing data to the serial port to facilitate the reverse
* engineering of communication protocols.
*
* Tap 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 2, or (at your option)
* any later version.
*
* Tap 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.
*
* The GNU General Public License is generally kept in a file called
* COPYING or LICENSE. If you do not have a copy of the license, write
* to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "readline.h"
/* Move the cursor one space to the left,
* don't perform range check and don't update
* position in line
*/
void
cursor_left (void)
{
if (input_window->_curx == 0)
{
input_window->_cury--;
input_window->_curx = window_width - 1;
}
else
input_window->_curx--;
}
/* Move the cursor one space to the right,
* don't perform range check and don't update
* position in line
*/
void
cursor_right (void)
{
if (input_window->_curx + 1 == window_width)
{
input_window->_cury++;
input_window->_curx = 0;
}
else
input_window->_curx++;
}
/* Kill the region from the mark to the point;
* bound to C-w
*/
void
kill_region (void)
{
long kstart, kend;
/* find the region to be killed */
if (mark_start == -1)
{
warning_beep ();
return;
}
else if (mark_start == current_position)
{
mark_start = -1;
return;
}
else if (mark_start < current_position)
{
int k;
kstart = mark_start;
kend = current_position;
for (k = 0; k < kend - kstart; k++) cursor_left ();
}
else
{
kstart = current_position;
kend = mark_start;
}
kill_to_ring (kstart, kend);
memmove (current_line + kstart, current_line + kend,
chars_in_line - kend + 1);
chars_in_line -= kend - kstart;
mark_start = -1;
current_position = kstart;
}
/* Kill from the point to the end of the line;
* bound to C-k
*/
void
kill_to_eol (void)
{
kill_to_ring (current_position, chars_in_line);
current_line[current_position] = 0;
chars_in_line = current_position;
}
/* Move the cursor left, doing range check and
* updating position in line;
* bound to C-b and the left arrow key
*/
void
move_cursor_left (void)
{
if (current_position == 0)
{
warning_beep ();
return;
}
current_position--;
cursor_left ();
}
/* Move the cursor right, doing range check and
* updating position in line;
* bound to C-f and the right arrow key
*/
void
move_cursor_right (void)
{
if (current_position == chars_in_line)
{
warning_beep ();
return;
}
current_position++;
cursor_right ();
}
/* Move the cursor to the start of the line;
* bound to C-a
*/
void
move_cursor_to_start (void)
{
int k;
for (k = 0; k < current_position; k++) cursor_left ();
current_position = 0;
}
/* Move the cursor to the end of the line;
* bound to C-e
*/
void
move_cursor_to_end (void)
{
int k;
for (k = 0; k < chars_in_line - current_position; k++) cursor_right ();
current_position = chars_in_line;
}
/* Transpose the character under the point with the one
* to the left performing range checking;
* bound to C-t
*/
void
transpose_chars (void)
{
char ch;
if (current_position == 0 || current_position == chars_in_line)
{
warning_beep ();
return;
}
ch = current_line[current_position];
current_line[current_position] = current_line[current_position - 1];
current_line[current_position - 1] = ch;
move_cursor_right ();
}
/* Insert CH into the line at CURRENT_POSITION
*/
void
insert_char (char ch)
{
if (chars_in_line + 1 == line_size)
{
current_line = xrealloc (current_line, line_size + GROW_SIZE);
line_size += GROW_SIZE;
}
memmove (current_line + current_position + 1,
current_line + current_position,
chars_in_line - current_position + 1);
current_line[current_position] = ch;
cursor_right ();
current_position++;
chars_in_line++;
}
/* Delete the character either under the point
* or to the left depending on the direction;
* bound to C-d and DEL as RIGHT and C-h and
* BACKSPACE as LEFT
*/
void
delete_char (enum direction which)
{
/* check if there is a char to delete */
if ((which == LEFT && current_position == 0)
|| (which == RIGHT && current_position == chars_in_line))
{
warning_beep ();
return;
}
if (which == LEFT)
{
current_position--;
cursor_left ();
}
memmove (current_line + current_position,
current_line + current_position + 1,
chars_in_line - current_position);
chars_in_line--;
}
/* Yank text out of the kill ring and insert into the line;
* bound to C-y
*/
void
yank_text (void)
{
int k;
char *insert_text;
insert_text = get_ring_entry (repeat_value);
for (k = 0; k < strlen (insert_text); k++)
{
insert_char (insert_text[k]);
}
}
/* Read in a number and repeat the following command that
* many times, or if the next command is yank, uses the
* count as an index into the kill ring;
* bound to C-u
*/
void
do_count (void)
{
int k, y_save, ok, ch = 0, x0, y0, line_pos;
/* save the sojourn */
x0 = input_window->_curx;
y0 = input_window->_cury;
line_pos = current_position;
move_cursor_to_end ();
waddstr (input_window, "\nC-u> "); wrefresh (input_window);
y_save = input_window->_cury;
repeat_value = 0;
/* read in a number */
while (1)
{
ch = wgetch (input_window);
if (ch >= '0' && ch <= '9')
{
repeat_value *= 10;
repeat_value += ch - '0';
waddch (input_window, ch); wrefresh (input_window);
}
else
{
/* if ok == 1 , the number has been read */
ok = (ch == '\n') ? 1 : 0;
break;
}
}
if (ok) /* got a \n */
{
waddch (input_window, ' '); wrefresh (input_window);
ch = readchar ();
}
/* restore the old positions */
input_window->_cury = y_save;
input_window->_curx = 0;
waddnstr (input_window, " ", window_width); wrefresh (input_window);
input_window->_curx = x0;
input_window->_cury = y0;
current_position = line_pos;
if (!ok) /* if a valid number wasn't read, just return */
{
repeat_value = 1;
warning_beep ();
return;
}
if (ch == ECYANK)
yank_text ();
else if (ch == ECQUIT)
ungetch ('\n');
else if (ch == ECCOUNT)
warning_beep ();
else
{
for (k = 0; k < repeat_value; k++)
{
if (ch > 0xFF)
dispatch_special (ch);
else
insert_char (ch);
}
}
repeat_value = 1;
}
|