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 { |
3219
|
484 eof = false; |
|
485 |
2926
|
486 fprintf (output_stream, prompt.c_str ()); |
|
487 fflush (output_stream); |
|
488 |
3970
|
489 return octave_fgetl (input_stream); |
2926
|
490 } |
|
491 |
|
492 void |
|
493 default_command_editor::do_set_input_stream (FILE *f) |
|
494 { |
|
495 input_stream = f; |
|
496 } |
|
497 |
|
498 FILE * |
|
499 default_command_editor::do_get_input_stream (void) |
|
500 { |
|
501 return input_stream; |
|
502 } |
|
503 |
|
504 void |
|
505 default_command_editor::do_set_output_stream (FILE *f) |
|
506 { |
|
507 output_stream = f; |
|
508 } |
|
509 |
|
510 FILE * |
|
511 default_command_editor::do_get_output_stream (void) |
|
512 { |
|
513 return output_stream; |
|
514 } |
|
515 |
|
516 void |
3504
|
517 default_command_editor::do_insert_text (const std::string&) |
2926
|
518 { |
|
519 // XXX FIXME XXX |
|
520 } |
|
521 |
|
522 void |
|
523 default_command_editor::do_newline (void) |
|
524 { |
|
525 // XXX FIXME XXX |
|
526 } |
|
527 |
|
528 bool |
|
529 command_editor::instance_ok (void) |
|
530 { |
|
531 bool retval = true; |
|
532 |
|
533 if (! instance) |
|
534 make_command_editor (); |
|
535 |
|
536 if (! instance) |
|
537 { |
2941
|
538 current_liboctave_error_handler |
2926
|
539 ("unable to create command history object!"); |
|
540 |
|
541 retval = false; |
|
542 } |
|
543 |
|
544 return retval; |
|
545 } |
|
546 |
|
547 void |
|
548 command_editor::make_command_editor (void) |
|
549 { |
|
550 #if defined (USE_READLINE) |
|
551 instance = new gnu_readline (); |
|
552 #else |
|
553 instance = new default_command_editor (); |
|
554 #endif |
|
555 } |
|
556 |
|
557 void |
3504
|
558 command_editor::set_name (const std::string& n) |
2926
|
559 { |
|
560 if (instance_ok ()) |
|
561 instance->do_set_name (n); |
|
562 } |
|
563 |
3504
|
564 std::string |
|
565 command_editor::readline (const std::string& prompt) |
2926
|
566 { |
3219
|
567 bool eof; |
|
568 |
|
569 return readline (prompt, eof); |
|
570 } |
|
571 |
3504
|
572 std::string |
|
573 command_editor::readline (const std::string& prompt, bool& eof) |
3219
|
574 { |
2926
|
575 return (instance_ok ()) |
3504
|
576 ? instance->do_readline (prompt, eof) : std::string (); |
2926
|
577 } |
|
578 |
|
579 void |
|
580 command_editor::set_input_stream (FILE *f) |
|
581 { |
|
582 if (instance_ok ()) |
|
583 instance->do_set_input_stream (f); |
|
584 } |
|
585 |
|
586 FILE * |
|
587 command_editor::get_input_stream (void) |
|
588 { |
|
589 return (instance_ok ()) |
|
590 ? instance->do_get_input_stream () : 0; |
|
591 } |
|
592 |
|
593 void |
|
594 command_editor::set_output_stream (FILE *f) |
|
595 { |
|
596 if (instance_ok ()) |
|
597 instance->do_set_output_stream (f); |
|
598 } |
|
599 |
|
600 FILE * |
|
601 command_editor::get_output_stream (void) |
|
602 { |
|
603 return (instance_ok ()) |
|
604 ? instance->do_get_output_stream () : 0; |
|
605 } |
|
606 |
|
607 int |
|
608 command_editor::terminal_rows (void) |
|
609 { |
|
610 return (instance_ok ()) |
|
611 ? instance->do_terminal_rows () : -1; |
|
612 } |
|
613 |
|
614 int |
|
615 command_editor::terminal_cols (void) |
|
616 { |
|
617 return (instance_ok ()) |
|
618 ? instance->do_terminal_cols () : -1; |
|
619 } |
|
620 |
|
621 void |
|
622 command_editor::clear_screen (void) |
|
623 { |
|
624 if (instance_ok ()) |
|
625 instance->do_clear_screen (); |
|
626 } |
|
627 |
3281
|
628 void |
|
629 command_editor::resize_terminal (void) |
|
630 { |
|
631 if (instance_ok ()) |
|
632 instance->do_resize_terminal (); |
|
633 } |
|
634 |
3504
|
635 std::string |
|
636 command_editor::decode_prompt_string (const std::string& s) |
2926
|
637 { |
|
638 return (instance_ok ()) |
3504
|
639 ? instance->do_decode_prompt_string (s) : std::string (); |
2926
|
640 } |
|
641 |
|
642 int |
|
643 command_editor::current_command_number (void) |
|
644 { |
|
645 return (instance_ok ()) |
|
646 ? instance->command_number : 0; |
|
647 } |
|
648 |
|
649 void |
|
650 command_editor::reset_current_command_number (int n) |
|
651 { |
|
652 if (instance_ok ()) |
|
653 instance->command_number = n; |
|
654 } |
|
655 |
|
656 void |
2967
|
657 command_editor::increment_current_command_number (void) |
|
658 { |
|
659 if (instance_ok ()) |
|
660 instance->command_number++; |
|
661 } |
|
662 |
|
663 void |
2926
|
664 command_editor::restore_terminal_state (void) |
|
665 { |
|
666 if (instance_ok ()) |
|
667 instance->do_restore_terminal_state (); |
|
668 } |
|
669 |
|
670 void |
|
671 command_editor::blink_matching_paren (bool flag) |
|
672 { |
|
673 if (instance_ok ()) |
|
674 instance->do_blink_matching_paren (flag); |
|
675 } |
|
676 |
|
677 void |
3933
|
678 command_editor::set_basic_word_break_characters (const std::string& s) |
|
679 { |
|
680 if (instance_ok ()) |
|
681 instance->do_set_basic_word_break_characters (s); |
|
682 } |
|
683 |
|
684 void |
|
685 command_editor::set_completer_word_break_characters (const std::string& s) |
|
686 { |
|
687 if (instance_ok ()) |
|
688 instance->do_set_completer_word_break_characters (s); |
|
689 } |
|
690 |
|
691 void |
3504
|
692 command_editor::set_basic_quote_characters (const std::string& s) |
2926
|
693 { |
|
694 if (instance_ok ()) |
3004
|
695 instance->do_set_basic_quote_characters (s); |
2926
|
696 } |
|
697 |
|
698 void |
|
699 command_editor::set_completion_append_character (char c) |
|
700 { |
|
701 if (instance_ok ()) |
|
702 instance->do_set_completion_append_character (c); |
|
703 } |
|
704 |
|
705 void |
2941
|
706 command_editor::set_completion_function (completion_fcn f) |
2926
|
707 { |
|
708 if (instance_ok ()) |
2941
|
709 instance->do_set_completion_function (f); |
|
710 } |
|
711 |
|
712 command_editor::completion_fcn |
|
713 command_editor::get_completion_function (void) |
|
714 { |
|
715 return (instance_ok ()) |
|
716 ? instance->do_get_completion_function () : 0; |
2926
|
717 } |
|
718 |
|
719 void |
3504
|
720 command_editor::insert_text (const std::string& text) |
2926
|
721 { |
|
722 if (instance_ok ()) |
|
723 instance->do_insert_text (text); |
|
724 } |
|
725 |
|
726 void |
|
727 command_editor::newline (void) |
|
728 { |
|
729 if (instance_ok ()) |
|
730 instance->do_newline (); |
|
731 } |
|
732 |
|
733 void |
|
734 command_editor::clear_undo_list (void) |
|
735 { |
|
736 if (instance_ok ()) |
|
737 instance->do_clear_undo_list (); |
|
738 } |
|
739 |
|
740 void |
3519
|
741 command_editor::set_startup_hook (startup_hook_fcn f) |
2926
|
742 { |
|
743 if (instance_ok ()) |
|
744 instance->do_set_startup_hook (f); |
|
745 } |
|
746 |
|
747 void |
|
748 command_editor::restore_startup_hook (void) |
|
749 { |
|
750 if (instance_ok ()) |
|
751 instance->do_restore_startup_hook (); |
|
752 } |
|
753 |
3189
|
754 void |
3519
|
755 command_editor::set_event_hook (event_hook_fcn f) |
3215
|
756 { |
|
757 if (instance_ok ()) |
|
758 instance->do_set_event_hook (f); |
|
759 } |
|
760 |
|
761 void |
|
762 command_editor::restore_event_hook (void) |
|
763 { |
|
764 if (instance_ok ()) |
|
765 instance->do_restore_event_hook (); |
|
766 } |
|
767 |
|
768 void |
3504
|
769 command_editor::read_init_file (const std::string& file) |
3189
|
770 { |
|
771 if (instance_ok ()) |
|
772 instance->do_read_init_file (file); |
|
773 } |
|
774 |
4143
|
775 bool |
|
776 command_editor::filename_completion_desired (bool arg) |
|
777 { |
|
778 return (instance_ok ()) |
|
779 ? instance->do_filename_completion_desired (arg) : false; |
|
780 } |
|
781 |
2926
|
782 // Return a string which will be printed as a prompt. The string may |
|
783 // contain special characters which are decoded as follows: |
|
784 // |
|
785 // \t the time |
|
786 // \d the date |
|
787 // \n CRLF |
|
788 // \s the name of the shell (program) |
|
789 // \w the current working directory |
|
790 // \W the last element of PWD |
|
791 // \u your username |
|
792 // \h the hostname |
|
793 // \# the command number of this command |
|
794 // \! the history number of this command |
|
795 // \$ a $ or a # if you are root |
|
796 // \<octal> character code in octal |
|
797 // \\ a backslash |
|
798 |
3504
|
799 std::string |
|
800 command_editor::do_decode_prompt_string (const std::string& s) |
2926
|
801 { |
3504
|
802 std::string result; |
|
803 std::string temp; |
2926
|
804 size_t i = 0; |
|
805 size_t slen = s.length (); |
|
806 int c; |
|
807 |
|
808 while (i < slen) |
|
809 { |
|
810 c = s[i]; |
|
811 |
|
812 i++; |
|
813 |
|
814 if (c == '\\') |
|
815 { |
|
816 c = s[i]; |
|
817 |
|
818 switch (c) |
|
819 { |
|
820 case '0': |
|
821 case '1': |
|
822 case '2': |
|
823 case '3': |
|
824 case '4': |
|
825 case '5': |
|
826 case '6': |
|
827 case '7': |
|
828 // Maybe convert an octal number. |
|
829 { |
|
830 int n = read_octal (s.substr (i, 3)); |
|
831 |
|
832 temp = "\\"; |
|
833 |
|
834 if (n != -1) |
|
835 { |
|
836 i += 3; |
|
837 temp[0] = n; |
|
838 } |
|
839 |
|
840 c = 0; |
|
841 goto add_string; |
|
842 } |
|
843 |
|
844 case 't': |
|
845 case 'd': |
|
846 // Make the current time/date into a string. |
|
847 { |
3260
|
848 octave_time now; |
2926
|
849 |
3260
|
850 temp = now.ctime (); |
2926
|
851 |
|
852 if (c == 't') |
|
853 { |
|
854 temp = temp.substr (11); |
|
855 temp.resize (8); |
|
856 } |
|
857 else |
|
858 temp.resize (10); |
|
859 |
|
860 goto add_string; |
|
861 } |
|
862 |
|
863 case 'n': |
|
864 { |
|
865 temp = newline_chars (); |
|
866 |
|
867 goto add_string; |
|
868 } |
|
869 |
|
870 case 's': |
|
871 { |
|
872 temp = octave_env::get_program_name (); |
|
873 temp = octave_env::base_pathname (temp); |
|
874 |
|
875 goto add_string; |
|
876 } |
|
877 |
|
878 case 'w': |
|
879 case 'W': |
|
880 { |
|
881 temp = octave_env::getcwd (); |
|
882 |
|
883 if (c == 'W') |
|
884 { |
|
885 size_t pos = temp.rfind ('/'); |
|
886 |
|
887 if (pos != NPOS && pos != 0) |
|
888 temp = temp.substr (pos + 1); |
|
889 } |
|
890 else |
|
891 temp = octave_env::polite_directory_format (temp); |
|
892 |
|
893 goto add_string; |
|
894 } |
|
895 |
|
896 case 'u': |
|
897 { |
|
898 temp = octave_env::get_user_name (); |
|
899 |
|
900 goto add_string; |
|
901 } |
|
902 |
|
903 case 'H': |
|
904 { |
|
905 temp = octave_env::get_host_name (); |
|
906 |
|
907 goto add_string; |
|
908 } |
|
909 |
|
910 case 'h': |
|
911 { |
|
912 temp = octave_env::get_host_name (); |
|
913 |
|
914 size_t pos = temp.find ('.'); |
|
915 |
|
916 if (pos != NPOS) |
|
917 temp.resize (pos); |
|
918 |
|
919 goto add_string; |
|
920 } |
|
921 |
|
922 case '#': |
|
923 { |
|
924 char number_buffer[128]; |
|
925 sprintf (number_buffer, "%d", command_number); |
|
926 temp = number_buffer; |
|
927 |
|
928 goto add_string; |
|
929 } |
|
930 |
|
931 case '!': |
|
932 { |
|
933 char number_buffer[128]; |
|
934 int num = command_history::current_number (); |
|
935 if (num > 0) |
|
936 sprintf (number_buffer, "%d", num); |
|
937 else |
|
938 strcpy (number_buffer, "!"); |
|
939 temp = number_buffer; |
|
940 |
|
941 goto add_string; |
|
942 } |
|
943 |
|
944 case '$': |
|
945 { |
4062
|
946 #if defined (HAVE_GETEUID) |
2926
|
947 temp = (::geteuid () == 0 ? "#" : "$"); |
4062
|
948 #else |
|
949 temp = "$"; |
|
950 #endif |
2926
|
951 |
|
952 goto add_string; |
|
953 } |
|
954 |
|
955 #if defined (USE_READLINE) |
|
956 case '[': |
|
957 case ']': |
|
958 { |
|
959 temp.resize (2); |
|
960 |
|
961 temp[0] = '\001'; |
|
962 temp[1] = ((c == '[') |
3519
|
963 ? ::octave_rl_prompt_start_ignore () |
|
964 : ::octave_rl_prompt_end_ignore ()); |
2926
|
965 |
|
966 goto add_string; |
|
967 } |
|
968 #endif |
|
969 |
|
970 case '\\': |
|
971 { |
|
972 temp = "\\"; |
|
973 |
|
974 goto add_string; |
|
975 } |
|
976 |
|
977 default: |
|
978 { |
|
979 temp = "\\ "; |
|
980 temp[1] = c; |
|
981 |
|
982 goto add_string; |
|
983 } |
|
984 |
|
985 add_string: |
|
986 { |
|
987 if (c) |
|
988 i++; |
|
989 |
|
990 result.append (temp); |
|
991 |
|
992 break; |
|
993 } |
|
994 } |
|
995 } |
|
996 else |
|
997 result += c; |
|
998 } |
|
999 |
|
1000 return result; |
|
1001 } |
|
1002 |
|
1003 // Return the octal number parsed from STRING, or -1 to indicate that |
|
1004 // the string contained a bad number. |
|
1005 |
|
1006 int |
3504
|
1007 command_editor::read_octal (const std::string& s) |
2926
|
1008 { |
|
1009 int result = 0; |
|
1010 int digits = 0; |
|
1011 |
|
1012 size_t i = 0; |
|
1013 size_t slen = s.length (); |
|
1014 |
|
1015 while (i < slen && s[i] >= '0' && s[i] < '8') |
|
1016 { |
|
1017 digits++; |
|
1018 result = (result * 8) + s[i] - '0'; |
|
1019 i++; |
|
1020 } |
|
1021 |
|
1022 if (! digits || result > 0777 || i < slen) |
|
1023 result = -1; |
|
1024 |
|
1025 return result; |
|
1026 } |
|
1027 |
|
1028 void |
|
1029 command_editor::error (int err_num) |
|
1030 { |
2941
|
1031 current_liboctave_error_handler ("%s", strerror (err_num)); |
2926
|
1032 } |
|
1033 |
|
1034 void |
3504
|
1035 command_editor::error (const std::string& s) |
2926
|
1036 { |
2941
|
1037 current_liboctave_error_handler ("%s", s.c_str ()); |
2926
|
1038 } |
|
1039 |
|
1040 /* |
|
1041 ;;; Local Variables: *** |
|
1042 ;;; mode: C++ *** |
|
1043 ;;; End: *** |
|
1044 */ |