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 { |
|
37 return add_history (line); |
|
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 { |
|
67 return stifle_history (n); |
|
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 { |
|
97 return using_history (); |
|
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 |
|
180 while (p) |
|
181 { |
|
182 if (*p) |
|
183 free (*p++); |
|
184 } |
|
185 |
|
186 free (retval); |
|
187 |
|
188 retval = 0; |
|
189 } |
|
190 |
|
191 hlist = history_list (); |
|
192 |
|
193 if (hlist) |
|
194 { |
|
195 int i, k; |
|
196 |
|
197 int beg = 0; |
|
198 int end = 0; |
|
199 while (hlist[end]) |
|
200 end++; |
|
201 |
|
202 beg = (limit < 0 || end < limit) ? 0 : (end - limit); |
|
203 |
|
204 retval = malloc ((end - beg + 1) * sizeof (char **)); |
|
205 |
|
206 k = 0; |
|
207 for (i = beg; i < end; i++) |
|
208 { |
|
209 char *tmp = 0; |
|
210 char buf[64]; |
|
211 |
|
212 if (number_lines) |
|
213 sprintf (buf, "%5d%c", i + history_base, |
|
214 hlist[i]->data ? '*' : ' '); |
|
215 |
|
216 tmp = malloc (strlen (buf) + 1); |
|
217 |
|
218 strcpy (tmp, buf); |
|
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 */ |