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