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 // alias h='selector -d -i -b -v -f <(history)'
36 #include <sys/ioctl.h>
44 const int buffer_size = 4096;
46 // Yeah, global variables!
48 int nb_lines_max = 1000;
49 char pattern_separator = ';';
50 int output_to_vt_buffer = 0;
52 int zsh_history = 0, bash_history = 0;
53 int inverse_order = 0;
54 int remove_duplicates = 0;
56 int case_sensitive = 0;
60 #define COLOR_MODELINE 1
61 #define COLOR_HIGHLIGHTED_LINE 2
63 //////////////////////////////////////////////////////////////////////
65 void inject_into_tty_buffer(char *string) {
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 string in the tty input buffer
72 for(char *k = string; *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 int string_to_positive_integer(char *string) {
96 for(char *s = string; *s; s++) {
97 if(*s >= '0' && *s <= '9') {
98 result = result * 10 + int(*s - '0');
104 cerr << "Value `" << string << "' is not a positive integer." << endl;
111 void error_feedback() {
119 //////////////////////////////////////////////////////////////////////
120 // A quick and dirty hash table
122 // The table itself stores index of the strings in a char
123 // **table. When a string is added, if it was already in the table,
124 // the new index replaces the previous one.
126 int *new_hash_table(int hash_table_size) {
128 result = new int[hash_table_size];
129 for(int k = 0; k < hash_table_size; k++) {
135 // Adds new_string in the table, associated to new_index. If this
136 // string was not already in the table, returns -1. Otherwise, returns
137 // the previous index it had.
139 int test_and_add(char *new_string, int new_index,
140 char **strings, int *hash_table, int hash_table_size) {
141 unsigned int code = 0;
143 // This is my recipe. I checked, it seems to work (as long as
144 // hash_table_size is not a multiple of 387433 that should be okay)
146 for(int k = 0; new_string[k]; k++) {
147 code = code * 387433 + (unsigned int) (new_string[k]);
150 code = code % hash_table_size;
152 while(hash_table[code] >= 0) {
153 // There is a string with that code
154 if(strcmp(new_string, strings[hash_table[code]]) == 0) {
155 // It is the same string, we keep a copy of the stored index
156 int result = hash_table[code];
158 hash_table[code] = new_index;
159 // And return the previous one
162 // This collision was not the same string, let's move to the next
164 code = (code + 1) % hash_table_size;
167 // This string was not already in there, store the index in the
168 // table and return -1
169 hash_table[code] = new_index;
173 //////////////////////////////////////////////////////////////////////
174 // A matcher matches either with a collection of substrings, or with a
182 char *splitted_patterns, **patterns;
185 int match(char *string, matcher_t *matcher) {
186 if(matcher->nb_patterns >= 0) {
187 if(matcher->case_sensitive) {
188 for(int n = 0; n < matcher->nb_patterns; n++) {
189 if(strstr(string, matcher->patterns[n]) == 0) return 0;
192 for(int n = 0; n < matcher->nb_patterns; n++) {
193 if(strcasestr(string, matcher->patterns[n]) == 0) return 0;
198 return regexec(&matcher->preg, string, 0, 0, 0) == 0;
202 void free_matcher(matcher_t *matcher) {
203 if(matcher->nb_patterns < 0) {
204 if(!matcher->regexp_error) regfree(&matcher->preg);
206 delete[] matcher->splitted_patterns;
207 delete[] matcher->patterns;
211 void initialize_matcher(int use_regexp, int case_sensitive,
212 matcher_t *matcher, const char *pattern) {
215 matcher->nb_patterns = -1;
216 matcher->regexp_error = regcomp(&matcher->preg, pattern, case_sensitive ? 0 : REG_ICASE);
218 matcher->regexp_error = 0;
219 matcher->nb_patterns = 1;
220 matcher->case_sensitive = case_sensitive;
222 for(const char *s = pattern; *s; s++) {
223 if(*s == pattern_separator) {
224 matcher->nb_patterns++;
228 matcher->splitted_patterns = new char[strlen(pattern) + 1];
229 matcher->patterns = new char*[matcher->nb_patterns];
231 strcpy(matcher->splitted_patterns, pattern);
234 char *last_pattern_start = matcher->splitted_patterns;
235 for(char *s = matcher->splitted_patterns; n < matcher->nb_patterns; s++) {
236 if(*s == pattern_separator || *s == '\0') {
238 matcher->patterns[n++] = last_pattern_start;
239 last_pattern_start = s + 1;
245 //////////////////////////////////////////////////////////////////////
248 void delete_char(char *buffer, int *position) {
249 if(buffer[*position]) {
251 while(c < buffer_size && buffer[c]) {
252 buffer[c] = buffer[c+1];
255 } else error_feedback();
258 void backspace_char(char *buffer, int *position) {
260 if(buffer[*position]) {
261 int c = *position - 1;
263 buffer[c] = buffer[c+1];
267 buffer[*position - 1] = '\0';
271 } else error_feedback();
274 void insert_char(char *buffer, int *position, char character) {
275 if(strlen(buffer) < buffer_size - 1) {
277 char t = buffer[c], u;
286 buffer[(*position)++] = character;
287 } else error_feedback();
290 void kill_before_cursor(char *buffer, int *position) {
292 while(buffer[*position + s]) {
293 buffer[s] = buffer[*position + s];
300 void kill_after_cursor(char *buffer, int *position) {
301 buffer[*position] = '\0';
304 //////////////////////////////////////////////////////////////////////
306 int previous_visible(int current_line, int nb_lines, char **lines, matcher_t *matcher) {
307 int line = current_line - 1;
308 while(line >= 0 && !match(lines[line], matcher)) line--;
312 int next_visible(int current_line, int nb_lines, char **lines, matcher_t *matcher) {
313 int line = current_line + 1;
314 while(line < nb_lines && !match(lines[line], matcher)) line++;
322 //////////////////////////////////////////////////////////////////////
324 void update_screen(int *current_line, int *temporary_line, int motion,
325 int nb_lines, char **lines,
329 char buffer[buffer_size];
332 initialize_matcher(use_regexp, case_sensitive, &matcher, pattern);
334 // We now take care of printing the lines per se
336 int console_width = getmaxx(stdscr);
337 int console_height = getmaxy(stdscr);
339 // First, we find a visible line. In priority: The current, or the
340 // first visible after it, or the first visible before it.
342 int nb_printed_lines = 0;
345 use_default_colors();
348 if(matcher.regexp_error) {
349 addstr("[regexp error]");
350 } else if(nb_lines > 0) {
352 if(match(lines[*current_line], &matcher)) {
353 new_line = *current_line;
355 new_line = next_visible(*current_line, nb_lines, lines, &matcher);
357 new_line = previous_visible(*current_line, nb_lines, lines, &matcher);
361 // If we found a visible line and we should move, let's move
363 if(new_line >= 0 && motion != 0) {
366 // We want to go down, let's find the first visible line below
367 for(int m = 0; l >= 0 && m < motion; m++) {
368 l = next_visible(l, nb_lines, lines, &matcher);
374 // We want to go up, let's find the first visible line above
375 for(int m = 0; l >= 0 && m < -motion; m++) {
376 l = previous_visible(l, nb_lines, lines, &matcher);
384 // Here new_line is either a line number matching the patterns, or -1
388 int first_line = new_line, last_line = new_line, nb_match = 1;
390 // We find the first and last line to show, so that the total of
391 // visible lines between them (them included) is console_height-1
393 while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
397 while(first_line > 0 && !match(lines[first_line], &matcher)) {
400 if(match(lines[first_line], &matcher)) {
405 if(nb_match < console_height - 1 && last_line < nb_lines - 1) {
407 while(last_line < nb_lines - 1 && !match(lines[last_line], &matcher)) {
411 if(match(lines[last_line], &matcher)) {
417 // Now we display them
419 for(int l = first_line; l <= last_line; l++) {
420 if(match(lines[l], &matcher)) {
423 while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
424 buffer[k] = lines[l][k];
428 // We fill the rest of the line with blanks if this is the
432 while(k < console_width) {
440 // Highlight the highlighted line ...
444 attron(COLOR_PAIR(COLOR_HIGHLIGHTED_LINE));
445 addnstr(buffer, console_width);
446 attroff(COLOR_PAIR(COLOR_HIGHLIGHTED_LINE));
449 addnstr(buffer, console_width);
453 addnstr(buffer, console_width);
461 *current_line = new_line;
465 *temporary_line = new_line;
467 if(nb_printed_lines == 0) {
468 addnstr("[no selection]\n", console_width);
471 addnstr("[empty choice]\n", console_width);
479 attron(COLOR_PAIR(COLOR_MODELINE));
484 for(int k = 0; k < console_width; k++) buffer[k] = ' ';
485 buffer[console_width] = '\0';
486 addnstr(buffer, console_width);
490 // There must be a more elegant way of moving the cursor at a
491 // location met during display
498 cursor_x += strlen(title) + 1;
501 sprintf(buffer, "%d/%d ", nb_printed_lines, nb_lines);
503 cursor_x += strlen(buffer);
505 addnstr(pattern, cursor_position);
506 cursor_x += cursor_position;
508 if(pattern[cursor_position]) {
509 addstr(pattern + cursor_position);
514 if(use_regexp || case_sensitive) {
532 attroff(COLOR_PAIR(COLOR_MODELINE));
540 free_matcher(&matcher);
543 //////////////////////////////////////////////////////////////////////
545 void read_file(const char *input_filename,
546 int nb_lines_max, int *nb_lines, char **lines,
547 int hash_table_size, int *hash_table) {
549 char buffer[buffer_size], raw_line[buffer_size];;
551 ifstream file(input_filename);
554 cerr << "Can not open " << input_filename << endl;
558 while(*nb_lines < nb_lines_max && !file.eof()) {
560 file.getline(raw_line, buffer_size);
565 cerr << "Line too long:" << endl;
566 cerr << raw_line << endl;
577 while(*u) { *s++ = *u++; }
583 if(zsh_history && *s == ':') {
584 while(*s && *s != ';') s++;
588 if(bash_history && (*s == ' ' || (*s >= '0' && *s <= '9'))) {
589 while(*s == ' ' || (*s >= '0' && *s <= '9')) s++;
595 dup = test_and_add(s, *nb_lines, lines, hash_table, hash_table_size);
601 lines[*nb_lines] = new char[strlen(s) + 1];
602 strcpy(lines[*nb_lines], s);
604 // The string was already in there, so we do not allocate a
605 // new string but use the pointer to the first occurence of it
606 lines[*nb_lines] = lines[dup];
615 //////////////////////////////////////////////////////////////////////
617 int main(int argc, char **argv) {
619 if(!ttyname(STDIN_FILENO)) {
620 cerr << "The standard input is not a tty." << endl;
624 int color_fg_modeline, color_bg_modeline;
625 int color_fg_highlight, color_bg_highlight;
627 color_fg_modeline = COLOR_WHITE;
628 color_bg_modeline = COLOR_BLACK;
629 color_fg_highlight = COLOR_BLACK;
630 color_bg_highlight = COLOR_YELLOW;
632 setlocale(LC_ALL, "");
634 char input_filename[buffer_size], output_filename[buffer_size];
636 strcpy(input_filename, "");
637 strcpy(output_filename, "");
640 int error = 0, show_help = 0;
641 int rest_are_files = 0;
643 while(!error && !show_help && i < argc && argv[i][0] == '-' && !rest_are_files) {
645 if(strcmp(argv[i], "-o") == 0) {
646 check_opt(argc, argv, i, 1, "<output filename>");
647 strncpy(output_filename, argv[i+1], buffer_size);
651 else if(strcmp(argv[i], "-s") == 0) {
652 check_opt(argc, argv, i, 1, "<pattern separator>");
653 pattern_separator = argv[i+1][0];
657 else if(strcmp(argv[i], "-v") == 0) {
658 output_to_vt_buffer = 1;
662 else if(strcmp(argv[i], "-m") == 0) {
667 else if(strcmp(argv[i], "-q") == 0) {
672 else if(strcmp(argv[i], "-f") == 0) {
673 check_opt(argc, argv, i, 1, "<input filename>");
674 strncpy(input_filename, argv[i+1], buffer_size);
678 else if(strcmp(argv[i], "-i") == 0) {
683 else if(strcmp(argv[i], "-b") == 0) {
688 else if(strcmp(argv[i], "-z") == 0) {
693 else if(strcmp(argv[i], "-d") == 0) {
694 remove_duplicates = 1;
698 else if(strcmp(argv[i], "-e") == 0) {
703 else if(strcmp(argv[i], "-a") == 0) {
706 else if(strcmp(argv[i], "-t") == 0) {
707 check_opt(argc, argv, i, 1, "<title>");
709 title = new char[strlen(argv[i+1]) + 1];
710 strcpy(title, argv[i+1]);
714 else if(strcmp(argv[i], "-l") == 0) {
715 check_opt(argc, argv, i, 1, "<maximum number of lines>");
716 nb_lines_max = string_to_positive_integer(argv[i+1]);
720 else if(strcmp(argv[i], "-c") == 0) {
721 check_opt(argc, argv, i, 4, "<fg modeline> <bg modeline> <fg highlight> <bg highlight>");
722 color_fg_modeline = string_to_positive_integer(argv[i + 1]);
723 color_bg_modeline = string_to_positive_integer(argv[i + 2]);
724 color_fg_highlight = string_to_positive_integer(argv[i + 3]);
725 color_bg_highlight = string_to_positive_integer(argv[i + 4]);
729 else if(strcmp(argv[i], "--") == 0) {
734 else if(strcmp(argv[i], "-h") == 0) {
740 cerr << "Unknown option " << argv[i] << "." << endl;
745 if(show_help || error) {
746 cerr << "Selector version " << VERSION << "-R" << REVISION_NUMBER
748 << "Written by Francois Fleuret <francois@fleuret.org>."
751 << "Usage: " << argv[0] << " [options] [<filename1> [<filename2> ...]]" << endl
753 << " -h show this help" << endl
754 << " -v inject the selected line in the tty" << endl
755 << " -d remove duplicated lines" << endl
756 << " -b remove the bash history line prefix" << endl
757 << " -z remove the zsh history line prefix" << endl
758 << " -i invert the order of lines" << endl
759 << " -e start in regexp mode" << endl
760 << " -a case sensitive" << endl
761 << " -m monochrome mode" << endl
762 << " -q make a flash instead of a beep on an edition error" << endl
763 << " -- rest of the arguments are filenames" << endl
764 << " -t <title>" << endl
765 << " add a title in the modeline" << endl
766 << " -c <fg modeline> <bg modeline> <fg highlight> <bg highlight>" << endl
767 << " set the display colors" << endl
768 << " -o <output filename>" << endl
769 << " set a file to write the selected line to" << endl
770 << " -s <pattern separator>" << endl
771 << " set the symbol to separate substrings in the pattern" << endl
772 << " -l <max number of lines>" << endl
773 << " set the maximum number of lines to take into account" << endl
779 char **lines = new char *[nb_lines_max];
782 int hash_table_size = nb_lines_max * 10;
785 if(remove_duplicates) {
786 hash_table = new_hash_table(hash_table_size);
789 if(input_filename[0]) {
790 read_file(input_filename,
791 nb_lines_max, &nb_lines, lines,
792 hash_table_size, hash_table);
797 nb_lines_max, &nb_lines, lines,
798 hash_table_size, hash_table);
804 // Now remove the null strings
807 for(int k = 0; k < nb_lines; k++) {
809 lines[n++] = lines[k];
816 for(int i = 0; i < nb_lines / 2; i++) {
817 char *s = lines[nb_lines - 1 - i];
818 lines[nb_lines - 1 - i] = lines[i];
823 char pattern[buffer_size];
828 //////////////////////////////////////////////////////////////////////
829 // Here we start to display with curse
835 // So that the arrow keys work
836 keypad(stdscr, TRUE);
844 if(color_fg_modeline < 0 || color_fg_modeline >= COLORS ||
845 color_bg_modeline < 0 || color_bg_modeline >= COLORS ||
846 color_fg_highlight < 0 || color_bg_highlight >= COLORS ||
847 color_bg_highlight < 0 || color_bg_highlight >= COLORS) {
850 cerr << "Color numbers have to be between 0 and " << COLORS - 1 << "." << endl;
854 init_pair(COLOR_MODELINE, color_fg_modeline, color_bg_modeline);
855 init_pair(COLOR_HIGHLIGHTED_LINE, color_fg_highlight, color_bg_highlight);
863 int current_line = 0, temporary_line = 0;
865 update_screen(¤t_line, &temporary_line, 0, nb_lines, lines, cursor_position, pattern);
873 if(key >= ' ' && key <= '~') { // Insert character
874 insert_char(pattern, &cursor_position, key);
877 else if(key == KEY_BACKSPACE ||
878 key == '\010' || // ^H
879 key == '\177') { // ^?
880 backspace_char(pattern, &cursor_position);
883 else if(key == KEY_DC ||
884 key == '\004') { // ^D
885 delete_char(pattern, &cursor_position);
888 else if(key == KEY_HOME) {
892 else if(key == KEY_END) {
893 current_line = nb_lines - 1;
896 else if(key == KEY_NPAGE) {
900 else if(key == KEY_PPAGE) {
904 else if(key == KEY_DOWN ||
905 key == '\016') { // ^N
909 else if(key == KEY_UP ||
910 key == '\020') { // ^P
914 else if(key == KEY_LEFT ||
915 key == '\002') { // ^B
916 if(cursor_position > 0) cursor_position--;
917 else error_feedback();
920 else if(key == KEY_RIGHT ||
921 key == '\006') { // ^F
922 if(pattern[cursor_position]) cursor_position++;
923 else error_feedback();
926 else if(key == '\001') { // ^A
930 else if(key == '\005') { // ^E
931 cursor_position = strlen(pattern);
934 else if(key == '\022') { // ^R
935 use_regexp = !use_regexp;
938 else if(key == '\011') { // ^I
939 case_sensitive = !case_sensitive;
942 else if(key == '\025') { // ^U
943 kill_before_cursor(pattern, &cursor_position);
946 else if(key == '\013') { // ^K
947 kill_after_cursor(pattern, &cursor_position);
950 update_screen(¤t_line, &temporary_line, motion,
951 nb_lines, lines, cursor_position, pattern);
953 } while(key != '\n' && key != KEY_ENTER && key != '\007'); // ^G
958 //////////////////////////////////////////////////////////////////////
959 // Here we come back to standard display
961 if((key == KEY_ENTER || key == '\n')) {
963 if(output_to_vt_buffer) {
964 if(temporary_line >= 0 && temporary_line < nb_lines) {
965 inject_into_tty_buffer(lines[temporary_line]);
969 if(output_filename[0]) {
970 ofstream out(output_filename);
972 cerr << "Can not open " << output_filename << " for writing." << endl;
975 if(temporary_line >= 0 && temporary_line < nb_lines) {
976 out << lines[temporary_line] << endl;
984 cout << "Aborted." << endl;
987 for(int l = 0; l < nb_lines; l++) {