Mercurial > octave-nkf
annotate liboctave/oct-rl-hist.c @ 10843:229675bb7647 ss-3-3-52
version is now 3.3.52
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sun, 01 Aug 2010 11:49:45 -0400 |
parents | 42d098307c30 |
children | a1deab9a6e71 |
rev | line source |
---|---|
3519 | 1 /* |
2 | |
7017 | 3 Copyright (C) 2000, 2003, 2005, 2007 John W. Eaton |
3519 | 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
3519 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
3519 | 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) | |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
133 free (discard->line); |
3519 | 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) | |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
151 retval = h->line; |
3519 | 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) |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
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]) | |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
198 end++; |
3519 | 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++) | |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
206 { |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
207 char *line = hlist[i]->line; |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
208 int len = line ? strlen (line) : 0; |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
209 char *tmp = malloc (len + 64); |
3519 | 210 |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
211 if (number_lines) |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
212 sprintf (tmp, "%5d%c%s", i + history_base, |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
213 hlist[i]->data ? '*' : ' ', |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
214 line ? line : ""); |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
215 else |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
216 sprintf (tmp, "%c%s", hlist[i]->data ? '*' : ' ', |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
217 line ? line : ""); |
3519 | 218 |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
219 retval[k++] = tmp; |
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
220 } |
3519 | 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) | |
10317
42d098307c30
untabify additional source files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
236 free (discard->line); |
3519 | 237 |
238 free (discard); | |
239 } | |
240 } | |
241 | |
242 #endif |