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