1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
2939
|
23 // Get command input interactively or from files. |
1
|
24 |
240
|
25 #ifdef HAVE_CONFIG_H |
1192
|
26 #include <config.h> |
1
|
27 #endif |
|
28 |
1343
|
29 #include <ctime> |
|
30 #include <cstdio> |
|
31 #include <cstdlib> |
|
32 #include <cstring> |
|
33 #include <cassert> |
|
34 #include <csignal> |
|
35 |
1728
|
36 #include <string> |
|
37 |
1349
|
38 #include <iostream.h> |
|
39 |
1350
|
40 #ifdef HAVE_UNISTD_H |
2442
|
41 #ifdef HAVE_SYS_TYPES_H |
529
|
42 #include <sys/types.h> |
2442
|
43 #endif |
529
|
44 #include <unistd.h> |
|
45 #endif |
1343
|
46 |
2927
|
47 #include "cmd-edit.h" |
1755
|
48 #include "str-vec.h" |
|
49 |
1352
|
50 #include "defun.h" |
|
51 #include "dirfns.h" |
1
|
52 #include "error.h" |
2181
|
53 #include "gripes.h" |
3016
|
54 #include "help.h" |
1
|
55 #include "input.h" |
1352
|
56 #include "oct-map.h" |
1742
|
57 #include "oct-hist.h" |
1670
|
58 #include "toplev.h" |
1755
|
59 #include "oct-obj.h" |
1
|
60 #include "pager.h" |
529
|
61 #include "parse.h" |
1352
|
62 #include "pathlen.h" |
1755
|
63 #include "pt-const.h" |
1352
|
64 #include "sighandlers.h" |
|
65 #include "symtab.h" |
1114
|
66 #include "sysdep.h" |
3098
|
67 #include "unwind-prot.h" |
1352
|
68 #include "utils.h" |
|
69 #include "variables.h" |
529
|
70 |
2181
|
71 // Primary prompt string. |
|
72 static string Vps1; |
|
73 |
|
74 // Secondary prompt string. |
|
75 static string Vps2; |
|
76 |
|
77 // String printed before echoed input (enabled by --echo-input). |
|
78 string Vps4; |
|
79 |
3019
|
80 // Echo commands as they are executed? |
|
81 // |
|
82 // 1 ==> echo commands read from script files |
|
83 // 2 ==> echo commands from functions |
|
84 // 4 ==> echo commands read from command line |
|
85 // |
|
86 // more than one state can be active at once. |
|
87 int Vecho_executing_commands; |
|
88 |
2181
|
89 // Character to append after successful command-line completion attempts. |
|
90 static char Vcompletion_append_char; |
|
91 |
1
|
92 // Global pointer for eval(). |
1755
|
93 string current_eval_string; |
1
|
94 |
3019
|
95 // TRUE means get input from current_eval_string. |
|
96 bool get_input_from_eval_string = false; |
1
|
97 |
3019
|
98 // TRUE means we're parsing a function file. |
|
99 bool reading_fcn_file = false; |
1
|
100 |
338
|
101 // Simple name of function file we are reading. |
1755
|
102 string curr_fcn_file_name; |
1606
|
103 |
|
104 // Full name of file we are reading. |
1755
|
105 string curr_fcn_file_full_name; |
1
|
106 |
3019
|
107 // TRUE means we're parsing a script file. |
|
108 bool reading_script_file = false; |
1
|
109 |
|
110 // If we are reading from an M-file, this is it. |
529
|
111 FILE *ff_instream = 0; |
1
|
112 |
3019
|
113 // TRUE means this is an interactive shell. |
|
114 bool interactive = false; |
1
|
115 |
3019
|
116 // TRUE means the user forced this shell to be interactive (-i). |
|
117 bool forced_interactive = false; |
1
|
118 |
|
119 // Should we issue a prompt? |
|
120 int promptflag = 1; |
|
121 |
|
122 // The current line of input, from wherever. |
1755
|
123 string current_input_line; |
1
|
124 |
2299
|
125 // TRUE after a call to completion_matches(). |
|
126 bool octave_completion_matches_called = false; |
|
127 |
1044
|
128 static void |
1755
|
129 do_input_echo (const string& input_string) |
1044
|
130 { |
1588
|
131 int do_echo = reading_script_file ? |
2205
|
132 (Vecho_executing_commands & ECHO_SCRIPTS) |
2618
|
133 : (Vecho_executing_commands & ECHO_CMD_LINE) && ! forced_interactive; |
1588
|
134 |
|
135 if (do_echo) |
1044
|
136 { |
1403
|
137 if (forced_interactive) |
|
138 { |
1755
|
139 if (promptflag > 0) |
2927
|
140 octave_stdout << command_editor::decode_prompt_string (Vps1); |
1755
|
141 else |
2927
|
142 octave_stdout << command_editor::decode_prompt_string (Vps2); |
1403
|
143 } |
|
144 else |
2927
|
145 octave_stdout << command_editor::decode_prompt_string (Vps4); |
1044
|
146 |
1755
|
147 if (! input_string.empty ()) |
1044
|
148 { |
2095
|
149 octave_stdout << input_string; |
1755
|
150 |
|
151 if (input_string[input_string.length () - 1] != '\n') |
2095
|
152 octave_stdout << "\n"; |
1044
|
153 } |
|
154 } |
|
155 } |
|
156 |
2927
|
157 string |
|
158 gnu_readline (const string& s, bool force_readline) |
1822
|
159 { |
2927
|
160 string retval; |
1822
|
161 |
2927
|
162 if (line_editing || force_readline) |
1822
|
163 { |
2927
|
164 retval = command_editor::readline (s); |
1898
|
165 |
2927
|
166 if (retval.empty ()) |
|
167 retval = "\n"; |
1822
|
168 } |
|
169 else |
|
170 { |
2927
|
171 if (! s.empty () && (interactive || forced_interactive)) |
2618
|
172 { |
2927
|
173 FILE *stream = command_editor::get_output_stream (); |
|
174 |
|
175 fprintf (stream, s.c_str ()); |
|
176 fflush (stream); |
2618
|
177 } |
1822
|
178 |
2927
|
179 FILE *curr_stream = command_editor::get_input_stream (); |
|
180 |
1822
|
181 if (reading_fcn_file || reading_script_file) |
|
182 curr_stream = ff_instream; |
|
183 |
2927
|
184 retval = octave_fgets (curr_stream); |
1822
|
185 } |
|
186 |
|
187 return retval; |
|
188 } |
581
|
189 |
2927
|
190 static string |
1
|
191 octave_gets (void) |
|
192 { |
2927
|
193 string retval; |
1
|
194 |
1822
|
195 if ((interactive || forced_interactive) |
|
196 && (! (reading_fcn_file || reading_script_file))) |
1
|
197 { |
2927
|
198 string ps = (promptflag > 0) ? Vps1 : Vps2; |
1755
|
199 |
2927
|
200 string prompt = command_editor::decode_prompt_string (ps); |
1
|
201 |
2618
|
202 pipe_handler_error_count = 0; |
|
203 |
|
204 flush_octave_stdout (); |
1
|
205 |
2095
|
206 octave_diary << prompt; |
581
|
207 |
2927
|
208 retval = gnu_readline (prompt); |
1
|
209 } |
|
210 else |
1822
|
211 retval = gnu_readline (""); |
1
|
212 |
2927
|
213 current_input_line = retval; |
1
|
214 |
1822
|
215 if (! current_input_line.empty ()) |
1
|
216 { |
1799
|
217 if (! input_from_startup_file) |
2927
|
218 command_history::add (current_input_line); |
1
|
219 |
2095
|
220 octave_diary << current_input_line; |
581
|
221 |
1822
|
222 do_input_echo (current_input_line); |
1
|
223 } |
581
|
224 |
2095
|
225 octave_diary << "\n"; |
581
|
226 |
1822
|
227 return retval; |
1
|
228 } |
|
229 |
581
|
230 // Read a line from the input stream. |
|
231 |
2927
|
232 static string |
1822
|
233 get_user_input (void) |
1
|
234 { |
2927
|
235 string retval; |
1
|
236 |
|
237 if (get_input_from_eval_string) |
|
238 { |
2927
|
239 retval = current_eval_string; |
1822
|
240 |
2927
|
241 size_t len = retval.length (); |
1822
|
242 |
2927
|
243 if (retval[len-1] != '\n') |
|
244 retval.append ("\n"); |
1822
|
245 } |
|
246 else |
|
247 retval = octave_gets (); |
|
248 |
2927
|
249 current_input_line = retval; |
1822
|
250 |
2114
|
251 if (! get_input_from_eval_string) |
|
252 input_line_number++; |
1822
|
253 |
|
254 return retval; |
|
255 } |
|
256 |
|
257 int |
|
258 octave_read (char *buf, unsigned max_size) |
|
259 { |
2927
|
260 // XXX FIXME XXX -- is this a safe way to buffer the input? |
|
261 |
|
262 static string input_buf; |
|
263 static const char *pos = 0; |
|
264 static size_t chars_left = 0; |
1822
|
265 |
|
266 int status = 0; |
|
267 |
2927
|
268 if (input_buf.empty ()) |
1822
|
269 { |
2927
|
270 pos = 0; |
|
271 |
|
272 input_buf = get_user_input (); |
1822
|
273 |
2927
|
274 chars_left = input_buf.length (); |
|
275 |
|
276 pos = input_buf.c_str (); |
1822
|
277 } |
|
278 |
|
279 if (chars_left > 0) |
|
280 { |
|
281 buf[0] = '\0'; |
|
282 |
2927
|
283 size_t len = max_size > 2 ? max_size - 2 : 0; |
1822
|
284 |
2927
|
285 assert (len > 0); |
|
286 |
|
287 strncpy (buf, pos, len); |
1822
|
288 |
|
289 if (chars_left > len) |
1
|
290 { |
1822
|
291 chars_left -= len; |
|
292 |
2927
|
293 pos += len; |
1822
|
294 |
|
295 buf[len] = '\0'; |
|
296 |
1
|
297 status = len; |
|
298 } |
|
299 else |
|
300 { |
2927
|
301 input_buf = ""; |
1755
|
302 |
1822
|
303 len = chars_left; |
|
304 |
|
305 if (buf[len-1] != '\n') |
|
306 buf[len++] = '\n'; |
1
|
307 |
1822
|
308 buf[len] = '\0'; |
1755
|
309 |
1822
|
310 status = len; |
1
|
311 } |
1044
|
312 } |
1822
|
313 else if (chars_left == 0) |
2862
|
314 { |
2927
|
315 input_buf = ""; |
2862
|
316 |
|
317 status = 0; |
|
318 } |
1822
|
319 else |
|
320 status = -1; |
1044
|
321 |
1
|
322 return status; |
|
323 } |
|
324 |
581
|
325 // Fix things up so that input can come from file `name', printing a |
|
326 // warning if the file doesn't exist. |
|
327 |
1
|
328 FILE * |
1750
|
329 get_input_from_file (const string& name, int warn) |
1
|
330 { |
529
|
331 FILE *instream = 0; |
1
|
332 |
1750
|
333 if (name.length () > 0) |
|
334 instream = fopen (name.c_str (), "r"); |
1
|
335 |
529
|
336 if (! instream && warn) |
1750
|
337 warning ("%s: no such file or directory", name.c_str ()); |
1
|
338 |
338
|
339 if (reading_fcn_file || reading_script_file) |
339
|
340 ff_instream = instream; |
1
|
341 else |
2927
|
342 command_editor::set_input_stream (instream); |
1
|
343 |
|
344 return instream; |
|
345 } |
|
346 |
581
|
347 // Fix things up so that input can come from the standard input. This |
|
348 // may need to become much more complicated, which is why it's in a |
|
349 // separate function. |
|
350 |
1
|
351 FILE * |
|
352 get_input_from_stdin (void) |
|
353 { |
2927
|
354 command_editor::set_input_stream (stdin); |
|
355 return command_editor::get_input_stream (); |
1
|
356 } |
|
357 |
2921
|
358 // XXX FIXME XXX -- make this generate file names when appropriate. |
|
359 |
2247
|
360 static string_vector |
2921
|
361 generate_possible_completions (const string& text, string& prefix, |
|
362 string& hint) |
1280
|
363 { |
2247
|
364 string_vector names; |
1280
|
365 |
2921
|
366 prefix = ""; |
1280
|
367 |
2921
|
368 if (! text.empty () && text != "." && text.rfind ('.') != NPOS) |
1430
|
369 names = generate_struct_completions (text, prefix, hint); |
|
370 else |
2247
|
371 names = make_name_list (); |
1280
|
372 |
2944
|
373 // Sort and remove duplicates. |
2348
|
374 |
2944
|
375 names.qsort (true); |
2348
|
376 |
1280
|
377 return names; |
|
378 } |
|
379 |
2944
|
380 static string |
|
381 generate_completion (const string& text, int state) |
1280
|
382 { |
2944
|
383 string retval; |
|
384 |
2921
|
385 static string prefix; |
|
386 static string hint; |
1280
|
387 |
2921
|
388 static size_t prefix_len = 0; |
|
389 static size_t hint_len = 0; |
1280
|
390 |
1
|
391 static int list_index = 0; |
2921
|
392 static int name_list_len = 0; |
2247
|
393 static string_vector name_list; |
1
|
394 |
1280
|
395 static int matches = 0; |
1
|
396 |
|
397 if (state == 0) |
|
398 { |
|
399 list_index = 0; |
|
400 |
2921
|
401 prefix = ""; |
1280
|
402 |
2921
|
403 hint = text; |
1280
|
404 |
|
405 name_list = generate_possible_completions (text, prefix, hint); |
|
406 |
2921
|
407 name_list_len = name_list.length (); |
|
408 |
|
409 prefix_len = prefix.length (); |
1280
|
410 |
2921
|
411 hint_len = hint.length (); |
1280
|
412 |
|
413 matches = 0; |
2247
|
414 |
2921
|
415 for (int i = 0; i < name_list_len; i++) |
|
416 if (! name_list[i].compare (hint, 0, hint_len)) |
2247
|
417 matches++; |
1
|
418 } |
|
419 |
2921
|
420 if (name_list_len > 0 && matches > 0) |
1
|
421 { |
2921
|
422 while (list_index < name_list_len) |
244
|
423 { |
2921
|
424 string name = name_list[list_index]; |
2247
|
425 |
1280
|
426 list_index++; |
2247
|
427 |
2921
|
428 if (! name.compare (hint, 0, hint_len)) |
1280
|
429 { |
2944
|
430 if (! prefix.empty ()) |
|
431 retval = prefix + "." + name; |
|
432 else |
|
433 retval = name; |
1280
|
434 |
2944
|
435 if (matches == 1 && looks_like_struct (retval)) |
2927
|
436 command_editor::set_completion_append_character ('.'); |
1280
|
437 else |
2927
|
438 command_editor::set_completion_append_character |
|
439 (Vcompletion_append_char); |
1280
|
440 |
2944
|
441 break; |
1280
|
442 } |
244
|
443 } |
1
|
444 } |
|
445 |
2944
|
446 return retval; |
1
|
447 } |
|
448 |
2927
|
449 void |
|
450 initialize_command_input (void) |
|
451 { |
|
452 // If we are using readline, this allows conditional parsing of the |
|
453 // .inputrc file. |
269
|
454 |
2927
|
455 command_editor::set_name ("Octave"); |
1358
|
456 |
3004
|
457 command_editor::set_basic_quote_characters ("\""); |
2944
|
458 |
|
459 command_editor::set_completion_function (generate_completion); |
269
|
460 } |
|
461 |
2927
|
462 static bool |
|
463 match_sans_spaces (const string& standard, const string& test) |
269
|
464 { |
2927
|
465 string tmp = test; |
269
|
466 |
2927
|
467 size_t beg = test.find_first_not_of (" \t"); |
|
468 size_t end = test.find_last_not_of (" \t"); |
|
469 size_t len = beg - end + 1; |
1
|
470 |
2927
|
471 return test.compare (standard, beg, len) == 0; |
529
|
472 } |
|
473 |
581
|
474 // If the user simply hits return, this will produce an empty matrix. |
|
475 |
2086
|
476 static octave_value_list |
3006
|
477 get_user_input (const octave_value_list& args, bool debug = false) |
529
|
478 { |
2086
|
479 octave_value retval; |
529
|
480 |
|
481 int nargin = args.length (); |
|
482 |
|
483 int read_as_string = 0; |
|
484 |
712
|
485 if (nargin == 2) |
529
|
486 read_as_string++; |
|
487 |
1761
|
488 string prompt ("debug> "); |
|
489 |
712
|
490 if (nargin > 0) |
529
|
491 { |
1761
|
492 prompt = args(0).string_value (); |
636
|
493 |
|
494 if (error_state) |
|
495 { |
|
496 error ("input: unrecognized argument"); |
|
497 return retval; |
|
498 } |
529
|
499 } |
|
500 |
|
501 again: |
|
502 |
2095
|
503 flush_octave_stdout (); |
529
|
504 |
2927
|
505 string input_buf = gnu_readline (prompt.c_str (), true); |
529
|
506 |
2927
|
507 if (! input_buf.empty ()) |
529
|
508 { |
1799
|
509 if (! input_from_startup_file) |
2927
|
510 command_history::add (input_buf); |
529
|
511 |
2927
|
512 size_t len = input_buf.length (); |
529
|
513 |
|
514 if (len < 1) |
|
515 { |
|
516 if (debug) |
|
517 goto again; |
|
518 else |
963
|
519 { |
|
520 if (read_as_string) |
|
521 return ""; |
|
522 else |
|
523 return Matrix (); |
|
524 } |
529
|
525 } |
|
526 |
|
527 if (match_sans_spaces ("exit", input_buf) |
|
528 || match_sans_spaces ("quit", input_buf) |
|
529 || match_sans_spaces ("return", input_buf)) |
963
|
530 { |
|
531 return retval; |
|
532 } |
529
|
533 else if (read_as_string) |
963
|
534 { |
3081
|
535 // XXX FIXME XXX -- fix gnu_readline and octave_gets instead! |
|
536 if (input_buf.length () == 1 && input_buf[0] == '\n') |
|
537 retval = ""; |
|
538 else |
|
539 retval = input_buf; |
963
|
540 } |
529
|
541 else |
|
542 { |
|
543 int parse_status = 0; |
2900
|
544 |
3098
|
545 retval = eval_string (input_buf, (! debug), parse_status); |
2900
|
546 |
3098
|
547 if (retval.is_undefined ()) |
963
|
548 retval = Matrix (); |
529
|
549 } |
|
550 } |
|
551 else |
|
552 error ("input: reading user-input failed!"); |
|
553 |
|
554 if (debug) |
3098
|
555 { |
|
556 // Clear error_state so that if errors were encountered while |
|
557 // evaluating user input, extra error messages will not be |
|
558 // printed after we return. |
|
559 |
|
560 error_state = 0; |
|
561 |
|
562 retval = octave_value (); |
|
563 |
|
564 goto again; |
|
565 } |
529
|
566 |
|
567 return retval; |
|
568 } |
|
569 |
1957
|
570 DEFUN (input, args, , |
529
|
571 "input (PROMPT [, S])\n\ |
|
572 \n\ |
2802
|
573 Prompt user for input. If the second argument is present, return\n\ |
529
|
574 value as a string.") |
|
575 { |
2086
|
576 octave_value_list retval; |
529
|
577 |
|
578 int nargin = args.length (); |
|
579 |
712
|
580 if (nargin == 1 || nargin == 2) |
1488
|
581 retval = get_user_input (args); |
529
|
582 else |
|
583 print_usage ("input"); |
|
584 |
|
585 return retval; |
|
586 } |
|
587 |
3098
|
588 static void |
|
589 restore_command_history (void *) |
|
590 { |
|
591 command_history::ignore_entries (! Vsaving_history); |
|
592 } |
|
593 |
1957
|
594 DEFUN (keyboard, args, , |
529
|
595 "keyboard (PROMPT)\n\ |
|
596 \n\ |
|
597 maybe help in debugging function files") |
|
598 { |
2086
|
599 octave_value_list retval; |
529
|
600 |
|
601 int nargin = args.length (); |
|
602 |
712
|
603 if (nargin == 0 || nargin == 1) |
3098
|
604 { |
|
605 unwind_protect::begin_frame ("keyboard"); |
|
606 |
|
607 // XXX FIXME XXX -- we shouldn't need both the |
|
608 // command_history object and the |
|
609 // Vsaving_history variable... |
|
610 command_history::ignore_entries (false); |
|
611 |
|
612 unwind_protect::add (restore_command_history, 0); |
|
613 |
|
614 unwind_protect_bool (Vsaving_history); |
|
615 |
|
616 Vsaving_history = true; |
|
617 |
|
618 retval = get_user_input (args, true); |
|
619 |
|
620 unwind_protect::run_frame ("keyboard"); |
|
621 } |
529
|
622 else |
|
623 print_usage ("keyboard"); |
|
624 |
|
625 return retval; |
|
626 } |
|
627 |
1957
|
628 DEFUN_TEXT(echo, args, , |
1588
|
629 "echo [options]\n\ |
|
630 \n\ |
|
631 echo [on|off] -- enable or disable echoing of commands as\n\ |
|
632 they are executed in script files\n\ |
|
633 \n\ |
|
634 echo [on all|off all] -- enable or disable echoing of commands as they\n\ |
|
635 are executed in script files and functions\n\ |
|
636 \n\ |
|
637 Without any arguments, toggle the current echo state.") |
|
638 { |
2086
|
639 octave_value_list retval; |
1588
|
640 |
1755
|
641 int argc = args.length () + 1; |
|
642 |
1968
|
643 string_vector argv = args.make_argv ("echo"); |
1755
|
644 |
|
645 if (error_state) |
|
646 return retval; |
1588
|
647 |
|
648 switch (argc) |
|
649 { |
|
650 case 1: |
|
651 { |
2205
|
652 if ((Vecho_executing_commands & ECHO_SCRIPTS) |
|
653 || (Vecho_executing_commands & ECHO_FUNCTIONS)) |
2431
|
654 bind_builtin_variable ("echo_executing_commands", |
2800
|
655 static_cast<double> (ECHO_OFF)); |
1588
|
656 else |
2431
|
657 bind_builtin_variable ("echo_executing_commands", |
2800
|
658 static_cast<double> (ECHO_SCRIPTS)); |
1588
|
659 } |
|
660 break; |
|
661 |
|
662 case 2: |
|
663 { |
1755
|
664 string arg = argv[1]; |
|
665 |
|
666 if (arg == "on") |
2431
|
667 bind_builtin_variable ("echo_executing_commands", |
2800
|
668 static_cast<double> (ECHO_SCRIPTS)); |
1755
|
669 else if (arg == "off") |
2431
|
670 bind_builtin_variable ("echo_executing_commands", |
2800
|
671 static_cast<double> (ECHO_OFF)); |
1588
|
672 else |
|
673 print_usage ("echo"); |
|
674 } |
|
675 break; |
|
676 |
|
677 case 3: |
|
678 { |
1755
|
679 string arg = argv[1]; |
|
680 |
|
681 if (arg == "on" && argv[2] == "all") |
2800
|
682 { |
|
683 int tmp = (ECHO_SCRIPTS | ECHO_FUNCTIONS); |
|
684 bind_builtin_variable ("echo_executing_commands", |
|
685 static_cast<double> (tmp)); |
|
686 } |
1755
|
687 else if (arg == "off" && argv[2] == "all") |
2431
|
688 bind_builtin_variable ("echo_executing_commands", |
2800
|
689 static_cast<double> (ECHO_OFF)); |
1588
|
690 else |
|
691 print_usage ("echo"); |
|
692 } |
|
693 break; |
|
694 |
|
695 default: |
|
696 print_usage ("echo"); |
|
697 break; |
|
698 } |
|
699 |
|
700 return retval; |
|
701 } |
|
702 |
2234
|
703 DEFUN (completion_matches, args, nargout, |
2299
|
704 "completion_matches (HINT): generate possible completions given HINT\n\ |
|
705 \n\ |
|
706 This function is provided for the benefit of programs like Emacs which\n\ |
|
707 might be controlling Octave and handling user input. The current command\n\ |
|
708 number is not incremented when this function is called. This is a feature,\n\ |
|
709 not a bug.") |
2234
|
710 { |
2281
|
711 octave_value retval; |
2234
|
712 |
|
713 int nargin = args.length (); |
|
714 |
|
715 if (nargin == 1) |
|
716 { |
2944
|
717 string hint = args(0).string_value (); |
2234
|
718 |
|
719 if (! error_state) |
|
720 { |
|
721 int n = 32; |
|
722 |
|
723 string_vector list (n); |
|
724 |
|
725 int k = 0; |
|
726 |
|
727 for (;;) |
|
728 { |
2944
|
729 string cmd = generate_completion (hint, k); |
2234
|
730 |
2944
|
731 if (! cmd.empty ()) |
2234
|
732 { |
2944
|
733 if (k == n) |
2234
|
734 { |
2944
|
735 n *= 2; |
|
736 list.resize (n); |
|
737 } |
2235
|
738 |
2944
|
739 list[k++] = cmd; |
2234
|
740 } |
|
741 else |
|
742 { |
|
743 list.resize (k); |
|
744 break; |
|
745 } |
|
746 } |
|
747 |
|
748 if (nargout > 0) |
2281
|
749 { |
|
750 if (! list.empty ()) |
|
751 retval = list; |
2282
|
752 else |
|
753 retval = ""; |
2281
|
754 } |
2234
|
755 else |
|
756 { |
2235
|
757 // We don't use string_vector::list_in_columns here |
|
758 // because it will be easier for Emacs if the names |
|
759 // appear in a single column. |
|
760 |
2234
|
761 int len = list.length (); |
|
762 |
|
763 for (int i = 0; i < len; i++) |
|
764 octave_stdout << list[i] << "\n"; |
|
765 } |
2299
|
766 |
|
767 octave_completion_matches_called = true; |
2234
|
768 } |
|
769 } |
|
770 else |
|
771 print_usage ("completion_matches"); |
|
772 |
|
773 return retval; |
|
774 } |
|
775 |
2181
|
776 static int |
|
777 ps1 (void) |
|
778 { |
|
779 int status = 0; |
|
780 |
|
781 Vps1 = builtin_string_variable ("PS1"); |
|
782 |
|
783 return status; |
|
784 } |
|
785 |
|
786 static int |
|
787 ps2 (void) |
|
788 { |
|
789 int status = 0; |
|
790 |
|
791 Vps2 = builtin_string_variable ("PS2"); |
|
792 |
|
793 return status; |
|
794 } |
|
795 |
|
796 static int |
|
797 ps4 (void) |
|
798 { |
|
799 int status = 0; |
|
800 |
|
801 Vps4 = builtin_string_variable ("PS4"); |
|
802 |
|
803 return status; |
|
804 } |
|
805 |
|
806 static int |
|
807 completion_append_char (void) |
|
808 { |
|
809 int status = 0; |
|
810 |
|
811 string s = builtin_string_variable ("completion_append_char"); |
|
812 |
|
813 switch (s.length ()) |
|
814 { |
|
815 case 1: |
|
816 Vcompletion_append_char = s[0]; |
|
817 break; |
|
818 |
|
819 case 0: |
|
820 Vcompletion_append_char = '\0'; |
|
821 break; |
|
822 |
|
823 default: |
|
824 warning ("completion_append_char must be a single character"); |
|
825 status = -1; |
|
826 break; |
|
827 } |
|
828 |
|
829 return status; |
|
830 } |
|
831 |
3019
|
832 static int |
|
833 echo_executing_commands (void) |
|
834 { |
|
835 Vecho_executing_commands = check_preference ("echo_executing_commands"); |
|
836 |
|
837 return 0; |
|
838 } |
|
839 |
2181
|
840 void |
|
841 symbols_of_input (void) |
|
842 { |
|
843 DEFVAR (PS1, "\\s:\\#> ", 0, ps1, |
|
844 "primary prompt string"); |
|
845 |
|
846 DEFVAR (PS2, "> ", 0, ps2, |
|
847 "secondary prompt string"); |
|
848 |
|
849 DEFVAR (PS4, "+ ", 0, ps4, |
|
850 "string printed before echoed input (enabled by --echo-input)"); |
|
851 |
|
852 DEFVAR (completion_append_char, " ", 0, completion_append_char, |
|
853 "the string to append after successful command-line completion attempts"); |
3019
|
854 |
|
855 DEFVAR (echo_executing_commands, static_cast<double> (ECHO_OFF), 0, |
|
856 echo_executing_commands, |
|
857 "echo commands as they are executed"); |
2181
|
858 } |
|
859 |
1
|
860 /* |
|
861 ;;; Local Variables: *** |
|
862 ;;; mode: C++ *** |
|
863 ;;; End: *** |
|
864 */ |