comparison src/oct-hist.cc @ 2926:66ef74ee5d9f

[project @ 1997-05-05 03:20:52 by jwe]
author jwe
date Mon, 05 May 1997 03:40:21 +0000
parents 64ff56723e75
children 87cb7614257d
comparison
equal deleted inserted replaced
2925:f0665dac8e33 2926:66ef74ee5d9f
46 #include <unistd.h> 46 #include <unistd.h>
47 #endif 47 #endif
48 48
49 #include "cmd-hist.h" 49 #include "cmd-hist.h"
50 #include "file-ops.h" 50 #include "file-ops.h"
51 #include "oct-env.h"
51 #include "str-vec.h" 52 #include "str-vec.h"
52 53
53 #include <defaults.h> 54 #include <defaults.h>
54 #include "defun.h" 55 #include "defun.h"
55 #include "error.h" 56 #include "error.h"
65 #include "variables.h" 66 #include "variables.h"
66 67
67 // Nonzero means input is coming from temporary history file. 68 // Nonzero means input is coming from temporary history file.
68 int input_from_tmp_history_file = 0; 69 int input_from_tmp_history_file = 0;
69 70
70 // Guess what?
71 command_history octave_command_history;
72
73 // Get some default values, possibly reading them from the 71 // Get some default values, possibly reading them from the
74 // environment. 72 // environment.
75 73
76 int 74 int
77 default_history_size (void) 75 default_history_size (void)
78 { 76 {
79 int size = 1024; 77 int size = 1024;
80 char *env_size = getenv ("OCTAVE_HISTSIZE"); 78
81 if (env_size) 79 string env_size = octave_env::getenv ("OCTAVE_HISTSIZE");
80
81 if (! env_size.empty ())
82 { 82 {
83 int val; 83 int val;
84 if (sscanf (env_size, "%d", &val) == 1) 84
85 if (sscanf (env_size.c_str (), "%d", &val) == 1)
85 size = val > 0 ? val : 0; 86 size = val > 0 ? val : 0;
86 } 87 }
88
87 return size; 89 return size;
88 } 90 }
89 91
90 string 92 string
91 default_history_file (void) 93 default_history_file (void)
92 { 94 {
93 string file; 95 string file;
94 96
95 char *env_file = getenv ("OCTAVE_HISTFILE"); 97 string env_file = octave_env::getenv ("OCTAVE_HISTFILE");
96 98
97 if (env_file) 99 if (! env_file.empty ())
98 { 100 {
99 fstream f (env_file, (ios::in | ios::out)); 101 fstream f (env_file.c_str (), (ios::in | ios::out));
100 102
101 if (f) 103 if (f)
102 { 104 {
103 file = env_file; 105 file = env_file;
104 f.close (); 106 f.close ();
105 } 107 }
106 } 108 }
107 109
108 if (file.empty ()) 110 if (file.empty ())
109 { 111 {
110 if (! Vhome_directory.empty ()) 112 string home_dir = octave_env::get_home_directory ();
111 { 113
112 file = Vhome_directory; 114 if (! home_dir.empty ())
115 {
116 file = home_dir;
113 file.append ("/.octave_hist"); 117 file.append ("/.octave_hist");
114 } 118 }
115 else 119 else
116 file = ".octave_hist"; 120 file = ".octave_hist";
117 } 121 }
138 if (option == "-r" || option == "-w" || option == "-a" 142 if (option == "-r" || option == "-w" || option == "-a"
139 || option == "-n") 143 || option == "-n")
140 { 144 {
141 if (i < argc - 1) 145 if (i < argc - 1)
142 { 146 {
143 string file = oct_tilde_expand (argv[i+1]); 147 string file = file_ops::tilde_expand (argv[i+1]);
144 octave_command_history.set_file (file); 148 command_history::set_file (file);
145 } 149 }
146 150
147 if (option == "-a") 151 if (option == "-a")
148 // Append `new' lines to file. 152 // Append `new' lines to file.
149 octave_command_history.append (); 153 command_history::append ();
150 154
151 else if (option == "-w") 155 else if (option == "-w")
152 // Write entire history. 156 // Write entire history.
153 octave_command_history.write (); 157 command_history::write ();
154 158
155 else if (option == "-r") 159 else if (option == "-r")
156 // Read entire file. 160 // Read entire file.
157 octave_command_history.read (); 161 command_history::read ();
158 162
159 else if (option == "-n") 163 else if (option == "-n")
160 // Read `new' history from file. 164 // Read `new' history from file.
161 octave_command_history.read_range (); 165 command_history::read_range ();
162 166
163 else 167 else
164 panic_impossible (); 168 panic_impossible ();
165 169
166 return; 170 return;
192 196
193 if (limit < 0) 197 if (limit < 0)
194 limit = -limit; 198 limit = -limit;
195 } 199 }
196 200
197 string_vector hlist = octave_command_history.list (limit, numbered_output); 201 string_vector hlist = command_history::list (limit, numbered_output);
198 202
199 int len = hlist.length (); 203 int len = hlist.length ();
200 204
201 for (i = 0; i < len; i++) 205 for (i = 0; i < len; i++)
202 octave_stdout << hlist[i] << "\n"; 206 octave_stdout << hlist[i] << "\n";
265 static void 269 static void
266 edit_history_repl_hist (const string& command) 270 edit_history_repl_hist (const string& command)
267 { 271 {
268 if (! command.empty ()) 272 if (! command.empty ())
269 { 273 {
270 string_vector hlist = octave_command_history.list (); 274 string_vector hlist = command_history::list ();
271 275
272 int len = hlist.length (); 276 int len = hlist.length ();
273 277
274 if (len > 0) 278 if (len > 0)
275 { 279 {
276 int i = len - 1; 280 int i = len - 1;
277 281
278 string histent = octave_command_history.get_entry (i); 282 string histent = command_history::get_entry (i);
279 283
280 if (! histent.empty ()) 284 if (! histent.empty ())
281 { 285 {
282 string cmd = command; 286 string cmd = command;
283 287
285 289
286 if (cmd[cmd_len - 1] == '\n') 290 if (cmd[cmd_len - 1] == '\n')
287 cmd.resize (cmd_len - 1); 291 cmd.resize (cmd_len - 1);
288 292
289 if (! cmd.empty ()) 293 if (! cmd.empty ())
290 octave_command_history.replace_entry (i, cmd); 294 command_history::replace_entry (i, cmd);
291 } 295 }
292 } 296 }
293 } 297 }
294 } 298 }
295 299
304 308
305 if (len > 0 && tmp[len-1] == '\n') 309 if (len > 0 && tmp[len-1] == '\n')
306 tmp.resize (len - 1); 310 tmp.resize (len - 1);
307 311
308 if (! tmp.empty ()) 312 if (! tmp.empty ())
309 octave_command_history.add (tmp); 313 command_history::add (tmp);
310 } 314 }
311 } 315 }
312 316
313 static string 317 static string
314 mk_tmp_hist_file (int argc, const string_vector& argv, 318 mk_tmp_hist_file (int argc, const string_vector& argv,
315 int insert_curr, const char *warn_for) 319 int insert_curr, const char *warn_for)
316 { 320 {
317 string retval; 321 string retval;
318 322
319 string_vector hlist = octave_command_history.list (); 323 string_vector hlist = command_history::list ();
320 324
321 int hist_count = hlist.length (); 325 int hist_count = hlist.length ();
322 326
323 // The current command line is already part of the history list by 327 // The current command line is already part of the history list by
324 // the time we get to this point. Delete it from the list. 328 // the time we get to this point. Delete it from the list.
325 329
326 hist_count -= 2; 330 hist_count -= 2;
327 331
328 if (! insert_curr) 332 if (! insert_curr)
329 octave_command_history.remove (hist_count); 333 command_history::remove (hist_count);
330 334
331 hist_count--; 335 hist_count--;
332 336
333 // If no numbers have been specified, the default is to edit the 337 // If no numbers have been specified, the default is to edit the
334 // last command in the history list. 338 // last command in the history list.
381 hist_end = hist_beg; 385 hist_end = hist_beg;
382 hist_beg = t; 386 hist_beg = t;
383 reverse = 1; 387 reverse = 1;
384 } 388 }
385 389
386 string name = oct_tempnam (); 390 string name = file_ops::tempnam ();
387 391
388 fstream file (name.c_str (), ios::out); 392 fstream file (name.c_str (), ios::out);
389 393
390 if (! file) 394 if (! file)
391 { 395 {