Mercurial > hg > octave-lyh
annotate libinterp/interpfcn/oct-hist.cc @ 15817:dbb7896f15c7
Fix argument passing to history() command
* oct_hist.cc (do_history): Save the one non-option argument in an
std::string, use the last such argument as the number of history
lines.
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Wed, 19 Dec 2012 10:47:11 -0500 |
parents | 1eee8d8c59fd |
children | bc7a130bbdfb |
rev | line source |
---|---|
1 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13951
diff
changeset
|
3 Copyright (C) 1993-2012 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
1 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
1 | 20 |
2939 | 21 */ |
22 | |
23 /* | |
24 | |
1 | 25 The functions listed below were adapted from similar functions from |
26 GNU Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 Free | |
27 Software Foundation, Inc. | |
28 | |
29 do_history edit_history_readline | |
30 do_edit_history edit_history_add_hist | |
31 | |
32 */ | |
33 | |
240 | 34 #ifdef HAVE_CONFIG_H |
1192 | 35 #include <config.h> |
1 | 36 #endif |
37 | |
1343 | 38 #include <cstdlib> |
39 #include <cstring> | |
40 | |
1750 | 41 #include <string> |
42 | |
3503 | 43 #include <fstream> |
1350 | 44 |
1 | 45 #include <sys/types.h> |
46 #include <unistd.h> | |
1350 | 47 |
1799 | 48 #include "cmd-hist.h" |
1802 | 49 #include "file-ops.h" |
3016 | 50 #include "lo-mappers.h" |
2926 | 51 #include "oct-env.h" |
5305 | 52 #include "oct-time.h" |
1755 | 53 #include "str-vec.h" |
54 | |
2492 | 55 #include <defaults.h> |
1352 | 56 #include "defun.h" |
1 | 57 #include "error.h" |
3016 | 58 #include "gripes.h" |
1 | 59 #include "input.h" |
1742 | 60 #include "oct-hist.h" |
1750 | 61 #include "oct-obj.h" |
1352 | 62 #include "pager.h" |
3018 | 63 #include "parse.h" |
1352 | 64 #include "sighandlers.h" |
1690 | 65 #include "sysdep.h" |
1750 | 66 #include "toplev.h" |
1352 | 67 #include "unwind-prot.h" |
68 #include "utils.h" | |
2205 | 69 #include "variables.h" |
1 | 70 |
3018 | 71 // TRUE means input is coming from temporary history file. |
72 bool input_from_tmp_history_file = false; | |
168 | 73 |
5305 | 74 static std::string |
1 | 75 default_history_file (void) |
76 { | |
3523 | 77 std::string file; |
1 | 78 |
3523 | 79 std::string env_file = octave_env::getenv ("OCTAVE_HISTFILE"); |
1755 | 80 |
2926 | 81 if (! env_file.empty ()) |
6141 | 82 file = env_file; |
1 | 83 |
1755 | 84 if (file.empty ()) |
7272 | 85 file = file_ops::concat (octave_env::get_home_directory (), |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
86 ".octave_hist"); |
1 | 87 |
88 return file; | |
89 } | |
90 | |
5305 | 91 static int |
92 default_history_size (void) | |
93 { | |
94 int size = 1024; | |
95 | |
96 std::string env_size = octave_env::getenv ("OCTAVE_HISTSIZE"); | |
97 | |
98 if (! env_size.empty ()) | |
99 { | |
100 int val; | |
101 | |
102 if (sscanf (env_size.c_str (), "%d", &val) == 1) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
103 size = val > 0 ? val : 0; |
5305 | 104 } |
105 | |
106 return size; | |
107 } | |
108 | |
11486
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
109 static std::string |
5305 | 110 default_history_timestamp_format (void) |
111 { | |
112 return | |
113 std::string ("# Octave " OCTAVE_VERSION ", %a %b %d %H:%M:%S %Y %Z <") | |
114 + octave_env::get_user_name () | |
115 + std::string ("@") | |
116 + octave_env::get_host_name () | |
117 + std::string (">"); | |
118 } | |
119 | |
5794 | 120 // The format of the timestamp marker written to the history file when |
121 // Octave exits. | |
122 static std::string Vhistory_timestamp_format_string | |
123 = default_history_timestamp_format (); | |
124 | |
581 | 125 // Display, save, or load history. Stolen and modified from bash. |
126 // | |
127 // Arg of -w FILENAME means write file, arg of -r FILENAME | |
128 // means read file, arg of -q means don't number lines. Arg of N | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
129 // means only display that many items. |
581 | 130 |
15814
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
131 static string_vector |
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
132 do_history (int argc, const string_vector& argv, bool output = true) |
1 | 133 { |
134 int numbered_output = 1; | |
135 | |
12990
07dc4839c05f
properly save and restore command history file name in history command (bug #32524)
John W. Eaton <jwe@octave.org>
parents:
12989
diff
changeset
|
136 unwind_protect frame; |
15814
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
137 string_vector hlist; |
12990
07dc4839c05f
properly save and restore command history file name in history command (bug #32524)
John W. Eaton <jwe@octave.org>
parents:
12989
diff
changeset
|
138 |
07dc4839c05f
properly save and restore command history file name in history command (bug #32524)
John W. Eaton <jwe@octave.org>
parents:
12989
diff
changeset
|
139 frame.add_fcn (command_history::set_file, command_history::file ()); |
07dc4839c05f
properly save and restore command history file name in history command (bug #32524)
John W. Eaton <jwe@octave.org>
parents:
12989
diff
changeset
|
140 |
15817
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
141 // Number of history lines to show |
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
142 std::string N; |
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
143 for (int i = 1; i < argc; i++) |
1 | 144 { |
3523 | 145 std::string option = argv[i]; |
2435 | 146 |
147 if (option == "-r" || option == "-w" || option == "-a" | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
148 || option == "-n") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
149 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
150 if (i < argc - 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
151 command_history::set_file (argv[i+1]); |
1 | 152 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
153 if (option == "-a") |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
154 // Append 'new' lines to file. |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
155 command_history::append (); |
2435 | 156 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
157 else if (option == "-w") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
158 // Write entire history. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
159 command_history::write (); |
777 | 160 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
161 else if (option == "-r") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
162 // Read entire file. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
163 command_history::read (); |
777 | 164 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
165 else if (option == "-n") |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
166 // Read 'new' history from file. |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
167 command_history::read_range (); |
777 | 168 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
169 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
170 panic_impossible (); |
2435 | 171 |
15814
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
172 return hlist; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
173 } |
1755 | 174 else if (argv[i] == "-q") |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
175 numbered_output = 0; |
1755 | 176 else if (argv[i] == "--") |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
177 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
178 i++; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
179 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
180 } |
1 | 181 else |
15817
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
182 // The last argument found in the command list that looks like |
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
183 // an integer will be used |
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
184 N = argv[i]; |
1 | 185 } |
186 | |
1799 | 187 int limit = -1; |
1 | 188 |
15817
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
189 if (N != "" && sscanf (N.c_str (), "%d", &limit) != 1) |
1 | 190 { |
15817
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
191 if (N[0] == '-') |
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
192 error ("history: unrecognized option '%s'", N.c_str ()); |
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
193 else |
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
194 error ("history: bad non-numeric arg '%s'", N.c_str ()); |
1799 | 195 |
15817
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
196 return hlist; |
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
197 } |
1 | 198 |
15817
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
199 if (limit < 0) |
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
200 limit = -limit; |
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
201 |
1 | 202 |
15814
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
203 hlist = command_history::list (limit, numbered_output); |
1 | 204 |
1799 | 205 int len = hlist.length (); |
206 | |
15814
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
207 if (output) |
15817
dbb7896f15c7
Fix argument passing to history() command
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15814
diff
changeset
|
208 for (int i = 0; i < len; i++) |
15814
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
209 octave_stdout << hlist[i] << "\n"; |
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
210 |
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
211 return hlist; |
1 | 212 } |
213 | |
581 | 214 // Read the edited history lines from STREAM and return them |
215 // one at a time. This can read unlimited length lines. The | |
2434 | 216 // caller should free the storage. |
581 | 217 |
1 | 218 static char * |
3551 | 219 edit_history_readline (std::fstream& stream) |
1 | 220 { |
221 char c; | |
222 int line_len = 128; | |
223 int lindex = 0; | |
224 char *line = new char [line_len]; | |
225 line[0] = '\0'; | |
226 | |
227 while (stream.get (c)) | |
228 { | |
229 if (lindex + 2 >= line_len) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
230 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
231 char *tmp_line = new char [line_len += 128]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
232 strcpy (tmp_line, line); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
233 delete [] line; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
234 line = tmp_line; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
235 } |
1 | 236 |
237 if (c == '\n') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
238 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
239 line[lindex++] = '\n'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
240 line[lindex++] = '\0'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
241 return line; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
242 } |
1 | 243 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
244 line[lindex++] = c; |
1 | 245 } |
246 | |
247 if (! lindex) | |
248 { | |
249 delete [] line; | |
529 | 250 return 0; |
1 | 251 } |
252 | |
253 if (lindex + 2 >= line_len) | |
254 { | |
255 char *tmp_line = new char [lindex+3]; | |
256 strcpy (tmp_line, line); | |
257 delete [] line; | |
258 line = tmp_line; | |
259 } | |
260 | |
1358 | 261 // Finish with newline if none in file. |
1 | 262 |
263 line[lindex++] = '\n'; | |
264 line[lindex++] = '\0'; | |
265 return line; | |
266 } | |
267 | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
268 // Use 'command' to replace the last entry in the history list, which, |
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
269 // by this time, is 'run_history blah...'. The intent is that the |
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
270 // new command becomes the history entry, and that 'fc' should never |
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
271 // appear in the history list. This way you can do 'run_history' to |
581 | 272 // your heart's content. |
273 | |
64 | 274 static void |
3523 | 275 edit_history_repl_hist (const std::string& command) |
64 | 276 { |
1799 | 277 if (! command.empty ()) |
278 { | |
2926 | 279 string_vector hlist = command_history::list (); |
64 | 280 |
1799 | 281 int len = hlist.length (); |
64 | 282 |
1799 | 283 if (len > 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
284 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
285 int i = len - 1; |
64 | 286 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
287 std::string histent = command_history::get_entry (i); |
64 | 288 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
289 if (! histent.empty ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
290 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
291 std::string cmd = command; |
64 | 292 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
293 int cmd_len = cmd.length (); |
64 | 294 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
295 if (cmd[cmd_len - 1] == '\n') |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
296 cmd.resize (cmd_len - 1); |
64 | 297 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
298 if (! cmd.empty ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
299 command_history::replace_entry (i, cmd); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
300 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
301 } |
64 | 302 } |
303 } | |
304 | |
1 | 305 static void |
3523 | 306 edit_history_add_hist (const std::string& line) |
1 | 307 { |
1799 | 308 if (! line.empty ()) |
1 | 309 { |
3523 | 310 std::string tmp = line; |
1 | 311 |
1799 | 312 int len = tmp.length (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
313 |
1799 | 314 if (len > 0 && tmp[len-1] == '\n') |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
315 tmp.resize (len - 1); |
1799 | 316 |
317 if (! tmp.empty ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
318 command_history::add (tmp); |
1 | 319 } |
320 } | |
321 | |
3536 | 322 static std::string |
1755 | 323 mk_tmp_hist_file (int argc, const string_vector& argv, |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
324 int insert_curr, const char *warn_for) |
1 | 325 { |
3523 | 326 std::string retval; |
1 | 327 |
2926 | 328 string_vector hlist = command_history::list (); |
1 | 329 |
1799 | 330 int hist_count = hlist.length (); |
1 | 331 |
1358 | 332 // The current command line is already part of the history list by |
333 // the time we get to this point. Delete it from the list. | |
1 | 334 |
335 hist_count -= 2; | |
1799 | 336 |
64 | 337 if (! insert_curr) |
2926 | 338 command_history::remove (hist_count); |
1799 | 339 |
1 | 340 hist_count--; |
341 | |
1358 | 342 // If no numbers have been specified, the default is to edit the |
343 // last command in the history list. | |
1 | 344 |
345 int hist_end = hist_count; | |
346 int hist_beg = hist_count; | |
347 int reverse = 0; | |
348 | |
1358 | 349 // Process options. |
1 | 350 |
351 int usage_error = 0; | |
352 if (argc == 3) | |
353 { | |
1755 | 354 if (sscanf (argv[1].c_str (), "%d", &hist_beg) != 1 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
355 || sscanf (argv[2].c_str (), "%d", &hist_end) != 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
356 usage_error = 1; |
1 | 357 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
358 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
359 hist_beg--; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
360 hist_end--; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
361 } |
1 | 362 } |
363 else if (argc == 2) | |
364 { | |
1755 | 365 if (sscanf (argv[1].c_str (), "%d", &hist_beg) != 1) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
366 usage_error = 1; |
1 | 367 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
368 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
369 hist_beg--; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
370 hist_end = hist_beg; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
371 } |
1 | 372 } |
373 | |
374 if (hist_beg < 0 || hist_end < 0 || hist_beg > hist_count | |
375 || hist_end > hist_count) | |
376 { | |
64 | 377 error ("%s: history specification out of range", warn_for); |
1799 | 378 return retval; |
1 | 379 } |
380 | |
381 if (usage_error) | |
382 { | |
64 | 383 usage ("%s [first] [last]", warn_for); |
1799 | 384 return retval; |
1 | 385 } |
386 | |
387 if (hist_end < hist_beg) | |
388 { | |
389 int t = hist_end; | |
390 hist_end = hist_beg; | |
391 hist_beg = t; | |
392 reverse = 1; | |
393 } | |
394 | |
10197
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
395 std::string name = octave_tempnam ("", "oct-"); |
1 | 396 |
3551 | 397 std::fstream file (name.c_str (), std::ios::out); |
64 | 398 |
1 | 399 if (! file) |
400 { | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
401 error ("%s: couldn't open temporary file '%s'", warn_for, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
402 name.c_str ()); |
1799 | 403 return retval; |
1 | 404 } |
405 | |
406 if (reverse) | |
407 { | |
408 for (int i = hist_end; i >= hist_beg; i--) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
409 file << hlist[i] << "\n"; |
1 | 410 } |
411 else | |
412 { | |
413 for (int i = hist_beg; i <= hist_end; i++) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
414 file << hlist[i] << "\n"; |
1 | 415 } |
416 | |
417 file.close (); | |
418 | |
1755 | 419 return name; |
64 | 420 } |
421 | |
10411 | 422 static void |
423 unlink_cleanup (const char *file) | |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
424 { |
10411 | 425 gnulib::unlink (file); |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
426 } |
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
427 |
1755 | 428 static void |
429 do_edit_history (int argc, const string_vector& argv) | |
64 | 430 { |
3523 | 431 std::string name = mk_tmp_hist_file (argc, argv, 0, "edit_history"); |
64 | 432 |
1755 | 433 if (name.empty ()) |
64 | 434 return; |
435 | |
1358 | 436 // Call up our favorite editor on the file of commands. |
1 | 437 |
5794 | 438 std::string cmd = VEDITOR; |
4469 | 439 cmd.append (" \""); |
1799 | 440 cmd.append (name); |
4469 | 441 cmd.append ("\""); |
1 | 442 |
1358 | 443 // Ignore interrupts while we are off editing commands. Should we |
444 // maybe avoid using system()? | |
1 | 445 |
2705 | 446 volatile octave_interrupt_handler old_interrupt_handler |
2554 | 447 = octave_ignore_interrupts (); |
1443 | 448 |
1799 | 449 system (cmd.c_str ()); |
1443 | 450 |
2554 | 451 octave_set_interrupt_handler (old_interrupt_handler); |
1 | 452 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
453 // Write the commands to the history file since source_file |
1358 | 454 // disables command line history while it executes. |
1 | 455 |
3551 | 456 std::fstream file (name.c_str (), std::ios::in); |
1 | 457 |
458 char *line; | |
64 | 459 int first = 1; |
529 | 460 while ((line = edit_history_readline (file)) != 0) |
1 | 461 { |
1358 | 462 // Skip blank lines. |
1 | 463 |
464 if (line[0] == '\n') | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
465 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
466 delete [] line; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
467 continue; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
468 } |
1 | 469 |
64 | 470 if (first) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
471 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
472 first = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
473 edit_history_repl_hist (line); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
474 } |
64 | 475 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10197
diff
changeset
|
476 edit_history_add_hist (line); |
1 | 477 } |
478 | |
479 file.close (); | |
480 | |
1358 | 481 // Turn on command echo, so the output from this will make better |
482 // sense. | |
1 | 483 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
484 unwind_protect frame; |
3018 | 485 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
486 frame.add_fcn (unlink_cleanup, name.c_str ()); |
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
487 frame.protect_var (Vecho_executing_commands); |
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
488 frame.protect_var (input_from_tmp_history_file); |
3018 | 489 |
2205 | 490 Vecho_executing_commands = ECHO_CMD_LINE; |
3018 | 491 input_from_tmp_history_file = true; |
1 | 492 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
493 source_file (name); |
1 | 494 } |
495 | |
1755 | 496 static void |
497 do_run_history (int argc, const string_vector& argv) | |
64 | 498 { |
3523 | 499 std::string name = mk_tmp_hist_file (argc, argv, 1, "run_history"); |
64 | 500 |
1755 | 501 if (name.empty ()) |
64 | 502 return; |
503 | |
1799 | 504 // Turn on command echo so the output from this will make better |
1358 | 505 // sense. |
64 | 506 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
507 unwind_protect frame; |
3018 | 508 |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
509 frame.add_fcn (unlink_cleanup, name.c_str ()); |
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
510 frame.protect_var (Vecho_executing_commands); |
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
511 frame.protect_var (input_from_tmp_history_file); |
3018 | 512 |
2205 | 513 Vecho_executing_commands = ECHO_CMD_LINE; |
3018 | 514 input_from_tmp_history_file = true; |
64 | 515 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
516 source_file (name); |
64 | 517 } |
518 | |
5305 | 519 void |
8273
2c1ba965b486
skip reading history file with --no-history option
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
520 initialize_history (bool read_history_file) |
5794 | 521 { |
12989
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
522 command_history::initialize (read_history_file, |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
523 default_history_file (), |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
524 default_history_size (), |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
525 octave_env::getenv ("OCTAVE_HISTCONTROL")); |
5794 | 526 } |
527 | |
528 void | |
5305 | 529 octave_history_write_timestamp (void) |
530 { | |
531 octave_localtime now; | |
532 | |
533 std::string timestamp = now.strftime (Vhistory_timestamp_format_string); | |
534 | |
535 if (! timestamp.empty ()) | |
536 command_history::add (timestamp); | |
537 } | |
538 | |
8746
5dd06f19e9be
handle commands in the lexer
John W. Eaton <jwe@octave.org>
parents:
8286
diff
changeset
|
539 DEFUN (edit_history, args, , |
3332 | 540 "-*- texinfo -*-\n\ |
11547 | 541 @deftypefn {Command} {} edit_history [@var{first}] [@var{last}]\n\ |
3332 | 542 If invoked with no arguments, @code{edit_history} allows you to edit the\n\ |
10840 | 543 history list using the editor named by the variable @w{@env{EDITOR}}. The\n\ |
3332 | 544 commands to be edited are first copied to a temporary file. When you\n\ |
545 exit the editor, Octave executes the commands that remain in the file.\n\ | |
12642
f96b9b9f141b
doc: Periodic grammarcheck and spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
546 It is often more convenient to use @code{edit_history} to define functions\n\ |
3332 | 547 rather than attempting to enter them directly on the command line.\n\ |
548 By default, the block of commands is executed as soon as you exit the\n\ | |
549 editor. To avoid executing any commands, simply delete all the lines\n\ | |
550 from the buffer before exiting the editor.\n\ | |
551 \n\ | |
552 The @code{edit_history} command takes two optional arguments specifying\n\ | |
553 the history numbers of first and last commands to edit. For example,\n\ | |
554 the command\n\ | |
529 | 555 \n\ |
3332 | 556 @example\n\ |
557 edit_history 13\n\ | |
558 @end example\n\ | |
559 \n\ | |
560 @noindent\n\ | |
561 extracts all the commands from the 13th through the last in the history\n\ | |
562 list. The command\n\ | |
563 \n\ | |
564 @example\n\ | |
565 edit_history 13 169\n\ | |
566 @end example\n\ | |
567 \n\ | |
568 @noindent\n\ | |
569 only extracts commands 13 through 169. Specifying a larger number for\n\ | |
570 the first command than the last command reverses the list of commands\n\ | |
571 before placing them in the buffer to be edited. If both arguments are\n\ | |
572 omitted, the previous command in the history list is used.\n\ | |
9035
57649dcecb55
Documentation cleanup of basics.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
573 @seealso{run_history}\n\ |
11547 | 574 @end deftypefn") |
529 | 575 { |
2086 | 576 octave_value_list retval; |
529 | 577 |
1755 | 578 int argc = args.length () + 1; |
579 | |
1968 | 580 string_vector argv = args.make_argv ("edit_history"); |
1755 | 581 |
582 if (error_state) | |
583 return retval; | |
529 | 584 |
585 do_edit_history (argc, argv); | |
586 | |
587 return retval; | |
588 } | |
589 | |
15814
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
590 DEFUN (history, args, nargout, |
3332 | 591 "-*- texinfo -*-\n\ |
15814
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
592 @deftypefn {Command} history options\n\ |
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
593 @deftypefnx {Built-in Function} {@var{h} = } history (@var{opt1}, @var{opt2}, @dots{})\n\ |
3332 | 594 If invoked with no arguments, @code{history} displays a list of commands\n\ |
595 that you have executed. Valid options are:\n\ | |
596 \n\ | |
597 @table @code\n\ | |
598 @item -w @var{file}\n\ | |
599 Write the current history to the file @var{file}. If the name is\n\ | |
600 omitted, use the default history file (normally @file{~/.octave_hist}).\n\ | |
529 | 601 \n\ |
3332 | 602 @item -r @var{file}\n\ |
12642
f96b9b9f141b
doc: Periodic grammarcheck and spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
603 Read the file @var{file}, appending its contents to the current\n\ |
11405
51b6193e90bb
Documentation fixes
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11368
diff
changeset
|
604 history list. If the name is omitted, use the default history file\n\ |
3332 | 605 (normally @file{~/.octave_hist}).\n\ |
606 \n\ | |
3499 | 607 @item @var{n}\n\ |
9035
57649dcecb55
Documentation cleanup of basics.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
608 Display only the most recent @var{n} lines of history.\n\ |
3332 | 609 \n\ |
610 @item -q\n\ | |
611 Don't number the displayed lines of history. This is useful for cutting\n\ | |
9035
57649dcecb55
Documentation cleanup of basics.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
612 and pasting commands using the X Window System.\n\ |
3332 | 613 @end table\n\ |
614 \n\ | |
615 For example, to display the five most recent commands that you have\n\ | |
616 typed without displaying line numbers, use the command\n\ | |
617 @kbd{history -q 5}.\n\ | |
15814
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
618 \n\ |
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
619 If invoked with a single output argument, the history will be saved to that\n\ |
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
620 argument as a cell string and will not be output to screen.\n\ |
11547 | 621 @end deftypefn") |
529 | 622 { |
2086 | 623 octave_value_list retval; |
529 | 624 |
1755 | 625 int argc = args.length () + 1; |
626 | |
1968 | 627 string_vector argv = args.make_argv ("history"); |
1755 | 628 |
629 if (error_state) | |
630 return retval; | |
529 | 631 |
15814
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
632 string_vector hlist; |
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
633 if (nargout > 0) |
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
634 { |
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
635 argv.append (std::string ("-q")); |
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
636 argc++; |
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
637 hlist = do_history (argc, argv, false); |
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
638 retval(0) = Cell (hlist); |
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
639 } |
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
640 else |
1eee8d8c59fd
Return history as a cell string when called with an output argument (bug #37947)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15467
diff
changeset
|
641 do_history (argc, argv, true); |
529 | 642 |
643 return retval; | |
644 } | |
645 | |
8746
5dd06f19e9be
handle commands in the lexer
John W. Eaton <jwe@octave.org>
parents:
8286
diff
changeset
|
646 DEFUN (run_history, args, , |
3332 | 647 "-*- texinfo -*-\n\ |
11547 | 648 @deftypefn {Command} {} run_history [@var{first}] [@var{last}]\n\ |
3332 | 649 Similar to @code{edit_history}, except that the editor is not invoked,\n\ |
650 and the commands are simply executed as they appear in the history list.\n\ | |
9035
57649dcecb55
Documentation cleanup of basics.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
651 @seealso{edit_history}\n\ |
11547 | 652 @end deftypefn") |
529 | 653 { |
2086 | 654 octave_value_list retval; |
529 | 655 |
1755 | 656 int argc = args.length () + 1; |
657 | |
1968 | 658 string_vector argv = args.make_argv ("run_history"); |
1755 | 659 |
660 if (error_state) | |
661 return retval; | |
529 | 662 |
663 do_run_history (argc, argv); | |
664 | |
665 return retval; | |
666 } | |
667 | |
11486
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
668 DEFUN (history_control, args, nargout, |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
669 "-*- texinfo -*-\n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
670 @deftypefn {Built-in Function} {@var{val} =} history_control ()\n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
671 @deftypefnx {Built-in Function} {@var{old_val} =} history_control (@var{new_val})\n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
672 Query or set the internal variable that specifies how commands are saved\n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
673 to the history list. The default value is an empty character string,\n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
674 but may be overridden by the environment variable\n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
675 @w{@env{OCTAVE_HISTCONTROL}}.\n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
676 \n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
677 The value of @code{history_control} is a colon-separated list of values\n\ |
11572
7d6d8c1e471f
Grammarcheck Texinfo for files in src directory.
Rik <octave@nomad.inbox5.com>
parents:
11547
diff
changeset
|
678 controlling how commands are saved on the history list. If the list\n\ |
7d6d8c1e471f
Grammarcheck Texinfo for files in src directory.
Rik <octave@nomad.inbox5.com>
parents:
11547
diff
changeset
|
679 of values includes @code{ignorespace}, lines which begin with a space\n\ |
11486
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
680 character are not saved in the history list. A value of @code{ignoredups}\n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
681 causes lines matching the previous history entry to not be saved.\n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
682 A value of @code{ignoreboth} is shorthand for @code{ignorespace} and\n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
683 @code{ignoredups}. A value of @code{erasedups} causes all previous lines\n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
684 matching the current line to be removed from the history list before that\n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
685 line is saved. Any value not in the above list is ignored. If\n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
686 @code{history_control} is the empty string, all commands are saved on\n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
687 the history list, subject to the value of @code{saving_history}.\n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
688 @seealso{history_file, history_size, history_timestamp_format_string, saving_history}\n\ |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
689 @end deftypefn") |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
690 { |
12989
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
691 std::string old_history_control = command_history::histcontrol (); |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
692 |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
693 std::string tmp = old_history_control; |
11486
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
694 |
12989
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
695 octave_value retval = set_internal_variable (tmp, args, nargout, |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
696 "history_control"); |
11486
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
697 |
12989
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
698 if (tmp != old_history_control) |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
699 command_history::process_histcontrol (tmp); |
11486
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
700 |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
701 return retval; |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
702 } |
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
703 |
5794 | 704 DEFUN (history_size, args, nargout, |
705 "-*- texinfo -*-\n\ | |
10840 | 706 @deftypefn {Built-in Function} {@var{val} =} history_size ()\n\ |
5794 | 707 @deftypefnx {Built-in Function} {@var{old_val} =} history_size (@var{new_val})\n\ |
708 Query or set the internal variable that specifies how many entries\n\ | |
709 to store in the history file. The default value is @code{1024},\n\ | |
10840 | 710 but may be overridden by the environment variable @w{@env{OCTAVE_HISTSIZE}}.\n\ |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8273
diff
changeset
|
711 @seealso{history_file, history_timestamp_format_string, saving_history}\n\ |
5794 | 712 @end deftypefn") |
3016 | 713 { |
12989
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
714 int old_history_size = command_history::size (); |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
715 |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
716 int tmp = old_history_size; |
5800 | 717 |
12989
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
718 octave_value retval = set_internal_variable (tmp, args, nargout, |
15215
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
719 "history_size", -1, |
9020dddc925a
use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents:
15195
diff
changeset
|
720 std::numeric_limits<int>::max ()); |
5800 | 721 |
12989
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
722 if (tmp != old_history_size) |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
723 command_history::set_size (tmp); |
5800 | 724 |
725 return retval; | |
3016 | 726 } |
727 | |
5794 | 728 DEFUN (history_file, args, nargout, |
729 "-*- texinfo -*-\n\ | |
10840 | 730 @deftypefn {Built-in Function} {@var{val} =} history_file ()\n\ |
5794 | 731 @deftypefnx {Built-in Function} {@var{old_val} =} history_file (@var{new_val})\n\ |
732 Query or set the internal variable that specifies the name of the\n\ | |
733 file used to store command history. The default value is\n\ | |
9134
a3739e27b017
Update section 2.4 of basics.txi
Rik <rdrider0-list@yahoo.com>
parents:
9035
diff
changeset
|
734 @file{~/.octave_hist}, but may be overridden by the environment\n\ |
10840 | 735 variable @w{@env{OCTAVE_HISTFILE}}.\n\ |
5794 | 736 @seealso{history_size, saving_history, history_timestamp_format_string}\n\ |
737 @end deftypefn") | |
5305 | 738 { |
12989
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
739 std::string old_history_file = command_history::file (); |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
740 |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
741 std::string tmp = old_history_file; |
5305 | 742 |
12989
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
743 octave_value retval = set_internal_variable (tmp, args, nargout, |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
744 "history_file"); |
5305 | 745 |
12989
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
746 if (tmp != old_history_file) |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
747 command_history::set_file (tmp); |
5305 | 748 |
5800 | 749 return retval; |
3016 | 750 } |
751 | |
5794 | 752 DEFUN (history_timestamp_format_string, args, nargout, |
753 "-*- texinfo -*-\n\ | |
10840 | 754 @deftypefn {Built-in Function} {@var{val} =} history_timestamp_format_string ()\n\ |
5794 | 755 @deftypefnx {Built-in Function} {@var{old_val} =} history_timestamp_format_string (@var{new_val})\n\ |
13951
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
12990
diff
changeset
|
756 @deftypefnx {Built-in Function} {} history_timestamp_format_string (@var{new_val}, \"local\")\n\ |
6653 | 757 Query or set the internal variable that specifies the format string\n\ |
5794 | 758 for the comment line that is written to the history file when Octave\n\ |
759 exits. The format string is passed to @code{strftime}. The default\n\ | |
760 value is\n\ | |
5305 | 761 \n\ |
762 @example\n\ | |
763 \"# Octave VERSION, %a %b %d %H:%M:%S %Y %Z <USER@@HOST>\"\n\ | |
764 @end example\n\ | |
13951
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
12990
diff
changeset
|
765 \n\ |
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
12990
diff
changeset
|
766 When called from inside a function with the \"local\" option, the variable is\n\ |
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
12990
diff
changeset
|
767 changed locally for the function and any subroutines it calls. The original\n\ |
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
12990
diff
changeset
|
768 variable value is restored when exiting the function.\n\ |
5794 | 769 @seealso{strftime, history_file, history_size, saving_history}\n\ |
770 @end deftypefn") | |
771 { | |
772 return SET_INTERNAL_VARIABLE (history_timestamp_format_string); | |
773 } | |
5305 | 774 |
5794 | 775 DEFUN (saving_history, args, nargout, |
776 "-*- texinfo -*-\n\ | |
10840 | 777 @deftypefn {Built-in Function} {@var{val} =} saving_history ()\n\ |
5794 | 778 @deftypefnx {Built-in Function} {@var{old_val} =} saving_history (@var{new_val})\n\ |
13951
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
12990
diff
changeset
|
779 @deftypefnx {Built-in Function} {} saving_history (@var{new_val}, \"local\")\n\ |
5794 | 780 Query or set the internal variable that controls whether commands entered\n\ |
781 on the command line are saved in the history file.\n\ | |
13951
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
12990
diff
changeset
|
782 \n\ |
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
12990
diff
changeset
|
783 When called from inside a function with the \"local\" option, the variable is\n\ |
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
12990
diff
changeset
|
784 changed locally for the function and any subroutines it calls. The original\n\ |
79aa00a94e9e
doc: Document "local" option for configuration variables.
Rik <octave@nomad.inbox5.com>
parents:
12990
diff
changeset
|
785 variable value is restored when exiting the function.\n\ |
11486
a1deab9a6e71
bash-like history control
Pascal Dupuis <Pascal.Dupuis@worldonline.be> and John W. Eaton <jwe@octave.org>
parents:
11405
diff
changeset
|
786 @seealso{history_control, history_file, history_size, history_timestamp_format_string}\n\ |
5794 | 787 @end deftypefn") |
788 { | |
12989
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
789 bool old_saving_history = ! command_history::ignoring_entries (); |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
790 |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
791 bool tmp = old_saving_history; |
5794 | 792 |
12989
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
793 octave_value retval = set_internal_variable (tmp, args, nargout, |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
794 "saving_history"); |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
795 |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
796 if (tmp != old_saving_history) |
00235a6446da
eliminate duplication of internal variables controlling command history
John W. Eaton <jwe@octave.org>
parents:
12642
diff
changeset
|
797 command_history::ignore_entries (! tmp); |
5794 | 798 |
799 return retval; | |
3016 | 800 } |