3 * selector is a simple shell command for selection of strings with a
4 * dynamic pattern-matching.
6 * Copyright (c) 2009 Francois Fleuret
7 * Written by Francois Fleuret <francois@fleuret.org>
9 * This file is part of selector.
11 * selector is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 3 as
13 * published by the Free Software Foundation.
15 * selector is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with selector. If not, see <http://www.gnu.org/licenses/>.
25 // To use it as a super-history-search for bash:
27 // alias h='selector -d -i -b -v -f <(history)'
29 // This software is highly Linux-specific, but I would be glad to get
30 // patches to make it work on other OS
40 #include <sys/ioctl.h>
48 const int buffer_size = 1024;
50 // Yeah, global variables!
52 int nb_lines_max = 1000;
53 char pattern_separator = ';';
54 int output_to_vt_buffer = 0;
56 int zsh_history = 0, bash_history = 0;
57 int inverse_order = 0;
58 int remove_duplicates = 0;
61 //////////////////////////////////////////////////////////////////////
63 // This looks severely Linux-only ...
65 void inject_into_tty_buffer(char *line) {
66 struct termios oldtio, newtio;
67 tcgetattr(STDIN_FILENO,&oldtio);
68 memset(&newtio, 0, sizeof(newtio));
69 // Set input mode (non-canonical, *no echo*,...)
70 tcsetattr(STDIN_FILENO, TCSANOW, &newtio);
71 // Put the selected line in the tty input buffer
72 for(char *k = line; *k; k++) {
73 ioctl(STDIN_FILENO, TIOCSTI, k);
75 // Restore the old settings
76 tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
79 //////////////////////////////////////////////////////////////////////
81 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
82 if(n_opt + n >= argc) {
83 cerr << "Missing argument for " << argv[n_opt] << "."
85 << "Expecting " << help << "."
91 //////////////////////////////////////////////////////////////////////
92 // A quick and dirty hash table
94 int *new_hash_table(int hash_table_size) {
96 result = new int[hash_table_size];
97 for(int k = 0; k < hash_table_size; k++) {
103 int test_and_add(char *new_string, int new_index,
104 char **strings, int *hash_table, int hash_table_size) {
105 unsigned int code = 0;
107 // This is my recipe. I checked, it seems to work (as long as
108 // hash_table_size is not a multiple of 387433 that should be okay)
110 for(int k = 0; new_string[k]; k++) {
111 code = code * 387433 + (unsigned int) (new_string[k]);
114 code = code % hash_table_size;
116 while(hash_table[code] >= 0) {
117 if(strcmp(new_string, strings[hash_table[code]]) == 0) {
118 int result = hash_table[code];
119 hash_table[code] = new_index;
122 code = (code + 1) % hash_table_size;
125 hash_table[code] = new_index;
130 //////////////////////////////////////////////////////////////////////
131 // A matcher matches either with a collection of substrings, or with a
138 char *splitted_patterns, **patterns;
141 int match(char *string, matcher_t *matcher) {
142 if(matcher->nb_patterns >= 0) {
143 for(int n = 0; n < matcher->nb_patterns; n++) {
144 if(strstr(string, matcher->patterns[n]) == 0) return 0;
148 return regexec(&matcher->preg, string, 0, 0, 0) == 0;
152 void free_matcher(matcher_t *matcher) {
153 if(matcher->nb_patterns >= 0) {
154 delete[] matcher->splitted_patterns;
155 delete[] matcher->patterns;
157 if(!matcher->regexp_error) regfree(&matcher->preg);
161 void initialize_matcher(int use_regexp, matcher_t *matcher, const char *pattern) {
163 matcher->nb_patterns = -1;
164 matcher->regexp_error = regcomp(&matcher->preg, pattern, REG_ICASE);
166 matcher->regexp_error = 0;
167 matcher->nb_patterns = 1;
169 for(const char *s = pattern; *s; s++) {
170 if(*s == pattern_separator) {
171 matcher->nb_patterns++;
175 matcher->splitted_patterns = new char[strlen(pattern) + 1];
176 matcher->patterns = new char*[matcher->nb_patterns];
178 strcpy(matcher->splitted_patterns, pattern);
181 char *last_pattern_start = matcher->splitted_patterns;
182 for(char *s = matcher->splitted_patterns; n < matcher->nb_patterns; s++) {
183 if(*s == pattern_separator || *s == '\0') {
185 matcher->patterns[n++] = last_pattern_start;
186 last_pattern_start = s + 1;
192 //////////////////////////////////////////////////////////////////////
194 int previous_visible(int current_line, int nb_lines, char **lines, matcher_t *matcher) {
195 int line = current_line - 1;
196 while(line >= 0 && !match(lines[line], matcher)) line--;
200 int next_visible(int current_line, int nb_lines, char **lines, matcher_t *matcher) {
201 int line = current_line + 1;
202 while(line < nb_lines && !match(lines[line], matcher)) line++;
210 //////////////////////////////////////////////////////////////////////
212 void update_screen(int *current_line, int *temporary_line, int motion,
213 int nb_lines, char **lines,
216 char buffer[buffer_size];
219 initialize_matcher(use_regexp, &matcher, pattern);
221 // We now take care of printing the lines per se
223 int console_width = getmaxx(stdscr);
224 int console_height = getmaxy(stdscr);
226 // First, we find a visible line. In priority: The current, or the
227 // first visible after it, or the first visible before it.
229 int nb_printed_lines = 0;
232 use_default_colors();
235 if(matcher.regexp_error) {
236 addstr("[regexp error]");
240 if(match(lines[*current_line], &matcher)) {
241 new_line = *current_line;
243 new_line = next_visible(*current_line, nb_lines, lines, &matcher);
245 new_line = previous_visible(*current_line, nb_lines, lines, &matcher);
249 // If we found a visible line and we should move, let's move
251 if(new_line >= 0 && motion != 0) {
254 // We want to go down, let's find the first visible line below
255 for(int m = 0; l >= 0 && m < motion; m++) {
256 l = next_visible(l, nb_lines, lines, &matcher);
262 // We want to go up, let's find the first visible line above
263 for(int m = 0; l >= 0 && m < -motion; m++) {
264 l = previous_visible(l, nb_lines, lines, &matcher);
272 // Here new_line is either a line number matching the patterns, or -1
276 int first_line = new_line, last_line = new_line, nb_match = 1;
278 // We find the first and last line to show, so that the total of
279 // visible lines between them (them include) is console_height - 1
281 while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
285 while(first_line > 0 && !match(lines[first_line], &matcher)) {
288 if(match(lines[first_line], &matcher)) {
293 if(nb_match < console_height - 1 && last_line < nb_lines - 1) {
295 while(last_line < nb_lines - 1 && !match(lines[last_line], &matcher)) {
299 if(match(lines[last_line], &matcher)) {
305 // Now we display them
307 for(int l = first_line; l <= last_line; l++) {
308 if(match(lines[l], &matcher)) {
311 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
312 buffer[k] = lines[l][k];
316 // We fill the rest of the line with blanks if either we did
317 // not clear() or if this is the highlighted line
320 while(k < console_width) {
328 // Highlight the highlighted line ...
332 attron(COLOR_PAIR(2));
333 addnstr(buffer, console_width);
334 attroff(COLOR_PAIR(2));
337 addnstr(buffer, console_width);
341 addnstr(buffer, console_width);
349 *current_line = new_line;
353 *temporary_line = new_line;
355 if(nb_printed_lines == 0) {
356 addnstr("[no selection]\n", console_width);
362 sprintf(buffer, "%d/%d pattern: %s%s",
366 use_regexp ? " [regexp]" : "");
368 for(int k = strlen(buffer); k < console_width; k++) buffer[k] = ' ';
369 buffer[console_width] = '\0';
373 attron(COLOR_PAIR(1));
374 addnstr(buffer, console_width);
375 attroff(COLOR_PAIR(1));
378 addnstr(buffer, console_width);
385 free_matcher(&matcher);
388 //////////////////////////////////////////////////////////////////////
390 int main(int argc, char **argv) {
392 if(!ttyname(STDIN_FILENO)) {
393 cerr << "The standard input is not a tty." << endl;
397 char buffer[buffer_size], raw_line[buffer_size];;
398 int color_fg_modeline, color_bg_modeline;
399 int color_fg_highlight, color_bg_highlight;
401 color_fg_modeline = COLOR_WHITE;
402 color_bg_modeline = COLOR_BLACK;
403 color_fg_highlight = COLOR_BLACK;
404 color_bg_highlight = COLOR_YELLOW;
406 setlocale(LC_ALL, "");
408 char input_filename[buffer_size], output_filename[buffer_size];
410 strcpy(input_filename, "");
411 strcpy(output_filename, "");
414 int error = 0, show_help = 0;
416 while(!error && !show_help && i < argc) {
418 if(strcmp(argv[i], "-o") == 0) {
419 check_opt(argc, argv, i, 1, "<output filename>");
420 strncpy(output_filename, argv[i+1], buffer_size);
424 else if(strcmp(argv[i], "-s") == 0) {
425 check_opt(argc, argv, i, 1, "<pattern separator>");
426 pattern_separator = argv[i+1][0];
430 else if(strcmp(argv[i], "-v") == 0) {
431 output_to_vt_buffer = 1;
435 else if(strcmp(argv[i], "-m") == 0) {
440 else if(strcmp(argv[i], "-f") == 0) {
441 check_opt(argc, argv, i, 1, "<input filename>");
442 strncpy(input_filename, argv[i+1], buffer_size);
446 else if(strcmp(argv[i], "-i") == 0) {
451 else if(strcmp(argv[i], "-b") == 0) {
456 else if(strcmp(argv[i], "-z") == 0) {
461 else if(strcmp(argv[i], "-d") == 0) {
462 remove_duplicates = 1;
466 else if(strcmp(argv[i], "-e") == 0) {
471 else if(strcmp(argv[i], "-l") == 0) {
472 check_opt(argc, argv, i, 1, "<maximum number of lines>");
473 nb_lines_max = atoi(argv[i+1]);
477 else if(strcmp(argv[i], "-c") == 0) {
478 check_opt(argc, argv, i, 4, "<fg modeline> <bg modeline> <fg highlight> <bg highlight>");
479 color_fg_modeline = atoi(argv[i+1]);
480 color_bg_modeline = atoi(argv[i+2]);
481 color_fg_highlight = atoi(argv[i+3]);
482 color_bg_highlight = atoi(argv[i+4]);
486 else if(strcmp(argv[i], "-h") == 0) {
492 cerr << "Unknown argument " << argv[i] << "." << endl;
497 if(show_help || error) {
498 cerr << "Selector version " << VERSION << "-R" << REVISION_NUMBER
500 << "Written by Francois Fleuret <francois@fleuret.org>."
512 << " [-c <fg modeline> <bg modeline> <fg highlight> <bg highlight>]"
513 << " [-o <output filename>]"
514 << " [-s <pattern separator>]"
515 << " [-l <max number of lines>]"
516 << " -f <input filename>"
522 char **lines = new char *[nb_lines_max];
524 if(!input_filename[0]) {
525 cerr << "You must specify a input file with -f." << endl;
531 ifstream file(input_filename);
534 cerr << "Can not open " << input_filename << endl;
538 int hash_table_size = nb_lines_max * 10;
541 if(remove_duplicates) {
542 hash_table = new_hash_table(hash_table_size);
545 while(nb_lines < nb_lines_max && !file.eof()) {
547 file.getline(raw_line, buffer_size);
549 if(strcmp(raw_line, "") != 0) {
558 while(*u) { *s++ = *u++; }
564 if(zsh_history && *s == ':') {
565 while(*s && *s != ';') s++;
569 if(bash_history && (*s == ' ' || (*s >= '0' && *s <= '9'))) {
570 while(*s == ' ' || (*s >= '0' && *s <= '9')) s++;
576 dup = test_and_add(s, nb_lines, lines, hash_table, hash_table_size);
582 lines[nb_lines] = new char[strlen(s) + 1];
583 strcpy(lines[nb_lines], s);
585 // We do not allocate a new string but use the pointer to the
586 // first occurence of it
587 lines[nb_lines] = lines[dup];
597 // Now remove the null strings
600 for(int k = 0; k < nb_lines; k++) {
602 lines[n++] = lines[k];
608 for(int i = 0; i < nb_lines/2; i++) {
609 char *s = lines[nb_lines - 1 - i];
610 lines[nb_lines - 1 - i] = lines[i];
615 char pattern[buffer_size];
620 //////////////////////////////////////////////////////////////////////
621 // Here we start to display with curse
630 // So that the arrow keys work
631 keypad(stdscr, TRUE);
636 if(color_fg_modeline < 0 || color_fg_modeline >= COLORS ||
637 color_bg_modeline < 0 || color_bg_modeline >= COLORS ||
638 color_fg_highlight < 0 || color_bg_highlight >= COLORS ||
639 color_bg_highlight < 0 || color_bg_highlight >= COLORS) {
643 cerr << "Color numbers have to be between 0 and " << COLORS - 1 << "." << endl;
646 init_pair(1, color_fg_modeline, color_bg_modeline);
647 init_pair(2, color_fg_highlight, color_bg_highlight);
654 int current_line = 0, temporary_line = 0;
656 update_screen(¤t_line, &temporary_line, 0, nb_lines, lines, pattern);
664 if(key >= ' ' && key <= '~') {
665 pattern[pattern_point++] = key;
666 pattern[pattern_point] = '\0';
669 else if(key == KEY_BACKSPACE || key == '
\b' || key == '
\7f' ||
670 key == KEY_DC || key == '
\ 4') {
671 if(pattern_point > 0) {
673 pattern[pattern_point] = '\0';
677 else if(key == KEY_HOME) {
681 else if(key == KEY_END) {
682 current_line = nb_lines - 1;
685 else if(key == KEY_NPAGE) {
689 else if(key == KEY_PPAGE) {
693 else if(key == KEY_DOWN || key == '
\ e') {
697 else if(key == KEY_UP || key == '
\10') {
701 else if(key == '
\12') {
702 use_regexp = !use_regexp;
705 else if(key == '
\15') {
707 pattern[pattern_point] = '\0';
710 update_screen(¤t_line, &temporary_line, motion,
711 nb_lines, lines, pattern);
713 } while(key != '\n' && key != KEY_ENTER && key != '
\a');
719 //////////////////////////////////////////////////////////////////////
720 // Here we come back to standard display
722 if((key == KEY_ENTER || key == '\n')) {
724 if(output_to_vt_buffer) {
725 if(temporary_line >= 0 && temporary_line < nb_lines) {
726 inject_into_tty_buffer(lines[temporary_line]);
730 if(output_filename[0]) {
731 ofstream out(output_filename);
733 cerr << "Can not open " << output_filename << " for writing." << endl;
736 if(temporary_line >= 0 && temporary_line < nb_lines) {
737 out << lines[temporary_line] << endl;
747 for(int l = 0; l < nb_lines; l++) {