GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-rl-edit.c
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2000-2013 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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
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/readline.h>
33 
34 #include "oct-rl-edit.h"
35 
36 #define OCTAVE_RL_SAVE_STRING(ss, s) \
37  static char *ss = 0; \
38  \
39  if (ss) \
40  { \
41  free (ss); \
42  ss = 0; \
43  } \
44  \
45  ss = malloc (strlen (s) + 1); \
46  \
47  strcpy (ss, s)
48 
49 void
51 {
52  rl_redisplay ();
53 }
54 
55 int
57 {
58  int rows, cols;
59  rl_get_screen_size (&rows, &cols);
60  return rows;
61 }
62 
63 int
65 {
66  int rows, cols;
67  rl_get_screen_size (&rows, &cols);
68  return cols;
69 }
70 
71 void
73 {
74  rl_variable_bind ("blink-matching-paren", val ? "1" : "0");
75 }
76 
77 /* It would be much simpler if we could just call _rl_clear_screen to
78  only clear the screen, but it is not a public function, and on some
79  systems, it is not exported from shared library versions of
80  readline, so we can't use it.
81 
82  Instead, temporarily redefine the redisplay function to do nothing.
83 
84  FIXME -- It would be safer to do this when protected from
85  interrupts... */
86 
87 static void
88 flush_stdout (void)
89 {
90  fflush (stdout);
91 }
92 
93 void
94 octave_rl_clear_screen (int skip_redisplay)
95 {
96  int ignore1 = 0;
97  int ignore2 = 0;
98 
99  if (skip_redisplay)
100  {
101  rl_voidfunc_t *saved_redisplay_function = rl_redisplay_function;
102 
103  rl_redisplay_function = flush_stdout;
104 
105  rl_clear_screen (ignore1, ignore2);
106 
107  rl_redisplay_function = saved_redisplay_function;
108  }
109  else
110  rl_clear_screen (ignore1, ignore2);
111 }
112 
113 void
115 {
116  rl_resize_terminal ();
117 }
118 
119 void
121 {
122  if (rl_deprep_term_function)
123  rl_deprep_term_function ();
124 }
125 
126 char *
127 octave_rl_copy_line (void)
128 {
129  return rl_copy_text (0, rl_end);
130 }
131 
132 void
133 octave_rl_replace_line (const char *s, int clear_undo)
134 {
135  rl_replace_line (s, clear_undo);
136 }
137 
138 void
139 octave_rl_insert_text (const char *s)
140 {
141  rl_insert_text (s);
142 }
143 
144 int
145 octave_rl_newline (int count, int key)
146 {
147  return rl_newline (count, key);
148 }
149 
150 const char *
152 {
153  return rl_line_buffer;
154 }
155 
156 int
157 octave_rl_do_undo (void)
158 {
159  return rl_do_undo ();
160 }
161 
162 void
164 {
165  if (rl_undo_list)
166  {
167  rl_free_undo_list ();
168 
169  rl_undo_list = 0;
170  }
171 }
172 
173 void
174 octave_rl_set_name (const char *n)
175 {
176  OCTAVE_RL_SAVE_STRING (nm, n);
177 
178  rl_readline_name = nm;
179 
180  /* Since we've already called rl_initialize, we need to re-read the
181  init file to take advantage of the conditional parsing feature
182  based on rl_readline_name; */
183 
184  rl_re_read_init_file (0, 0);
185 }
186 
187 char *
188 octave_rl_readline (const char *prompt)
189 {
190  return readline (prompt);
191 }
192 
193 void
195 {
196  rl_instream = f;
197 }
198 
199 FILE *
201 {
202  return rl_instream;
203 }
204 
205 void
207 {
208  rl_outstream = f;
209 }
210 
211 FILE *
213 {
214  return rl_outstream;
215 }
216 
217 void
218 octave_rl_read_init_file (const char *f)
219 {
220  rl_read_init_file (f);
221 }
222 
223 void
225 {
226  rl_re_read_init_file (0, 0);
227 }
228 
229 int
231 {
232  int retval = rl_filename_completion_desired;
233  rl_filename_completion_desired = arg;
234  return retval;
235 }
236 
237 int
239 {
240  int retval = rl_filename_quoting_desired;
241  rl_filename_quoting_desired = arg;
242  return retval;
243 }
244 
245 void
246 octave_rl_done (int arg)
247 {
248  rl_done = arg;
249 }
250 
251 char *
253 {
254  return rl_filename_completion_function (text, state);
255 }
256 
257 void
259 {
260  OCTAVE_RL_SAVE_STRING (ss, s);
261 
262  rl_basic_word_break_characters = ss;
263 }
264 
265 void
267 {
268  OCTAVE_RL_SAVE_STRING (ss, s);
269 
270  rl_completer_word_break_characters = ss;
271 }
272 
273 void
275 {
276  OCTAVE_RL_SAVE_STRING (ss, s);
277 
278  rl_basic_quote_characters = ss;
279 }
280 
281 void
283 {
284  OCTAVE_RL_SAVE_STRING (ss, s);
285 
286  rl_filename_quote_characters = ss;
287 }
288 
289 void
291 {
292  OCTAVE_RL_SAVE_STRING (ss, s);
293 
294  rl_completer_quote_characters = ss;
295 }
296 
297 void
299 {
300  rl_completion_append_character = c;
301 }
302 
303 void
305 {
306  rl_attempted_completion_function = f;
307 }
308 
309 void
311 {
312  rl_filename_quoting_function = f;
313 }
314 
315 void
317 {
318  rl_filename_dequoting_function = f;
319 }
320 
321 void
323 {
324  rl_char_is_quoted_p = f;
325 }
326 
327 void
329 {
330  rl_startup_hook = f;
331 }
332 
335 {
336  return rl_startup_hook;
337 }
338 
339 void
341 {
342  rl_pre_input_hook = f;
343 }
344 
347 {
348  return rl_pre_input_hook;
349 }
350 
351 void
353 {
354  rl_event_hook = f;
355 }
356 
359 {
360  return rl_event_hook;
361 }
362 
363 char **
365 {
366  return rl_completion_matches (text, f);
367 }
368 
369 char
371 {
372  return RL_PROMPT_START_IGNORE;
373 }
374 
375 char
377 {
378  return RL_PROMPT_END_IGNORE;
379 }
380 
381 void
382 octave_rl_add_defun (const char *name, rl_fcn_ptr f, char key)
383 {
384  rl_add_defun (name, f, key);
385 }
386 
387 void
388 octave_rl_set_terminal_name (const char *term)
389 {
390  OCTAVE_RL_SAVE_STRING (saved_term, term);
391 
392  rl_terminal_name = saved_term;
393 }
394 
395 void
397 {
398 #if defined (__WIN32__) && ! defined (__CYGWIN__)
399  rl_catch_signals = 0;
400 #endif
401 
402  rl_initialize ();
403 }
404 
405 int
406 octave_rl_history_search_forward (int count, int ignore)
407 {
408  return rl_history_search_forward (count, ignore);
409 }
410 
411 int
412 octave_rl_history_search_backward (int count, int ignore)
413 {
414  return rl_history_search_backward (count, ignore);
415 }
416 
417 char
418 octave_rl_ctrl (char c)
419 {
420  return CTRL (c);
421 }
422 
423 char
424 octave_rl_meta (char c)
425 {
426  return META (c);
427 }
428 
429 #endif