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