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