3 * selector is a simple command line utility for selection of strings
4 * with a 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:
26 // selector -q -b -i -d -v -w -l ${HISTSIZE} <(history)
36 #include <sys/ioctl.h>
43 const int buffer_size = 4096;
45 // Yeah, global variables!
47 int nb_lines_max = 1000;
48 char pattern_separator = ';';
49 char label_separator = '\0';
50 int output_to_vt_buffer = 0;
51 int add_control_qs = 0;
55 int inverse_order = 0;
56 int remove_duplicates = 0;
58 int case_sensitive = 0;
62 int attr_modeline, attr_focus_line, attr_error;
64 //////////////////////////////////////////////////////////////////////
66 void inject_into_tty_buffer(char *string) {
67 struct termios oldtio, newtio;
69 tcgetattr(STDIN_FILENO, &oldtio);
70 memset(&newtio, 0, sizeof(newtio));
71 // Set input mode (non-canonical, *no echo*,...)
72 tcsetattr(STDIN_FILENO, TCSANOW, &newtio);
73 const char control_q = '\021';
74 // Put the selected string in the tty input buffer
75 for(k = string; *k; k++) {
76 if(add_control_qs && !(*k >= ' ' && *k <= '~')) {
77 // Add ^Q to quote control characters
78 ioctl(STDIN_FILENO, TIOCSTI, &control_q);
80 ioctl(STDIN_FILENO, TIOCSTI, k);
82 // Restore the old settings
83 tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
86 //////////////////////////////////////////////////////////////////////
88 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
89 if(n_opt + n >= argc) {
90 fprintf(stderr, "Missing argument for %s, expecting %s.\n",
96 int string_to_positive_integer(char *string) {
102 for(s = string; *s; s++) {
103 if(*s >= '0' && *s <= '9') {
104 result = result * 10 + (int) (*s - '0');
110 fprintf(stderr, "Value `%s' is not a positive integer.\n", string);
117 void error_feedback() {
125 //////////////////////////////////////////////////////////////////////
126 // A quick and dirty hash table
128 // The table itself stores indexes of the strings taken in a char
129 // **table. When a string is added, if it was already in the table,
130 // the new index replaces the previous one.
132 int *new_hash_table(int hash_table_size) {
134 result = (int *) malloc(hash_table_size * sizeof(int));
135 for(k = 0; k < hash_table_size; k++) {
141 // Adds new_string in the table, associated to new_index. If this
142 // string was not already in the table, returns -1. Otherwise, returns
143 // the previous index it had.
145 int test_and_add(char *new_string, int new_index,
147 int *hash_table, int hash_table_size) {
149 unsigned int code = 0;
152 // This is my recipe. I checked, it seems to work (as long as
153 // hash_table_size is not a multiple of 387433 that should be okay)
155 for(k = 0; new_string[k]; k++) {
156 code = code * 387433 + (unsigned int) (new_string[k]);
159 code = code % hash_table_size;
161 while(hash_table[code] >= 0) {
162 // There is a string with that code
163 if(strcmp(new_string, strings[hash_table[code]]) == 0) {
164 // It is the same string, we keep a copy of the stored index
165 int result = hash_table[code];
167 hash_table[code] = new_index;
168 // And return the previous one
171 // This collision was not the same string, let's move to the next
173 code = (code + 1) % hash_table_size;
176 // This string was not already in there, store the index in the
177 // table and return -1
178 hash_table[code] = new_index;
182 //////////////////////////////////////////////////////////////////////
183 // A matcher matches either with a collection of substrings, or with a
191 char *splitted_patterns, **patterns;
194 int match(char *string, matcher_t *matcher) {
196 if(matcher->nb_patterns >= 0) {
197 if(matcher->case_sensitive) {
198 for(n = 0; n < matcher->nb_patterns; n++) {
199 if(strstr(string, matcher->patterns[n]) == 0) return 0;
202 for(n = 0; n < matcher->nb_patterns; n++) {
203 if(strcasestr(string, matcher->patterns[n]) == 0) return 0;
208 return regexec(&matcher->preg, string, 0, 0, 0) == 0;
212 void free_matcher(matcher_t *matcher) {
213 if(matcher->nb_patterns < 0) {
214 if(!matcher->regexp_error) regfree(&matcher->preg);
216 free(matcher->splitted_patterns);
217 free(matcher->patterns);
221 void initialize_matcher(int use_regexp, int case_sensitive,
222 matcher_t *matcher, const char *pattern) {
227 matcher->nb_patterns = -1;
228 matcher->regexp_error = regcomp(&matcher->preg, pattern, case_sensitive ? 0 : REG_ICASE);
230 matcher->regexp_error = 0;
231 matcher->nb_patterns = 1;
232 matcher->case_sensitive = case_sensitive;
234 for(s = pattern; *s; s++) {
235 if(*s == pattern_separator) {
236 matcher->nb_patterns++;
240 matcher->splitted_patterns = (char *) malloc((strlen(pattern) + 1) * sizeof(char));
241 matcher->patterns = (char **) malloc(matcher->nb_patterns * sizeof(char *));
243 strcpy(matcher->splitted_patterns, pattern);
246 char *last_pattern_start = matcher->splitted_patterns;
247 for(t = matcher->splitted_patterns; n < matcher->nb_patterns; t++) {
248 if(*t == pattern_separator || *t == '\0') {
250 matcher->patterns[n++] = last_pattern_start;
251 last_pattern_start = t + 1;
257 //////////////////////////////////////////////////////////////////////
260 void delete_char(char *buffer, int *position) {
261 if(buffer[*position]) {
263 while(c < buffer_size && buffer[c]) {
264 buffer[c] = buffer[c+1];
267 } else error_feedback();
270 void backspace_char(char *buffer, int *position) {
272 if(buffer[*position]) {
273 int c = *position - 1;
275 buffer[c] = buffer[c+1];
279 buffer[*position - 1] = '\0';
283 } else error_feedback();
286 void insert_char(char *buffer, int *position, char character) {
287 if(strlen(buffer) < buffer_size - 1) {
289 char t = buffer[c], u;
298 buffer[(*position)++] = character;
299 } else error_feedback();
302 void kill_before_cursor(char *buffer, int *position) {
304 while(buffer[*position + s]) {
305 buffer[s] = buffer[*position + s];
312 void kill_after_cursor(char *buffer, int *position) {
313 buffer[*position] = '\0';
316 //////////////////////////////////////////////////////////////////////
318 int previous_visible(int current_line, int nb_lines, char **lines, matcher_t *matcher) {
319 int line = current_line - 1;
320 while(line >= 0 && !match(lines[line], matcher)) line--;
324 int next_visible(int current_line, int nb_lines, char **lines, matcher_t *matcher) {
325 int line = current_line + 1;
326 while(line < nb_lines && !match(lines[line], matcher)) line++;
334 //////////////////////////////////////////////////////////////////////
336 // The value passed to this routine in current_focus_line is the index
337 // of the line we should have highlited if there was no motion and if
338 // it matched the matcher. So, the line actually highlighted is the
339 // first one matching the matcher in that order: (1)
340 // current_focus_line after motion, (2) the first with a greater
341 // index, (3) the first with a lesser index.
343 // The index of the line actually shown highlighted is written in
344 // displayed_focus_line (it can be -1)
346 // If there is a motion and a line is actually shown highlighted, its
347 // value is written in current_focus_line.
349 void update_screen(int *current_focus_line, int *displayed_focus_line,
351 int nb_lines, char **lines,
355 char buffer[buffer_size];
359 initialize_matcher(use_regexp, case_sensitive, &matcher, pattern);
361 int console_width = getmaxx(stdscr);
362 int console_height = getmaxy(stdscr);
364 // First, we find a visible line.
366 int nb_printed_lines = 0;
368 use_default_colors();
372 if(matcher.regexp_error) {
374 addnstr("Regexp syntax error", console_width);
376 } else if(nb_lines > 0) {
378 if(match(lines[*current_focus_line], &matcher)) {
379 new_focus_line = *current_focus_line;
381 new_focus_line = next_visible(*current_focus_line, nb_lines, lines, &matcher);
382 if(new_focus_line < 0) {
383 new_focus_line = previous_visible(*current_focus_line, nb_lines, lines, &matcher);
387 // If we found a visible line and we should move, let's move
389 if(new_focus_line >= 0 && motion != 0) {
390 int l = new_focus_line;
392 // We want to go down, let's find the first visible line below
393 for(m = 0; l >= 0 && m < motion; m++) {
394 l = next_visible(l, nb_lines, lines, &matcher);
400 // We want to go up, let's find the first visible line above
401 for(m = 0; l >= 0 && m < -motion; m++) {
402 l = previous_visible(l, nb_lines, lines, &matcher);
410 // Here new_focus_line is either a line number matching the pattern, or -1
412 if(new_focus_line >= 0) {
414 int first_line = new_focus_line, last_line = new_focus_line, nb_match = 1;
416 // We find the first and last line to show, so that the total of
417 // visible lines between them (them included) is console_height-1
419 while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
423 while(first_line > 0 && !match(lines[first_line], &matcher)) {
426 if(match(lines[first_line], &matcher)) {
431 if(nb_match < console_height - 1 && last_line < nb_lines - 1) {
433 while(last_line < nb_lines - 1 && !match(lines[last_line], &matcher)) {
437 if(match(lines[last_line], &matcher)) {
443 // Now we display them
445 for(l = first_line; l <= last_line; l++) {
446 if(match(lines[l], &matcher)) {
449 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
450 buffer[k] = lines[l][k];
454 // We fill the rest of the line with blanks if this is the
457 if(l == new_focus_line) {
458 while(k < console_width) {
468 // Highlight the highlighted line ...
470 if(l == new_focus_line) {
471 attron(attr_focus_line);
472 addnstr(buffer, console_width);
473 attroff(attr_focus_line);
475 addnstr(buffer, console_width);
482 // If we are on a focused line and we moved, this become the new
486 *current_focus_line = new_focus_line;
490 *displayed_focus_line = new_focus_line;
492 if(nb_printed_lines == 0) {
494 addnstr("No selection", console_width);
499 addnstr("Empty choice", console_width);
509 attron(attr_modeline);
511 for(k = 0; k < console_width; k++) buffer[k] = ' ';
512 buffer[console_width] = '\0';
513 addnstr(buffer, console_width);
517 // There must be a more elegant way of moving the cursor at a
518 // location met during display
525 cursor_x += strlen(title) + 1;
528 sprintf(buffer, "%d/%d ", nb_printed_lines, nb_lines);
530 cursor_x += strlen(buffer);
532 addnstr(pattern, cursor_position);
533 cursor_x += cursor_position;
535 if(pattern[cursor_position]) {
536 addstr(pattern + cursor_position);
541 if(use_regexp || case_sensitive) {
558 attroff(attr_modeline);
563 free_matcher(&matcher);
566 //////////////////////////////////////////////////////////////////////
568 void read_file(const char *input_filename,
569 int nb_lines_max, int *nb_lines, char **lines,
570 int hash_table_size, int *hash_table) {
572 char raw_line[buffer_size];
574 FILE *file = fopen(input_filename, "r");
577 fprintf(stderr, "Can not open `%s'.\n", input_filename);
581 int start = 0, end = 0, k;
583 while(*nb_lines < nb_lines_max && (end > start || !feof(file))) {
585 while(eol < end && raw_line[eol] != '\n') eol++;
588 for(k = 0; k < end - start; k++) {
589 raw_line[k] = raw_line[k + start];
594 end += fread(raw_line + end, sizeof(char), buffer_size - end, file);
595 while(eol < end && raw_line[eol] != '\n') eol++;
598 if(eol == buffer_size) {
599 raw_line[buffer_size - 1] = '\0';
600 fprintf(stderr, "Line too long:\n");
601 fprintf(stderr, raw_line);
602 fprintf(stderr, "\n");
606 raw_line[eol] = '\0';
608 char *t = raw_line + start;
610 // Remove the zsh history prefix
612 if(zsh_history && *t == ':') {
613 while(*t && *t != ';') t++;
617 // Remove the bash history prefix
620 while(*t == ' ') t++;
621 while(*t >= '0' && *t <= '9') t++;
622 while(*t == ' ') t++;
625 // Check for duplicates with the hash table and insert the line
626 // in the list if necessary
631 dup = test_and_add(t, *nb_lines, lines, hash_table, hash_table_size);
637 lines[*nb_lines] = (char *) malloc((strlen(t) + 1) * sizeof(char));
638 strcpy(lines[*nb_lines], t);
640 // The string was already in there, so we do not allocate a
641 // new string but use the pointer to the first occurence of it
642 lines[*nb_lines] = lines[dup];
652 //////////////////////////////////////////////////////////////////////
654 int main(int argc, char **argv) {
656 if(!ttyname(STDIN_FILENO)) {
657 fprintf(stderr, "The standard input is not a tty.\n");
661 char input_filename[buffer_size], output_filename[buffer_size];
663 int error = 0, show_help = 0;
664 int rest_are_files = 0;
666 int color_fg_modeline, color_bg_modeline;
667 int color_fg_highlight, color_bg_highlight;
669 color_fg_modeline = COLOR_WHITE;
670 color_bg_modeline = COLOR_BLACK;
671 color_fg_highlight = COLOR_BLACK;
672 color_bg_highlight = COLOR_YELLOW;
674 setlocale(LC_ALL, "");
676 strcpy(input_filename, "");
677 strcpy(output_filename, "");
680 while(!error && !show_help && i < argc && argv[i][0] == '-' && !rest_are_files) {
682 if(strcmp(argv[i], "-o") == 0) {
683 check_opt(argc, argv, i, 1, "<output filename>");
684 strncpy(output_filename, argv[i+1], buffer_size);
688 else if(strcmp(argv[i], "-s") == 0) {
689 check_opt(argc, argv, i, 1, "<pattern separator>");
690 pattern_separator = argv[i+1][0];
694 else if(strcmp(argv[i], "-x") == 0) {
695 check_opt(argc, argv, i, 1, "<label separator>");
696 label_separator = argv[i+1][0];
700 else if(strcmp(argv[i], "-v") == 0) {
701 output_to_vt_buffer = 1;
705 else if(strcmp(argv[i], "-w") == 0) {
710 else if(strcmp(argv[i], "-m") == 0) {
715 else if(strcmp(argv[i], "-q") == 0) {
720 else if(strcmp(argv[i], "-f") == 0) {
721 check_opt(argc, argv, i, 1, "<input filename>");
722 strncpy(input_filename, argv[i+1], buffer_size);
726 else if(strcmp(argv[i], "-i") == 0) {
731 else if(strcmp(argv[i], "-b") == 0) {
736 else if(strcmp(argv[i], "-z") == 0) {
741 else if(strcmp(argv[i], "-d") == 0) {
742 remove_duplicates = 1;
746 else if(strcmp(argv[i], "-e") == 0) {
751 else if(strcmp(argv[i], "-a") == 0) {
756 else if(strcmp(argv[i], "-t") == 0) {
757 check_opt(argc, argv, i, 1, "<title>");
759 title = (char *) malloc((strlen(argv[i+1]) + 1) * sizeof(char));
760 strcpy(title, argv[i+1]);
764 else if(strcmp(argv[i], "-l") == 0) {
765 check_opt(argc, argv, i, 1, "<maximum number of lines>");
766 nb_lines_max = string_to_positive_integer(argv[i+1]);
770 else if(strcmp(argv[i], "-c") == 0) {
771 check_opt(argc, argv, i, 4, "<fg modeline> <bg modeline> <fg highlight> <bg highlight>");
772 color_fg_modeline = string_to_positive_integer(argv[i + 1]);
773 color_bg_modeline = string_to_positive_integer(argv[i + 2]);
774 color_fg_highlight = string_to_positive_integer(argv[i + 3]);
775 color_bg_highlight = string_to_positive_integer(argv[i + 4]);
779 else if(strcmp(argv[i], "--") == 0) {
784 else if(strcmp(argv[i], "-h") == 0) {
790 fprintf(stderr, "Unknown option %s.\n", argv[i]);
795 if(show_help || error) {
796 fprintf(stderr, "Selector version %s-R%s\n", VERSION, REVISION_NUMBER);
797 fprintf(stderr, "Written by Francois Fleuret <francois@fleuret.org>.\n");
798 fprintf(stderr, "Usage: %s [options] [<filename1> [<filename2> ...]]\n", argv[0]);
799 fprintf(stderr, "\n");
800 fprintf(stderr, " -h show this help\n");
801 fprintf(stderr, " -v inject the selected line in the tty\n");
802 fprintf(stderr, " -w quote control characters with ^Qs when using -v\n");
803 fprintf(stderr, " -d remove duplicated lines\n");
804 fprintf(stderr, " -b remove the bash history line prefix\n");
805 fprintf(stderr, " -z remove the zsh history line prefix\n");
806 fprintf(stderr, " -i invert the order of lines\n");
807 fprintf(stderr, " -e start in regexp mode\n");
808 fprintf(stderr, " -a start in case sensitive mode\n");
809 fprintf(stderr, " -m monochrome mode\n");
810 fprintf(stderr, " -q make a flash instead of a beep on an edition error\n");
811 fprintf(stderr, " -- all following arguments are filenames\n");
812 fprintf(stderr, " -t <title>\n");
813 fprintf(stderr, " add a title in the modeline\n");
814 fprintf(stderr, " -c <fg modeline> <bg modeline> <fg highlight> <bg highlight>\n");
815 fprintf(stderr, " set the display colors\n");
816 fprintf(stderr, " -o <output filename>\n");
817 fprintf(stderr, " set a file to write the selected line to\n");
818 fprintf(stderr, " -s <pattern separator>\n");
819 fprintf(stderr, " set the symbol to separate substrings in the pattern\n");
820 fprintf(stderr, " -x <label separator>\n");
821 fprintf(stderr, " set the symbol to terminate the label\n");
822 fprintf(stderr, " -l <max number of lines>\n");
823 fprintf(stderr, " set the maximum number of lines to take into account\n");
824 fprintf(stderr, "\n");
828 char **lines = (char **) malloc(nb_lines_max * sizeof(char *));
831 int hash_table_size = nb_lines_max * 10;
834 if(remove_duplicates) {
835 hash_table = new_hash_table(hash_table_size);
838 if(input_filename[0]) {
839 read_file(input_filename,
840 nb_lines_max, &nb_lines, lines,
841 hash_table_size, hash_table);
846 nb_lines_max, &nb_lines, lines,
847 hash_table_size, hash_table);
853 // Now remove the null strings
856 for(k = 0; k < nb_lines; k++) {
858 lines[n++] = lines[k];
865 for(i = 0; i < nb_lines / 2; i++) {
866 char *s = lines[nb_lines - 1 - i];
867 lines[nb_lines - 1 - i] = lines[i];
872 // Build the labels from the strings, take only the part before the
873 // label_separator and transform control characters to printable
876 char **labels = (char **) malloc(nb_lines * sizeof(char *));
877 for(l = 0; l < nb_lines; l++) {
882 while(*t && *t != label_separator) {
886 labels[l] = (char *) malloc((e + 1) * sizeof(char));
889 while(*t && *t != label_separator) {
891 while(*u) { *s++ = *u++; }
896 char pattern[buffer_size];
902 //////////////////////////////////////////////////////////////////////
903 // Here we start to display with curse
909 intrflush(stdscr, FALSE);
911 // So that the arrow keys work
912 keypad(stdscr, TRUE);
914 attr_error = A_STANDOUT;
915 attr_modeline = A_REVERSE;
916 attr_focus_line = A_STANDOUT;
918 if(with_colors && has_colors()) {
922 if(color_fg_modeline < 0 || color_fg_modeline >= COLORS ||
923 color_bg_modeline < 0 || color_bg_modeline >= COLORS ||
924 color_fg_highlight < 0 || color_bg_highlight >= COLORS ||
925 color_bg_highlight < 0 || color_bg_highlight >= COLORS) {
928 fprintf(stderr, "Color numbers have to be between 0 and %d.\n", COLORS - 1);
932 init_pair(1, color_fg_modeline, color_bg_modeline);
933 attr_modeline = COLOR_PAIR(1);
935 init_pair(2, color_fg_highlight, color_bg_highlight);
936 attr_focus_line = COLOR_PAIR(2);
938 init_pair(3, COLOR_WHITE, COLOR_RED);
939 attr_error = COLOR_PAIR(3);
944 int current_focus_line = 0, displayed_focus_line = 0;
946 update_screen(¤t_focus_line, &displayed_focus_line,
948 nb_lines, labels, cursor_position, pattern);
956 if(key >= ' ' && key <= '~') { // Insert character
957 insert_char(pattern, &cursor_position, key);
960 else if(key == KEY_BACKSPACE ||
961 key == '\010' || // ^H
962 key == '\177') { // ^?
963 backspace_char(pattern, &cursor_position);
966 else if(key == KEY_DC ||
967 key == '\004') { // ^D
968 delete_char(pattern, &cursor_position);
971 else if(key == KEY_HOME) {
972 current_focus_line = 0;
975 else if(key == KEY_END) {
976 current_focus_line = nb_lines - 1;
979 else if(key == KEY_NPAGE) {
983 else if(key == KEY_PPAGE) {
987 else if(key == KEY_DOWN ||
988 key == '\016') { // ^N
992 else if(key == KEY_UP ||
993 key == '\020') { // ^P
997 else if(key == KEY_LEFT ||
998 key == '\002') { // ^B
999 if(cursor_position > 0) cursor_position--;
1000 else error_feedback();
1003 else if(key == KEY_RIGHT ||
1004 key == '\006') { // ^F
1005 if(pattern[cursor_position]) cursor_position++;
1006 else error_feedback();
1009 else if(key == '\001') { // ^A
1010 cursor_position = 0;
1013 else if(key == '\005') { // ^E
1014 cursor_position = strlen(pattern);
1017 else if(key == '\022') { // ^R
1018 use_regexp = !use_regexp;
1021 else if(key == '\011') { // ^I
1022 case_sensitive = !case_sensitive;
1025 else if(key == '\025') { // ^U
1026 kill_before_cursor(pattern, &cursor_position);
1029 else if(key == '\013') { // ^K
1030 kill_after_cursor(pattern, &cursor_position);
1033 else if(key == '\014') { // ^L
1034 // I suspect that we may sometime mess up the display
1038 update_screen(¤t_focus_line, &displayed_focus_line,
1040 nb_lines, labels, cursor_position, pattern);
1042 } while(key != '\007' && // ^G
1043 key != '\033' && // ^[ (escape)
1050 //////////////////////////////////////////////////////////////////////
1051 // Here we come back to standard display
1053 if((key == KEY_ENTER || key == '\n')) {
1057 if(displayed_focus_line >= 0 && displayed_focus_line < nb_lines) {
1058 t = lines[displayed_focus_line];
1059 if(label_separator) {
1060 while(*t && *t != label_separator) t++;
1067 if(output_to_vt_buffer && t) {
1068 inject_into_tty_buffer(t);
1071 if(output_filename[0]) {
1072 FILE *out = fopen(output_filename, "w");
1079 fprintf(stderr, "Can not open %s for writing.\n", output_filename);
1086 printf("Aborted.\n");
1089 for(l = 0; l < nb_lines; l++) {