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