1 /* readline.c --- line editing support for Guile */
3 /* Copyright (C) 1997,1999,2000,2001, 2002, 2003, 2006, 2007, 2008,
4 * 2009, 2010, 2013 Free Software Foundation, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this software; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
29 #ifdef HAVE_RL_GETC_FUNCTION
34 #include <readline/readline.h>
35 #include <readline/history.h>
37 #include <sys/select.h>
40 #include "libguile/validate.h"
41 #include "guile-readline/readline.h"
43 scm_t_option scm_readline_opts
[] = {
44 { SCM_OPTION_BOOLEAN
, "history-file", 1,
45 "Use history file." },
46 { SCM_OPTION_INTEGER
, "history-length", 200,
48 { SCM_OPTION_INTEGER
, "bounce-parens", 500,
49 "Time (ms) to show matching opening parenthesis (0 = off)."},
53 extern void stifle_history (int max
);
55 SCM_DEFINE (scm_readline_options
, "readline-options-interface", 0, 1, 0,
58 #define FUNC_NAME s_scm_readline_options
60 SCM ans
= scm_options (setting
,
63 stifle_history (SCM_HISTORY_LENGTH
);
72 size_t len
= strlen (s
);
73 char *new = malloc (len
+ 1);
77 #endif /* HAVE_STRDUP */
79 #ifndef HAVE_RL_CLEANUP_AFTER_SIGNAL
81 /* These are readline functions added in release 2.3. They will work
82 * together with readline-2.1 and 2.2. (The readline interface is
83 * disabled for earlier releases.)
84 * They are declared static; if we want to use them elsewhere, then
85 * we need external declarations for them, but at the moment, I don't
86 * think anything else in Guile ought to use these.
89 extern void _rl_clean_up_for_exit ();
90 extern void _rl_kill_kbd_macro ();
91 extern int _rl_init_argument ();
94 rl_cleanup_after_signal ()
96 #ifdef HAVE_RL_CLEAR_SIGNALS
97 _rl_clean_up_for_exit ();
99 (*rl_deprep_term_function
) ();
100 #ifdef HAVE_RL_CLEAR_SIGNALS
103 rl_pending_input
= 0;
107 rl_free_line_state ()
109 register HIST_ENTRY
*entry
;
113 entry
= current_history ();
115 entry
->data
= (char *)NULL
;
117 _rl_kill_kbd_macro ();
119 _rl_init_argument ();
122 #endif /* !HAVE_RL_CLEANUP_AFTER_SIGNAL */
125 static SCM input_port
;
126 static SCM output_port
;
127 static SCM before_read
;
130 current_input_getc (FILE *in SCM_UNUSED
)
132 if (promptp
&& scm_is_true (before_read
))
134 scm_apply (before_read
, SCM_EOL
, SCM_EOL
);
137 return scm_get_byte_or_eof (input_port
);
140 static int in_readline
= 0;
141 static SCM reentry_barrier_mutex
;
143 static SCM
internal_readline (SCM text
);
144 static void unwind_readline (void *unused
);
145 static void reentry_barrier (void);
148 SCM_DEFINE (scm_readline
, "%readline", 0, 4, 0,
149 (SCM text
, SCM inp
, SCM outp
, SCM read_hook
),
151 #define FUNC_NAME s_scm_readline
157 before_read
= SCM_BOOL_F
;
159 if (!SCM_UNBNDP (text
))
161 if (!scm_is_string (text
))
164 scm_wrong_type_arg (s_scm_readline
, SCM_ARG1
, text
);
168 if (!((SCM_UNBNDP (inp
) && SCM_OPINFPORTP (scm_current_input_port ()))
169 || SCM_OPINFPORTP (inp
)))
172 scm_misc_error (s_scm_readline
,
173 "Input port is not open or not a file port",
177 if (!((SCM_UNBNDP (outp
) && SCM_OPOUTFPORTP (scm_current_output_port ()))
178 || SCM_OPOUTFPORTP (outp
)))
181 scm_misc_error (s_scm_readline
,
182 "Output port is not open or not a file port",
186 if (!(SCM_UNBNDP (read_hook
) || scm_is_false (read_hook
)))
188 if (scm_is_false (scm_thunk_p (read_hook
)))
191 scm_wrong_type_arg (s_scm_readline
, SCM_ARG4
, read_hook
);
193 before_read
= read_hook
;
196 scm_readline_init_ports (inp
, outp
);
198 scm_dynwind_begin (0);
199 scm_dynwind_unwind_handler (unwind_readline
, NULL
, 0);
201 ans
= internal_readline (text
);
205 fclose (rl_instream
);
206 fclose (rl_outstream
);
218 /* We should rather use scm_try_mutex when it becomes available */
219 scm_lock_mutex (reentry_barrier_mutex
);
224 scm_unlock_mutex (reentry_barrier_mutex
);
226 scm_misc_error (s_scm_readline
, "readline is not reentrant", SCM_EOL
);
229 /* This function is only called on nonlocal exit from readline(). */
231 unwind_readline (void *unused
)
233 rl_free_line_state ();
234 rl_cleanup_after_signal ();
235 fputc ('\n', rl_outstream
); /* We don't want next output on this line */
236 fclose (rl_instream
);
237 fclose (rl_outstream
);
242 internal_readline (SCM text
)
246 char *prompt
= SCM_UNBNDP (text
) ? "" : scm_to_locale_string (text
);
249 s
= readline (prompt
);
251 ret
= scm_from_port_string (s
, output_port
);
255 if (!SCM_UNBNDP (text
))
263 stream_from_fport (SCM port
, char *mode
, const char *subr
)
268 fd
= dup (((struct scm_t_fport
*) SCM_STREAM (port
))->fdes
);
275 f
= fdopen (fd
, mode
);
286 scm_readline_init_ports (SCM inp
, SCM outp
)
288 if (SCM_UNBNDP (inp
))
289 inp
= scm_current_input_port ();
291 if (SCM_UNBNDP (outp
))
292 outp
= scm_current_output_port ();
294 if (!SCM_OPINFPORTP (inp
)) {
296 "Input port is not open or not a file port",
300 if (!SCM_OPOUTFPORTP (outp
)) {
302 "Output port is not open or not a file port",
308 rl_instream
= stream_from_fport (inp
, "r", s_scm_readline
);
309 rl_outstream
= stream_from_fport (outp
, "w", s_scm_readline
);
314 SCM_DEFINE (scm_add_history
, "add-history", 1, 0, 0,
317 #define FUNC_NAME s_scm_add_history
321 s
= scm_to_locale_string (text
);
325 return SCM_UNSPECIFIED
;
330 SCM_DEFINE (scm_read_history
, "read-history", 1, 0, 0,
333 #define FUNC_NAME s_scm_read_history
338 filename
= scm_to_locale_string (file
);
339 ret
= scm_from_bool (!read_history (filename
));
346 SCM_DEFINE (scm_write_history
, "write-history", 1, 0, 0,
349 #define FUNC_NAME s_scm_write_history
354 filename
= scm_to_locale_string (file
);
355 ret
= scm_from_bool (!write_history (filename
));
361 SCM_DEFINE (scm_clear_history
, "clear-history", 0, 0, 0,
363 "Clear the history buffer of the readline machinery.")
364 #define FUNC_NAME s_scm_clear_history
367 return SCM_UNSPECIFIED
;
372 SCM_DEFINE (scm_filename_completion_function
, "filename-completion-function", 2, 0, 0,
373 (SCM text
, SCM continuep
),
375 #define FUNC_NAME s_scm_filename_completion_function
379 char *c_text
= scm_to_locale_string (text
);
380 #ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION
381 s
= rl_filename_completion_function (c_text
, scm_is_true (continuep
));
383 s
= filename_completion_function (c_text
, scm_is_true (continuep
));
385 ans
= scm_take_locale_string (s
);
392 * The following has been modified from code contributed by
393 * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
396 SCM scm_readline_completion_function_var
;
399 completion_function (char *text
, int continuep
)
401 SCM compfunc
= SCM_VARIABLE_REF (scm_readline_completion_function_var
);
404 if (scm_is_false (compfunc
))
405 return NULL
; /* #f => completion disabled */
408 SCM t
= scm_from_locale_string (text
);
409 SCM c
= scm_from_bool (continuep
);
410 res
= scm_apply (compfunc
, scm_list_2 (t
, c
), SCM_EOL
);
412 if (scm_is_false (res
))
415 return scm_to_locale_string (res
);
419 #if HAVE_RL_GET_KEYMAP
420 /*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
422 static int match_paren (int x
, int k
);
423 static int find_matching_paren (int k
);
424 static void init_bouncing_parens ();
427 init_bouncing_parens ()
429 if (strncmp (rl_get_keymap_name (rl_get_keymap ()), "vi", 2))
431 rl_bind_key (')', match_paren
);
432 rl_bind_key (']', match_paren
);
433 rl_bind_key ('}', match_paren
);
438 find_matching_paren(int k
)
442 int end_parens_found
= 0;
444 /* Choose the corresponding opening bracket. */
445 if (k
== ')') c
= '(';
446 else if (k
== ']') c
= '[';
447 else if (k
== '}') c
= '{';
449 for (i
=rl_point
-2; i
>=0; i
--)
451 /* Is the current character part of a character literal? */
453 && rl_line_buffer
[i
- 1] == '\\'
454 && rl_line_buffer
[i
- 2] == '#')
456 else if (rl_line_buffer
[i
] == k
)
458 else if (rl_line_buffer
[i
] == '"')
460 /* Skip over a string literal. */
461 for (i
--; i
>= 0; i
--)
462 if (rl_line_buffer
[i
] == '"'
464 && rl_line_buffer
[i
- 1] == '\\'))
467 else if (rl_line_buffer
[i
] == c
)
469 if (end_parens_found
==0)
471 else --end_parens_found
;
478 match_paren (int x
, int k
)
483 struct timeval timeout
;
486 if (!SCM_READLINE_BOUNCE_PARENS
)
489 /* Did we just insert a quoted paren? If so, then don't bounce. */
490 if (rl_point
- 1 >= 1
491 && rl_line_buffer
[rl_point
- 2] == '\\')
494 tmp
= 1000 * SCM_READLINE_BOUNCE_PARENS
;
495 timeout
.tv_sec
= tmp
/ 1000000;
496 timeout
.tv_usec
= tmp
% 1000000;
498 fno
= fileno (rl_instream
);
499 FD_SET (fno
, &readset
);
504 rl_point
= find_matching_paren (k
);
508 select (fno
+ 1, &readset
, NULL
, NULL
, &timeout
);
514 #endif /* HAVE_RL_GET_KEYMAP */
516 #endif /* HAVE_RL_GETC_FUNCTION */
521 #ifdef HAVE_RL_GETC_FUNCTION
522 #include "guile-readline/readline.x"
523 scm_readline_completion_function_var
524 = scm_c_define ("*readline-completion-function*", SCM_BOOL_F
);
525 rl_getc_function
= current_input_getc
;
526 #if defined (_RL_FUNCTION_TYPEDEF)
527 rl_completion_entry_function
= (rl_compentry_func_t
*) completion_function
;
529 rl_completion_entry_function
= (Function
*) completion_function
;
531 rl_basic_word_break_characters
= " \t\n\"'`;()";
532 rl_readline_name
= "Guile";
534 /* Let Guile handle signals. */
535 #if defined (HAVE_DECL_RL_CATCH_SIGNALS) && HAVE_DECL_RL_CATCH_SIGNALS
536 rl_catch_signals
= 0;
539 /* But let readline handle SIGWINCH. */
540 #if defined (HAVE_DECL_RL_CATCH_SIGWINCH) && HAVE_DECL_RL_CATCH_SIGWINCH
541 rl_catch_sigwinch
= 1;
544 reentry_barrier_mutex
= scm_make_mutex ();
545 scm_init_opts (scm_readline_options
,
547 #if HAVE_RL_GET_KEYMAP
548 init_bouncing_parens();
550 scm_add_feature ("readline");
551 #endif /* HAVE_RL_GETC_FUNCTION */