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 |
661
|
23 /* |
|
24 |
1822
|
25 The 2 functions listed below were adapted from similar functions |
661
|
26 from GNU Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 |
|
27 Free Software Foundation, Inc. |
|
28 |
1822
|
29 read_octal decode_prompt_string |
661
|
30 |
|
31 */ |
|
32 |
1
|
33 // Use the GNU readline library for command line editing and hisory. |
|
34 |
240
|
35 #ifdef HAVE_CONFIG_H |
1192
|
36 #include <config.h> |
1
|
37 #endif |
|
38 |
1343
|
39 #include <ctime> |
|
40 #include <cstdio> |
|
41 #include <cstdlib> |
|
42 #include <cstring> |
|
43 #include <cassert> |
|
44 #include <csignal> |
|
45 |
1728
|
46 #include <string> |
|
47 |
1349
|
48 #include <iostream.h> |
|
49 |
1350
|
50 #ifdef HAVE_UNISTD_H |
2442
|
51 #ifdef HAVE_SYS_TYPES_H |
529
|
52 #include <sys/types.h> |
2442
|
53 #endif |
529
|
54 #include <unistd.h> |
|
55 #endif |
1343
|
56 |
2927
|
57 #include "cmd-edit.h" |
1755
|
58 #include "str-vec.h" |
|
59 |
1352
|
60 #include "defun.h" |
|
61 #include "dirfns.h" |
1
|
62 #include "error.h" |
2181
|
63 #include "gripes.h" |
1352
|
64 #include "help.h" |
1
|
65 #include "input.h" |
1352
|
66 #include "oct-map.h" |
1742
|
67 #include "oct-hist.h" |
1670
|
68 #include "toplev.h" |
1755
|
69 #include "oct-obj.h" |
1
|
70 #include "pager.h" |
529
|
71 #include "parse.h" |
1352
|
72 #include "pathlen.h" |
1755
|
73 #include "pt-const.h" |
1352
|
74 #include "sighandlers.h" |
|
75 #include "symtab.h" |
1114
|
76 #include "sysdep.h" |
1352
|
77 #include "utils.h" |
|
78 #include "variables.h" |
529
|
79 |
2181
|
80 // Primary prompt string. |
|
81 static string Vps1; |
|
82 |
|
83 // Secondary prompt string. |
|
84 static string Vps2; |
|
85 |
|
86 // String printed before echoed input (enabled by --echo-input). |
|
87 string Vps4; |
|
88 |
|
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 |
|
95 // Nonzero means get input from current_eval_string. |
|
96 int get_input_from_eval_string = 0; |
|
97 |
338
|
98 // Nonzero means we're parsing a function file. |
|
99 int reading_fcn_file = 0; |
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 |
|
107 // Nonzero means we're parsing a script file. |
|
108 int reading_script_file = 0; |
|
109 |
|
110 // If we are reading from an M-file, this is it. |
529
|
111 FILE *ff_instream = 0; |
1
|
112 |
|
113 // Nonzero means this is an interactive shell. |
|
114 int interactive = 0; |
|
115 |
|
116 // Nonzero means the user forced this shell to be interactive (-i). |
|
117 int forced_interactive = 0; |
|
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 |
2348
|
373 names.qsort (); |
|
374 |
|
375 // Remove duplicates. |
|
376 |
|
377 // XXX FIXME XXX -- maybe this should be defined for all Array objects. |
|
378 |
|
379 int k = 0; |
|
380 |
|
381 int len = names.length (); |
|
382 |
|
383 for (int i = 1; i < len; i++) |
|
384 { |
|
385 if (names[i] != names[k]) |
|
386 { |
|
387 k++; |
|
388 |
|
389 if (k != i) |
|
390 names[k] = names[i]; |
|
391 } |
|
392 } |
|
393 |
|
394 names.resize (k+1); |
|
395 |
1280
|
396 return names; |
|
397 } |
|
398 |
|
399 static char * |
|
400 command_generator (const char *text, int state) |
|
401 { |
2921
|
402 static string prefix; |
|
403 static string hint; |
1280
|
404 |
2921
|
405 static size_t prefix_len = 0; |
|
406 static size_t hint_len = 0; |
1280
|
407 |
1
|
408 static int list_index = 0; |
2921
|
409 static int name_list_len = 0; |
2247
|
410 static string_vector name_list; |
1
|
411 |
1280
|
412 static int matches = 0; |
1
|
413 |
|
414 if (state == 0) |
|
415 { |
|
416 list_index = 0; |
|
417 |
2921
|
418 prefix = ""; |
1280
|
419 |
2921
|
420 hint = text; |
1280
|
421 |
|
422 name_list = generate_possible_completions (text, prefix, hint); |
|
423 |
2921
|
424 name_list_len = name_list.length (); |
|
425 |
|
426 prefix_len = prefix.length (); |
1280
|
427 |
2921
|
428 hint_len = hint.length (); |
1280
|
429 |
|
430 matches = 0; |
2247
|
431 |
2921
|
432 for (int i = 0; i < name_list_len; i++) |
|
433 if (! name_list[i].compare (hint, 0, hint_len)) |
2247
|
434 matches++; |
1
|
435 } |
|
436 |
2921
|
437 if (name_list_len > 0 && matches > 0) |
1
|
438 { |
2921
|
439 while (list_index < name_list_len) |
244
|
440 { |
2921
|
441 string name = name_list[list_index]; |
2247
|
442 |
1280
|
443 list_index++; |
2247
|
444 |
2921
|
445 if (! name.compare (hint, 0, hint_len)) |
1280
|
446 { |
2921
|
447 int len = 2 + prefix_len + name.length (); |
|
448 |
2800
|
449 char *buf = static_cast<char *> (malloc (len)); |
1280
|
450 |
2921
|
451 if (! prefix.empty ()) |
1280
|
452 { |
2921
|
453 strcpy (buf, prefix.c_str ()); |
1280
|
454 strcat (buf, "."); |
2921
|
455 strcat (buf, name.c_str ()); |
1280
|
456 } |
|
457 else |
2921
|
458 strcpy (buf, name.c_str ()); |
1280
|
459 |
|
460 if (matches == 1 && looks_like_struct (buf)) |
2927
|
461 command_editor::set_completion_append_character ('.'); |
1280
|
462 else |
2927
|
463 command_editor::set_completion_append_character |
|
464 (Vcompletion_append_char); |
1280
|
465 |
|
466 return buf; |
|
467 } |
244
|
468 } |
1
|
469 } |
|
470 |
529
|
471 return 0; |
1
|
472 } |
|
473 |
2927
|
474 #if 0 |
269
|
475 static char ** |
1488
|
476 command_completer (char *text, int /* start */, int /* end */) |
1
|
477 { |
529
|
478 char **matches = 0; |
1
|
479 matches = completion_matches (text, command_generator); |
|
480 return matches; |
|
481 } |
2927
|
482 #endif |
269
|
483 |
2927
|
484 void |
|
485 initialize_command_input (void) |
|
486 { |
|
487 // If we are using readline, this allows conditional parsing of the |
|
488 // .inputrc file. |
269
|
489 |
2927
|
490 command_editor::set_name ("Octave"); |
1358
|
491 |
2927
|
492 command_editor::set_paren_string_delimiters ("\""); |
269
|
493 } |
|
494 |
2927
|
495 static bool |
|
496 match_sans_spaces (const string& standard, const string& test) |
269
|
497 { |
2927
|
498 string tmp = test; |
269
|
499 |
2927
|
500 size_t beg = test.find_first_not_of (" \t"); |
|
501 size_t end = test.find_last_not_of (" \t"); |
|
502 size_t len = beg - end + 1; |
1
|
503 |
2927
|
504 return test.compare (standard, beg, len) == 0; |
529
|
505 } |
|
506 |
581
|
507 // If the user simply hits return, this will produce an empty matrix. |
|
508 |
2086
|
509 static octave_value_list |
|
510 get_user_input (const octave_value_list& args, int debug = 0) |
529
|
511 { |
2086
|
512 octave_value retval; |
529
|
513 |
|
514 int nargin = args.length (); |
|
515 |
|
516 int read_as_string = 0; |
|
517 |
712
|
518 if (nargin == 2) |
529
|
519 read_as_string++; |
|
520 |
1761
|
521 string prompt ("debug> "); |
|
522 |
712
|
523 if (nargin > 0) |
529
|
524 { |
1761
|
525 prompt = args(0).string_value (); |
636
|
526 |
|
527 if (error_state) |
|
528 { |
|
529 error ("input: unrecognized argument"); |
|
530 return retval; |
|
531 } |
529
|
532 } |
|
533 |
|
534 again: |
|
535 |
2095
|
536 flush_octave_stdout (); |
529
|
537 |
2927
|
538 string input_buf = gnu_readline (prompt.c_str (), true); |
529
|
539 |
2927
|
540 if (! input_buf.empty ()) |
529
|
541 { |
1799
|
542 if (! input_from_startup_file) |
2927
|
543 command_history::add (input_buf); |
529
|
544 |
2927
|
545 size_t len = input_buf.length (); |
529
|
546 |
|
547 if (len < 1) |
|
548 { |
|
549 if (debug) |
|
550 goto again; |
|
551 else |
963
|
552 { |
|
553 if (read_as_string) |
|
554 return ""; |
|
555 else |
|
556 return Matrix (); |
|
557 } |
529
|
558 } |
|
559 |
|
560 if (match_sans_spaces ("exit", input_buf) |
|
561 || match_sans_spaces ("quit", input_buf) |
|
562 || match_sans_spaces ("return", input_buf)) |
963
|
563 { |
|
564 return retval; |
|
565 } |
529
|
566 else if (read_as_string) |
963
|
567 { |
|
568 retval = input_buf; |
|
569 } |
529
|
570 else |
|
571 { |
|
572 int parse_status = 0; |
2900
|
573 |
2896
|
574 retval = eval_string (input_buf, true, parse_status); |
2900
|
575 |
581
|
576 if (retval.is_defined ()) |
|
577 { |
|
578 if (debug) |
2900
|
579 retval.print (octave_stdout); |
581
|
580 } |
|
581 else |
963
|
582 retval = Matrix (); |
529
|
583 } |
|
584 } |
|
585 else |
|
586 error ("input: reading user-input failed!"); |
|
587 |
|
588 if (debug) |
|
589 goto again; |
|
590 |
|
591 return retval; |
|
592 } |
|
593 |
1957
|
594 DEFUN (input, args, , |
529
|
595 "input (PROMPT [, S])\n\ |
|
596 \n\ |
2802
|
597 Prompt user for input. If the second argument is present, return\n\ |
529
|
598 value as a string.") |
|
599 { |
2086
|
600 octave_value_list retval; |
529
|
601 |
|
602 int nargin = args.length (); |
|
603 |
712
|
604 if (nargin == 1 || nargin == 2) |
1488
|
605 retval = get_user_input (args); |
529
|
606 else |
|
607 print_usage ("input"); |
|
608 |
|
609 return retval; |
|
610 } |
|
611 |
1957
|
612 DEFUN (keyboard, args, , |
529
|
613 "keyboard (PROMPT)\n\ |
|
614 \n\ |
|
615 maybe help in debugging function files") |
|
616 { |
2086
|
617 octave_value_list retval; |
529
|
618 |
|
619 int nargin = args.length (); |
|
620 |
712
|
621 if (nargin == 0 || nargin == 1) |
1488
|
622 retval = get_user_input (args, 1); |
529
|
623 else |
|
624 print_usage ("keyboard"); |
|
625 |
|
626 return retval; |
|
627 } |
|
628 |
1957
|
629 DEFUN_TEXT(echo, args, , |
1588
|
630 "echo [options]\n\ |
|
631 \n\ |
|
632 echo [on|off] -- enable or disable echoing of commands as\n\ |
|
633 they are executed in script files\n\ |
|
634 \n\ |
|
635 echo [on all|off all] -- enable or disable echoing of commands as they\n\ |
|
636 are executed in script files and functions\n\ |
|
637 \n\ |
|
638 Without any arguments, toggle the current echo state.") |
|
639 { |
2086
|
640 octave_value_list retval; |
1588
|
641 |
1755
|
642 int argc = args.length () + 1; |
|
643 |
1968
|
644 string_vector argv = args.make_argv ("echo"); |
1755
|
645 |
|
646 if (error_state) |
|
647 return retval; |
1588
|
648 |
|
649 switch (argc) |
|
650 { |
|
651 case 1: |
|
652 { |
2205
|
653 if ((Vecho_executing_commands & ECHO_SCRIPTS) |
|
654 || (Vecho_executing_commands & ECHO_FUNCTIONS)) |
2431
|
655 bind_builtin_variable ("echo_executing_commands", |
2800
|
656 static_cast<double> (ECHO_OFF)); |
1588
|
657 else |
2431
|
658 bind_builtin_variable ("echo_executing_commands", |
2800
|
659 static_cast<double> (ECHO_SCRIPTS)); |
1588
|
660 } |
|
661 break; |
|
662 |
|
663 case 2: |
|
664 { |
1755
|
665 string arg = argv[1]; |
|
666 |
|
667 if (arg == "on") |
2431
|
668 bind_builtin_variable ("echo_executing_commands", |
2800
|
669 static_cast<double> (ECHO_SCRIPTS)); |
1755
|
670 else if (arg == "off") |
2431
|
671 bind_builtin_variable ("echo_executing_commands", |
2800
|
672 static_cast<double> (ECHO_OFF)); |
1588
|
673 else |
|
674 print_usage ("echo"); |
|
675 } |
|
676 break; |
|
677 |
|
678 case 3: |
|
679 { |
1755
|
680 string arg = argv[1]; |
|
681 |
|
682 if (arg == "on" && argv[2] == "all") |
2800
|
683 { |
|
684 int tmp = (ECHO_SCRIPTS | ECHO_FUNCTIONS); |
|
685 bind_builtin_variable ("echo_executing_commands", |
|
686 static_cast<double> (tmp)); |
|
687 } |
1755
|
688 else if (arg == "off" && argv[2] == "all") |
2431
|
689 bind_builtin_variable ("echo_executing_commands", |
2800
|
690 static_cast<double> (ECHO_OFF)); |
1588
|
691 else |
|
692 print_usage ("echo"); |
|
693 } |
|
694 break; |
|
695 |
|
696 default: |
|
697 print_usage ("echo"); |
|
698 break; |
|
699 } |
|
700 |
|
701 return retval; |
|
702 } |
|
703 |
2234
|
704 DEFUN (completion_matches, args, nargout, |
2299
|
705 "completion_matches (HINT): generate possible completions given HINT\n\ |
|
706 \n\ |
|
707 This function is provided for the benefit of programs like Emacs which\n\ |
|
708 might be controlling Octave and handling user input. The current command\n\ |
|
709 number is not incremented when this function is called. This is a feature,\n\ |
|
710 not a bug.") |
2234
|
711 { |
2281
|
712 octave_value retval; |
2234
|
713 |
|
714 int nargin = args.length (); |
|
715 |
|
716 if (nargin == 1) |
|
717 { |
|
718 string hint_string = args(0).string_value (); |
|
719 |
|
720 if (! error_state) |
|
721 { |
|
722 int n = 32; |
|
723 |
|
724 string_vector list (n); |
|
725 |
|
726 const char *hint = hint_string.c_str (); |
|
727 |
|
728 int k = 0; |
|
729 |
|
730 for (;;) |
|
731 { |
|
732 const char *cmd = command_generator (hint, k); |
|
733 |
|
734 if (cmd) |
|
735 { |
2235
|
736 if (*cmd) |
2234
|
737 { |
2235
|
738 if (k == n) |
|
739 { |
|
740 n *= 2; |
|
741 list.resize (n); |
|
742 } |
|
743 |
|
744 list[k++] = cmd; |
2234
|
745 } |
|
746 } |
|
747 else |
|
748 { |
|
749 list.resize (k); |
|
750 break; |
|
751 } |
|
752 } |
|
753 |
|
754 if (nargout > 0) |
2281
|
755 { |
|
756 if (! list.empty ()) |
|
757 retval = list; |
2282
|
758 else |
|
759 retval = ""; |
2281
|
760 } |
2234
|
761 else |
|
762 { |
2235
|
763 // We don't use string_vector::list_in_columns here |
|
764 // because it will be easier for Emacs if the names |
|
765 // appear in a single column. |
|
766 |
2234
|
767 int len = list.length (); |
|
768 |
|
769 for (int i = 0; i < len; i++) |
|
770 octave_stdout << list[i] << "\n"; |
|
771 } |
2299
|
772 |
|
773 octave_completion_matches_called = true; |
2234
|
774 } |
|
775 } |
|
776 else |
|
777 print_usage ("completion_matches"); |
|
778 |
|
779 return retval; |
|
780 } |
|
781 |
2181
|
782 static int |
|
783 ps1 (void) |
|
784 { |
|
785 int status = 0; |
|
786 |
|
787 Vps1 = builtin_string_variable ("PS1"); |
|
788 |
|
789 return status; |
|
790 } |
|
791 |
|
792 static int |
|
793 ps2 (void) |
|
794 { |
|
795 int status = 0; |
|
796 |
|
797 Vps2 = builtin_string_variable ("PS2"); |
|
798 |
|
799 return status; |
|
800 } |
|
801 |
|
802 static int |
|
803 ps4 (void) |
|
804 { |
|
805 int status = 0; |
|
806 |
|
807 Vps4 = builtin_string_variable ("PS4"); |
|
808 |
|
809 return status; |
|
810 } |
|
811 |
|
812 static int |
|
813 completion_append_char (void) |
|
814 { |
|
815 int status = 0; |
|
816 |
|
817 string s = builtin_string_variable ("completion_append_char"); |
|
818 |
|
819 switch (s.length ()) |
|
820 { |
|
821 case 1: |
|
822 Vcompletion_append_char = s[0]; |
|
823 break; |
|
824 |
|
825 case 0: |
|
826 Vcompletion_append_char = '\0'; |
|
827 break; |
|
828 |
|
829 default: |
|
830 warning ("completion_append_char must be a single character"); |
|
831 status = -1; |
|
832 break; |
|
833 } |
|
834 |
|
835 return status; |
|
836 } |
|
837 |
|
838 void |
|
839 symbols_of_input (void) |
|
840 { |
|
841 DEFVAR (PS1, "\\s:\\#> ", 0, ps1, |
|
842 "primary prompt string"); |
|
843 |
|
844 DEFVAR (PS2, "> ", 0, ps2, |
|
845 "secondary prompt string"); |
|
846 |
|
847 DEFVAR (PS4, "+ ", 0, ps4, |
|
848 "string printed before echoed input (enabled by --echo-input)"); |
|
849 |
|
850 DEFVAR (completion_append_char, " ", 0, completion_append_char, |
|
851 "the string to append after successful command-line completion attempts"); |
|
852 } |
|
853 |
1
|
854 /* |
|
855 ;;; Local Variables: *** |
|
856 ;;; mode: C++ *** |
|
857 ;;; End: *** |
|
858 */ |