2926
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2926
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3225
|
28 #include <cstdlib> |
2926
|
29 #include <cstring> |
|
30 |
|
31 #include <string> |
|
32 |
|
33 #ifdef HAVE_UNISTD_H |
|
34 #ifdef HAVE_SYS_TYPES_H |
|
35 #include <sys/types.h> |
|
36 #endif |
|
37 #include <unistd.h> |
|
38 #endif |
|
39 |
5247
|
40 #include "quit.h" |
|
41 |
2926
|
42 #include "cmd-edit.h" |
|
43 #include "cmd-hist.h" |
5872
|
44 #include "file-ops.h" |
2926
|
45 #include "lo-error.h" |
|
46 #include "lo-utils.h" |
|
47 #include "oct-env.h" |
3260
|
48 #include "oct-time.h" |
2926
|
49 |
|
50 command_editor *command_editor::instance = 0; |
|
51 |
6913
|
52 std::set<command_editor::startup_hook_fcn> command_editor::startup_hook_set; |
|
53 |
|
54 std::set<command_editor::event_hook_fcn> command_editor::event_hook_set; |
|
55 |
2926
|
56 #if defined (USE_READLINE) |
|
57 |
|
58 #include <cstdio> |
|
59 #include <cstdlib> |
|
60 |
3519
|
61 #include "oct-rl-edit.h" |
2926
|
62 |
|
63 class |
|
64 gnu_readline : public command_editor |
|
65 { |
|
66 public: |
|
67 |
3519
|
68 typedef command_editor::startup_hook_fcn startup_hook_fcn; |
|
69 |
6913
|
70 typedef command_editor::event_hook_fcn event_hook_fcn; |
2926
|
71 |
2941
|
72 typedef command_editor::completion_fcn completion_fcn; |
|
73 |
2926
|
74 gnu_readline (void); |
|
75 |
|
76 ~gnu_readline (void) { } |
|
77 |
3504
|
78 void do_set_name (const std::string& n); |
2926
|
79 |
3504
|
80 std::string do_readline (const std::string& prompt, bool& eof); |
2926
|
81 |
|
82 void do_set_input_stream (FILE *f); |
|
83 |
|
84 FILE *do_get_input_stream (void); |
|
85 |
|
86 void do_set_output_stream (FILE *f); |
|
87 |
|
88 FILE *do_get_output_stream (void); |
|
89 |
|
90 int do_terminal_rows (void); |
|
91 |
|
92 int do_terminal_cols (void); |
|
93 |
|
94 void do_clear_screen (void); |
|
95 |
3281
|
96 void do_resize_terminal (void); |
|
97 |
3504
|
98 std::string newline_chars (void); |
2926
|
99 |
|
100 void do_restore_terminal_state (void); |
|
101 |
|
102 void do_blink_matching_paren (bool flag); |
|
103 |
3933
|
104 void do_set_basic_word_break_characters (const std::string& s); |
|
105 |
|
106 void do_set_completer_word_break_characters (const std::string& s); |
|
107 |
3504
|
108 void do_set_basic_quote_characters (const std::string& s); |
2926
|
109 |
|
110 void do_set_completion_append_character (char c); |
|
111 |
2941
|
112 void do_set_completion_function (completion_fcn f); |
|
113 |
|
114 completion_fcn do_get_completion_function (void) const; |
2926
|
115 |
4604
|
116 string_vector |
|
117 do_generate_filename_completions (const std::string& text); |
|
118 |
3504
|
119 void do_insert_text (const std::string& text); |
2926
|
120 |
|
121 void do_newline (void); |
|
122 |
|
123 void do_clear_undo_list (void); |
|
124 |
6913
|
125 void set_startup_hook (startup_hook_fcn f); |
|
126 |
|
127 void restore_startup_hook (void); |
2926
|
128 |
6913
|
129 void set_event_hook (event_hook_fcn f); |
2926
|
130 |
6913
|
131 void restore_event_hook (void); |
3215
|
132 |
|
133 void do_restore_event_hook (void); |
|
134 |
3504
|
135 void do_read_init_file (const std::string& file); |
3189
|
136 |
4143
|
137 bool do_filename_completion_desired (bool); |
|
138 |
3519
|
139 static int operate_and_get_next (int, int); |
2926
|
140 |
3951
|
141 static int history_search_backward (int, int); |
|
142 |
|
143 static int history_search_forward (int, int); |
|
144 |
2926
|
145 private: |
|
146 |
3519
|
147 startup_hook_fcn previous_startup_hook; |
2926
|
148 |
3519
|
149 event_hook_fcn previous_event_hook; |
3215
|
150 |
2941
|
151 completion_fcn completion_function; |
|
152 |
|
153 static char *command_generator (const char *text, int state); |
|
154 |
3519
|
155 static char **command_completer (const char *text, int start, int end); |
2926
|
156 }; |
|
157 |
|
158 gnu_readline::gnu_readline () |
3215
|
159 : command_editor (), previous_startup_hook (0), |
|
160 previous_event_hook (0), completion_function (0) |
2926
|
161 { |
5775
|
162 // FIXME -- need interface to rl_add_defun, rl_initialize, and |
3519
|
163 // a function to set rl_terminal_name |
3225
|
164 |
3520
|
165 std::string term = octave_env::getenv ("TERM"); |
3519
|
166 |
|
167 octave_rl_set_terminal_name (term.c_str ()); |
|
168 |
|
169 octave_rl_initialize (); |
2926
|
170 |
|
171 do_blink_matching_paren (true); |
|
172 |
5452
|
173 // Bind operate-and-get-next. |
2926
|
174 |
3519
|
175 octave_rl_add_defun ("operate-and-get-next", |
|
176 gnu_readline::operate_and_get_next, |
|
177 octave_rl_ctrl ('O')); |
2926
|
178 |
5452
|
179 // And the history search functions. |
2926
|
180 |
3519
|
181 octave_rl_add_defun ("history-search-backward", |
3951
|
182 gnu_readline::history_search_backward, |
3519
|
183 octave_rl_meta ('P')); |
2926
|
184 |
3519
|
185 octave_rl_add_defun ("history-search-forward", |
3951
|
186 gnu_readline::history_search_forward, |
3519
|
187 octave_rl_meta ('N')); |
2926
|
188 } |
|
189 |
3519
|
190 |
|
191 |
2926
|
192 void |
3519
|
193 gnu_readline::do_set_name (const std::string& nm) |
2926
|
194 { |
3519
|
195 ::octave_rl_set_name (nm.c_str ()); |
2926
|
196 } |
|
197 |
3504
|
198 std::string |
|
199 gnu_readline::do_readline (const std::string& prompt, bool& eof) |
2926
|
200 { |
3504
|
201 std::string retval; |
2926
|
202 |
3219
|
203 eof = false; |
|
204 |
5247
|
205 char *line = 0; |
|
206 |
5336
|
207 const char *p = prompt.c_str (); |
|
208 |
5452
|
209 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5247
|
210 |
5336
|
211 line = ::octave_rl_readline (p); |
5247
|
212 |
5452
|
213 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
2926
|
214 |
|
215 if (line) |
|
216 { |
|
217 retval = line; |
|
218 |
|
219 free (line); |
|
220 } |
3219
|
221 else |
|
222 eof = true; |
2926
|
223 |
|
224 return retval; |
|
225 } |
|
226 |
|
227 void |
|
228 gnu_readline::do_set_input_stream (FILE *f) |
|
229 { |
3519
|
230 ::octave_rl_set_input_stream (f); |
2926
|
231 } |
|
232 |
|
233 FILE * |
|
234 gnu_readline::do_get_input_stream (void) |
|
235 { |
3519
|
236 return ::octave_rl_get_input_stream (); |
2926
|
237 } |
|
238 |
|
239 void |
|
240 gnu_readline::do_set_output_stream (FILE *f) |
|
241 { |
3519
|
242 ::octave_rl_set_output_stream (f); |
2926
|
243 } |
|
244 |
|
245 FILE * |
|
246 gnu_readline::do_get_output_stream (void) |
|
247 { |
3519
|
248 return ::octave_rl_get_output_stream (); |
2926
|
249 } |
|
250 |
|
251 // GNU readline handles SIGWINCH, so these values have a good chance |
|
252 // of being correct even if the window changes size (they may be |
|
253 // wrong if, for example, the luser changes the window size while the |
|
254 // pager is running, and the signal is handled by the pager instead of |
|
255 // us. |
|
256 |
|
257 int |
|
258 gnu_readline::do_terminal_rows (void) |
|
259 { |
3519
|
260 int sh = ::octave_rl_screen_height (); |
|
261 |
|
262 return sh > 0 ? sh : 24; |
2926
|
263 } |
|
264 |
|
265 int |
|
266 gnu_readline::do_terminal_cols (void) |
|
267 { |
3519
|
268 int sw = ::octave_rl_screen_width (); |
|
269 |
|
270 return sw > 0 ? sw : 80; |
2926
|
271 } |
|
272 |
|
273 void |
|
274 gnu_readline::do_clear_screen (void) |
|
275 { |
3519
|
276 ::octave_rl_clear_screen (); |
2926
|
277 } |
|
278 |
3281
|
279 void |
|
280 gnu_readline::do_resize_terminal (void) |
|
281 { |
3519
|
282 ::octave_rl_resize_terminal (); |
3281
|
283 } |
|
284 |
3504
|
285 std::string |
2926
|
286 gnu_readline::newline_chars (void) |
|
287 { |
|
288 return "\r\n"; |
|
289 } |
|
290 |
|
291 void |
|
292 gnu_readline::do_restore_terminal_state (void) |
|
293 { |
3519
|
294 ::octave_rl_restore_terminal_state (); |
2926
|
295 } |
|
296 |
|
297 void |
|
298 gnu_readline::do_blink_matching_paren (bool flag) |
|
299 { |
3779
|
300 ::octave_rl_enable_paren_matching (flag ? 1 : 0); |
2926
|
301 } |
|
302 |
|
303 void |
3933
|
304 gnu_readline::do_set_basic_word_break_characters (const std::string& s) |
|
305 { |
|
306 ::octave_rl_set_basic_word_break_characters (s.c_str ()); |
|
307 } |
|
308 |
|
309 void |
|
310 gnu_readline::do_set_completer_word_break_characters (const std::string& s) |
|
311 { |
|
312 ::octave_rl_set_completer_word_break_characters (s.c_str ()); |
|
313 } |
|
314 |
|
315 void |
3504
|
316 gnu_readline::do_set_basic_quote_characters (const std::string& s) |
2926
|
317 { |
3519
|
318 ::octave_rl_set_basic_quote_characters (s.c_str ()); |
2926
|
319 } |
|
320 |
|
321 void |
|
322 gnu_readline::do_set_completion_append_character (char c) |
|
323 { |
3519
|
324 ::octave_rl_set_completion_append_character (c); |
2926
|
325 } |
|
326 |
|
327 void |
2941
|
328 gnu_readline::do_set_completion_function (completion_fcn f) |
2926
|
329 { |
2941
|
330 completion_function = f; |
|
331 |
3519
|
332 rl_attempted_completion_fcn_ptr fp |
|
333 = f ? gnu_readline::command_completer : 0; |
2941
|
334 |
3519
|
335 ::octave_rl_set_completion_function (fp); |
2941
|
336 } |
|
337 |
|
338 gnu_readline::completion_fcn |
|
339 gnu_readline::do_get_completion_function (void) const |
|
340 { |
|
341 return completion_function; |
2926
|
342 } |
|
343 |
4604
|
344 string_vector |
|
345 gnu_readline::do_generate_filename_completions (const std::string& text) |
|
346 { |
|
347 string_vector retval; |
|
348 |
|
349 int n = 0; |
|
350 int count = 0; |
|
351 |
|
352 char *fn = 0; |
|
353 |
|
354 while (1) |
|
355 { |
|
356 fn = ::octave_rl_filename_completion_function (text.c_str (), count); |
|
357 |
|
358 if (fn) |
|
359 { |
|
360 if (count == n) |
|
361 { |
|
362 // Famous last words: Most large directories will not |
|
363 // have more than a few hundred files, so we should not |
|
364 // resize too many times even if the growth is linear... |
|
365 |
|
366 n += 100; |
|
367 retval.resize (n); |
|
368 } |
|
369 |
|
370 retval[count++] = fn; |
|
371 |
|
372 free (fn); |
|
373 } |
|
374 else |
|
375 break; |
|
376 } |
|
377 |
|
378 retval.resize (count); |
|
379 |
|
380 return retval; |
|
381 } |
|
382 |
2926
|
383 void |
3504
|
384 gnu_readline::do_insert_text (const std::string& text) |
2926
|
385 { |
3519
|
386 ::octave_rl_insert_text (text.c_str ()); |
2926
|
387 } |
|
388 |
|
389 void |
|
390 gnu_readline::do_newline (void) |
|
391 { |
3519
|
392 ::octave_rl_newline (); |
2926
|
393 } |
|
394 |
|
395 void |
|
396 gnu_readline::do_clear_undo_list () |
|
397 { |
3519
|
398 ::octave_rl_clear_undo_list (); |
2926
|
399 } |
|
400 |
|
401 void |
6913
|
402 gnu_readline::set_startup_hook (startup_hook_fcn f) |
2926
|
403 { |
3519
|
404 previous_startup_hook = ::octave_rl_get_startup_hook (); |
2926
|
405 |
6917
|
406 if (f != previous_startup_hook) |
|
407 ::octave_rl_set_startup_hook (f); |
2926
|
408 } |
|
409 |
|
410 void |
6913
|
411 gnu_readline::restore_startup_hook (void) |
2926
|
412 { |
3519
|
413 ::octave_rl_set_startup_hook (previous_startup_hook); |
2926
|
414 } |
|
415 |
|
416 void |
6913
|
417 gnu_readline::set_event_hook (event_hook_fcn f) |
3215
|
418 { |
3519
|
419 previous_event_hook = octave_rl_get_event_hook (); |
3215
|
420 |
3519
|
421 ::octave_rl_set_event_hook (f); |
3215
|
422 } |
|
423 |
|
424 void |
6913
|
425 gnu_readline::restore_event_hook (void) |
3215
|
426 { |
3519
|
427 ::octave_rl_set_event_hook (previous_event_hook); |
3215
|
428 } |
|
429 |
|
430 void |
3504
|
431 gnu_readline::do_read_init_file (const std::string& file) |
3189
|
432 { |
3519
|
433 ::octave_rl_read_init_file (file.c_str ()); |
3189
|
434 } |
|
435 |
4143
|
436 bool |
|
437 gnu_readline::do_filename_completion_desired (bool arg) |
|
438 { |
|
439 return ::octave_rl_filename_completion_desired (arg); |
|
440 } |
|
441 |
3519
|
442 int |
2926
|
443 gnu_readline::operate_and_get_next (int /* count */, int /* c */) |
|
444 { |
|
445 // Accept the current line. |
|
446 |
|
447 command_editor::newline (); |
|
448 |
|
449 // Find the current line, and find the next line to use. |
|
450 |
|
451 int x_where = command_history::where (); |
|
452 |
|
453 int x_length = command_history::length (); |
|
454 |
|
455 if ((command_history::is_stifled () |
|
456 && (x_length >= command_history::max_input_history ())) |
|
457 || (x_where >= x_length - 1)) |
|
458 command_history::set_mark (x_where); |
|
459 else |
|
460 command_history::set_mark (x_where + 1); |
|
461 |
6913
|
462 command_editor::add_startup_hook (command_history::goto_mark); |
3519
|
463 |
|
464 return 0; |
2926
|
465 } |
|
466 |
3951
|
467 int |
|
468 gnu_readline::history_search_backward (int count, int c) |
|
469 { |
|
470 return octave_rl_history_search_backward (count, c); |
|
471 } |
|
472 |
|
473 int |
|
474 gnu_readline::history_search_forward (int count, int c) |
|
475 { |
|
476 return octave_rl_history_search_forward (count, c); |
|
477 } |
|
478 |
2941
|
479 char * |
|
480 gnu_readline::command_generator (const char *text, int state) |
|
481 { |
|
482 char *retval = 0; |
|
483 |
|
484 completion_fcn f = command_editor::get_completion_function (); |
|
485 |
3504
|
486 std::string tmp = f (text, state); |
2941
|
487 |
|
488 size_t len = tmp.length (); |
|
489 |
|
490 if (len > 0) |
|
491 { |
|
492 retval = static_cast<char *> (malloc (len+1)); |
|
493 |
|
494 strcpy (retval, tmp.c_str ()); |
|
495 } |
|
496 |
|
497 return retval; |
|
498 } |
|
499 |
|
500 char ** |
3519
|
501 gnu_readline::command_completer (const char *text, int, int) |
2941
|
502 { |
|
503 char **matches = 0; |
3519
|
504 matches |
|
505 = ::octave_rl_completion_matches (text, gnu_readline::command_generator); |
2941
|
506 return matches; |
|
507 } |
|
508 |
2926
|
509 #endif |
|
510 |
|
511 class |
|
512 default_command_editor : public command_editor |
|
513 { |
|
514 public: |
|
515 |
|
516 default_command_editor (void) |
|
517 : command_editor (), input_stream (stdin), output_stream (stdout) { } |
|
518 |
|
519 ~default_command_editor (void) { } |
|
520 |
3504
|
521 std::string do_readline (const std::string& prompt, bool& eof); |
2926
|
522 |
|
523 void do_set_input_stream (FILE *f); |
|
524 |
|
525 FILE *do_get_input_stream (void); |
|
526 |
|
527 void do_set_output_stream (FILE *f); |
|
528 |
|
529 FILE *do_get_output_stream (void); |
|
530 |
4604
|
531 string_vector do_generate_filename_completions (const std::string& text); |
|
532 |
3504
|
533 void do_insert_text (const std::string&); |
2926
|
534 |
|
535 void do_newline (void); |
|
536 |
|
537 private: |
|
538 |
|
539 FILE *input_stream; |
|
540 |
|
541 FILE *output_stream; |
|
542 }; |
|
543 |
3504
|
544 std::string |
|
545 default_command_editor::do_readline (const std::string& prompt, bool& eof) |
2926
|
546 { |
|
547 fprintf (output_stream, prompt.c_str ()); |
|
548 fflush (output_stream); |
|
549 |
4527
|
550 return octave_fgetl (input_stream, eof); |
2926
|
551 } |
|
552 |
|
553 void |
|
554 default_command_editor::do_set_input_stream (FILE *f) |
|
555 { |
|
556 input_stream = f; |
|
557 } |
|
558 |
|
559 FILE * |
|
560 default_command_editor::do_get_input_stream (void) |
|
561 { |
|
562 return input_stream; |
|
563 } |
|
564 |
|
565 void |
|
566 default_command_editor::do_set_output_stream (FILE *f) |
|
567 { |
|
568 output_stream = f; |
|
569 } |
|
570 |
|
571 FILE * |
|
572 default_command_editor::do_get_output_stream (void) |
|
573 { |
|
574 return output_stream; |
|
575 } |
|
576 |
4604
|
577 string_vector |
4663
|
578 default_command_editor::do_generate_filename_completions (const std::string&) |
4604
|
579 { |
5775
|
580 // FIXME |
4604
|
581 return string_vector (); |
|
582 } |
|
583 |
2926
|
584 void |
3504
|
585 default_command_editor::do_insert_text (const std::string&) |
2926
|
586 { |
5775
|
587 // FIXME |
2926
|
588 } |
|
589 |
|
590 void |
|
591 default_command_editor::do_newline (void) |
|
592 { |
5775
|
593 // FIXME |
2926
|
594 } |
|
595 |
|
596 bool |
|
597 command_editor::instance_ok (void) |
|
598 { |
|
599 bool retval = true; |
|
600 |
|
601 if (! instance) |
|
602 make_command_editor (); |
|
603 |
|
604 if (! instance) |
|
605 { |
2941
|
606 current_liboctave_error_handler |
2926
|
607 ("unable to create command history object!"); |
|
608 |
|
609 retval = false; |
|
610 } |
|
611 |
|
612 return retval; |
|
613 } |
|
614 |
|
615 void |
|
616 command_editor::make_command_editor (void) |
|
617 { |
|
618 #if defined (USE_READLINE) |
|
619 instance = new gnu_readline (); |
|
620 #else |
|
621 instance = new default_command_editor (); |
|
622 #endif |
|
623 } |
|
624 |
6913
|
625 int |
|
626 command_editor::startup_handler (void) |
|
627 { |
|
628 for (startup_hook_set_iterator p = startup_hook_set.begin (); |
|
629 p != startup_hook_set.end (); p++) |
|
630 { |
|
631 startup_hook_fcn f = *p; |
|
632 |
|
633 if (f) |
|
634 f (); |
|
635 } |
|
636 |
|
637 return 0; |
|
638 } |
|
639 |
|
640 int |
|
641 command_editor::event_handler (void) |
|
642 { |
|
643 for (event_hook_set_iterator p = event_hook_set.begin (); |
|
644 p != event_hook_set.end (); p++) |
|
645 { |
|
646 event_hook_fcn f = *p; |
|
647 |
|
648 if (f) |
|
649 f (); |
|
650 } |
|
651 |
|
652 return 0; |
|
653 } |
|
654 |
2926
|
655 void |
3504
|
656 command_editor::set_name (const std::string& n) |
2926
|
657 { |
|
658 if (instance_ok ()) |
|
659 instance->do_set_name (n); |
|
660 } |
|
661 |
3504
|
662 std::string |
|
663 command_editor::readline (const std::string& prompt) |
2926
|
664 { |
3219
|
665 bool eof; |
|
666 |
|
667 return readline (prompt, eof); |
|
668 } |
|
669 |
3504
|
670 std::string |
|
671 command_editor::readline (const std::string& prompt, bool& eof) |
3219
|
672 { |
2926
|
673 return (instance_ok ()) |
3504
|
674 ? instance->do_readline (prompt, eof) : std::string (); |
2926
|
675 } |
|
676 |
|
677 void |
|
678 command_editor::set_input_stream (FILE *f) |
|
679 { |
|
680 if (instance_ok ()) |
|
681 instance->do_set_input_stream (f); |
|
682 } |
|
683 |
|
684 FILE * |
|
685 command_editor::get_input_stream (void) |
|
686 { |
|
687 return (instance_ok ()) |
|
688 ? instance->do_get_input_stream () : 0; |
|
689 } |
|
690 |
|
691 void |
|
692 command_editor::set_output_stream (FILE *f) |
|
693 { |
|
694 if (instance_ok ()) |
|
695 instance->do_set_output_stream (f); |
|
696 } |
|
697 |
|
698 FILE * |
|
699 command_editor::get_output_stream (void) |
|
700 { |
|
701 return (instance_ok ()) |
|
702 ? instance->do_get_output_stream () : 0; |
|
703 } |
|
704 |
|
705 int |
|
706 command_editor::terminal_rows (void) |
|
707 { |
|
708 return (instance_ok ()) |
|
709 ? instance->do_terminal_rows () : -1; |
|
710 } |
|
711 |
|
712 int |
|
713 command_editor::terminal_cols (void) |
|
714 { |
|
715 return (instance_ok ()) |
|
716 ? instance->do_terminal_cols () : -1; |
|
717 } |
|
718 |
|
719 void |
|
720 command_editor::clear_screen (void) |
|
721 { |
|
722 if (instance_ok ()) |
|
723 instance->do_clear_screen (); |
|
724 } |
|
725 |
3281
|
726 void |
|
727 command_editor::resize_terminal (void) |
|
728 { |
|
729 if (instance_ok ()) |
|
730 instance->do_resize_terminal (); |
|
731 } |
|
732 |
3504
|
733 std::string |
|
734 command_editor::decode_prompt_string (const std::string& s) |
2926
|
735 { |
|
736 return (instance_ok ()) |
3504
|
737 ? instance->do_decode_prompt_string (s) : std::string (); |
2926
|
738 } |
|
739 |
|
740 int |
|
741 command_editor::current_command_number (void) |
|
742 { |
|
743 return (instance_ok ()) |
|
744 ? instance->command_number : 0; |
|
745 } |
|
746 |
|
747 void |
|
748 command_editor::reset_current_command_number (int n) |
|
749 { |
|
750 if (instance_ok ()) |
|
751 instance->command_number = n; |
|
752 } |
|
753 |
|
754 void |
2967
|
755 command_editor::increment_current_command_number (void) |
|
756 { |
|
757 if (instance_ok ()) |
|
758 instance->command_number++; |
|
759 } |
|
760 |
|
761 void |
2926
|
762 command_editor::restore_terminal_state (void) |
|
763 { |
|
764 if (instance_ok ()) |
|
765 instance->do_restore_terminal_state (); |
|
766 } |
|
767 |
|
768 void |
|
769 command_editor::blink_matching_paren (bool flag) |
|
770 { |
|
771 if (instance_ok ()) |
|
772 instance->do_blink_matching_paren (flag); |
|
773 } |
|
774 |
|
775 void |
3933
|
776 command_editor::set_basic_word_break_characters (const std::string& s) |
|
777 { |
|
778 if (instance_ok ()) |
|
779 instance->do_set_basic_word_break_characters (s); |
|
780 } |
|
781 |
|
782 void |
|
783 command_editor::set_completer_word_break_characters (const std::string& s) |
|
784 { |
|
785 if (instance_ok ()) |
|
786 instance->do_set_completer_word_break_characters (s); |
|
787 } |
|
788 |
|
789 void |
3504
|
790 command_editor::set_basic_quote_characters (const std::string& s) |
2926
|
791 { |
|
792 if (instance_ok ()) |
3004
|
793 instance->do_set_basic_quote_characters (s); |
2926
|
794 } |
|
795 |
|
796 void |
|
797 command_editor::set_completion_append_character (char c) |
|
798 { |
|
799 if (instance_ok ()) |
|
800 instance->do_set_completion_append_character (c); |
|
801 } |
|
802 |
|
803 void |
2941
|
804 command_editor::set_completion_function (completion_fcn f) |
2926
|
805 { |
|
806 if (instance_ok ()) |
2941
|
807 instance->do_set_completion_function (f); |
|
808 } |
|
809 |
|
810 command_editor::completion_fcn |
|
811 command_editor::get_completion_function (void) |
|
812 { |
|
813 return (instance_ok ()) |
|
814 ? instance->do_get_completion_function () : 0; |
2926
|
815 } |
|
816 |
4604
|
817 string_vector |
|
818 command_editor::generate_filename_completions (const std::string& text) |
|
819 { |
|
820 return (instance_ok ()) |
|
821 ? instance->do_generate_filename_completions (text) : string_vector (); |
|
822 } |
|
823 |
2926
|
824 void |
3504
|
825 command_editor::insert_text (const std::string& text) |
2926
|
826 { |
|
827 if (instance_ok ()) |
|
828 instance->do_insert_text (text); |
|
829 } |
|
830 |
|
831 void |
|
832 command_editor::newline (void) |
|
833 { |
|
834 if (instance_ok ()) |
|
835 instance->do_newline (); |
|
836 } |
|
837 |
|
838 void |
|
839 command_editor::clear_undo_list (void) |
|
840 { |
|
841 if (instance_ok ()) |
|
842 instance->do_clear_undo_list (); |
|
843 } |
|
844 |
|
845 void |
6913
|
846 command_editor::add_startup_hook (startup_hook_fcn f) |
2926
|
847 { |
|
848 if (instance_ok ()) |
6913
|
849 { |
|
850 startup_hook_set.insert (f); |
|
851 |
|
852 instance->set_startup_hook (startup_handler); |
|
853 } |
2926
|
854 } |
|
855 |
|
856 void |
6913
|
857 command_editor::remove_startup_hook (startup_hook_fcn f) |
2926
|
858 { |
|
859 if (instance_ok ()) |
6913
|
860 { |
|
861 startup_hook_set_iterator p = startup_hook_set.find (f); |
|
862 |
|
863 if (p != startup_hook_set.end ()) |
6916
|
864 startup_hook_set.erase (p); |
6913
|
865 |
|
866 if (startup_hook_set.empty ()) |
|
867 instance->restore_startup_hook (); |
|
868 } |
2926
|
869 } |
|
870 |
3189
|
871 void |
6913
|
872 command_editor::add_event_hook (event_hook_fcn f) |
3215
|
873 { |
|
874 if (instance_ok ()) |
6913
|
875 { |
|
876 event_hook_set.insert (f); |
|
877 |
|
878 instance->set_event_hook (event_handler); |
|
879 } |
3215
|
880 } |
|
881 |
|
882 void |
6913
|
883 command_editor::remove_event_hook (event_hook_fcn f) |
3215
|
884 { |
|
885 if (instance_ok ()) |
6913
|
886 { |
|
887 event_hook_set_iterator p = event_hook_set.find (f); |
|
888 |
|
889 if (p != event_hook_set.end ()) |
|
890 event_hook_set.erase (p); |
|
891 |
|
892 if (event_hook_set.empty ()) |
|
893 instance->restore_event_hook (); |
|
894 } |
3215
|
895 } |
|
896 |
|
897 void |
5872
|
898 command_editor::read_init_file (const std::string& file_arg) |
3189
|
899 { |
|
900 if (instance_ok ()) |
5872
|
901 { |
|
902 std::string file = file_ops::tilde_expand (file_arg); |
|
903 |
|
904 instance->do_read_init_file (file); |
|
905 } |
3189
|
906 } |
|
907 |
4143
|
908 bool |
|
909 command_editor::filename_completion_desired (bool arg) |
|
910 { |
|
911 return (instance_ok ()) |
|
912 ? instance->do_filename_completion_desired (arg) : false; |
|
913 } |
|
914 |
2926
|
915 // Return a string which will be printed as a prompt. The string may |
|
916 // contain special characters which are decoded as follows: |
|
917 // |
5442
|
918 // \a bell (ascii 07) |
2926
|
919 // \d the date |
5442
|
920 // \e escape (ascii 033) |
|
921 // \h the hostname up to the first `.' |
|
922 // \H the hostname |
2926
|
923 // \n CRLF |
5442
|
924 // \r CR |
2926
|
925 // \s the name of the shell (program) |
5442
|
926 // \t the time |
|
927 // \T the time in 12-hour hh:mm:ss format |
|
928 // \@ the time in 12-hour hh:mm am/pm format |
|
929 // \A the time in 24-hour hh:mm format |
|
930 // \u your username |
2926
|
931 // \w the current working directory |
|
932 // \W the last element of PWD |
5442
|
933 // \! the history number of this command |
2926
|
934 // \# the command number of this command |
|
935 // \$ a $ or a # if you are root |
5442
|
936 // \nnn character code nnn in octal |
2926
|
937 // \\ a backslash |
5442
|
938 // \[ begin a sequence of non-printing chars |
|
939 // \] end a sequence of non-printing chars |
2926
|
940 |
3504
|
941 std::string |
|
942 command_editor::do_decode_prompt_string (const std::string& s) |
2926
|
943 { |
3504
|
944 std::string result; |
|
945 std::string temp; |
2926
|
946 size_t i = 0; |
|
947 size_t slen = s.length (); |
|
948 int c; |
|
949 |
|
950 while (i < slen) |
|
951 { |
|
952 c = s[i]; |
|
953 |
|
954 i++; |
|
955 |
|
956 if (c == '\\') |
|
957 { |
|
958 c = s[i]; |
|
959 |
|
960 switch (c) |
|
961 { |
|
962 case '0': |
|
963 case '1': |
|
964 case '2': |
|
965 case '3': |
|
966 case '4': |
|
967 case '5': |
|
968 case '6': |
|
969 case '7': |
|
970 // Maybe convert an octal number. |
|
971 { |
|
972 int n = read_octal (s.substr (i, 3)); |
|
973 |
|
974 temp = "\\"; |
|
975 |
|
976 if (n != -1) |
|
977 { |
|
978 i += 3; |
|
979 temp[0] = n; |
|
980 } |
|
981 |
|
982 c = 0; |
|
983 goto add_string; |
|
984 } |
5442
|
985 |
|
986 case 'a': |
|
987 { |
|
988 temp = '\a'; |
|
989 |
|
990 goto add_string; |
|
991 } |
|
992 |
|
993 case 'e': |
|
994 { |
|
995 temp = '\033'; |
|
996 |
|
997 goto add_string; |
|
998 } |
|
999 |
|
1000 case 'r': |
|
1001 { |
|
1002 temp = '\r'; |
|
1003 |
|
1004 goto add_string; |
|
1005 } |
|
1006 |
|
1007 case 'd': |
2926
|
1008 case 't': |
5442
|
1009 case 'T': |
|
1010 case '@': |
|
1011 case 'A': |
2926
|
1012 // Make the current time/date into a string. |
|
1013 { |
5442
|
1014 octave_localtime now; |
2926
|
1015 |
5442
|
1016 if (c == 'd') |
|
1017 temp = now.strftime ("%a %b %d"); |
|
1018 else if (c == 't') |
|
1019 temp = now.strftime ("%H:%M:%S"); |
|
1020 else if (c == 'T') |
|
1021 temp = now.strftime ("%I:%M:%S"); |
|
1022 else if (c == '@') |
|
1023 temp = now.strftime ("%I:%M %p"); |
|
1024 else if (c == 'A') |
|
1025 temp = now.strftime ("%H:%M"); |
2926
|
1026 |
|
1027 goto add_string; |
|
1028 } |
|
1029 |
|
1030 case 'n': |
|
1031 { |
|
1032 temp = newline_chars (); |
|
1033 |
|
1034 goto add_string; |
|
1035 } |
|
1036 |
|
1037 case 's': |
|
1038 { |
|
1039 temp = octave_env::get_program_name (); |
|
1040 temp = octave_env::base_pathname (temp); |
|
1041 |
|
1042 goto add_string; |
|
1043 } |
5442
|
1044 |
2926
|
1045 case 'w': |
|
1046 case 'W': |
|
1047 { |
|
1048 temp = octave_env::getcwd (); |
|
1049 |
5442
|
1050 std::string home_dir = octave_env::get_home_directory (); |
|
1051 |
|
1052 if (c == 'W' && (home_dir.empty () || temp != home_dir)) |
2926
|
1053 { |
5442
|
1054 if (temp != "/" && temp != "//") |
|
1055 { |
|
1056 size_t pos = temp.rfind ('/'); |
2926
|
1057 |
5442
|
1058 if (pos != NPOS && pos != 0) |
|
1059 temp = temp.substr (pos + 1); |
|
1060 } |
2926
|
1061 } |
|
1062 else |
|
1063 temp = octave_env::polite_directory_format (temp); |
|
1064 |
|
1065 goto add_string; |
|
1066 } |
5442
|
1067 |
2926
|
1068 case 'u': |
|
1069 { |
|
1070 temp = octave_env::get_user_name (); |
|
1071 |
|
1072 goto add_string; |
|
1073 } |
|
1074 |
|
1075 case 'H': |
|
1076 { |
|
1077 temp = octave_env::get_host_name (); |
|
1078 |
|
1079 goto add_string; |
|
1080 } |
|
1081 |
|
1082 case 'h': |
|
1083 { |
|
1084 temp = octave_env::get_host_name (); |
|
1085 |
|
1086 size_t pos = temp.find ('.'); |
|
1087 |
|
1088 if (pos != NPOS) |
|
1089 temp.resize (pos); |
|
1090 |
|
1091 goto add_string; |
|
1092 } |
|
1093 |
|
1094 case '#': |
|
1095 { |
|
1096 char number_buffer[128]; |
|
1097 sprintf (number_buffer, "%d", command_number); |
|
1098 temp = number_buffer; |
|
1099 |
|
1100 goto add_string; |
|
1101 } |
|
1102 |
|
1103 case '!': |
|
1104 { |
|
1105 char number_buffer[128]; |
|
1106 int num = command_history::current_number (); |
|
1107 if (num > 0) |
|
1108 sprintf (number_buffer, "%d", num); |
|
1109 else |
|
1110 strcpy (number_buffer, "!"); |
|
1111 temp = number_buffer; |
|
1112 |
|
1113 goto add_string; |
|
1114 } |
|
1115 |
|
1116 case '$': |
|
1117 { |
4062
|
1118 #if defined (HAVE_GETEUID) |
2926
|
1119 temp = (::geteuid () == 0 ? "#" : "$"); |
4062
|
1120 #else |
|
1121 temp = "$"; |
|
1122 #endif |
2926
|
1123 |
|
1124 goto add_string; |
|
1125 } |
|
1126 |
|
1127 #if defined (USE_READLINE) |
|
1128 case '[': |
|
1129 case ']': |
|
1130 { |
6940
|
1131 temp.resize (1); |
2926
|
1132 |
6940
|
1133 temp[0] = ((c == '[') |
3519
|
1134 ? ::octave_rl_prompt_start_ignore () |
|
1135 : ::octave_rl_prompt_end_ignore ()); |
2926
|
1136 |
|
1137 goto add_string; |
|
1138 } |
|
1139 #endif |
|
1140 |
|
1141 case '\\': |
|
1142 { |
|
1143 temp = "\\"; |
|
1144 |
|
1145 goto add_string; |
|
1146 } |
|
1147 |
|
1148 default: |
|
1149 { |
|
1150 temp = "\\ "; |
|
1151 temp[1] = c; |
|
1152 |
|
1153 goto add_string; |
|
1154 } |
|
1155 |
|
1156 add_string: |
|
1157 { |
|
1158 if (c) |
|
1159 i++; |
|
1160 |
|
1161 result.append (temp); |
|
1162 |
|
1163 break; |
|
1164 } |
|
1165 } |
|
1166 } |
|
1167 else |
|
1168 result += c; |
|
1169 } |
|
1170 |
|
1171 return result; |
|
1172 } |
|
1173 |
|
1174 // Return the octal number parsed from STRING, or -1 to indicate that |
|
1175 // the string contained a bad number. |
|
1176 |
|
1177 int |
3504
|
1178 command_editor::read_octal (const std::string& s) |
2926
|
1179 { |
|
1180 int result = 0; |
|
1181 int digits = 0; |
|
1182 |
|
1183 size_t i = 0; |
|
1184 size_t slen = s.length (); |
|
1185 |
|
1186 while (i < slen && s[i] >= '0' && s[i] < '8') |
|
1187 { |
|
1188 digits++; |
|
1189 result = (result * 8) + s[i] - '0'; |
|
1190 i++; |
|
1191 } |
|
1192 |
|
1193 if (! digits || result > 0777 || i < slen) |
|
1194 result = -1; |
|
1195 |
|
1196 return result; |
|
1197 } |
|
1198 |
|
1199 void |
|
1200 command_editor::error (int err_num) |
|
1201 { |
2941
|
1202 current_liboctave_error_handler ("%s", strerror (err_num)); |
2926
|
1203 } |
|
1204 |
|
1205 void |
3504
|
1206 command_editor::error (const std::string& s) |
2926
|
1207 { |
2941
|
1208 current_liboctave_error_handler ("%s", s.c_str ()); |
2926
|
1209 } |
|
1210 |
|
1211 /* |
|
1212 ;;; Local Variables: *** |
|
1213 ;;; mode: C++ *** |
|
1214 ;;; End: *** |
|
1215 */ |