Cosmetics.
[selector.git] / selector.cc
1
2 /*
3  *  selector is a simple command line utility for selection of strings
4  *  with a dynamic pattern-matching.
5  *
6  *  Copyright (c) 2009 Francois Fleuret
7  *  Written by Francois Fleuret <francois@fleuret.org>
8  *
9  *  This file is part of selector.
10  *
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.
14  *
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.
19  *
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/>.
22  *
23  */
24
25 // To use it as a super-history-search for bash:
26 // selector -q -b -i -d -v -w -l ${HISTSIZE} <(history)
27
28 #include <fstream>
29 #include <iostream>
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ncurses.h>
35 #include <fcntl.h>
36 #include <sys/ioctl.h>
37 #include <termios.h>
38 #include <regex.h>
39
40 using namespace std;
41
42 #define VERSION "1.0"
43
44 const int buffer_size = 4096;
45
46 // Yeah, global variables!
47
48 int nb_lines_max = 1000;
49 char pattern_separator = ';';
50 char label_separator = '\0';
51 int output_to_vt_buffer = 0;
52 int add_control_qs = 0;
53 int with_colors = 1;
54 int zsh_history = 0;
55 int bash_history = 0;
56 int inverse_order = 0;
57 int remove_duplicates = 0;
58 int use_regexp = 0;
59 int case_sensitive = 0;
60 char *title = 0;
61 int error_flash = 0;
62
63 int attr_modeline, attr_focus_line, attr_error;
64
65 //////////////////////////////////////////////////////////////////////
66
67 void inject_into_tty_buffer(char *string) {
68   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(const char *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);
79     }
80     ioctl(STDIN_FILENO, TIOCSTI, k);
81   }
82   // Restore the old settings
83   tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
84 }
85
86 //////////////////////////////////////////////////////////////////////
87
88 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
89   if(n_opt + n >= argc) {
90     cerr << "Missing argument for " << argv[n_opt] << "."
91          << " "
92          << "Expecting " << help << "."
93          << endl;
94     exit(1);
95   }
96 }
97
98 int string_to_positive_integer(char *string) {
99   int error = 0;
100   int result = 0;
101
102   if(*string) {
103     for(char *s = string; *s; s++) {
104       if(*s >= '0' && *s <= '9') {
105         result = result * 10 + int(*s - '0');
106       } else error = 1;
107     }
108   } else error = 1;
109
110   if(error) {
111     cerr << "Value `" << string << "' is not a positive integer." << endl;
112     exit(1);
113   }
114
115   return result;
116 }
117
118 void error_feedback() {
119   if(error_flash) {
120     flash();
121   } else {
122     beep();
123   }
124 }
125
126 //////////////////////////////////////////////////////////////////////
127 // A quick and dirty hash table
128
129 // The table itself stores indexes of the strings taken in a char
130 // **table. When a string is added, if it was already in the table,
131 // the new index replaces the previous one.
132
133 int *new_hash_table(int hash_table_size) {
134   int *result;
135   result = new int[hash_table_size];
136   for(int k = 0; k < hash_table_size; k++) {
137     result[k] = -1;
138   }
139   return result;
140 }
141
142 // Adds new_string in the table, associated to new_index. If this
143 // string was not already in the table, returns -1. Otherwise, returns
144 // the previous index it had.
145
146 int test_and_add(char *new_string, int new_index,
147                  char **strings,
148                  int *hash_table, int hash_table_size) {
149
150   unsigned int code = 0;
151
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)
154
155   for(int k = 0; new_string[k]; k++) {
156     code = code * 387433 + (unsigned int) (new_string[k]);
157   }
158
159   code = code % hash_table_size;
160
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];
166       // Put the new one
167       hash_table[code] = new_index;
168       // And return the previous one
169       return result;
170     }
171     // This collision was not the same string, let's move to the next
172     // in the table
173     code = (code + 1) % hash_table_size;
174   }
175
176   // This string was not already in there, store the index in the
177   // table and return -1
178   hash_table[code] = new_index;
179   return -1;
180 }
181
182 //////////////////////////////////////////////////////////////////////
183 // A matcher matches either with a collection of substrings, or with a
184 // regexp
185
186 struct matcher_t {
187   regex_t preg;
188   int regexp_error;
189   int nb_patterns;
190   int case_sensitive;
191   char *splitted_patterns, **patterns;
192 };
193
194 int match(char *string, matcher_t *matcher) {
195   if(matcher->nb_patterns >= 0) {
196     if(matcher->case_sensitive) {
197       for(int n = 0; n < matcher->nb_patterns; n++) {
198         if(strstr(string, matcher->patterns[n]) == 0) return 0;
199       }
200     } else {
201       for(int n = 0; n < matcher->nb_patterns; n++) {
202         if(strcasestr(string, matcher->patterns[n]) == 0) return 0;
203       }
204     }
205     return 1;
206   } else {
207     return regexec(&matcher->preg, string, 0, 0, 0) == 0;
208   }
209 }
210
211 void free_matcher(matcher_t *matcher) {
212   if(matcher->nb_patterns < 0) {
213     if(!matcher->regexp_error) regfree(&matcher->preg);
214   } else {
215     delete[] matcher->splitted_patterns;
216     delete[] matcher->patterns;
217   }
218 }
219
220 void initialize_matcher(int use_regexp, int case_sensitive,
221                         matcher_t *matcher, const char *pattern) {
222
223   if(use_regexp) {
224     matcher->nb_patterns = -1;
225     matcher->regexp_error = regcomp(&matcher->preg, pattern, case_sensitive ? 0 : REG_ICASE);
226   } else {
227     matcher->regexp_error = 0;
228     matcher->nb_patterns = 1;
229     matcher->case_sensitive = case_sensitive;
230
231     for(const char *s = pattern; *s; s++) {
232       if(*s == pattern_separator) {
233         matcher->nb_patterns++;
234       }
235     }
236
237     matcher->splitted_patterns = new char[strlen(pattern) + 1];
238     matcher->patterns = new char*[matcher->nb_patterns];
239
240     strcpy(matcher->splitted_patterns, pattern);
241
242     int n = 0;
243     char *last_pattern_start = matcher->splitted_patterns;
244     for(char *s = matcher->splitted_patterns; n < matcher->nb_patterns; s++) {
245       if(*s == pattern_separator || *s == '\0') {
246         *s = '\0';
247         matcher->patterns[n++] = last_pattern_start;
248         last_pattern_start = s + 1;
249       }
250     }
251   }
252 }
253
254 //////////////////////////////////////////////////////////////////////
255 // Buffer edition
256
257 void delete_char(char *buffer, int *position) {
258   if(buffer[*position]) {
259     int c = *position;
260     while(c < buffer_size && buffer[c]) {
261       buffer[c] = buffer[c+1];
262       c++;
263     }
264   } else error_feedback();
265 }
266
267 void backspace_char(char *buffer, int *position) {
268   if(*position > 0) {
269     if(buffer[*position]) {
270       int c = *position - 1;
271       while(buffer[c]) {
272         buffer[c] = buffer[c+1];
273         c++;
274       }
275     } else {
276       buffer[*position - 1] = '\0';
277     }
278
279     (*position)--;
280   } else error_feedback();
281 }
282
283 void insert_char(char *buffer, int *position, char character) {
284   if(strlen(buffer) < buffer_size - 1) {
285     int c = *position;
286     char t = buffer[c], u;
287     while(t) {
288       c++;
289       u = buffer[c];
290       buffer[c] = t;
291       t = u;
292     }
293     c++;
294     buffer[c] = '\0';
295     buffer[(*position)++] = character;
296   } else error_feedback();
297 }
298
299 void kill_before_cursor(char *buffer, int *position) {
300   int s = 0;
301   while(buffer[*position + s]) {
302     buffer[s] = buffer[*position + s];
303     s++;
304   }
305   buffer[s] = '\0';
306   *position = 0;
307 }
308
309 void kill_after_cursor(char *buffer, int *position) {
310   buffer[*position] = '\0';
311 }
312
313 //////////////////////////////////////////////////////////////////////
314
315 int previous_visible(int current_line, int nb_lines, char **lines, matcher_t *matcher) {
316   int line = current_line - 1;
317   while(line >= 0 && !match(lines[line], matcher)) line--;
318   return line;
319 }
320
321 int next_visible(int current_line, int nb_lines, char **lines, matcher_t *matcher) {
322   int line = current_line + 1;
323   while(line < nb_lines && !match(lines[line], matcher)) line++;
324
325   if(line < nb_lines)
326     return line;
327   else
328     return -1;
329 }
330
331 //////////////////////////////////////////////////////////////////////
332
333 // The value passed to this routine in current_focus_line is the index
334 // of the line to highlight if it matches the matcher. The line
335 // highlighted is the first one matching the matcher in that order:
336 // (1) current_focus_line after motion, (2) the first with a greater
337 // index, (3) the first with a lesser index.
338
339 // The index of the line actually shown highlighted is written in
340 // displayed_focus_line (it can be -1)
341
342 // If there is a motion and a line is actually shown highlighted, its
343 // value is written in current_focus_line
344
345 void update_screen(int *current_focus_line, int *displayed_focus_line,
346                    int motion,
347                    int nb_lines, char **lines,
348                    int cursor_position,
349                    char *pattern) {
350
351   char buffer[buffer_size];
352   matcher_t matcher;
353
354   initialize_matcher(use_regexp, case_sensitive, &matcher, pattern);
355
356   int console_width = getmaxx(stdscr);
357   int console_height = getmaxy(stdscr);
358
359   // First, we find a visible line. In priority: The current, or the
360   // first visible after it, or the first visible before it.
361
362   int nb_printed_lines = 0;
363
364   use_default_colors();
365
366   addstr("\n");
367
368   if(matcher.regexp_error) {
369     attron(attr_error);
370     addnstr("Regexp syntax error", console_width);
371     attroff(attr_error);
372   } else if(nb_lines > 0) {
373     int new_focus_line;
374     if(match(lines[*current_focus_line], &matcher)) {
375       new_focus_line = *current_focus_line;
376     } else {
377       new_focus_line = next_visible(*current_focus_line, nb_lines, lines, &matcher);
378       if(new_focus_line < 0) {
379         new_focus_line = previous_visible(*current_focus_line, nb_lines, lines, &matcher);
380       }
381     }
382
383     // If we found a visible line and we should move, let's move
384
385     if(new_focus_line >= 0 && motion != 0) {
386       int l = new_focus_line;
387       if(motion > 0) {
388         // We want to go down, let's find the first visible line below
389         for(int m = 0; l >= 0 && m < motion; m++) {
390           l = next_visible(l, nb_lines, lines, &matcher);
391           if(l >= 0) {
392             new_focus_line = l;
393           }
394         }
395       } else {
396         // We want to go up, let's find the first visible line above
397         for(int m = 0; l >= 0 && m < -motion; m++) {
398           l = previous_visible(l, nb_lines, lines, &matcher);
399           if(l >= 0) {
400             new_focus_line = l;
401           }
402         }
403       }
404     }
405
406     // Here new_focus_line is either a line number matching the pattern, or -1
407
408     if(new_focus_line >= 0) {
409
410       int first_line = new_focus_line, last_line = new_focus_line, nb_match = 1;
411
412       // We find the first and last line to show, so that the total of
413       // visible lines between them (them included) is console_height-1
414
415       while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
416
417         if(first_line > 0) {
418           first_line--;
419           while(first_line > 0 && !match(lines[first_line], &matcher)) {
420             first_line--;
421           }
422           if(match(lines[first_line], &matcher)) {
423             nb_match++;
424           }
425         }
426
427         if(nb_match < console_height - 1 && last_line < nb_lines - 1) {
428           last_line++;
429           while(last_line < nb_lines - 1 && !match(lines[last_line], &matcher)) {
430             last_line++;
431           }
432
433           if(match(lines[last_line], &matcher)) {
434             nb_match++;
435           }
436         }
437       }
438
439       // Now we display them
440
441       for(int l = first_line; l <= last_line; l++) {
442         if(match(lines[l], &matcher)) {
443           int k = 0;
444
445           while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
446             buffer[k] = lines[l][k];
447             k++;
448           }
449
450           // We fill the rest of the line with blanks if this is the
451           // highlighted line
452
453           if(l == new_focus_line) {
454             while(k < console_width) {
455               buffer[k++] = ' ';
456             }
457           }
458
459           buffer[k++] = '\n';
460           buffer[k++] = '\0';
461
462           clrtoeol();
463
464           // Highlight the highlighted line ...
465
466           if(l == new_focus_line) {
467             attron(attr_focus_line);
468             addnstr(buffer, console_width);
469             attroff(attr_focus_line);
470           } else {
471             addnstr(buffer, console_width);
472           }
473
474           nb_printed_lines++;
475         }
476       }
477
478       // If we are on a focused line and we moved, this become the new
479       // focus line
480
481       if(motion != 0) {
482         *current_focus_line = new_focus_line;
483       }
484     }
485
486     *displayed_focus_line = new_focus_line;
487
488     if(nb_printed_lines == 0) {
489       attron(attr_error);
490       addnstr("No selection", console_width);
491       attroff(attr_error);
492     }
493   } else {
494     attron(attr_error);
495     addnstr("Empty choice", console_width);
496     attroff(attr_error);
497   }
498
499   clrtobot();
500
501   // Draw the modeline
502
503   move(0, 0);
504
505   attron(attr_modeline);
506
507   for(int k = 0; k < console_width; k++) buffer[k] = ' ';
508   buffer[console_width] = '\0';
509   addnstr(buffer, console_width);
510
511   move(0, 0);
512
513   // There must be a more elegant way of moving the cursor at a
514   // location met during display
515
516   int cursor_x = 0;
517
518   if(title) {
519     addstr(title);
520     addstr(" ");
521     cursor_x += strlen(title) + 1;
522   }
523
524   sprintf(buffer, "%d/%d ", nb_printed_lines, nb_lines);
525   addstr(buffer);
526   cursor_x += strlen(buffer);
527
528   addnstr(pattern, cursor_position);
529   cursor_x += cursor_position;
530
531   if(pattern[cursor_position]) {
532     addstr(pattern + cursor_position);
533   } else {
534     addstr(" ");
535   }
536
537   if(use_regexp || case_sensitive) {
538     addstr(" [");
539     if(use_regexp) {
540       addstr("regexp");
541     }
542
543     if(case_sensitive) {
544       if(use_regexp) {
545         addstr(",");
546       }
547       addstr("case");
548     }
549     addstr("]");
550   }
551
552   move(0, cursor_x);
553
554   attroff(attr_modeline);
555
556   // We are done
557
558   refresh();
559   free_matcher(&matcher);
560 }
561
562 //////////////////////////////////////////////////////////////////////
563
564 void read_file(const char *input_filename,
565                int nb_lines_max, int *nb_lines, char **lines,
566                int hash_table_size, int *hash_table) {
567
568   char raw_line[buffer_size];;
569
570   ifstream file(input_filename);
571
572   if(file.fail()) {
573     cerr << "Can not open " << input_filename << endl;
574     exit(1);
575   }
576
577   while(*nb_lines < nb_lines_max && !file.eof()) {
578
579     file.getline(raw_line, buffer_size);
580
581     if(raw_line[0]) {
582
583       if(file.fail()) {
584         cerr << "Line too long:" << endl;
585         cerr << raw_line << endl;
586         exit(1);
587       }
588
589       char *t;
590
591       t = raw_line;
592
593       // Remove the zsh history prefix
594
595       if(zsh_history && *t == ':') {
596         while(*t && *t != ';') t++;
597         if(*t == ';') t++;
598       }
599
600       // Remove the bash history prefix
601
602       if(bash_history) {
603         while(*t == ' ') t++;
604         while(*t >= '0' && *t <= '9') t++;
605         while(*t == ' ') t++;
606       }
607
608       // Check for duplicates with the hash table and insert the line
609       // in the list if necessary
610
611       int dup;
612
613       if(hash_table) {
614         dup = test_and_add(t, *nb_lines, lines, hash_table, hash_table_size);
615       } else {
616         dup = -1;
617       }
618
619       if(dup < 0) {
620         lines[*nb_lines] = new char[strlen(t) + 1];
621         strcpy(lines[*nb_lines], t);
622       } else {
623         // The string was already in there, so we do not allocate a
624         // new string but use the pointer to the first occurence of it
625         lines[*nb_lines] = lines[dup];
626         lines[dup] = 0;
627       }
628
629       (*nb_lines)++;
630     }
631   }
632 }
633
634 //////////////////////////////////////////////////////////////////////
635
636 int main(int argc, char **argv) {
637
638   if(!ttyname(STDIN_FILENO)) {
639     cerr << "The standard input is not a tty." << endl;
640     exit(1);
641   }
642
643   int color_fg_modeline, color_bg_modeline;
644   int color_fg_highlight, color_bg_highlight;
645
646   color_fg_modeline  = COLOR_WHITE;
647   color_bg_modeline  = COLOR_BLACK;
648   color_fg_highlight = COLOR_BLACK;
649   color_bg_highlight = COLOR_YELLOW;
650
651   setlocale(LC_ALL, "");
652
653   char input_filename[buffer_size], output_filename[buffer_size];
654
655   strcpy(input_filename, "");
656   strcpy(output_filename, "");
657
658   int i = 1;
659   int error = 0, show_help = 0;
660   int rest_are_files = 0;
661
662   while(!error && !show_help && i < argc && argv[i][0] == '-' && !rest_are_files) {
663
664     if(strcmp(argv[i], "-o") == 0) {
665       check_opt(argc, argv, i, 1, "<output filename>");
666       strncpy(output_filename, argv[i+1], buffer_size);
667       i += 2;
668     }
669
670     else if(strcmp(argv[i], "-s") == 0) {
671       check_opt(argc, argv, i, 1, "<pattern separator>");
672       pattern_separator = argv[i+1][0];
673       i += 2;
674     }
675
676     else if(strcmp(argv[i], "-x") == 0) {
677       check_opt(argc, argv, i, 1, "<label separator>");
678       label_separator = argv[i+1][0];
679       i += 2;
680     }
681
682     else if(strcmp(argv[i], "-v") == 0) {
683       output_to_vt_buffer = 1;
684       i++;
685     }
686
687     else if(strcmp(argv[i], "-w") == 0) {
688       add_control_qs = 1;
689       i++;
690     }
691
692     else if(strcmp(argv[i], "-m") == 0) {
693       with_colors = 0;
694       i++;
695     }
696
697     else if(strcmp(argv[i], "-q") == 0) {
698       error_flash = 1;
699       i++;
700     }
701
702     else if(strcmp(argv[i], "-f") == 0) {
703       check_opt(argc, argv, i, 1, "<input filename>");
704       strncpy(input_filename, argv[i+1], buffer_size);
705       i += 2;
706     }
707
708     else if(strcmp(argv[i], "-i") == 0) {
709       inverse_order = 1;
710       i++;
711     }
712
713     else if(strcmp(argv[i], "-b") == 0) {
714       bash_history = 1;
715       i++;
716     }
717
718     else if(strcmp(argv[i], "-z") == 0) {
719       zsh_history = 1;
720       i++;
721     }
722
723     else if(strcmp(argv[i], "-d") == 0) {
724       remove_duplicates = 1;
725       i++;
726     }
727
728     else if(strcmp(argv[i], "-e") == 0) {
729       use_regexp = 1;
730       i++;
731     }
732
733     else if(strcmp(argv[i], "-a") == 0) {
734       case_sensitive = 1;
735       i++;
736     }
737
738     else if(strcmp(argv[i], "-t") == 0) {
739       check_opt(argc, argv, i, 1, "<title>");
740       delete[] title;
741       title = new char[strlen(argv[i+1]) + 1];
742       strcpy(title, argv[i+1]);
743       i += 2;
744     }
745
746     else if(strcmp(argv[i], "-l") == 0) {
747       check_opt(argc, argv, i, 1, "<maximum number of lines>");
748       nb_lines_max = string_to_positive_integer(argv[i+1]);
749       i += 2;
750     }
751
752     else if(strcmp(argv[i], "-c") == 0) {
753       check_opt(argc, argv, i, 4, "<fg modeline> <bg modeline> <fg highlight> <bg highlight>");
754       color_fg_modeline = string_to_positive_integer(argv[i + 1]);
755       color_bg_modeline = string_to_positive_integer(argv[i + 2]);
756       color_fg_highlight = string_to_positive_integer(argv[i + 3]);
757       color_bg_highlight = string_to_positive_integer(argv[i + 4]);
758       i += 5;
759     }
760
761     else if(strcmp(argv[i], "--") == 0) {
762       rest_are_files = 1;
763       i++;
764     }
765
766     else if(strcmp(argv[i], "-h") == 0) {
767       show_help = 1;
768       i++;
769     }
770
771     else {
772       cerr << "Unknown option " << argv[i] << "." << endl;
773       error = 1;
774     }
775   }
776
777   if(show_help || error) {
778     cerr << "Selector version " << VERSION << "-R" << REVISION_NUMBER
779          << endl
780          << "Written by Francois Fleuret <francois@fleuret.org>."
781          << endl
782          << endl
783          << "Usage: " << argv[0] << " [options] [<filename1> [<filename2> ...]]" << endl
784          << endl
785          << " -h      show this help" << endl
786          << " -v      inject the selected line in the tty" << endl
787          << " -w      quote control characters with ^Qs when using -v" << endl
788          << " -d      remove duplicated lines" << endl
789          << " -b      remove the bash history line prefix" << endl
790          << " -z      remove the zsh history line prefix" << endl
791          << " -i      invert the order of lines" << endl
792          << " -e      start in regexp mode" << endl
793          << " -a      case sensitive" << endl
794          << " -m      monochrome mode" << endl
795          << " -q      make a flash instead of a beep on an edition error" << endl
796          << " --      all following arguments are filenames" << endl
797          << " -t <title>" << endl
798          << "         add a title in the modeline" << endl
799          << " -c <fg modeline> <bg modeline> <fg highlight> <bg highlight>" << endl
800          << "         set the display colors" << endl
801          << " -o <output filename>" << endl
802          << "         set a file to write the selected line to" << endl
803          << " -s <pattern separator>" << endl
804          << "         set the symbol to separate substrings in the pattern" << endl
805          << " -x <label separator>" << endl
806          << "         set the symbol to terminate the label" << endl
807          << " -l <max number of lines>" << endl
808          << "         set the maximum number of lines to take into account" << endl
809          << endl;
810
811     exit(error);
812   }
813
814   char **lines = new char *[nb_lines_max];
815
816   int nb_lines = 0;
817   int hash_table_size = nb_lines_max * 10;
818   int *hash_table = 0;
819
820   if(remove_duplicates) {
821     hash_table = new_hash_table(hash_table_size);
822   }
823
824   if(input_filename[0]) {
825     read_file(input_filename,
826               nb_lines_max, &nb_lines, lines,
827               hash_table_size, hash_table);
828   }
829
830   while(i < argc) {
831     read_file(argv[i],
832               nb_lines_max, &nb_lines, lines,
833               hash_table_size, hash_table);
834     i++;
835   }
836
837   delete[] hash_table;
838
839   // Now remove the null strings
840
841   int n = 0;
842   for(int k = 0; k < nb_lines; k++) {
843     if(lines[k]) {
844       lines[n++] = lines[k];
845     }
846   }
847
848   nb_lines = n;
849
850   if(inverse_order) {
851     for(int i = 0; i < nb_lines / 2; i++) {
852       char *s = lines[nb_lines - 1 - i];
853       lines[nb_lines - 1 - i] = lines[i];
854       lines[i] = s;
855     }
856   }
857
858   // Build the labels from the strings, take only the part before the
859   // label_separator and transform control characters to printable
860   // ones
861
862   char **labels = new char *[nb_lines];
863   for(int l = 0; l < nb_lines; l++) {
864     char *s, *t;
865     const char *u;
866     t = lines[l];
867     int e = 0;
868     while(*t && *t != label_separator) {
869       u = unctrl(*t++);
870       e += strlen(u);
871     }
872     labels[l] = new char[e + 1];
873     t = lines[l];
874     s = labels[l];
875     while(*t && *t != label_separator) {
876       u = unctrl(*t++);
877       while(*u) { *s++ = *u++; }
878     }
879     *s = '\0';
880   }
881
882   char pattern[buffer_size];
883   pattern[0] = '\0';
884
885   int cursor_position;
886   cursor_position = 0;
887
888   //////////////////////////////////////////////////////////////////////
889   // Here we start to display with curse
890
891   initscr();
892
893   noecho();
894
895   // So that the arrow keys work
896   keypad(stdscr, TRUE);
897
898   attr_error = A_STANDOUT;
899   attr_modeline = A_REVERSE;
900   attr_focus_line = A_STANDOUT;
901
902   if(with_colors && has_colors()) {
903
904     start_color();
905
906     if(color_fg_modeline < 0  || color_fg_modeline >= COLORS ||
907        color_bg_modeline < 0  || color_bg_modeline >= COLORS ||
908        color_fg_highlight < 0 || color_bg_highlight >= COLORS ||
909        color_bg_highlight < 0 || color_bg_highlight >= COLORS) {
910       echo();
911       endwin();
912       cerr << "Color numbers have to be between 0 and " << COLORS - 1 << "." << endl;
913       exit(1);
914     }
915
916     init_pair(1, color_fg_modeline, color_bg_modeline);
917     attr_modeline = COLOR_PAIR(1);
918
919     init_pair(2, color_fg_highlight, color_bg_highlight);
920     attr_focus_line = COLOR_PAIR(2);
921
922     init_pair(3, COLOR_WHITE, COLOR_RED);
923     attr_error = COLOR_PAIR(3);
924
925   }
926
927   int key;
928   int current_focus_line = 0, displayed_focus_line = 0;
929
930   update_screen(&current_focus_line, &displayed_focus_line,
931                 0,
932                 nb_lines, labels, cursor_position, pattern);
933
934   do {
935
936     key = getch();
937
938     int motion = 0;
939
940     if(key >= ' ' && key <= '~') { // Insert character
941       insert_char(pattern, &cursor_position, key);
942     }
943
944     else if(key == KEY_BACKSPACE ||
945             key == '\010' || // ^H
946             key == '\177') { // ^?
947       backspace_char(pattern, &cursor_position);
948     }
949
950     else if(key == KEY_DC ||
951             key == '\004') { // ^D
952       delete_char(pattern, &cursor_position);
953     }
954
955     else if(key == KEY_HOME) {
956       current_focus_line = 0;
957     }
958
959     else if(key == KEY_END) {
960       current_focus_line = nb_lines - 1;
961     }
962
963     else if(key == KEY_NPAGE) {
964       motion = 10;
965     }
966
967     else if(key == KEY_PPAGE) {
968       motion = -10;
969     }
970
971     else if(key == KEY_DOWN ||
972             key == '\016') { // ^N
973       motion = 1;
974     }
975
976     else if(key == KEY_UP ||
977             key == '\020') { // ^P
978       motion = -1;
979     }
980
981     else if(key == KEY_LEFT ||
982             key == '\002') { // ^B
983       if(cursor_position > 0) cursor_position--;
984       else error_feedback();
985     }
986
987     else if(key == KEY_RIGHT ||
988             key == '\006') { // ^F
989       if(pattern[cursor_position]) cursor_position++;
990       else error_feedback();
991     }
992
993     else if(key == '\001') { // ^A
994       cursor_position = 0;
995     }
996
997     else if(key == '\005') { // ^E
998       cursor_position = strlen(pattern);
999     }
1000
1001     else if(key == '\022') { // ^R
1002       use_regexp = !use_regexp;
1003     }
1004
1005     else if(key == '\011') { // ^I
1006       case_sensitive = !case_sensitive;
1007     }
1008
1009     else if(key == '\025') { // ^U
1010       kill_before_cursor(pattern, &cursor_position);
1011     }
1012
1013     else if(key == '\013') { // ^K
1014       kill_after_cursor(pattern, &cursor_position);
1015     }
1016
1017     else if(key == '\014') { // ^L
1018       // I suspect that we may sometime mess up the display
1019       clear();
1020     }
1021
1022     update_screen(&current_focus_line, &displayed_focus_line,
1023                   motion,
1024                   nb_lines, labels, cursor_position, pattern);
1025
1026   } while(key != '\007' && // ^G
1027           key != '\033' && // ^[ (escape)
1028           key != '\n' &&
1029           key != KEY_ENTER);
1030
1031   echo();
1032   endwin();
1033
1034   //////////////////////////////////////////////////////////////////////
1035   // Here we come back to standard display
1036
1037   if((key == KEY_ENTER || key == '\n')) {
1038
1039     if(output_to_vt_buffer) {
1040       if(displayed_focus_line >= 0 && displayed_focus_line < nb_lines) {
1041         inject_into_tty_buffer(lines[displayed_focus_line]);
1042       }
1043     }
1044
1045     if(output_filename[0]) {
1046       ofstream out(output_filename);
1047       if(out.fail()) {
1048         cerr << "Can not open " << output_filename << " for writing." << endl;
1049         exit(1);
1050       } else {
1051         if(displayed_focus_line >= 0 && displayed_focus_line < nb_lines) {
1052           out << lines[displayed_focus_line] << endl;
1053         } else {
1054           out << endl;
1055         }
1056       }
1057       out.flush();
1058     }
1059
1060   } else {
1061     cout << "Aborted." << endl;
1062   }
1063
1064   for(int l = 0; l < nb_lines; l++) {
1065     delete[] lines[l];
1066     delete[] labels[l];
1067   }
1068
1069   delete[] labels;
1070   delete[] lines;
1071   delete[] title;
1072
1073   exit(0);
1074 }