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 { |
5775
|
155 // FIXME -- need interface to rl_add_defun, rl_initialize, and |
3519
|
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 |
5452
|
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 |
5452
|
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 |
5452
|
202 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5247
|
203 |
5336
|
204 line = ::octave_rl_readline (p); |
5247
|
205 |
5452
|
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 { |
5775
|
572 // FIXME |
4604
|
573 return string_vector (); |
|
574 } |
|
575 |
2926
|
576 void |
3504
|
577 default_command_editor::do_insert_text (const std::string&) |
2926
|
578 { |
5775
|
579 // FIXME |
2926
|
580 } |
|
581 |
|
582 void |
|
583 default_command_editor::do_newline (void) |
|
584 { |
5775
|
585 // FIXME |
2926
|
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 // |
5442
|
852 // \a bell (ascii 07) |
2926
|
853 // \d the date |
5442
|
854 // \e escape (ascii 033) |
|
855 // \h the hostname up to the first `.' |
|
856 // \H the hostname |
2926
|
857 // \n CRLF |
5442
|
858 // \r CR |
2926
|
859 // \s the name of the shell (program) |
5442
|
860 // \t the time |
|
861 // \T the time in 12-hour hh:mm:ss format |
|
862 // \@ the time in 12-hour hh:mm am/pm format |
|
863 // \A the time in 24-hour hh:mm format |
|
864 // \u your username |
2926
|
865 // \w the current working directory |
|
866 // \W the last element of PWD |
5442
|
867 // \! the history number of this command |
2926
|
868 // \# the command number of this command |
|
869 // \$ a $ or a # if you are root |
5442
|
870 // \nnn character code nnn in octal |
2926
|
871 // \\ a backslash |
5442
|
872 // \[ begin a sequence of non-printing chars |
|
873 // \] end a sequence of non-printing chars |
2926
|
874 |
3504
|
875 std::string |
|
876 command_editor::do_decode_prompt_string (const std::string& s) |
2926
|
877 { |
3504
|
878 std::string result; |
|
879 std::string temp; |
2926
|
880 size_t i = 0; |
|
881 size_t slen = s.length (); |
|
882 int c; |
|
883 |
|
884 while (i < slen) |
|
885 { |
|
886 c = s[i]; |
|
887 |
|
888 i++; |
|
889 |
|
890 if (c == '\\') |
|
891 { |
|
892 c = s[i]; |
|
893 |
|
894 switch (c) |
|
895 { |
|
896 case '0': |
|
897 case '1': |
|
898 case '2': |
|
899 case '3': |
|
900 case '4': |
|
901 case '5': |
|
902 case '6': |
|
903 case '7': |
|
904 // Maybe convert an octal number. |
|
905 { |
|
906 int n = read_octal (s.substr (i, 3)); |
|
907 |
|
908 temp = "\\"; |
|
909 |
|
910 if (n != -1) |
|
911 { |
|
912 i += 3; |
|
913 temp[0] = n; |
|
914 } |
|
915 |
|
916 c = 0; |
|
917 goto add_string; |
|
918 } |
5442
|
919 |
|
920 case 'a': |
|
921 { |
|
922 temp = '\a'; |
|
923 |
|
924 goto add_string; |
|
925 } |
|
926 |
|
927 case 'e': |
|
928 { |
|
929 temp = '\033'; |
|
930 |
|
931 goto add_string; |
|
932 } |
|
933 |
|
934 case 'r': |
|
935 { |
|
936 temp = '\r'; |
|
937 |
|
938 goto add_string; |
|
939 } |
|
940 |
|
941 case 'd': |
2926
|
942 case 't': |
5442
|
943 case 'T': |
|
944 case '@': |
|
945 case 'A': |
2926
|
946 // Make the current time/date into a string. |
|
947 { |
5442
|
948 octave_localtime now; |
2926
|
949 |
5442
|
950 if (c == 'd') |
|
951 temp = now.strftime ("%a %b %d"); |
|
952 else if (c == 't') |
|
953 temp = now.strftime ("%H:%M:%S"); |
|
954 else if (c == 'T') |
|
955 temp = now.strftime ("%I:%M:%S"); |
|
956 else if (c == '@') |
|
957 temp = now.strftime ("%I:%M %p"); |
|
958 else if (c == 'A') |
|
959 temp = now.strftime ("%H:%M"); |
2926
|
960 |
|
961 goto add_string; |
|
962 } |
|
963 |
|
964 case 'n': |
|
965 { |
|
966 temp = newline_chars (); |
|
967 |
|
968 goto add_string; |
|
969 } |
|
970 |
|
971 case 's': |
|
972 { |
|
973 temp = octave_env::get_program_name (); |
|
974 temp = octave_env::base_pathname (temp); |
|
975 |
|
976 goto add_string; |
|
977 } |
5442
|
978 |
2926
|
979 case 'w': |
|
980 case 'W': |
|
981 { |
|
982 temp = octave_env::getcwd (); |
|
983 |
5442
|
984 std::string home_dir = octave_env::get_home_directory (); |
|
985 |
|
986 if (c == 'W' && (home_dir.empty () || temp != home_dir)) |
2926
|
987 { |
5442
|
988 if (temp != "/" && temp != "//") |
|
989 { |
|
990 size_t pos = temp.rfind ('/'); |
2926
|
991 |
5442
|
992 if (pos != NPOS && pos != 0) |
|
993 temp = temp.substr (pos + 1); |
|
994 } |
2926
|
995 } |
|
996 else |
|
997 temp = octave_env::polite_directory_format (temp); |
|
998 |
|
999 goto add_string; |
|
1000 } |
5442
|
1001 |
2926
|
1002 case 'u': |
|
1003 { |
|
1004 temp = octave_env::get_user_name (); |
|
1005 |
|
1006 goto add_string; |
|
1007 } |
|
1008 |
|
1009 case 'H': |
|
1010 { |
|
1011 temp = octave_env::get_host_name (); |
|
1012 |
|
1013 goto add_string; |
|
1014 } |
|
1015 |
|
1016 case 'h': |
|
1017 { |
|
1018 temp = octave_env::get_host_name (); |
|
1019 |
|
1020 size_t pos = temp.find ('.'); |
|
1021 |
|
1022 if (pos != NPOS) |
|
1023 temp.resize (pos); |
|
1024 |
|
1025 goto add_string; |
|
1026 } |
|
1027 |
|
1028 case '#': |
|
1029 { |
|
1030 char number_buffer[128]; |
|
1031 sprintf (number_buffer, "%d", command_number); |
|
1032 temp = number_buffer; |
|
1033 |
|
1034 goto add_string; |
|
1035 } |
|
1036 |
|
1037 case '!': |
|
1038 { |
|
1039 char number_buffer[128]; |
|
1040 int num = command_history::current_number (); |
|
1041 if (num > 0) |
|
1042 sprintf (number_buffer, "%d", num); |
|
1043 else |
|
1044 strcpy (number_buffer, "!"); |
|
1045 temp = number_buffer; |
|
1046 |
|
1047 goto add_string; |
|
1048 } |
|
1049 |
|
1050 case '$': |
|
1051 { |
4062
|
1052 #if defined (HAVE_GETEUID) |
2926
|
1053 temp = (::geteuid () == 0 ? "#" : "$"); |
4062
|
1054 #else |
|
1055 temp = "$"; |
|
1056 #endif |
2926
|
1057 |
|
1058 goto add_string; |
|
1059 } |
|
1060 |
|
1061 #if defined (USE_READLINE) |
|
1062 case '[': |
|
1063 case ']': |
|
1064 { |
|
1065 temp.resize (2); |
|
1066 |
|
1067 temp[0] = '\001'; |
|
1068 temp[1] = ((c == '[') |
3519
|
1069 ? ::octave_rl_prompt_start_ignore () |
|
1070 : ::octave_rl_prompt_end_ignore ()); |
2926
|
1071 |
|
1072 goto add_string; |
|
1073 } |
|
1074 #endif |
|
1075 |
|
1076 case '\\': |
|
1077 { |
|
1078 temp = "\\"; |
|
1079 |
|
1080 goto add_string; |
|
1081 } |
|
1082 |
|
1083 default: |
|
1084 { |
|
1085 temp = "\\ "; |
|
1086 temp[1] = c; |
|
1087 |
|
1088 goto add_string; |
|
1089 } |
|
1090 |
|
1091 add_string: |
|
1092 { |
|
1093 if (c) |
|
1094 i++; |
|
1095 |
|
1096 result.append (temp); |
|
1097 |
|
1098 break; |
|
1099 } |
|
1100 } |
|
1101 } |
|
1102 else |
|
1103 result += c; |
|
1104 } |
|
1105 |
|
1106 return result; |
|
1107 } |
|
1108 |
|
1109 // Return the octal number parsed from STRING, or -1 to indicate that |
|
1110 // the string contained a bad number. |
|
1111 |
|
1112 int |
3504
|
1113 command_editor::read_octal (const std::string& s) |
2926
|
1114 { |
|
1115 int result = 0; |
|
1116 int digits = 0; |
|
1117 |
|
1118 size_t i = 0; |
|
1119 size_t slen = s.length (); |
|
1120 |
|
1121 while (i < slen && s[i] >= '0' && s[i] < '8') |
|
1122 { |
|
1123 digits++; |
|
1124 result = (result * 8) + s[i] - '0'; |
|
1125 i++; |
|
1126 } |
|
1127 |
|
1128 if (! digits || result > 0777 || i < slen) |
|
1129 result = -1; |
|
1130 |
|
1131 return result; |
|
1132 } |
|
1133 |
|
1134 void |
|
1135 command_editor::error (int err_num) |
|
1136 { |
2941
|
1137 current_liboctave_error_handler ("%s", strerror (err_num)); |
2926
|
1138 } |
|
1139 |
|
1140 void |
3504
|
1141 command_editor::error (const std::string& s) |
2926
|
1142 { |
2941
|
1143 current_liboctave_error_handler ("%s", s.c_str ()); |
2926
|
1144 } |
|
1145 |
|
1146 /* |
|
1147 ;;; Local Variables: *** |
|
1148 ;;; mode: C++ *** |
|
1149 ;;; End: *** |
|
1150 */ |