Mercurial > hg > octave-nkf
annotate src/oct-hist.cc @ 7715:5b4d278ec828
parse scripts completely before executing
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 16 Apr 2008 15:09:56 -0400 |
parents | 05ee52d7fad6 |
children | 2c1ba965b486 |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2003, |
4 2005, 2006, 2007 John W. Eaton | |
1 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
1 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
1 | 21 |
2939 | 22 */ |
23 | |
24 /* | |
25 | |
1 | 26 The functions listed below were adapted from similar functions from |
27 GNU Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 Free | |
28 Software Foundation, Inc. | |
29 | |
30 do_history edit_history_readline | |
31 do_edit_history edit_history_add_hist | |
32 | |
33 */ | |
34 | |
240 | 35 #ifdef HAVE_CONFIG_H |
1192 | 36 #include <config.h> |
1 | 37 #endif |
38 | |
1343 | 39 #include <cstdlib> |
40 #include <cstring> | |
41 | |
1750 | 42 #include <string> |
43 | |
3503 | 44 #include <fstream> |
1350 | 45 |
46 #ifdef HAVE_UNISTD_H | |
2442 | 47 #ifdef HAVE_SYS_TYPES_H |
1 | 48 #include <sys/types.h> |
2442 | 49 #endif |
1 | 50 #include <unistd.h> |
51 #endif | |
1350 | 52 |
1799 | 53 #include "cmd-hist.h" |
1802 | 54 #include "file-ops.h" |
3016 | 55 #include "lo-mappers.h" |
2926 | 56 #include "oct-env.h" |
5305 | 57 #include "oct-time.h" |
1755 | 58 #include "str-vec.h" |
59 | |
2492 | 60 #include <defaults.h> |
1352 | 61 #include "defun.h" |
1 | 62 #include "error.h" |
3016 | 63 #include "gripes.h" |
1 | 64 #include "input.h" |
1742 | 65 #include "oct-hist.h" |
1750 | 66 #include "oct-obj.h" |
1352 | 67 #include "pager.h" |
3018 | 68 #include "parse.h" |
1352 | 69 #include "sighandlers.h" |
1690 | 70 #include "sysdep.h" |
1750 | 71 #include "toplev.h" |
1352 | 72 #include "unwind-prot.h" |
73 #include "utils.h" | |
2205 | 74 #include "variables.h" |
1 | 75 |
3018 | 76 // TRUE means input is coming from temporary history file. |
77 bool input_from_tmp_history_file = false; | |
168 | 78 |
5305 | 79 static std::string |
1 | 80 default_history_file (void) |
81 { | |
3523 | 82 std::string file; |
1 | 83 |
3523 | 84 std::string env_file = octave_env::getenv ("OCTAVE_HISTFILE"); |
1755 | 85 |
2926 | 86 if (! env_file.empty ()) |
6141 | 87 file = env_file; |
1 | 88 |
1755 | 89 if (file.empty ()) |
7272 | 90 file = file_ops::concat (octave_env::get_home_directory (), |
91 ".octave_hist"); | |
1 | 92 |
93 return file; | |
94 } | |
95 | |
5794 | 96 // Where history is saved. |
97 static std::string Vhistory_file = default_history_file (); | |
98 | |
5305 | 99 static int |
100 default_history_size (void) | |
101 { | |
102 int size = 1024; | |
103 | |
104 std::string env_size = octave_env::getenv ("OCTAVE_HISTSIZE"); | |
105 | |
106 if (! env_size.empty ()) | |
107 { | |
108 int val; | |
109 | |
110 if (sscanf (env_size.c_str (), "%d", &val) == 1) | |
111 size = val > 0 ? val : 0; | |
112 } | |
113 | |
114 return size; | |
115 } | |
116 | |
5794 | 117 // The number of lines to keep in the history file. |
118 static int Vhistory_size = default_history_size (); | |
119 | |
5305 | 120 static std::string |
121 default_history_timestamp_format (void) | |
122 { | |
123 return | |
124 std::string ("# Octave " OCTAVE_VERSION ", %a %b %d %H:%M:%S %Y %Z <") | |
125 + octave_env::get_user_name () | |
126 + std::string ("@") | |
127 + octave_env::get_host_name () | |
128 + std::string (">"); | |
129 } | |
130 | |
5794 | 131 // The format of the timestamp marker written to the history file when |
132 // Octave exits. | |
133 static std::string Vhistory_timestamp_format_string | |
134 = default_history_timestamp_format (); | |
135 | |
136 // TRUE if we are saving history. | |
137 bool Vsaving_history = true; | |
138 | |
581 | 139 // Display, save, or load history. Stolen and modified from bash. |
140 // | |
141 // Arg of -w FILENAME means write file, arg of -r FILENAME | |
142 // means read file, arg of -q means don't number lines. Arg of N | |
143 // means only display that many items. | |
144 | |
1755 | 145 static void |
146 do_history (int argc, const string_vector& argv) | |
1 | 147 { |
148 int numbered_output = 1; | |
149 | |
1755 | 150 int i; |
151 for (i = 1; i < argc; i++) | |
1 | 152 { |
3523 | 153 std::string option = argv[i]; |
2435 | 154 |
155 if (option == "-r" || option == "-w" || option == "-a" | |
156 || option == "-n") | |
1 | 157 { |
1755 | 158 if (i < argc - 1) |
5872 | 159 command_history::set_file (argv[i+1]); |
1 | 160 |
2435 | 161 if (option == "-a") |
162 // Append `new' lines to file. | |
2926 | 163 command_history::append (); |
2435 | 164 |
165 else if (option == "-w") | |
166 // Write entire history. | |
2926 | 167 command_history::write (); |
777 | 168 |
2435 | 169 else if (option == "-r") |
170 // Read entire file. | |
2926 | 171 command_history::read (); |
777 | 172 |
2435 | 173 else if (option == "-n") |
174 // Read `new' history from file. | |
2926 | 175 command_history::read_range (); |
777 | 176 |
2435 | 177 else |
178 panic_impossible (); | |
179 | |
1 | 180 return; |
181 } | |
1755 | 182 else if (argv[i] == "-q") |
1 | 183 numbered_output = 0; |
1755 | 184 else if (argv[i] == "--") |
1 | 185 { |
1755 | 186 i++; |
1 | 187 break; |
188 } | |
189 else | |
190 break; | |
191 } | |
192 | |
1799 | 193 int limit = -1; |
1 | 194 |
1755 | 195 if (i < argc) |
1 | 196 { |
1755 | 197 if (sscanf (argv[i].c_str (), "%d", &limit) != 1) |
1 | 198 { |
1755 | 199 if (argv[i][0] == '-') |
200 error ("history: unrecognized option `%s'", argv[i].c_str ()); | |
1 | 201 else |
1755 | 202 error ("history: bad non-numeric arg `%s'", argv[i].c_str ()); |
1799 | 203 |
1 | 204 return; |
205 } | |
206 | |
207 if (limit < 0) | |
208 limit = -limit; | |
1799 | 209 } |
1 | 210 |
2926 | 211 string_vector hlist = command_history::list (limit, numbered_output); |
1 | 212 |
1799 | 213 int len = hlist.length (); |
214 | |
2095 | 215 for (i = 0; i < len; i++) |
216 octave_stdout << hlist[i] << "\n"; | |
1 | 217 } |
218 | |
581 | 219 // Read the edited history lines from STREAM and return them |
220 // one at a time. This can read unlimited length lines. The | |
2434 | 221 // caller should free the storage. |
581 | 222 |
1 | 223 static char * |
3551 | 224 edit_history_readline (std::fstream& stream) |
1 | 225 { |
226 char c; | |
227 int line_len = 128; | |
228 int lindex = 0; | |
229 char *line = new char [line_len]; | |
230 line[0] = '\0'; | |
231 | |
232 while (stream.get (c)) | |
233 { | |
234 if (lindex + 2 >= line_len) | |
235 { | |
236 char *tmp_line = new char [line_len += 128]; | |
237 strcpy (tmp_line, line); | |
238 delete [] line; | |
239 line = tmp_line; | |
240 } | |
241 | |
242 if (c == '\n') | |
243 { | |
244 line[lindex++] = '\n'; | |
245 line[lindex++] = '\0'; | |
246 return line; | |
247 } | |
248 else | |
249 line[lindex++] = c; | |
250 } | |
251 | |
252 if (! lindex) | |
253 { | |
254 delete [] line; | |
529 | 255 return 0; |
1 | 256 } |
257 | |
258 if (lindex + 2 >= line_len) | |
259 { | |
260 char *tmp_line = new char [lindex+3]; | |
261 strcpy (tmp_line, line); | |
262 delete [] line; | |
263 line = tmp_line; | |
264 } | |
265 | |
1358 | 266 // Finish with newline if none in file. |
1 | 267 |
268 line[lindex++] = '\n'; | |
269 line[lindex++] = '\0'; | |
270 return line; | |
271 } | |
272 | |
581 | 273 // Use `command' to replace the last entry in the history list, which, |
274 // by this time, is `run_history blah...'. The intent is that the | |
1799 | 275 // new command becomes the history entry, and that `fc' should never |
581 | 276 // appear in the history list. This way you can do `run_history' to |
277 // your heart's content. | |
278 | |
64 | 279 static void |
3523 | 280 edit_history_repl_hist (const std::string& command) |
64 | 281 { |
1799 | 282 if (! command.empty ()) |
283 { | |
2926 | 284 string_vector hlist = command_history::list (); |
64 | 285 |
1799 | 286 int len = hlist.length (); |
64 | 287 |
1799 | 288 if (len > 0) |
289 { | |
290 int i = len - 1; | |
64 | 291 |
3523 | 292 std::string histent = command_history::get_entry (i); |
64 | 293 |
1799 | 294 if (! histent.empty ()) |
295 { | |
3523 | 296 std::string cmd = command; |
64 | 297 |
1799 | 298 int cmd_len = cmd.length (); |
64 | 299 |
1799 | 300 if (cmd[cmd_len - 1] == '\n') |
301 cmd.resize (cmd_len - 1); | |
64 | 302 |
1799 | 303 if (! cmd.empty ()) |
2926 | 304 command_history::replace_entry (i, cmd); |
1799 | 305 } |
64 | 306 } |
307 } | |
308 } | |
309 | |
1 | 310 static void |
3523 | 311 edit_history_add_hist (const std::string& line) |
1 | 312 { |
1799 | 313 if (! line.empty ()) |
1 | 314 { |
3523 | 315 std::string tmp = line; |
1 | 316 |
1799 | 317 int len = tmp.length (); |
318 | |
319 if (len > 0 && tmp[len-1] == '\n') | |
320 tmp.resize (len - 1); | |
321 | |
322 if (! tmp.empty ()) | |
2926 | 323 command_history::add (tmp); |
1 | 324 } |
325 } | |
326 | |
3536 | 327 static std::string |
1755 | 328 mk_tmp_hist_file (int argc, const string_vector& argv, |
2804 | 329 int insert_curr, const char *warn_for) |
1 | 330 { |
3523 | 331 std::string retval; |
1 | 332 |
2926 | 333 string_vector hlist = command_history::list (); |
1 | 334 |
1799 | 335 int hist_count = hlist.length (); |
1 | 336 |
1358 | 337 // The current command line is already part of the history list by |
338 // the time we get to this point. Delete it from the list. | |
1 | 339 |
340 hist_count -= 2; | |
1799 | 341 |
64 | 342 if (! insert_curr) |
2926 | 343 command_history::remove (hist_count); |
1799 | 344 |
1 | 345 hist_count--; |
346 | |
1358 | 347 // If no numbers have been specified, the default is to edit the |
348 // last command in the history list. | |
1 | 349 |
350 int hist_end = hist_count; | |
351 int hist_beg = hist_count; | |
352 int reverse = 0; | |
353 | |
1358 | 354 // Process options. |
1 | 355 |
356 int usage_error = 0; | |
357 if (argc == 3) | |
358 { | |
1755 | 359 if (sscanf (argv[1].c_str (), "%d", &hist_beg) != 1 |
360 || sscanf (argv[2].c_str (), "%d", &hist_end) != 1) | |
1 | 361 usage_error = 1; |
362 else | |
363 { | |
364 hist_beg--; | |
365 hist_end--; | |
366 } | |
367 } | |
368 else if (argc == 2) | |
369 { | |
1755 | 370 if (sscanf (argv[1].c_str (), "%d", &hist_beg) != 1) |
1 | 371 usage_error = 1; |
372 else | |
373 { | |
374 hist_beg--; | |
375 hist_end = hist_beg; | |
376 } | |
377 } | |
378 | |
379 if (hist_beg < 0 || hist_end < 0 || hist_beg > hist_count | |
380 || hist_end > hist_count) | |
381 { | |
64 | 382 error ("%s: history specification out of range", warn_for); |
1799 | 383 return retval; |
1 | 384 } |
385 | |
386 if (usage_error) | |
387 { | |
64 | 388 usage ("%s [first] [last]", warn_for); |
1799 | 389 return retval; |
1 | 390 } |
391 | |
392 if (hist_end < hist_beg) | |
393 { | |
394 int t = hist_end; | |
395 hist_end = hist_beg; | |
396 hist_beg = t; | |
397 reverse = 1; | |
398 } | |
399 | |
3523 | 400 std::string name = file_ops::tempnam ("", "oct-"); |
1 | 401 |
3551 | 402 std::fstream file (name.c_str (), std::ios::out); |
64 | 403 |
1 | 404 if (! file) |
405 { | |
1755 | 406 error ("%s: couldn't open temporary file `%s'", warn_for, |
407 name.c_str ()); | |
1799 | 408 return retval; |
1 | 409 } |
410 | |
411 if (reverse) | |
412 { | |
413 for (int i = hist_end; i >= hist_beg; i--) | |
1799 | 414 file << hlist[i] << "\n"; |
1 | 415 } |
416 else | |
417 { | |
418 for (int i = hist_beg; i <= hist_end; i++) | |
1799 | 419 file << hlist[i] << "\n"; |
1 | 420 } |
421 | |
422 file.close (); | |
423 | |
1755 | 424 return name; |
64 | 425 } |
426 | |
1755 | 427 static void |
428 do_edit_history (int argc, const string_vector& argv) | |
64 | 429 { |
3523 | 430 std::string name = mk_tmp_hist_file (argc, argv, 0, "edit_history"); |
64 | 431 |
1755 | 432 if (name.empty ()) |
64 | 433 return; |
434 | |
1358 | 435 // Call up our favorite editor on the file of commands. |
1 | 436 |
5794 | 437 std::string cmd = VEDITOR; |
4469 | 438 cmd.append (" \""); |
1799 | 439 cmd.append (name); |
4469 | 440 cmd.append ("\""); |
1 | 441 |
1358 | 442 // Ignore interrupts while we are off editing commands. Should we |
443 // maybe avoid using system()? | |
1 | 444 |
2705 | 445 volatile octave_interrupt_handler old_interrupt_handler |
2554 | 446 = octave_ignore_interrupts (); |
1443 | 447 |
1799 | 448 system (cmd.c_str ()); |
1443 | 449 |
2554 | 450 octave_set_interrupt_handler (old_interrupt_handler); |
1 | 451 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
452 // Write the commands to the history file since source_file |
1358 | 453 // disables command line history while it executes. |
1 | 454 |
3551 | 455 std::fstream file (name.c_str (), std::ios::in); |
1 | 456 |
457 char *line; | |
64 | 458 int first = 1; |
529 | 459 while ((line = edit_history_readline (file)) != 0) |
1 | 460 { |
1358 | 461 // Skip blank lines. |
1 | 462 |
463 if (line[0] == '\n') | |
464 { | |
465 delete [] line; | |
466 continue; | |
467 } | |
468 | |
64 | 469 if (first) |
470 { | |
471 first = 0; | |
472 edit_history_repl_hist (line); | |
473 } | |
474 else | |
475 edit_history_add_hist (line); | |
1 | 476 } |
477 | |
478 file.close (); | |
479 | |
1358 | 480 // Turn on command echo, so the output from this will make better |
481 // sense. | |
1 | 482 |
2985 | 483 unwind_protect::begin_frame ("do_edit_history"); |
3018 | 484 |
2205 | 485 unwind_protect_int (Vecho_executing_commands); |
3018 | 486 unwind_protect_bool (input_from_tmp_history_file); |
487 | |
2205 | 488 Vecho_executing_commands = ECHO_CMD_LINE; |
3018 | 489 input_from_tmp_history_file = true; |
1 | 490 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
491 source_file (name); |
1 | 492 |
2985 | 493 unwind_protect::run_frame ("do_edit_history"); |
1 | 494 |
1358 | 495 // Delete the temporary file. Should probably be done with an |
496 // unwind_protect. | |
1 | 497 |
1755 | 498 unlink (name.c_str ()); |
1 | 499 } |
500 | |
1755 | 501 static void |
502 do_run_history (int argc, const string_vector& argv) | |
64 | 503 { |
3523 | 504 std::string name = mk_tmp_hist_file (argc, argv, 1, "run_history"); |
64 | 505 |
1755 | 506 if (name.empty ()) |
64 | 507 return; |
508 | |
1799 | 509 // Turn on command echo so the output from this will make better |
1358 | 510 // sense. |
64 | 511 |
2985 | 512 unwind_protect::begin_frame ("do_run_history"); |
3018 | 513 |
2205 | 514 unwind_protect_int (Vecho_executing_commands); |
3018 | 515 unwind_protect_bool (input_from_tmp_history_file); |
516 | |
2205 | 517 Vecho_executing_commands = ECHO_CMD_LINE; |
3018 | 518 input_from_tmp_history_file = true; |
64 | 519 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
520 source_file (name); |
64 | 521 |
2985 | 522 unwind_protect::run_frame ("do_run_history"); |
64 | 523 |
1799 | 524 // Delete the temporary file. |
525 | |
5775 | 526 // FIXME -- should probably be done using an unwind_protect. |
64 | 527 |
1755 | 528 unlink (name.c_str ()); |
64 | 529 } |
530 | |
5305 | 531 void |
5794 | 532 initialize_history (void) |
533 { | |
5872 | 534 command_history::set_file (Vhistory_file); |
5800 | 535 command_history::set_size (Vhistory_size); |
536 | |
5794 | 537 command_history::read (false); |
538 } | |
539 | |
540 void | |
5305 | 541 octave_history_write_timestamp (void) |
542 { | |
543 octave_localtime now; | |
544 | |
545 std::string timestamp = now.strftime (Vhistory_timestamp_format_string); | |
546 | |
547 if (! timestamp.empty ()) | |
548 command_history::add (timestamp); | |
549 } | |
550 | |
4208 | 551 DEFCMD (edit_history, args, , |
3332 | 552 "-*- texinfo -*-\n\ |
553 @deffn {Command} edit_history options\n\ | |
554 If invoked with no arguments, @code{edit_history} allows you to edit the\n\ | |
555 history list using the editor named by the variable @code{EDITOR}. The\n\ | |
556 commands to be edited are first copied to a temporary file. When you\n\ | |
557 exit the editor, Octave executes the commands that remain in the file.\n\ | |
558 It is often more convenient to use @code{edit_history} to define functions \n\ | |
559 rather than attempting to enter them directly on the command line.\n\ | |
560 By default, the block of commands is executed as soon as you exit the\n\ | |
561 editor. To avoid executing any commands, simply delete all the lines\n\ | |
562 from the buffer before exiting the editor.\n\ | |
563 \n\ | |
564 The @code{edit_history} command takes two optional arguments specifying\n\ | |
565 the history numbers of first and last commands to edit. For example,\n\ | |
566 the command\n\ | |
529 | 567 \n\ |
3332 | 568 @example\n\ |
569 edit_history 13\n\ | |
570 @end example\n\ | |
571 \n\ | |
572 @noindent\n\ | |
573 extracts all the commands from the 13th through the last in the history\n\ | |
574 list. The command\n\ | |
575 \n\ | |
576 @example\n\ | |
577 edit_history 13 169\n\ | |
578 @end example\n\ | |
579 \n\ | |
580 @noindent\n\ | |
581 only extracts commands 13 through 169. Specifying a larger number for\n\ | |
582 the first command than the last command reverses the list of commands\n\ | |
583 before placing them in the buffer to be edited. If both arguments are\n\ | |
584 omitted, the previous command in the history list is used.\n\ | |
3333 | 585 @end deffn") |
529 | 586 { |
2086 | 587 octave_value_list retval; |
529 | 588 |
1755 | 589 int argc = args.length () + 1; |
590 | |
1968 | 591 string_vector argv = args.make_argv ("edit_history"); |
1755 | 592 |
593 if (error_state) | |
594 return retval; | |
529 | 595 |
596 do_edit_history (argc, argv); | |
597 | |
598 return retval; | |
599 } | |
600 | |
4208 | 601 DEFCMD (history, args, , |
3332 | 602 "-*- texinfo -*-\n\ |
603 @deffn {Command} history options\n\ | |
604 If invoked with no arguments, @code{history} displays a list of commands\n\ | |
605 that you have executed. Valid options are:\n\ | |
606 \n\ | |
607 @table @code\n\ | |
608 @item -w @var{file}\n\ | |
609 Write the current history to the file @var{file}. If the name is\n\ | |
610 omitted, use the default history file (normally @file{~/.octave_hist}).\n\ | |
529 | 611 \n\ |
3332 | 612 @item -r @var{file}\n\ |
613 Read the file @var{file}, replacing the current history list with its\n\ | |
614 contents. If the name is omitted, use the default history file\n\ | |
615 (normally @file{~/.octave_hist}).\n\ | |
616 \n\ | |
3499 | 617 @item @var{n}\n\ |
618 Only display the most recent @var{n} lines of history.\n\ | |
3332 | 619 \n\ |
620 @item -q\n\ | |
621 Don't number the displayed lines of history. This is useful for cutting\n\ | |
622 and pasting commands if you are using the X Window System.\n\ | |
623 @end table\n\ | |
624 \n\ | |
625 For example, to display the five most recent commands that you have\n\ | |
626 typed without displaying line numbers, use the command\n\ | |
627 @kbd{history -q 5}.\n\ | |
3333 | 628 @end deffn") |
529 | 629 { |
2086 | 630 octave_value_list retval; |
529 | 631 |
1755 | 632 int argc = args.length () + 1; |
633 | |
1968 | 634 string_vector argv = args.make_argv ("history"); |
1755 | 635 |
636 if (error_state) | |
637 return retval; | |
529 | 638 |
639 do_history (argc, argv); | |
640 | |
641 return retval; | |
642 } | |
643 | |
4208 | 644 DEFCMD (run_history, args, , |
3332 | 645 "-*- texinfo -*-\n\ |
646 @deffn {Command} run_history [first] [last]\n\ | |
647 Similar to @code{edit_history}, except that the editor is not invoked,\n\ | |
648 and the commands are simply executed as they appear in the history list.\n\ | |
3333 | 649 @end deffn") |
529 | 650 { |
2086 | 651 octave_value_list retval; |
529 | 652 |
1755 | 653 int argc = args.length () + 1; |
654 | |
1968 | 655 string_vector argv = args.make_argv ("run_history"); |
1755 | 656 |
657 if (error_state) | |
658 return retval; | |
529 | 659 |
660 do_run_history (argc, argv); | |
661 | |
662 return retval; | |
663 } | |
664 | |
5794 | 665 DEFUN (history_size, args, nargout, |
666 "-*- texinfo -*-\n\ | |
667 @deftypefn {Built-in Function} {@var{val} =} history_size ()\n\ | |
668 @deftypefnx {Built-in Function} {@var{old_val} =} history_size (@var{new_val})\n\ | |
669 Query or set the internal variable that specifies how many entries\n\ | |
670 to store in the history file. The default value is @code{1024},\n\ | |
671 but may be overridden by the environment variable @code{OCTAVE_HISTSIZE}.\n\ | |
672 @seealso{history_file, history_timestamp_format, saving_history}\n\ | |
673 @end deftypefn") | |
3016 | 674 { |
5800 | 675 int saved_history_size = Vhistory_size; |
676 | |
677 octave_value retval | |
678 = SET_INTERNAL_VARIABLE_WITH_LIMITS (history_size, -1, INT_MAX); | |
679 | |
680 if (Vhistory_size != saved_history_size) | |
681 command_history::set_size (Vhistory_size); | |
682 | |
683 return retval; | |
3016 | 684 } |
685 | |
5794 | 686 DEFUN (history_file, args, nargout, |
687 "-*- texinfo -*-\n\ | |
688 @deftypefn {Built-in Function} {@var{val} =} history_file ()\n\ | |
689 @deftypefnx {Built-in Function} {@var{old_val} =} history_file (@var{new_val})\n\ | |
690 Query or set the internal variable that specifies the name of the\n\ | |
691 file used to store command history. The default value is\n\ | |
692 @code{\"~/.octave_hist\"}, but may be overridden by the environment\n\ | |
693 variable @code{OCTAVE_HISTFILE}.\n\ | |
694 @seealso{history_size, saving_history, history_timestamp_format_string}\n\ | |
695 @end deftypefn") | |
5305 | 696 { |
5794 | 697 std::string saved_history_file = Vhistory_file; |
5305 | 698 |
5794 | 699 octave_value retval = SET_INTERNAL_VARIABLE (history_file); |
5305 | 700 |
5794 | 701 if (Vhistory_file != saved_history_file) |
5872 | 702 command_history::set_file (Vhistory_file); |
5305 | 703 |
5800 | 704 return retval; |
3016 | 705 } |
706 | |
5794 | 707 DEFUN (history_timestamp_format_string, args, nargout, |
708 "-*- texinfo -*-\n\ | |
709 @deftypefn {Built-in Function} {@var{val} =} history_timestamp_format_string ()\n\ | |
710 @deftypefnx {Built-in Function} {@var{old_val} =} history_timestamp_format_string (@var{new_val})\n\ | |
6653 | 711 Query or set the internal variable that specifies the format string\n\ |
5794 | 712 for the comment line that is written to the history file when Octave\n\ |
713 exits. The format string is passed to @code{strftime}. The default\n\ | |
714 value is\n\ | |
5305 | 715 \n\ |
716 @example\n\ | |
717 \"# Octave VERSION, %a %b %d %H:%M:%S %Y %Z <USER@@HOST>\"\n\ | |
718 @end example\n\ | |
5794 | 719 @seealso{strftime, history_file, history_size, saving_history}\n\ |
720 @end deftypefn") | |
721 { | |
722 return SET_INTERNAL_VARIABLE (history_timestamp_format_string); | |
723 } | |
5305 | 724 |
5794 | 725 DEFUN (saving_history, args, nargout, |
726 "-*- texinfo -*-\n\ | |
727 @deftypefn {Built-in Function} {@var{val} =} saving_history ()\n\ | |
728 @deftypefnx {Built-in Function} {@var{old_val} =} saving_history (@var{new_val})\n\ | |
729 Query or set the internal variable that controls whether commands entered\n\ | |
730 on the command line are saved in the history file.\n\ | |
6615 | 731 @seealso{history_file, history_size, history_timestamp_format}\n\ |
5794 | 732 @end deftypefn") |
733 { | |
734 octave_value retval = SET_INTERNAL_VARIABLE (saving_history); | |
735 | |
736 command_history::ignore_entries (! Vsaving_history); | |
737 | |
738 return retval; | |
3016 | 739 } |
740 | |
1 | 741 /* |
742 ;;; Local Variables: *** | |
743 ;;; mode: C++ *** | |
744 ;;; End: *** | |
745 */ |