Removed a bug when inserting a char in the middle of the pattern.
[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
215 int previous_visible(int current_line, int nb_lines, char **lines, matcher_t *matcher) {
216   int line = current_line - 1;
217   while(line >= 0 && !match(lines[line], matcher)) line--;
218   return line;
219 }
220
221 int next_visible(int current_line, int nb_lines, char **lines, matcher_t *matcher) {
222   int line = current_line + 1;
223   while(line < nb_lines && !match(lines[line], matcher)) line++;
224
225   if(line < nb_lines)
226     return line;
227   else
228     return -1;
229 }
230
231 //////////////////////////////////////////////////////////////////////
232
233 void update_screen(int *current_line, int *temporary_line, int motion,
234                    int nb_lines, char **lines,
235                    int cursor_position,
236                    char *pattern) {
237
238   char buffer[buffer_size];
239   matcher_t matcher;
240
241   initialize_matcher(use_regexp, case_sensitive, &matcher, pattern);
242
243   // We now take care of printing the lines per se
244
245   int console_width = getmaxx(stdscr);
246   int console_height = getmaxy(stdscr);
247
248   // First, we find a visible line. In priority: The current, or the
249   // first visible after it, or the first visible before it.
250
251   int nb_printed_lines = 0;
252
253   clear();
254   use_default_colors();
255   addstr("\n");
256
257   if(matcher.regexp_error) {
258     addstr("[regexp error]");
259   } else if(nb_lines > 0) {
260     int new_line;
261     if(match(lines[*current_line], &matcher)) {
262       new_line = *current_line;
263     } else {
264       new_line = next_visible(*current_line, nb_lines, lines, &matcher);
265       if(new_line < 0) {
266         new_line = previous_visible(*current_line, nb_lines, lines, &matcher);
267       }
268     }
269
270     // If we found a visible line and we should move, let's move
271
272     if(new_line >= 0 && motion != 0) {
273       int l = new_line;
274       if(motion > 0) {
275         // We want to go down, let's find the first visible line below
276         for(int m = 0; l >= 0 && m < motion; m++) {
277           l = next_visible(l, nb_lines, lines, &matcher);
278           if(l >= 0) {
279             new_line = l;
280           }
281         }
282       } else {
283         // We want to go up, let's find the first visible line above
284         for(int m = 0; l >= 0 && m < -motion; m++) {
285           l = previous_visible(l, nb_lines, lines, &matcher);
286           if(l >= 0) {
287             new_line = l;
288           }
289         }
290       }
291     }
292
293     // Here new_line is either a line number matching the patterns, or -1
294
295     if(new_line >= 0) {
296
297       int first_line = new_line, last_line = new_line, nb_match = 1;
298
299       // We find the first and last line to show, so that the total of
300       // visible lines between them (them include) is console_height - 1
301
302       while(nb_match < console_height-1 && (first_line > 0 || last_line < nb_lines - 1)) {
303
304         if(first_line > 0) {
305           first_line--;
306           while(first_line > 0 && !match(lines[first_line], &matcher)) {
307             first_line--;
308           }
309           if(match(lines[first_line], &matcher)) {
310             nb_match++;
311           }
312         }
313
314         if(nb_match < console_height - 1 && last_line < nb_lines - 1) {
315           last_line++;
316           while(last_line < nb_lines - 1 && !match(lines[last_line], &matcher)) {
317             last_line++;
318           }
319
320           if(match(lines[last_line], &matcher)) {
321             nb_match++;
322           }
323         }
324       }
325
326       // Now we display them
327
328       for(int l = first_line; l <= last_line; l++) {
329         if(match(lines[l], &matcher)) {
330           int k = 0;
331
332           while(lines[l][k] && k < buffer_size - 2 && k < console_width - 2) {
333             buffer[k] = lines[l][k];
334             k++;
335           }
336
337           // We fill the rest of the line with blanks if either we did
338           // not clear() or if this is the highlighted line
339
340           if(l == new_line) {
341             while(k < console_width) {
342               buffer[k++] = ' ';
343             }
344           }
345
346           buffer[k++] = '\n';
347           buffer[k++] = '\0';
348
349           // Highlight the highlighted line ...
350
351           if(l == new_line) {
352             if(with_colors) {
353               attron(COLOR_PAIR(2));
354               addnstr(buffer, console_width);
355               attroff(COLOR_PAIR(2));
356             } else {
357               attron(A_STANDOUT);
358               addnstr(buffer, console_width);
359               attroff(A_STANDOUT);
360             }
361           } else {
362             addnstr(buffer, console_width);
363           }
364
365           nb_printed_lines++;
366         }
367       }
368
369       if(motion != 0) {
370         *current_line = new_line;
371       }
372     }
373
374     *temporary_line = new_line;
375
376     if(nb_printed_lines == 0) {
377       addnstr("[no selection]\n", console_width);
378     }
379   } else {
380     addnstr("[empty choice]\n", console_width);
381   }
382
383   // Draw the modeline
384
385   move(0, 0);
386   if(with_colors) {
387     attron(COLOR_PAIR(1));
388   } else {
389     attron(A_REVERSE);
390   }
391
392   for(int k = 0; k < console_width; k++) buffer[k] = ' ';
393   buffer[console_width] = '\0';
394   addnstr(buffer, console_width);
395
396   move(0, 0);
397
398   if(title) {
399     addstr(title);
400     addstr(" ");
401   }
402
403   printw("%d/%d ", nb_printed_lines, nb_lines);
404
405   addnstr(pattern, cursor_position);
406
407   // Now we print the cursor. All that mess to have reverse video with
408   // and without color.
409
410   if(with_colors) {
411     attroff(COLOR_PAIR(1));
412     attron(COLOR_PAIR(3));
413   } else {
414     attroff(A_REVERSE);
415   }
416
417   if(pattern[cursor_position]) {
418     addnstr(&pattern[cursor_position], 1);
419   } else {
420     addstr(" ");
421   }
422
423   if(with_colors) {
424     attroff(COLOR_PAIR(3));
425     attron(COLOR_PAIR(1));
426   } else {
427     attron(A_REVERSE);
428   }
429
430   if(pattern[cursor_position]) {
431     addstr(pattern + cursor_position + 1);
432   }
433
434   // Finished printing the cursor
435
436   if(use_regexp) {
437     addstr(" [regexp]");
438   }
439
440   if(with_colors) {
441     attroff(COLOR_PAIR(1));
442   } else {
443     attroff(A_REVERSE);
444   }
445
446   // We are done
447
448   refresh();
449   free_matcher(&matcher);
450 }
451
452 //////////////////////////////////////////////////////////////////////
453
454 int main(int argc, char **argv) {
455
456   if(!ttyname(STDIN_FILENO)) {
457     cerr << "The standard input is not a tty." << endl;
458     exit(1);
459   }
460
461   char buffer[buffer_size], raw_line[buffer_size];;
462   int color_fg_modeline, color_bg_modeline;
463   int color_fg_highlight, color_bg_highlight;
464
465   color_fg_modeline  = COLOR_WHITE;
466   color_bg_modeline  = COLOR_BLACK;
467   color_fg_highlight = COLOR_BLACK;
468   color_bg_highlight = COLOR_YELLOW;
469
470   setlocale(LC_ALL, "");
471
472   char input_filename[buffer_size], output_filename[buffer_size];
473
474   strcpy(input_filename, "");
475   strcpy(output_filename, "");
476
477   int i = 1;
478   int error = 0, show_help = 0;
479
480   while(!error && !show_help && i < argc) {
481
482     if(strcmp(argv[i], "-o") == 0) {
483       check_opt(argc, argv, i, 1, "<output filename>");
484       strncpy(output_filename, argv[i+1], buffer_size);
485       i += 2;
486     }
487
488     else if(strcmp(argv[i], "-s") == 0) {
489       check_opt(argc, argv, i, 1, "<pattern separator>");
490       pattern_separator = argv[i+1][0];
491       i += 2;
492     }
493
494     else if(strcmp(argv[i], "-v") == 0) {
495       output_to_vt_buffer = 1;
496       i++;
497     }
498
499     else if(strcmp(argv[i], "-m") == 0) {
500       with_colors = 0;
501       i++;
502     }
503
504     else if(strcmp(argv[i], "-f") == 0) {
505       check_opt(argc, argv, i, 1, "<input filename>");
506       strncpy(input_filename, argv[i+1], buffer_size);
507       i += 2;
508     }
509
510     else if(strcmp(argv[i], "-i") == 0) {
511       inverse_order = 1;
512       i++;
513     }
514
515     else if(strcmp(argv[i], "-b") == 0) {
516       bash_history = 1;
517       i++;
518     }
519
520     else if(strcmp(argv[i], "-z") == 0) {
521       zsh_history = 1;
522       i++;
523     }
524
525     else if(strcmp(argv[i], "-d") == 0) {
526       remove_duplicates = 1;
527       i++;
528     }
529
530     else if(strcmp(argv[i], "-e") == 0) {
531       use_regexp = 1;
532       i++;
533     }
534
535     else if(strcmp(argv[i], "-a") == 0) {
536       case_sensitive = 1;
537       i++;
538     }
539
540     else if(strcmp(argv[i], "-t") == 0) {
541       check_opt(argc, argv, i, 1, "<title>");
542       delete[] title;
543       title = new char[strlen(argv[i+1]) + 1];
544       strcpy(title, argv[i+1]);
545       i += 2;
546     }
547
548     else if(strcmp(argv[i], "-l") == 0) {
549       check_opt(argc, argv, i, 1, "<maximum number of lines>");
550       nb_lines_max = atoi(argv[i+1]);
551       i += 2;
552     }
553
554     else if(strcmp(argv[i], "-c") == 0) {
555       check_opt(argc, argv, i, 4, "<fg modeline> <bg modeline> <fg highlight> <bg highlight>");
556       color_fg_modeline = atoi(argv[i+1]);
557       color_bg_modeline = atoi(argv[i+2]);
558       color_fg_highlight = atoi(argv[i+3]);
559       color_bg_highlight = atoi(argv[i+4]);
560       i += 5;
561     }
562
563     else if(strcmp(argv[i], "-h") == 0) {
564       show_help = 1;
565       i++;
566     }
567
568     else {
569       cerr << "Unknown argument " << argv[i] << "." << endl;
570       error = 1;
571     }
572   }
573
574   if(show_help || error) {
575     cerr << "Selector version " << VERSION << "-R" << REVISION_NUMBER
576          << endl
577          << "Written by Francois Fleuret <francois@fleuret.org>."
578          << endl
579          << endl
580          << "Usage: " << argv[0] << " [options] -f <file>" << endl
581          << endl
582          << " -h      show this help" << endl
583          << " -v      inject the selected line in the tty" << endl
584          << " -d      remove duplicated lines" << endl
585          << " -b      remove the bash history line prefix" << endl
586          << " -z      remove the zsh history line prefix" << endl
587          << " -i      invert the order of lines" << endl
588          << " -e      start in regexp mode" << endl
589          << " -a      case sensitive" << endl
590          << " -m      monochrome mode" << endl
591          << " -t <title>" << endl
592          << "         add a title in the modeline" << endl
593          << " -c <fg modeline> <bg modeline> <fg highlight> <bg highlight>" << endl
594          << "         set the display colors" << endl
595          << " -o <output filename>" << endl
596          << "         set a file to write the selected line to" << endl
597          << " -s <pattern separator>" << endl
598          << "         set the symbol to separate substrings in the pattern" << endl
599          << " -l <max number of lines>" << endl
600          << "         set the maximum number of lines to take into account" << endl
601          << endl;
602
603     exit(error);
604   }
605
606   char **lines = new char *[nb_lines_max];
607
608   if(!input_filename[0]) {
609     cerr << "You must specify a input file with -f." << endl;
610     exit(1);
611   }
612
613   int nb_lines = 0;
614
615   ifstream file(input_filename);
616
617   if(file.fail()) {
618     cerr << "Can not open " << input_filename << endl;
619     return 1;
620   }
621
622   int hash_table_size = nb_lines_max * 10;
623   int *hash_table = 0;
624
625   if(remove_duplicates) {
626     hash_table = new_hash_table(hash_table_size);
627   }
628
629   while(nb_lines < nb_lines_max && !file.eof()) {
630
631     file.getline(raw_line, buffer_size);
632
633     if(raw_line[0]) {
634
635       if(file.fail()) {
636         cerr << "Line too long:" << endl;
637         cerr << raw_line << endl;
638         exit(1);
639       }
640
641       char *s, *t;
642       const char *u;
643
644       s = buffer;
645       t = raw_line;
646       while(*t) {
647         u = unctrl(*t++);
648         while(*u) { *s++ = *u++; }
649       }
650       *s = '\0';
651
652       s = buffer;
653
654       if(zsh_history && *s == ':') {
655         while(*s && *s != ';') s++;
656         if(*s == ';') s++;
657       }
658
659       if(bash_history && (*s == ' ' || (*s >= '0' && *s <= '9'))) {
660         while(*s == ' ' || (*s >= '0' && *s <= '9')) s++;
661       }
662
663       int dup;
664
665       if(hash_table) {
666         dup = test_and_add(s, nb_lines, lines, hash_table, hash_table_size);
667       } else {
668         dup = -1;
669       }
670
671       if(dup < 0) {
672         lines[nb_lines] = new char[strlen(s) + 1];
673         strcpy(lines[nb_lines], s);
674       } else {
675         // The string was already in there, so we do not allocate a
676         // new string but use the pointer to the first occurence of it
677         lines[nb_lines] = lines[dup];
678         lines[dup] = 0;
679       }
680
681       nb_lines++;
682     }
683   }
684
685   delete[] hash_table;
686
687   // Now remove the null strings
688
689   int n = 0;
690   for(int k = 0; k < nb_lines; k++) {
691     if(lines[k]) {
692       lines[n++] = lines[k];
693     }
694   }
695   nb_lines = n;
696
697   if(inverse_order) {
698     for(int i = 0; i < nb_lines/2; i++) {
699       char *s = lines[nb_lines - 1 - i];
700       lines[nb_lines - 1 - i] = lines[i];
701       lines[i] = s;
702     }
703   }
704
705   char pattern[buffer_size];
706   pattern[0] = '\0';
707   int cursor_position;
708   cursor_position = 0;
709
710   //////////////////////////////////////////////////////////////////////
711   // Here we start to display with curse
712
713   initscr();
714
715   noecho();
716
717   // Hide the cursor
718   curs_set(0);
719
720   // So that the arrow keys work
721   keypad(stdscr, TRUE);
722
723   if(with_colors) {
724     if(has_colors()) {
725       start_color();
726       if(color_fg_modeline < 0  || color_fg_modeline >= COLORS ||
727          color_bg_modeline < 0  || color_bg_modeline >= COLORS ||
728          color_fg_highlight < 0 || color_bg_highlight >= COLORS ||
729          color_bg_highlight < 0 || color_bg_highlight >= COLORS) {
730         echo();
731         curs_set(1);
732         endwin();
733         cerr << "Color numbers have to be between 0 and " << COLORS - 1 << "." << endl;
734         exit(1);
735       }
736       init_pair(1, color_fg_modeline, color_bg_modeline);
737       init_pair(2, color_fg_highlight, color_bg_highlight);
738       init_pair(3, color_bg_modeline, color_fg_modeline);
739     } else {
740       with_colors = 0;
741     }
742   }
743
744   int key;
745   int current_line = 0, temporary_line = 0;
746
747   update_screen(&current_line, &temporary_line, 0, nb_lines, lines, cursor_position, pattern);
748
749   do {
750
751     key = getch();
752
753     int motion = 0;
754
755     if(key >= ' ' && key <= '~') { // Insert character
756       int c = cursor_position;
757       char t = pattern[c], u;
758       while(t) {
759         c++;
760         u = pattern[c];
761         pattern[c] = t;
762         t = u;
763       }
764       c++;
765       pattern[c] = '\0';
766       pattern[cursor_position++] = key;
767     }
768
769     else if(key == KEY_BACKSPACE || key == '\b' || key == '\7f') {
770       if(cursor_position > 0) {
771         if(pattern[cursor_position]) {
772           int c = cursor_position-1;
773           while(pattern[c]) {
774             pattern[c] = pattern[c+1];
775             c++;
776           }
777         } else {
778           pattern[cursor_position - 1] = '\0';
779         }
780         cursor_position--;
781       }
782     }
783
784     else if(key == KEY_DC || key == '\ 4') {
785       if(pattern[cursor_position]) {
786         int c = cursor_position;
787         while(pattern[c]) {
788           pattern[c] = pattern[c+1];
789           c++;
790         }
791       }
792     }
793
794     else if(key == KEY_HOME) {
795       current_line = 0;
796     }
797
798     else if(key == KEY_END) {
799       current_line = nb_lines - 1;
800     }
801
802     else if(key == KEY_NPAGE) {
803       motion = 10;
804     }
805
806     else if(key == KEY_PPAGE) {
807       motion = -10;
808     }
809
810     else if(key == KEY_DOWN || key == '\ e') {
811       motion = 1;
812     }
813
814     else if(key == KEY_UP || key == '\10') {
815       motion = -1;
816     }
817
818     else if(key == KEY_LEFT || key == '\ 2') {
819       if(cursor_position > 0) cursor_position--;
820     }
821
822     else if(key == KEY_RIGHT || key == '\ 6') {
823       if(pattern[cursor_position]) cursor_position++;
824     }
825
826     else if(key == '\ 1') {
827       cursor_position = 0;
828     }
829
830     else if(key == '\ 5') {
831       cursor_position = strlen(pattern);
832     }
833
834     else if(key == '\12') {
835       use_regexp = !use_regexp;
836     }
837
838     else if(key == '\15') {
839       int s = 0;
840       while(pattern[cursor_position + s]) {
841         pattern[s] = pattern[cursor_position + s];
842         s++;
843       }
844       pattern[s] = '\0';
845       cursor_position = 0;
846     }
847
848     else if(key == '\v') {
849       pattern[cursor_position] = '\0';
850     }
851
852     update_screen(&current_line, &temporary_line, motion,
853                   nb_lines, lines, cursor_position, pattern);
854
855   } while(key != '\n' && key != KEY_ENTER && key != '\a');
856
857   echo();
858   curs_set(1);
859   endwin();
860
861   //////////////////////////////////////////////////////////////////////
862   // Here we come back to standard display
863
864   if((key == KEY_ENTER || key == '\n')) {
865
866     if(output_to_vt_buffer) {
867       if(temporary_line >= 0 && temporary_line < nb_lines) {
868         inject_into_tty_buffer(lines[temporary_line]);
869       }
870     }
871
872     if(output_filename[0]) {
873       ofstream out(output_filename);
874       if(out.fail()) {
875         cerr << "Can not open " << output_filename << " for writing." << endl;
876         exit(1);
877       } else {
878         if(temporary_line >= 0 && temporary_line < nb_lines) {
879           out << lines[temporary_line] << endl;
880         } else {
881           out << endl;
882         }
883       }
884       out.flush();
885     }
886
887   }
888
889   for(int l = 0; l < nb_lines; l++) {
890     delete[] lines[l];
891   }
892
893   delete[] lines;
894   delete[] title;
895
896   exit(0);
897 }