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 struct history search_history
;
49 static char *search_history_filename
;
50 static char *history_search_text
= NULL
;
52 static void update_search_line(const char *text
, int restricted
)
54 int len
= strlen(text
);
55 char ch
= search_direction
== SEARCH_FORWARD
? '/' : '?';
58 buf
= xnew(char, len
+ 2);
62 memcpy(ptr
, text
, len
+ 1);
63 cmdline_set_text(buf
);
67 static int search_line_empty(void)
71 if (cmdline
.clen
== 0)
75 ch
= search_direction
== SEARCH_FORWARD
? '/' : '?';
76 return cmdline
.line
[0] == ch
;
79 static void parse_line(const char **text
, int *restricted
)
81 char ch
= search_direction
== SEARCH_FORWARD
? '/' : '?';
84 if (cmdline
.line
[0] == ch
) {
85 /* //WORDS or ??WORDS */
88 *text
= cmdline
.line
+ r
;
92 static void reset_history_search(void)
94 history_reset_search(&search_history
);
95 free(history_search_text
);
96 history_search_text
= NULL
;
99 static void backspace(void)
101 if (cmdline
.clen
> 0) {
104 input_mode
= NORMAL_MODE
;
108 static void delete(void)
111 int restricted
= search_restricted
;
115 parse_line(&text
, &search_restricted
);
117 search(searchable
, text
, search_direction
, 0);
119 /* restore old value */
120 search_restricted
= restricted
;
123 void search_text(const char *text
, int restricted
, int beginning
)
126 /* cmdline is "/", "?", "//" or "??" */
128 /* use old search string */
129 search_restricted
= restricted
;
130 if (!search_next(searchable
, search_str
, search_direction
))
134 /* set new search string and add it to the history */
136 search_str
= xstrdup(text
);
137 history_add_line(&search_history
, text
);
139 /* search not yet done if up or down arrow was pressed */
140 search_restricted
= restricted
;
141 if (!search(searchable
, search_str
, search_direction
, beginning
))
146 void search_mode_ch(uchar ch
)
165 cmdline_move_right();
170 parse_line(&text
, &restricted
);
172 history_add_line(&search_history
, text
);
175 input_mode
= NORMAL_MODE
;
178 parse_line(&text
, &restricted
);
179 search_text(text
, restricted
, 0);
181 input_mode
= NORMAL_MODE
;
187 cmdline_backspace_to_bol();
197 /* start from beginning if this is first char */
198 int beginning
= search_line_empty();
202 * don't set search_{str,restricted} here because
203 * search can be cancelled by pressing ESC
205 restricted
= search_restricted
;
207 cmdline_insert_ch(ch
);
208 parse_line(&text
, &search_restricted
);
209 search(searchable
, text
, search_direction
, beginning
);
211 /* restore old value */
212 search_restricted
= restricted
;
216 reset_history_search();
219 void search_mode_key(int key
)
235 cmdline_move_right();
244 parse_line(&text
, &restricted
);
245 if (history_search_text
== NULL
)
246 history_search_text
= xstrdup(text
);
247 text
= history_search_forward(&search_history
, history_search_text
);
249 update_search_line(text
, restricted
);
252 if (history_search_text
) {
253 parse_line(&text
, &restricted
);
254 text
= history_search_backward(&search_history
, history_search_text
);
256 update_search_line(text
, restricted
);
258 update_search_line(history_search_text
, restricted
);
265 reset_history_search();
268 void search_mode_init(void)
270 search_history_filename
= xstrjoin(cmus_config_dir
, "/search-history");
271 history_load(&search_history
, search_history_filename
, 100);
274 void search_mode_exit(void)
276 history_save(&search_history
);
277 free(search_history_filename
);