3519
|
1 /* |
|
2 |
|
3 Copyright (C) 2000 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #if defined (USE_READLINE) |
|
28 |
|
29 #include <stdio.h> |
|
30 #include <stdlib.h> |
|
31 |
|
32 #include <readline/history.h> |
|
33 |
|
34 void |
|
35 octave_add_history (const char *line) |
|
36 { |
3520
|
37 add_history (line); |
3519
|
38 } |
|
39 |
|
40 int |
|
41 octave_where_history (void) |
|
42 { |
|
43 return where_history (); |
|
44 } |
|
45 |
|
46 int |
|
47 octave_history_length (void) |
|
48 { |
|
49 return history_length; |
|
50 } |
|
51 |
|
52 int |
|
53 octave_max_input_history (void) |
|
54 { |
|
55 return max_input_history; |
|
56 } |
|
57 |
|
58 int |
|
59 octave_history_base (void) |
|
60 { |
|
61 return history_base; |
|
62 } |
|
63 |
|
64 void |
|
65 octave_stifle_history (int n) |
|
66 { |
3520
|
67 stifle_history (n); |
3519
|
68 } |
|
69 |
|
70 int |
|
71 octave_unstifle_history (void) |
|
72 { |
|
73 return unstifle_history (); |
|
74 } |
|
75 |
|
76 int |
|
77 octave_history_is_stifled (void) |
|
78 { |
|
79 return history_is_stifled (); |
|
80 } |
|
81 |
|
82 int |
|
83 octave_history_set_pos (int n) |
|
84 { |
|
85 return history_set_pos (n); |
|
86 } |
|
87 |
|
88 int |
|
89 octave_read_history (const char *f) |
|
90 { |
|
91 return read_history (f); |
|
92 } |
|
93 |
|
94 void |
|
95 octave_using_history (void) |
|
96 { |
3520
|
97 using_history (); |
3519
|
98 } |
|
99 |
|
100 int |
|
101 octave_read_history_range (const char *f, int b, int e) |
|
102 { |
|
103 return read_history_range (f, b, e); |
|
104 } |
|
105 |
|
106 int |
|
107 octave_write_history (const char *f) |
|
108 { |
|
109 return write_history (f); |
|
110 } |
|
111 |
|
112 int |
|
113 octave_append_history (int n, const char *f) |
|
114 { |
|
115 return append_history (n, f); |
|
116 } |
|
117 |
|
118 int |
|
119 octave_history_truncate_file (const char *f, int n) |
|
120 { |
|
121 return history_truncate_file (f, n); |
|
122 } |
|
123 |
|
124 void |
|
125 octave_remove_history (int n) |
|
126 { |
|
127 HIST_ENTRY *discard = remove_history (n); |
|
128 |
|
129 if (discard) |
|
130 { |
|
131 if (discard->line) |
|
132 free (discard->line); |
|
133 |
|
134 free (discard); |
|
135 } |
|
136 } |
|
137 |
|
138 char * |
|
139 octave_history_goto_mark (int n) |
|
140 { |
|
141 HIST_ENTRY *h; |
|
142 |
|
143 char *retval = 0; |
|
144 |
|
145 if (history_set_pos (n)) |
|
146 { |
|
147 h = current_history (); |
|
148 |
|
149 if (h) |
|
150 retval = h->line; |
|
151 } |
|
152 |
|
153 return retval; |
|
154 } |
|
155 |
|
156 char * |
|
157 octave_history_get (int n) |
|
158 { |
|
159 char *retval = 0; |
|
160 |
|
161 HIST_ENTRY *h = history_get (n); |
|
162 |
|
163 if (h) |
|
164 retval = h->line; |
|
165 |
|
166 return retval; |
|
167 } |
|
168 |
|
169 char ** |
|
170 octave_history_list (int limit, int number_lines) |
|
171 { |
|
172 static char **retval = 0; |
|
173 |
|
174 HIST_ENTRY **hlist = 0; |
|
175 |
|
176 if (retval) |
|
177 { |
|
178 char **p = retval; |
|
179 |
3598
|
180 while (*p) |
|
181 free (*p++); |
3519
|
182 |
|
183 free (retval); |
|
184 |
|
185 retval = 0; |
|
186 } |
|
187 |
|
188 hlist = history_list (); |
|
189 |
|
190 if (hlist) |
|
191 { |
|
192 int i, k; |
|
193 |
|
194 int beg = 0; |
|
195 int end = 0; |
|
196 while (hlist[end]) |
|
197 end++; |
|
198 |
|
199 beg = (limit < 0 || end < limit) ? 0 : (end - limit); |
|
200 |
|
201 retval = malloc ((end - beg + 1) * sizeof (char **)); |
|
202 |
|
203 k = 0; |
|
204 for (i = beg; i < end; i++) |
|
205 { |
3524
|
206 char *line = hlist[i]->line; |
|
207 int len = line ? strlen (line) : 0; |
|
208 char *tmp = malloc (len + 64); |
3519
|
209 |
|
210 if (number_lines) |
3524
|
211 sprintf (tmp, "%5d%c%s", i + history_base, |
3541
|
212 hlist[i]->data ? '*' : ' ', |
3524
|
213 line ? line : ""); |
3598
|
214 else |
|
215 sprintf (tmp, "%c%s", hlist[i]->data ? '*' : ' ', |
|
216 line ? line : ""); |
3519
|
217 |
|
218 retval[k++] = tmp; |
|
219 } |
|
220 |
|
221 retval[k] = 0; |
|
222 } |
|
223 |
|
224 return retval; |
|
225 } |
|
226 |
|
227 void |
|
228 octave_replace_history_entry (int which, const char *line) |
|
229 { |
|
230 HIST_ENTRY *discard = replace_history_entry (which, line, 0); |
|
231 |
|
232 if (discard) |
|
233 { |
|
234 if (discard->line) |
|
235 free (discard->line); |
|
236 |
|
237 free (discard); |
|
238 } |
|
239 } |
|
240 |
|
241 #endif |
|
242 |
|
243 /* |
|
244 ;;; Local Variables: *** |
|
245 ;;; mode: C++ *** |
|
246 ;;; End: *** |
|
247 */ |