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 input_mode
= NORMAL_MODE
;
104 void search_mode_ch(uchar ch
)
113 parse_line(&text
, &restricted
);
115 history_add_line(&search_history
, text
);
118 input_mode
= NORMAL_MODE
;
121 parse_line(&text
, &restricted
);
123 /* cmdline is "/", "?", "//" or "??" */
125 /* use old search string */
126 search_restricted
= restricted
;
127 search_found
= search_next(searchable
, search_str
, search_direction
);
130 /* set new search string and add it to the history */
132 search_str
= xstrdup(text
);
133 history_add_line(&search_history
, text
);
135 /* search not yet done if up or down arrow was pressed */
136 search_restricted
= restricted
;
137 search_found
= search(searchable
, search_str
, search_direction
, 0);
142 input_mode
= NORMAL_MODE
;
151 /* start from beginning if this is first char */
152 int beginning
= search_line_empty();
156 * don't set search_{str,restricted} here because
157 * search can be cancelled by pressing ESC
159 restricted
= search_restricted
;
161 cmdline_insert_ch(ch
);
162 parse_line(&text
, &search_restricted
);
163 search_found
= search(searchable
, text
, search_direction
, beginning
);
165 /* restore old value */
166 search_restricted
= restricted
;
170 reset_history_search();
173 void search_mode_key(int key
)
181 restricted
= search_restricted
;
184 parse_line(&text
, &search_restricted
);
186 search_found
= search(searchable
, text
, search_direction
, 0);
188 /* restore old value */
189 search_restricted
= restricted
;
198 cmdline_move_right();
207 parse_line(&text
, &restricted
);
208 if (history_search_text
== NULL
)
209 history_search_text
= xstrdup(text
);
210 text
= history_search_forward(&search_history
, history_search_text
);
212 update_search_line(text
, restricted
);
215 if (history_search_text
) {
216 parse_line(&text
, &restricted
);
217 text
= history_search_backward(&search_history
, history_search_text
);
219 update_search_line(text
, restricted
);
221 update_search_line(history_search_text
, restricted
);
228 reset_history_search();
231 void search_mode_init(void)
233 search_history_filename
= xstrjoin(cmus_config_dir
, "/search-history");
234 history_load(&search_history
, search_history_filename
, 100);
237 void search_mode_exit(void)
239 history_save(&search_history
);
240 free(search_history_filename
);