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