a58e2bba8c66c5a117e7620baf643fb4356cd4d9
[selector.git] / selector.cc
1
2 /*
3  *  selector is a simple shell command for selection of strings with a
4  *  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 // alias h='selector -d -i -b -v -f <(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 int output_to_vt_buffer = 0;
51 int with_colors = 1;
52 int zsh_history = 0, bash_history = 0;
53 int inverse_order = 0;
54 int remove_duplicates = 0;
55 int use_regexp = 0;
56 int case_sensitive = 0;
57 char *title = 0;
58
59 //////////////////////////////////////////////////////////////////////
60
61 void inject_into_tty_buffer(char *string) {
62   struct termios oldtio, newtio;
63   tcgetattr(STDIN_FILENO, &oldtio);
64   memset(&newtio, 0, sizeof(newtio));
65   // Set input mode (non-canonical, *no echo*,...)
66   tcsetattr(STDIN_FILENO, TCSANOW, &newtio);
67   // Put the selected string in the tty input buffer
68   for(char *k = string; *k; k++) {
69     ioctl(STDIN_FILENO, TIOCSTI, k);
70   }
71   // Restore the old settings
72   tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
73 }
74
75 //////////////////////////////////////////////////////////////////////
76
77 void check_opt(int argc, char **argv, int n_opt, int n, const char *help) {
78   if(n_opt + n >= argc) {
79     cerr << "Missing argument for " << argv[n_opt] << "."
80          << " "
81          << "Expecting " << help << "."
82          << endl;
83     exit(1);
84   }
85 }
86
87 //////////////////////////////////////////////////////////////////////
88 // A quick and dirty hash table
89
90 // The table itself stores index of the strings in a char
91 // **table. When a string is added, if it was already in the table,
92 // the new index replaces the previous one.
93
94 int *new_hash_table(int hash_table_size) {
95   int *result;
96   result = new int[hash_table_size];
97   for(int k = 0; k < hash_table_size; k++) {
98     result[k] = -1;
99   }
100   return result;
101 }
102
103 // Adds new_string in the table, associated to new_index. If this
104 // string was not already in the table, returns -1. Otherwise, returns
105 // the previous index it had.
106
107 int test_and_add(char *new_string, int new_index,
108                  char **strings, int *hash_table, int hash_table_size) {
109   unsigned int code = 0;
110
111   // This is my recipe. I checked, it seems to work (as long as
112   // hash_table_size is not a multiple of 387433 that should be okay)
113
114   for(int k = 0; new_string[k]; k++) {
115     code = code * 387433 + (unsigned int) (new_string[k]);
116   }
117
118   code = code % hash_table_size;
119
120   while(hash_table[code] >= 0) {
121     // There is a string with that code
122     if(strcmp(new_string, strings[hash_table[code]]) == 0) {
123       // It is the same string, we keep a copy of the stored index
124       int result = hash_table[code];
125       // Put the new one
126       hash_table[code] = new_index;
127       // And return the previous one
128       return result;
129     }
130     // This collision was not the same string, let's move to the next
131     // in the table
132     code = (code + 1) % hash_table_size;
133   }
134
135   // This string was not already in there, store the index in the
136   // table and return -1
137   hash_table[code] = new_index;
138   return -1;
139 }
140
141 //////////////////////////////////////////////////////////////////////
142 // A matcher matches either with a collection of substrings, or with a
143 // regexp
144
145 struct matcher_t {
146   regex_t preg;
147   int regexp_error;
148   int nb_patterns;
149   int case_sensitive;
150   char *splitted_patterns, **patterns;
151 };
152
153 int match(char *string, matcher_t *matcher) {
154   if(matcher->nb_patterns >= 0) {
155     if(matcher->case_sensitive) {
156       for(int n = 0; n < matcher->nb_patterns; n++) {
157         if(strstr(string, matcher->patterns[n]) == 0) return 0;
158       }
159     } else {
160       for(int n = 0; n < matcher->nb_patterns; n++) {
161         if(strcasestr(string, matcher->patterns[n]) == 0) return 0;
162       }
163     }
164     return 1;
165   } else {
166     return regexec(&matcher->preg, string, 0, 0, 0) == 0;
167   }
168 }
169
170 void free_matcher(matcher_t *matcher) {
171   if(matcher->nb_patterns >= 0) {
172     delete[] matcher->splitted_patterns;
173     delete[] matcher->patterns;
174   } else {
175     if(!matcher->regexp_error) regfree(&matcher->preg);
176   }
177 }
178
179 void initialize_matcher(int use_regexp, int case_sensitive,
180                         matcher_t *matcher, const char *pattern) {
181
182   if(use_regexp) {
183     matcher->nb_patterns = -1;
184     matcher->regexp_error = regcomp(&matcher->preg, pattern, case_sensitive ? 0 : REG_ICASE);
185   } else {
186     matcher->regexp_error = 0;
187     matcher->nb_patterns = 1;
188     matcher->case_sensitive = case_sensitive;
189
190     for(const char *s = pattern; *s; s++) {
191       if(*s == pattern_separator) {
192         matcher->nb_patterns++;
193       }
194     }
195
196     matcher->splitted_patterns = new char[strlen(pattern) + 1];
197     matcher->patterns = new char*[matcher->nb_patterns];
198
199     strcpy(matcher->splitted_patterns, pattern);
200
201     int n = 0;
202     char *last_pattern_start = matcher->splitted_patterns;
203     for(char *s = matcher->splitted_patterns; n < matcher->nb_patterns; s++) {
204       if(*s == pattern_separator || *s == '\0') {
205         *s = '\0';
206         matcher->patterns[n++] = last_pattern_start;
207         last_pattern_start = s + 1;
208       }
209     }
210   }
211 }
212
213 //////////////////////////////////////////////////////////////////////
214 // Buffer edition
215
216 void delete_char(char *buffer, int *position) {
217   if(buffer[*position]) {
218     int c = *position;
219     while(c < buffer_size && buffer[c]) {
220       buffer[c] = buffer[c+1];
221       c++;
222     }
223   }
224 }
225
226 void backspace_char(char *buffer, int *position) {
227   if(*position > 0) {
228     if(buffer[*position]) {
229       int c = *position - 1;
230       while(buffer[c]) {
231         buffer[c] = buffer[c+1];
232         c++;
233       }
234     } else {
235       buffer[*position - 1] = '\0';
236     }
237
238     (*position)--;
239   }
240 }
241
242 void insert_char(char *buffer, int *position, char character) {
243   if(strlen(buffer) < buffer_size - 1) {
244     int c = *position;
245     char t = buffer[c], u;
246     while(t) {
247       c++;
248       u = buffer[c];
249       buffer[c] = t;
250       t = u;
251     }
252     c++;
253     buffer[c] = '\0';
254     buffer[(*position)++] = character;
255   }
256 }
257
258 void kill_before_cursor(char *buffer, int *position) {
259   int s = 0;
260   while(buffer[*position + s]) {
261     buffer[s] = buffer[*position + s];
262     s++;
263   }
264   buffer[s] = '\0';
265   *position = 0;
266 }
267
268 void kill_after_cursor(char *buffer, int *position) {
269   buffer[*position] = '\0';
270 }
271
272 //////////////////////////////////////////////////////////////////////
273
274 int previous_visible(int current_line, int nb_lines, char **lines, matcher_t *matcher) {
275   int line = current_line - 1;
276   while(line >= 0 && !match(lines[line], matcher)) line--;
277   return line;
278 }
279
280 int next_visible(int current_line, int nb_lines, char **lines, matcher_t *matcher) {
281   int line = current_line + 1;
282   while(line < nb_lines && !match(lines[line], matcher)) line++;
283
284   if(line < nb_lines)
285     return line;
286   else
287     return -1;
288 }
289
290 //////////////////////////////////////////////////////////////////////
291
292 void update_screen(int *current_line, int *temporary_line, int motion,
293                    int nb_lines, char **lines,
294                    int cursor_position,
295                    char *pattern) {
296
297   char buffer[buffer_size];
298   matcher_t matcher;
299
300   initialize_matcher(use_regexp, case_sensitive, &matcher, pattern);
301
302   // We now take care of printing the lines per se
303
304   int console_width = getmaxx(stdscr);
305   int console_height = getmaxy(stdscr);
306
307   // First, we find a visible line. In priority: The current, or the
308   // first visible after it, or the first visible before it.
309
310   int nb_printed_lines = 0;
311
312   clear();
313   use_default_colors();
314   addstr("\n");
315
316   if(matcher.regexp_error) {
317     addstr("[regexp error]");
318   } else if(nb_lines > 0) {
319     int new_line;
320     if(match(lines[*current_line], &matcher)) {
321       new_line = *current_line;
322     } else {
323       new_line = next_visible(*current_line, nb_lines, lines, &matcher);
324       if(new_line < 0) {
325         new_line = previous_visible(*current_line, nb_lines, lines, &matcher);
326       }
327     }
328
329     // If we found a visible line and we should move, let's move
330
331     if(new_line >= 0 && motion != 0) {
332       int l = new_line;
333       if(motion > 0) {
334         // We want to go down, let's find the first visible line below
335         for(int m = 0; l >= 0 && m < motion; m++) {
336           l = next_visible(l, nb_lines, lines, &matcher);
337           if(l >= 0) {
338             new_line = l;
339           }
340         }
341       } else {
342         // We want to go up, let's find the first visible line above
343         for(int m = 0; l >= 0 && m < -motion; m++) {
344           l = previous_visible(l, nb_lines, lines, &matcher);
345           if(l >= 0) {
346             new_line = l;
347           }
348         }
349       }
350     }
351
352     // Here new_line is either a line number matching the patterns, or -1
353
354     if(new_line >= 0) {
355
356       int first_line = new_line, last_line = new_line, nb_match = 1;
357
358       // We find the first and last line to show, so that the total of
359       // visible lines between them (them include) is console_height - 1
360
361       while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
362
363         if(first_line > 0) {
364           first_line--;
365           while(first_line > 0 && !match(lines[first_line], &matcher)) {
366             first_line--;
367           }
368           if(match(lines[first_line], &matcher)) {
369             nb_match++;
370           }
371         }
372
373         if(nb_match < console_height - 1 && last_line < nb_lines - 1) {
374           last_line++;
375           while(last_line < nb_lines - 1 && !match(lines[last_line], &matcher)) {
376             last_line++;
377           }
378
379           if(match(lines[last_line], &matcher)) {
380             nb_match++;
381           }
382         }
383       }
384
385       // Now we display them
386
387       for(int l = first_line; l <= last_line; l++) {
388         if(match(lines[l], &matcher)) {
389           int k = 0;
390
391           while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
392             buffer[k] = lines[l][k];
393             k++;
394           }
395
396           // We fill the rest of the line with blanks if either we did
397           // not clear() or if this is the highlighted line
398
399           if(l == new_line) {
400             while(k < console_width) {
401               buffer[k++] = ' ';
402             }
403           }
404
405           buffer[k++] = '\n';
406           buffer[k++] = '\0';
407
408           // Highlight the highlighted line ...
409
410           if(l == new_line) {
411             if(with_colors) {
412               attron(COLOR_PAIR(2));
413               addnstr(buffer, console_width);
414               attroff(COLOR_PAIR(2));
415             } else {
416               attron(A_STANDOUT);
417               addnstr(buffer, console_width);
418               attroff(A_STANDOUT);
419             }
420           } else {
421             addnstr(buffer, console_width);
422           }
423
424           nb_printed_lines++;
425         }
426       }
427
428       if(motion != 0) {
429         *current_line = new_line;
430       }
431     }
432
433     *temporary_line = new_line;
434
435     if(nb_printed_lines == 0) {
436       addnstr("[no selection]\n", console_width);
437     }
438   } else {
439     addnstr("[empty choice]\n", console_width);
440   }
441
442   // Draw the modeline
443
444   move(0, 0);
445
446   if(with_colors) {
447     attron(COLOR_PAIR(1));
448   } else {
449     attron(A_REVERSE);
450   }
451
452   for(int k = 0; k < console_width; k++) buffer[k] = ' ';
453   buffer[console_width] = '\0';
454   addnstr(buffer, console_width);
455
456   move(0, 0);
457
458   // There must be a more elegant way of moving the cursor at a
459   // location met during display
460
461   int cursor_x = 0;
462
463   if(title) {
464     addstr(title);
465     addstr(" ");
466     cursor_x += strlen(title) + 1;
467   }
468
469   sprintf(buffer, "%d/%d ", nb_printed_lines, nb_lines);
470   addstr(buffer);
471   cursor_x += strlen(buffer);
472
473   addnstr(pattern, cursor_position);
474   cursor_x += cursor_position;
475
476   if(pattern[cursor_position]) {
477     addstr(pattern + cursor_position);
478   } else {
479     addstr(" ");
480   }
481
482   if(use_regexp) {
483     addstr(" [regexp]");
484   }
485
486   move(0, cursor_x);
487
488   if(with_colors) {
489     attroff(COLOR_PAIR(1));
490   } else {
491     attroff(A_REVERSE);
492   }
493
494   // We are done
495
496   refresh();
497   free_matcher(&matcher);
498 }
499
500 //////////////////////////////////////////////////////////////////////
501
502 int main(int argc, char **argv) {
503
504   if(!ttyname(STDIN_FILENO)) {
505     cerr << "The standard input is not a tty." << endl;
506     exit(1);
507   }
508
509   char buffer[buffer_size], raw_line[buffer_size];;
510   int color_fg_modeline, color_bg_modeline;
511   int color_fg_highlight, color_bg_highlight;
512
513   color_fg_modeline  = COLOR_WHITE;
514   color_bg_modeline  = COLOR_BLACK;
515   color_fg_highlight = COLOR_BLACK;
516   color_bg_highlight = COLOR_YELLOW;
517
518   setlocale(LC_ALL, "");
519
520   char input_filename[buffer_size], output_filename[buffer_size];
521
522   strcpy(input_filename, "");
523   strcpy(output_filename, "");
524
525   int i = 1;
526   int error = 0, show_help = 0;
527
528   while(!error && !show_help && i < argc) {
529
530     if(strcmp(argv[i], "-o") == 0) {
531       check_opt(argc, argv, i, 1, "<output filename>");
532       strncpy(output_filename, argv[i+1], buffer_size);
533       i += 2;
534     }
535
536     else if(strcmp(argv[i], "-s") == 0) {
537       check_opt(argc, argv, i, 1, "<pattern separator>");
538       pattern_separator = argv[i+1][0];
539       i += 2;
540     }
541
542     else if(strcmp(argv[i], "-v") == 0) {
543       output_to_vt_buffer = 1;
544       i++;
545     }
546
547     else if(strcmp(argv[i], "-m") == 0) {
548       with_colors = 0;
549       i++;
550     }
551
552     else if(strcmp(argv[i], "-f") == 0) {
553       check_opt(argc, argv, i, 1, "<input filename>");
554       strncpy(input_filename, argv[i+1], buffer_size);
555       i += 2;
556     }
557
558     else if(strcmp(argv[i], "-i") == 0) {
559       inverse_order = 1;
560       i++;
561     }
562
563     else if(strcmp(argv[i], "-b") == 0) {
564       bash_history = 1;
565       i++;
566     }
567
568     else if(strcmp(argv[i], "-z") == 0) {
569       zsh_history = 1;
570       i++;
571     }
572
573     else if(strcmp(argv[i], "-d") == 0) {
574       remove_duplicates = 1;
575       i++;
576     }
577
578     else if(strcmp(argv[i], "-e") == 0) {
579       use_regexp = 1;
580       i++;
581     }
582
583     else if(strcmp(argv[i], "-a") == 0) {
584       case_sensitive = 1;
585       i++;
586     }
587
588     else if(strcmp(argv[i], "-t") == 0) {
589       check_opt(argc, argv, i, 1, "<title>");
590       delete[] title;
591       title = new char[strlen(argv[i+1]) + 1];
592       strcpy(title, argv[i+1]);
593       i += 2;
594     }
595
596     else if(strcmp(argv[i], "-l") == 0) {
597       check_opt(argc, argv, i, 1, "<maximum number of lines>");
598       nb_lines_max = atoi(argv[i+1]);
599       i += 2;
600     }
601
602     else if(strcmp(argv[i], "-c") == 0) {
603       check_opt(argc, argv, i, 4, "<fg modeline> <bg modeline> <fg highlight> <bg highlight>");
604       color_fg_modeline = atoi(argv[i+1]);
605       color_bg_modeline = atoi(argv[i+2]);
606       color_fg_highlight = atoi(argv[i+3]);
607       color_bg_highlight = atoi(argv[i+4]);
608       i += 5;
609     }
610
611     else if(strcmp(argv[i], "-h") == 0) {
612       show_help = 1;
613       i++;
614     }
615
616     else {
617       cerr << "Unknown argument " << argv[i] << "." << endl;
618       error = 1;
619     }
620   }
621
622   if(show_help || error) {
623     cerr << "Selector version " << VERSION << "-R" << REVISION_NUMBER
624          << endl
625          << "Written by Francois Fleuret <francois@fleuret.org>."
626          << endl
627          << endl
628          << "Usage: " << argv[0] << " [options] -f <file>" << endl
629          << endl
630          << " -h      show this help" << endl
631          << " -v      inject the selected line in the tty" << endl
632          << " -d      remove duplicated lines" << endl
633          << " -b      remove the bash history line prefix" << endl
634          << " -z      remove the zsh history line prefix" << endl
635          << " -i      invert the order of lines" << endl
636          << " -e      start in regexp mode" << endl
637          << " -a      case sensitive" << endl
638          << " -m      monochrome mode" << endl
639          << " -t <title>" << endl
640          << "         add a title in the modeline" << endl
641          << " -c <fg modeline> <bg modeline> <fg highlight> <bg highlight>" << endl
642          << "         set the display colors" << endl
643          << " -o <output filename>" << endl
644          << "         set a file to write the selected line to" << endl
645          << " -s <pattern separator>" << endl
646          << "         set the symbol to separate substrings in the pattern" << endl
647          << " -l <max number of lines>" << endl
648          << "         set the maximum number of lines to take into account" << endl
649          << endl;
650
651     exit(error);
652   }
653
654   char **lines = new char *[nb_lines_max];
655
656   if(!input_filename[0]) {
657     cerr << "You must specify a input file with -f." << endl;
658     exit(1);
659   }
660
661   int nb_lines = 0;
662
663   ifstream file(input_filename);
664
665   if(file.fail()) {
666     cerr << "Can not open " << input_filename << endl;
667     return 1;
668   }
669
670   int hash_table_size = nb_lines_max * 10;
671   int *hash_table = 0;
672
673   if(remove_duplicates) {
674     hash_table = new_hash_table(hash_table_size);
675   }
676
677   while(nb_lines < nb_lines_max && !file.eof()) {
678
679     file.getline(raw_line, buffer_size);
680
681     if(raw_line[0]) {
682
683       if(file.fail()) {
684         cerr << "Line too long:" << endl;
685         cerr << raw_line << endl;
686         exit(1);
687       }
688
689       char *s, *t;
690       const char *u;
691
692       s = buffer;
693       t = raw_line;
694       while(*t) {
695         u = unctrl(*t++);
696         while(*u) { *s++ = *u++; }
697       }
698       *s = '\0';
699
700       s = buffer;
701
702       if(zsh_history && *s == ':') {
703         while(*s && *s != ';') s++;
704         if(*s == ';') s++;
705       }
706
707       if(bash_history && (*s == ' ' || (*s >= '0' && *s <= '9'))) {
708         while(*s == ' ' || (*s >= '0' && *s <= '9')) s++;
709       }
710
711       int dup;
712
713       if(hash_table) {
714         dup = test_and_add(s, nb_lines, lines, hash_table, hash_table_size);
715       } else {
716         dup = -1;
717       }
718
719       if(dup < 0) {
720         lines[nb_lines] = new char[strlen(s) + 1];
721         strcpy(lines[nb_lines], s);
722       } else {
723         // The string was already in there, so we do not allocate a
724         // new string but use the pointer to the first occurence of it
725         lines[nb_lines] = lines[dup];
726         lines[dup] = 0;
727       }
728
729       nb_lines++;
730     }
731   }
732
733   delete[] hash_table;
734
735   // Now remove the null strings
736
737   int n = 0;
738   for(int k = 0; k < nb_lines; k++) {
739     if(lines[k]) {
740       lines[n++] = lines[k];
741     }
742   }
743
744   nb_lines = n;
745
746   if(inverse_order) {
747     for(int i = 0; i < nb_lines / 2; i++) {
748       char *s = lines[nb_lines - 1 - i];
749       lines[nb_lines - 1 - i] = lines[i];
750       lines[i] = s;
751     }
752   }
753
754   char pattern[buffer_size];
755   pattern[0] = '\0';
756   int cursor_position;
757   cursor_position = 0;
758
759   //////////////////////////////////////////////////////////////////////
760   // Here we start to display with curse
761
762   initscr();
763
764   noecho();
765
766   // Hide the cursor
767   // curs_set(0);
768
769   // So that the arrow keys work
770   keypad(stdscr, TRUE);
771
772   if(with_colors) {
773     if(has_colors()) {
774       start_color();
775       if(color_fg_modeline < 0  || color_fg_modeline >= COLORS ||
776          color_bg_modeline < 0  || color_bg_modeline >= COLORS ||
777          color_fg_highlight < 0 || color_bg_highlight >= COLORS ||
778          color_bg_highlight < 0 || color_bg_highlight >= COLORS) {
779         echo();
780         // curs_set(1);
781         endwin();
782         cerr << "Color numbers have to be between 0 and " << COLORS - 1 << "." << endl;
783         exit(1);
784       }
785       init_pair(1, color_fg_modeline, color_bg_modeline);
786       init_pair(2, color_fg_highlight, color_bg_highlight);
787       init_pair(3, color_bg_modeline, color_fg_modeline);
788     } else {
789       with_colors = 0;
790     }
791   }
792
793   int key;
794   int current_line = 0, temporary_line = 0;
795
796   update_screen(&current_line, &temporary_line, 0, nb_lines, lines, cursor_position, pattern);
797
798   do {
799
800     key = getch();
801
802     int motion = 0;
803
804     if(key >= ' ' && key <= '~') { // Insert character
805       insert_char(pattern, &cursor_position, key);
806     }
807
808     else if(key == KEY_BACKSPACE ||
809             key == '\010' || // ^H
810             key == '\177') { // ^?
811       backspace_char(pattern, &cursor_position);
812     }
813
814     else if(key == KEY_DC ||
815             key == '\004') { // ^D
816       delete_char(pattern, &cursor_position);
817     }
818
819     else if(key == KEY_HOME) {
820       current_line = 0;
821     }
822
823     else if(key == KEY_END) {
824       current_line = nb_lines - 1;
825     }
826
827     else if(key == KEY_NPAGE) {
828       motion = 10;
829     }
830
831     else if(key == KEY_PPAGE) {
832       motion = -10;
833     }
834
835     else if(key == KEY_DOWN ||
836             key == '\016') { // ^N
837       motion = 1;
838     }
839
840     else if(key == KEY_UP ||
841             key == '\020') { // ^P
842       motion = -1;
843     }
844
845     else if(key == KEY_LEFT ||
846             key == '\002') { // ^B
847       if(cursor_position > 0) cursor_position--;
848     }
849
850     else if(key == KEY_RIGHT ||
851             key == '\006') { // ^F
852       if(pattern[cursor_position]) cursor_position++;
853     }
854
855     else if(key == '\001') { // ^A
856       cursor_position = 0;
857     }
858
859     else if(key == '\005') { // ^E
860       cursor_position = strlen(pattern);
861     }
862
863     else if(key == '\022') { // ^R
864       use_regexp = !use_regexp;
865     }
866
867     else if(key == '\025') { // ^U
868       kill_before_cursor(pattern, &cursor_position);
869     }
870
871     else if(key == '\013') { // ^K
872       kill_after_cursor(pattern, &cursor_position);
873     }
874
875     update_screen(&current_line, &temporary_line, motion,
876                   nb_lines, lines, cursor_position, pattern);
877
878   } while(key != '\n' && key != KEY_ENTER && key != '\007'); // ^G
879
880   echo();
881   // curs_set(1);
882   endwin();
883
884   //////////////////////////////////////////////////////////////////////
885   // Here we come back to standard display
886
887   if((key == KEY_ENTER || key == '\n')) {
888
889     if(output_to_vt_buffer) {
890       if(temporary_line >= 0 && temporary_line < nb_lines) {
891         inject_into_tty_buffer(lines[temporary_line]);
892       }
893     }
894
895     if(output_filename[0]) {
896       ofstream out(output_filename);
897       if(out.fail()) {
898         cerr << "Can not open " << output_filename << " for writing." << endl;
899         exit(1);
900       } else {
901         if(temporary_line >= 0 && temporary_line < nb_lines) {
902           out << lines[temporary_line] << endl;
903         } else {
904           out << endl;
905         }
906       }
907       out.flush();
908     }
909
910   }
911
912   for(int l = 0; l < nb_lines; l++) {
913     delete[] lines[l];
914   }
915
916   delete[] lines;
917   delete[] title;
918
919   exit(0);
920 }