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