forked from cmus/cmus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_mode.c
332 lines (303 loc) · 7.24 KB
/
search_mode.c
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
/*
* Copyright 2008-2013 Various Authors
* Copyright 2004 Timo Hirvonen
*
* This program 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 of the
* License, or (at your option) any later version.
*
* This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "search_mode.h"
#include "cmdline.h"
#include "history.h"
#include "ui_curses.h"
#include "search.h"
#include "xmalloc.h"
#include "xstrjoin.h"
#include "misc.h"
#include "lib.h"
#include "command_mode.h"
#include "keys.h"
#include <ctype.h>
/* this is set in ui_curses.c */
enum search_direction search_direction = SEARCH_FORWARD;
/* current search string, this is set _only_ when user presses enter
* this string is used when 'n' or 'N' is pressed
* incremental search does not use this, it uses cmdline.line directly
*/
char *search_str = NULL;
int search_restricted = 0;
static struct history search_history;
static char *search_history_filename;
static char *history_search_text = NULL;
static void update_search_line(const char *text, int restricted)
{
int len = strlen(text);
char ch = search_direction == SEARCH_FORWARD ? '/' : '?';
char *buf, *ptr;
buf = xnew(char, len + 2);
ptr = buf;
if (restricted)
*ptr++ = ch;
memcpy(ptr, text, len + 1);
cmdline_set_text(buf);
free(buf);
}
static int search_line_empty(void)
{
char ch;
if (cmdline.clen == 0)
return 1;
if (cmdline.clen > 1)
return 0;
ch = search_direction == SEARCH_FORWARD ? '/' : '?';
return cmdline.line[0] == ch;
}
static void parse_line(const char **text, int *restricted)
{
char ch = search_direction == SEARCH_FORWARD ? '/' : '?';
int r = 0;
if (cmdline.line[0] == ch) {
/* //WORDS or ??WORDS */
r = 1;
}
*text = cmdline.line + r;
*restricted = r;
}
static void reset_history_search(void)
{
history_reset_search(&search_history);
free(history_search_text);
history_search_text = NULL;
}
static void backspace(void)
{
if (cmdline.clen > 0) {
cmdline_backspace();
} else {
input_mode = NORMAL_MODE;
}
}
static void delete(void)
{
/* save old value */
int restricted = search_restricted;
const char *text;
cmdline_delete_ch();
parse_line(&text, &search_restricted);
if (text[0])
search(searchable, text, search_direction, 0);
/* restore old value */
search_restricted = restricted;
}
void search_text(const char *text, int restricted, int beginning)
{
if (text[0] == 0) {
/* cmdline is "/", "?", "//" or "??" */
if (search_str) {
/* use old search string */
search_restricted = restricted;
if (!search_next(searchable, search_str, search_direction))
search_not_found();
}
} else {
/* set new search string and add it to the history */
free(search_str);
search_str = xstrdup(text);
history_add_line(&search_history, text);
/* search not yet done if up or down arrow was pressed */
search_restricted = restricted;
if (!search(searchable, search_str, search_direction, beginning))
search_not_found();
}
}
void search_mode_ch(uchar ch)
{
const char *text;
int restricted;
switch (ch) {
case 0x01: // ^A
cmdline_move_home();
break;
case 0x02: // ^B
cmdline_move_left();
break;
case 0x04: // ^D
delete();
break;
case 0x05: // ^E
cmdline_move_end();
break;
case 0x06: // ^F
cmdline_move_right();
break;
case 0x03: // ^C
case 0x07: // ^G
case 0x1B: // ESC
parse_line(&text, &restricted);
if (text[0]) {
history_add_line(&search_history, text);
cmdline_clear();
}
input_mode = NORMAL_MODE;
break;
case 0x0A:
parse_line(&text, &restricted);
search_text(text, restricted, 0);
cmdline_clear();
input_mode = NORMAL_MODE;
break;
case 0x0B:
cmdline_clear_end();
break;
case 0x10: // ^P
search_mode_key(KEY_UP);
return;
case 0xE: // ^N
search_mode_key(KEY_DOWN);
return;
case 0x15:
cmdline_backspace_to_bol();
break;
case 0x17: // ^W
cmdline_backward_delete_word(cmdline_word_delimiters);
break;
case 0x08: // ^H
case 127:
backspace();
break;
default:
if (ch < 0x20) {
return;
} else {
/* start from beginning if this is first char */
int beginning = search_line_empty();
/* save old value
*
* don't set search_{str,restricted} here because
* search can be cancelled by pressing ESC
*/
restricted = search_restricted;
cmdline_insert_ch(ch);
parse_line(&text, &search_restricted);
search(searchable, text, search_direction, beginning);
/* restore old value */
search_restricted = restricted;
}
break;
}
reset_history_search();
}
void search_mode_escape(int c)
{
switch (c) {
case 98:
cmdline_backward_word(cmdline_filename_delimiters);
break;
case 100:
cmdline_delete_word(cmdline_filename_delimiters);
break;
case 102:
cmdline_forward_word(cmdline_filename_delimiters);
break;
case 127:
case KEY_BACKSPACE:
cmdline_backward_delete_word(cmdline_filename_delimiters);
break;
}
reset_history_search();
}
void search_mode_key(int key)
{
const char *text;
int restricted;
switch (key) {
case KEY_DC:
delete();
break;
case KEY_BACKSPACE:
backspace();
break;
case KEY_LEFT:
cmdline_move_left();
return;
case KEY_RIGHT:
cmdline_move_right();
return;
case KEY_HOME:
cmdline_move_home();
return;
case KEY_END:
cmdline_move_end();
return;
case KEY_UP:
parse_line(&text, &restricted);
if (history_search_text == NULL)
history_search_text = xstrdup(text);
text = history_search_forward(&search_history, history_search_text);
if (text)
update_search_line(text, restricted);
return;
case KEY_DOWN:
if (history_search_text) {
parse_line(&text, &restricted);
text = history_search_backward(&search_history, history_search_text);
if (text) {
update_search_line(text, restricted);
} else {
update_search_line(history_search_text, restricted);
}
}
return;
default:
return;
}
reset_history_search();
}
void search_mode_mouse(MEVENT *event)
{
if ((event->bstate & BUTTON1_PRESSED) || (event->bstate & BUTTON3_PRESSED)) {
const char *text;
int restricted;
if (event->y <= window_get_nr_rows(current_win()) + 2) {
parse_line(&text, &restricted);
if (text[0]) {
history_add_line(&search_history, text);
cmdline_clear();
}
input_mode = NORMAL_MODE;
normal_mode_mouse(event);
return;
}
if (event->x == 0)
return;
int i = event->x > cmdline.clen ? cmdline.clen : event->x - 1;
while (i < cmdline.cpos)
cmdline_move_left();
while (i > cmdline.cpos)
cmdline_move_right();
} else if (event->bstate & BUTTON4_PRESSED) {
search_mode_key(KEY_UP);
} else if (event->bstate & BUTTON5_PRESSED) {
search_mode_key(KEY_DOWN);
}
}
void search_mode_init(void)
{
search_history_filename = xstrjoin(cmus_config_dir, "/search-history");
history_load(&search_history, search_history_filename, 100);
}
void search_mode_exit(void)
{
history_save(&search_history);
history_free(&search_history);
free(search_history_filename);
}