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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
3519
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #if defined (USE_READLINE) |
|
29 |
|
30 #include <stdio.h> |
|
31 #include <stdlib.h> |
4587
|
32 #include <string.h> |
3519
|
33 |
|
34 #include <readline/history.h> |
|
35 |
|
36 void |
|
37 octave_add_history (const char *line) |
|
38 { |
3520
|
39 add_history (line); |
3519
|
40 } |
|
41 |
|
42 int |
|
43 octave_where_history (void) |
|
44 { |
|
45 return where_history (); |
|
46 } |
|
47 |
|
48 int |
|
49 octave_history_length (void) |
|
50 { |
|
51 return history_length; |
|
52 } |
|
53 |
|
54 int |
|
55 octave_max_input_history (void) |
|
56 { |
|
57 return max_input_history; |
|
58 } |
|
59 |
|
60 int |
|
61 octave_history_base (void) |
|
62 { |
|
63 return history_base; |
|
64 } |
|
65 |
|
66 void |
|
67 octave_stifle_history (int n) |
|
68 { |
3520
|
69 stifle_history (n); |
3519
|
70 } |
|
71 |
|
72 int |
|
73 octave_unstifle_history (void) |
|
74 { |
|
75 return unstifle_history (); |
|
76 } |
|
77 |
|
78 int |
|
79 octave_history_is_stifled (void) |
|
80 { |
|
81 return history_is_stifled (); |
|
82 } |
|
83 |
|
84 int |
|
85 octave_history_set_pos (int n) |
|
86 { |
|
87 return history_set_pos (n); |
|
88 } |
|
89 |
|
90 int |
|
91 octave_read_history (const char *f) |
|
92 { |
|
93 return read_history (f); |
|
94 } |
|
95 |
|
96 void |
|
97 octave_using_history (void) |
|
98 { |
3520
|
99 using_history (); |
3519
|
100 } |
|
101 |
|
102 int |
|
103 octave_read_history_range (const char *f, int b, int e) |
|
104 { |
|
105 return read_history_range (f, b, e); |
|
106 } |
|
107 |
|
108 int |
|
109 octave_write_history (const char *f) |
|
110 { |
|
111 return write_history (f); |
|
112 } |
|
113 |
|
114 int |
|
115 octave_append_history (int n, const char *f) |
|
116 { |
|
117 return append_history (n, f); |
|
118 } |
|
119 |
|
120 int |
|
121 octave_history_truncate_file (const char *f, int n) |
|
122 { |
|
123 return history_truncate_file (f, n); |
|
124 } |
|
125 |
|
126 void |
|
127 octave_remove_history (int n) |
|
128 { |
|
129 HIST_ENTRY *discard = remove_history (n); |
|
130 |
|
131 if (discard) |
|
132 { |
|
133 if (discard->line) |
|
134 free (discard->line); |
|
135 |
|
136 free (discard); |
|
137 } |
|
138 } |
|
139 |
|
140 char * |
|
141 octave_history_goto_mark (int n) |
|
142 { |
|
143 HIST_ENTRY *h; |
|
144 |
|
145 char *retval = 0; |
|
146 |
|
147 if (history_set_pos (n)) |
|
148 { |
|
149 h = current_history (); |
|
150 |
|
151 if (h) |
|
152 retval = h->line; |
|
153 } |
|
154 |
|
155 return retval; |
|
156 } |
|
157 |
|
158 char * |
|
159 octave_history_get (int n) |
|
160 { |
|
161 char *retval = 0; |
|
162 |
|
163 HIST_ENTRY *h = history_get (n); |
|
164 |
|
165 if (h) |
|
166 retval = h->line; |
|
167 |
|
168 return retval; |
|
169 } |
|
170 |
|
171 char ** |
|
172 octave_history_list (int limit, int number_lines) |
|
173 { |
|
174 static char **retval = 0; |
|
175 |
|
176 HIST_ENTRY **hlist = 0; |
|
177 |
|
178 if (retval) |
|
179 { |
|
180 char **p = retval; |
|
181 |
3598
|
182 while (*p) |
|
183 free (*p++); |
3519
|
184 |
|
185 free (retval); |
|
186 |
|
187 retval = 0; |
|
188 } |
|
189 |
|
190 hlist = history_list (); |
|
191 |
|
192 if (hlist) |
|
193 { |
|
194 int i, k; |
|
195 |
|
196 int beg = 0; |
|
197 int end = 0; |
|
198 while (hlist[end]) |
|
199 end++; |
|
200 |
|
201 beg = (limit < 0 || end < limit) ? 0 : (end - limit); |
|
202 |
|
203 retval = malloc ((end - beg + 1) * sizeof (char **)); |
|
204 |
|
205 k = 0; |
|
206 for (i = beg; i < end; i++) |
|
207 { |
3524
|
208 char *line = hlist[i]->line; |
|
209 int len = line ? strlen (line) : 0; |
|
210 char *tmp = malloc (len + 64); |
3519
|
211 |
|
212 if (number_lines) |
3524
|
213 sprintf (tmp, "%5d%c%s", i + history_base, |
3541
|
214 hlist[i]->data ? '*' : ' ', |
3524
|
215 line ? line : ""); |
3598
|
216 else |
|
217 sprintf (tmp, "%c%s", hlist[i]->data ? '*' : ' ', |
|
218 line ? line : ""); |
3519
|
219 |
|
220 retval[k++] = tmp; |
|
221 } |
|
222 |
|
223 retval[k] = 0; |
|
224 } |
|
225 |
|
226 return retval; |
|
227 } |
|
228 |
|
229 void |
|
230 octave_replace_history_entry (int which, const char *line) |
|
231 { |
|
232 HIST_ENTRY *discard = replace_history_entry (which, line, 0); |
|
233 |
|
234 if (discard) |
|
235 { |
|
236 if (discard->line) |
|
237 free (discard->line); |
|
238 |
|
239 free (discard); |
|
240 } |
|
241 } |
|
242 |
|
243 #endif |
|
244 |
|
245 /* |
|
246 ;;; Local Variables: *** |
|
247 ;;; mode: C++ *** |
|
248 ;;; End: *** |
|
249 */ |