GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-rl-hist.c
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2000-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "oct-rl-hist.h"
28 
29 #if defined (USE_READLINE)
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include <readline/history.h>
36 
37 /* check_history_control, hc_erasedup, and the core of
38  octave_add_history were borrowed from Bash. */
39 
40 /* Check LINE against what HISTCONTROL says to do. Returns 1 if the line
41  should be saved; 0 if it should be discarded. */
42 static int
43 check_history_control (const char *line, int history_control)
44 {
45  HIST_ENTRY *temp;
46  int r;
47 
48  if (history_control == 0)
49  return 1;
50 
51  /* ignorespace or ignoreboth */
52  if ((history_control & HC_IGNSPACE) && *line == ' ')
53  return 0;
54 
55  /* ignoredups or ignoreboth */
56  if (history_control & HC_IGNDUPS)
57  {
58  using_history ();
59  temp = previous_history ();
60 
61  r = (temp == 0 || strcmp (temp->line, line));
62 
63  using_history ();
64 
65  if (r == 0)
66  return r;
67  }
68 
69  return 1;
70 }
71 
72 /* Remove all entries matching LINE from the history list. Triggered when
73  HISTCONTROL includes `erasedups'. */
74 
75 static void
76 hc_erasedups (const char *line)
77 {
78  HIST_ENTRY *temp;
79  int r;
80 
81  using_history ();
82  while ((temp = previous_history ()))
83  {
84  if (! strcmp (temp->line, line))
85  {
86  r = where_history ();
87  remove_history (r);
88  }
89  }
90  using_history ();
91 }
92 
93 /* Check LINE against HISTCONTROL and add it to the history if it's OK.
94  Returns 1 if the line was saved in the history, 0 otherwise. */
95 
96 int
97 octave_add_history (const char *line, int history_control)
98 {
99  if (check_history_control (line, history_control))
100  {
101  /* We're committed to saving the line. If the user has requested it,
102  remove other matching lines from the history. */
103 
104  if (history_control & HC_ERASEDUPS)
105  hc_erasedups (line);
106 
107  add_history (line);
108 
109  return 1;
110  }
111 
112  return 0;
113 }
114 
115 int
117 {
118  return where_history ();
119 }
120 
121 int
123 {
124  return history_length;
125 }
126 
127 int
129 {
130  return max_input_history;
131 }
132 
133 int
134 octave_history_base (void)
135 {
136  return history_base;
137 }
138 
139 void
140 octave_stifle_history (int n)
141 {
142  stifle_history (n);
143 }
144 
145 int
147 {
148  return unstifle_history ();
149 }
150 
151 int
153 {
154  return history_is_stifled ();
155 }
156 
157 int
159 {
160  return history_set_pos (n);
161 }
162 
163 int
164 octave_read_history (const char *f)
165 {
166  return read_history (f);
167 }
168 
169 void
171 {
172  using_history ();
173 }
174 
175 int
176 octave_read_history_range (const char *f, int b, int e)
177 {
178  return read_history_range (f, b, e);
179 }
180 
181 int
182 octave_write_history (const char *f)
183 {
184  return write_history (f);
185 }
186 
187 int
188 octave_append_history (int n, const char *f)
189 {
190  return append_history (n, f);
191 }
192 
193 int
194 octave_history_truncate_file (const char *f, int n)
195 {
196  return history_truncate_file (f, n);
197 }
198 
199 void
200 octave_remove_history (int n)
201 {
202  HIST_ENTRY *discard = remove_history (n);
203 
204  if (discard)
205  free (discard->line);
206 
207  free (discard);
208 }
209 
210 void
212 {
213  clear_history ();
214 }
215 
216 char *
218 {
219  HIST_ENTRY *h;
220 
221  char *retval = 0;
222 
223  if (history_set_pos (n))
224  {
225  h = current_history ();
226 
227  if (h)
228  retval = h->line;
229  }
230 
231  return retval;
232 }
233 
234 char *
235 octave_history_get (int n)
236 {
237  char *retval = 0;
238 
239  HIST_ENTRY *h = history_get (n);
240 
241  if (h)
242  retval = h->line;
243 
244  return retval;
245 }
246 
247 char **
248 octave_history_list (int limit, int number_lines)
249 {
250  static char **retval = 0;
251 
252  HIST_ENTRY **hlist = 0;
253 
254  if (retval)
255  {
256  char **p = retval;
257 
258  while (*p)
259  free (*p++);
260 
261  free (retval);
262 
263  retval = 0;
264  }
265 
266  hlist = history_list ();
267 
268  if (hlist)
269  {
270  int i, k;
271 
272  int beg = 0;
273  int end = 0;
274  while (hlist[end])
275  end++;
276 
277  beg = (limit < 0 || end < limit) ? 0 : (end - limit);
278 
279  retval = malloc ((size_t) (end - beg + 1) * sizeof (char **));
280 
281  k = 0;
282  for (i = beg; i < end; i++)
283  {
284  char *line = hlist[i]->line;
285  size_t len = line ? strlen (line) : 0;
286  char *tmp = malloc (len + 64);
287 
288  if (number_lines)
289  sprintf (tmp, "%5d %s", i + history_base,
290  line ? line : "");
291  else
292  strcpy (tmp, line ? line : "");
293 
294  retval[k++] = tmp;
295  }
296 
297  retval[k] = 0;
298  }
299 
300  return retval;
301 }
302 
303 void
304 octave_replace_history_entry (int which, const char *line)
305 {
306  HIST_ENTRY *discard = replace_history_entry (which, line, 0);
307 
308  if (discard)
309  free (discard->line);
310 
311  free (discard);
312 }
313 
314 #endif
void octave_replace_history_entry(int, const char *)
void octave_stifle_history(int)
char * octave_history_get(int n)
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE * f
int octave_history_is_stifled(void)
for large enough k
Definition: lu.cc:617
int octave_add_history(const char *, int)
int octave_history_truncate_file(const char *, int)
int octave_max_input_history(void)
int octave_append_history(int, const char *)
void octave_remove_history(int)
i e
Definition: data.cc:2591
char * octave_history_goto_mark(int n)
int octave_history_length(void)
double h
Definition: graphics.cc:11808
int octave_history_base(void)
bool strcmp(const T &str_a, const T &str_b)
True if strings are the same.
Definition: oct-string.cc:112
double tmp
Definition: data.cc:6252
octave_value retval
Definition: data.cc:6246
int octave_read_history(const char *)
T::size_type strlen(const typename T::value_type *str)
Definition: oct-string.cc:75
int octave_where_history(void)
char ** octave_history_list(int, int)
void octave_using_history(void)
int octave_read_history_range(const char *, int, int)
p
Definition: lu.cc:138
b
Definition: cellfun.cc:400
int octave_history_set_pos(int)
for i
Definition: data.cc:5264
int octave_write_history(const char *)
void octave_clear_history(void)
line(const graphics_handle &mh, const graphics_handle &p)
Definition: graphics.in.h:4357
int octave_unstifle_history(void)