2 * Copyright 2004 Timo Hirvonen
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 #include "search_mode.h"
23 #include "ui_curses.h"
38 /* this is set in ui_curses.c */
39 enum search_direction search_direction
= SEARCH_FORWARD
;
41 /* current search string, this is set _only_ when user presses enter
42 * this string is used when 'n' or 'N' is pressed
43 * incremental search does not use this, it uses cmdline.line directly
45 char *search_str
= NULL
;
46 int search_restricted
= 0;
48 static int search_found
= 0;
49 static struct history search_history
;
50 static char *search_history_filename
;
51 static char *history_search_text
= NULL
;
53 static void update_search_line(const char *text
, int restricted
)
55 int len
= strlen(text
);
56 char ch
= search_direction
== SEARCH_FORWARD
? '/' : '?';
59 buf
= xnew(char, len
+ 2);
63 memcpy(ptr
, text
, len
+ 1);
64 cmdline_set_text(buf
);
68 static int search_line_empty(void)
72 if (cmdline
.clen
== 0)
76 ch
= search_direction
== SEARCH_FORWARD
? '/' : '?';
77 return cmdline
.line
[0] == ch
;
80 static void parse_line(const char **text
, int *restricted
)
82 char ch
= search_direction
== SEARCH_FORWARD
? '/' : '?';
85 if (cmdline
.line
[0] == ch
) {
86 /* //WORDS or ??WORDS */
89 *text
= cmdline
.line
+ r
;
93 static void reset_history_search(void)
95 history_reset_search(&search_history
);
96 free(history_search_text
);
97 history_search_text
= NULL
;
100 static void backspace(void)
102 if (cmdline
.clen
> 0) {
105 input_mode
= NORMAL_MODE
;
109 static void delete(void)
112 int restricted
= search_restricted
;
116 parse_line(&text
, &search_restricted
);
118 search_found
= search(searchable
, text
, search_direction
, 0);
120 /* restore old value */
121 search_restricted
= restricted
;
124 void search_mode_ch(uchar ch
)
143 cmdline_move_right();
148 parse_line(&text
, &restricted
);
150 history_add_line(&search_history
, text
);
153 input_mode
= NORMAL_MODE
;
156 parse_line(&text
, &restricted
);
158 /* cmdline is "/", "?", "//" or "??" */
160 /* use old search string */
161 search_restricted
= restricted
;
162 search_found
= search_next(searchable
, search_str
, search_direction
);
165 /* set new search string and add it to the history */
167 search_str
= xstrdup(text
);
168 history_add_line(&search_history
, text
);
170 /* search not yet done if up or down arrow was pressed */
171 search_restricted
= restricted
;
172 search_found
= search(searchable
, search_str
, search_direction
, 0);
177 input_mode
= NORMAL_MODE
;
183 cmdline_backspace_to_bol();
192 /* start from beginning if this is first char */
193 int beginning
= search_line_empty();
197 * don't set search_{str,restricted} here because
198 * search can be cancelled by pressing ESC
200 restricted
= search_restricted
;
202 cmdline_insert_ch(ch
);
203 parse_line(&text
, &search_restricted
);
204 search_found
= search(searchable
, text
, search_direction
, beginning
);
206 /* restore old value */
207 search_restricted
= restricted
;
211 reset_history_search();
214 void search_mode_key(int key
)
230 cmdline_move_right();
239 parse_line(&text
, &restricted
);
240 if (history_search_text
== NULL
)
241 history_search_text
= xstrdup(text
);
242 text
= history_search_forward(&search_history
, history_search_text
);
244 update_search_line(text
, restricted
);
247 if (history_search_text
) {
248 parse_line(&text
, &restricted
);
249 text
= history_search_backward(&search_history
, history_search_text
);
251 update_search_line(text
, restricted
);
253 update_search_line(history_search_text
, restricted
);
260 reset_history_search();
263 void search_mode_init(void)
265 search_history_filename
= xstrjoin(cmus_config_dir
, "/search-history");
266 history_load(&search_history
, search_history_filename
, 100);
269 void search_mode_exit(void)
271 history_save(&search_history
);
272 free(search_history_filename
);