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>
33 /* this is set in ui_curses.c */
34 enum search_direction search_direction
= SEARCH_FORWARD
;
36 /* current search string, this is set _only_ when user presses enter
37 * this string is used when 'n' or 'N' is pressed
38 * incremental search does not use this, it uses cmdline.line directly
40 char *search_str
= NULL
;
41 int search_restricted
= 0;
43 static int search_found
= 0;
44 static struct history search_history
;
45 static char *search_history_filename
;
46 static char *history_search_text
= NULL
;
48 static void update_search_line(const char *text
, int restricted
)
50 int len
= strlen(text
);
51 char ch
= search_direction
== SEARCH_FORWARD
? '/' : '?';
54 buf
= xnew(char, len
+ 2);
58 memcpy(ptr
, text
, len
+ 1);
59 cmdline_set_text(buf
);
63 static int search_line_empty(void)
67 if (cmdline
.clen
== 0)
71 ch
= search_direction
== SEARCH_FORWARD
? '/' : '?';
72 return cmdline
.line
[0] == ch
;
75 static void parse_line(const char **text
, int *restricted
)
77 char ch
= search_direction
== SEARCH_FORWARD
? '/' : '?';
80 if (cmdline
.line
[0] == ch
) {
81 /* //WORDS or ??WORDS */
84 *text
= cmdline
.line
+ r
;
88 static void reset_history_search(void)
90 history_reset_search(&search_history
);
91 free(history_search_text
);
92 history_search_text
= NULL
;
95 static void backspace(void)
97 if (cmdline
.clen
> 0) {
100 ui_curses_input_mode
= NORMAL_MODE
;
104 void search_mode_ch(uchar ch
)
111 parse_line(&text
, &restricted
);
113 history_add_line(&search_history
, text
);
116 ui_curses_input_mode
= NORMAL_MODE
;
119 parse_line(&text
, &restricted
);
121 /* cmdline is "/", "?", "//" or "??" */
123 /* use old search string */
124 search_restricted
= restricted
;
125 search_found
= search_next(searchable
, search_str
, search_direction
);
128 /* set new search string and add it to the history */
130 search_str
= xstrdup(text
);
131 history_add_line(&search_history
, text
);
133 /* search not yet done if up or down arrow was pressed */
134 search_restricted
= restricted
;
135 search_found
= search(searchable
, search_str
, search_direction
, 0);
139 ui_curses_search_not_found();
140 ui_curses_input_mode
= NORMAL_MODE
;
149 /* start from beginning if this is first char */
150 int beginning
= search_line_empty();
154 * don't set search_{str,restricted} here because
155 * search can be cancelled by pressing ESC
157 restricted
= search_restricted
;
159 cmdline_insert_ch(ch
);
160 parse_line(&text
, &search_restricted
);
161 search_found
= search(searchable
, text
, search_direction
, beginning
);
163 /* restore old value */
164 search_restricted
= restricted
;
168 reset_history_search();
171 void search_mode_key(int key
)
179 restricted
= search_restricted
;
182 parse_line(&text
, &search_restricted
);
184 search_found
= search(searchable
, text
, search_direction
, 0);
186 /* restore old value */
187 search_restricted
= restricted
;
196 cmdline_move_right();
205 parse_line(&text
, &restricted
);
206 if (history_search_text
== NULL
)
207 history_search_text
= xstrdup(text
);
208 text
= history_search_forward(&search_history
, history_search_text
);
210 update_search_line(text
, restricted
);
213 if (history_search_text
) {
214 parse_line(&text
, &restricted
);
215 text
= history_search_backward(&search_history
, history_search_text
);
217 update_search_line(text
, restricted
);
219 update_search_line(history_search_text
, restricted
);
226 reset_history_search();
229 void search_mode_init(void)
231 search_history_filename
= xstrjoin(cmus_cache_dir
, "/ui_curses_search_history");
232 history_load(&search_history
, search_history_filename
, 100);
235 void search_mode_exit(void)
237 history_save(&search_history
);
238 free(search_history_filename
);