1 /* readline.c --- line editing support for Guile */
3 /* Copyright (C) 1997,1999,2000,2001, 2002, 2003, 2006, 2007, 2008, 2009, 2010, 2013 Free Software Foundation, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software; see the file COPYING. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
28 #ifdef HAVE_RL_GETC_FUNCTION
33 #include <readline/readline.h>
34 #include <readline/history.h>
36 #include <sys/select.h>
39 #include "libguile/validate.h"
40 #include "guile-readline/readline.h"
42 scm_t_option scm_readline_opts
[] = {
43 { SCM_OPTION_BOOLEAN
, "history-file", 1,
44 "Use history file." },
45 { SCM_OPTION_INTEGER
, "history-length", 200,
47 { SCM_OPTION_INTEGER
, "bounce-parens", 500,
48 "Time (ms) to show matching opening parenthesis (0 = off)."},
52 extern void stifle_history (int max
);
54 SCM_DEFINE (scm_readline_options
, "readline-options-interface", 0, 1, 0,
57 #define FUNC_NAME s_scm_readline_options
59 SCM ans
= scm_options (setting
,
62 stifle_history (SCM_HISTORY_LENGTH
);
71 size_t len
= strlen (s
);
72 char *new = malloc (len
+ 1);
76 #endif /* HAVE_STRDUP */
78 #ifndef HAVE_RL_CLEANUP_AFTER_SIGNAL
80 /* These are readline functions added in release 2.3. They will work
81 * together with readline-2.1 and 2.2. (The readline interface is
82 * disabled for earlier releases.)
83 * They are declared static; if we want to use them elsewhere, then
84 * we need external declarations for them, but at the moment, I don't
85 * think anything else in Guile ought to use these.
88 extern void _rl_clean_up_for_exit ();
89 extern void _rl_kill_kbd_macro ();
90 extern int _rl_init_argument ();
93 rl_cleanup_after_signal ()
95 #ifdef HAVE_RL_CLEAR_SIGNALS
96 _rl_clean_up_for_exit ();
98 (*rl_deprep_term_function
) ();
99 #ifdef HAVE_RL_CLEAR_SIGNALS
102 rl_pending_input
= 0;
106 rl_free_line_state ()
108 register HIST_ENTRY
*entry
;
112 entry
= current_history ();
114 entry
->data
= (char *)NULL
;
116 _rl_kill_kbd_macro ();
118 _rl_init_argument ();
121 #endif /* !HAVE_RL_CLEANUP_AFTER_SIGNAL */
124 static SCM input_port
;
125 static SCM output_port
;
126 static SCM before_read
;
129 current_input_getc (FILE *in SCM_UNUSED
)
131 if (promptp
&& scm_is_true (before_read
))
133 scm_apply (before_read
, SCM_EOL
, SCM_EOL
);
136 return scm_get_byte_or_eof (input_port
);
139 static int in_readline
= 0;
140 static SCM reentry_barrier_mutex
;
142 static SCM
internal_readline (SCM text
);
143 static void unwind_readline (void *unused
);
144 static void reentry_barrier (void);
147 SCM_DEFINE (scm_readline
, "%readline", 0, 4, 0,
148 (SCM text
, SCM inp
, SCM outp
, SCM read_hook
),
150 #define FUNC_NAME s_scm_readline
156 before_read
= SCM_BOOL_F
;
158 if (!SCM_UNBNDP (text
))
160 if (!scm_is_string (text
))
163 scm_wrong_type_arg (s_scm_readline
, SCM_ARG1
, text
);
167 if (!((SCM_UNBNDP (inp
) && SCM_OPINFPORTP (scm_current_input_port ()))
168 || SCM_OPINFPORTP (inp
)))
171 scm_misc_error (s_scm_readline
,
172 "Input port is not open or not a file port",
176 if (!((SCM_UNBNDP (outp
) && SCM_OPOUTFPORTP (scm_current_output_port ()))
177 || SCM_OPOUTFPORTP (outp
)))
180 scm_misc_error (s_scm_readline
,
181 "Output port is not open or not a file port",
185 if (!(SCM_UNBNDP (read_hook
) || scm_is_false (read_hook
)))
187 if (scm_is_false (scm_thunk_p (read_hook
)))
190 scm_wrong_type_arg (s_scm_readline
, SCM_ARG4
, read_hook
);
192 before_read
= read_hook
;
195 scm_readline_init_ports (inp
, outp
);
197 scm_dynwind_begin (0);
198 scm_dynwind_unwind_handler (unwind_readline
, NULL
, 0);
200 ans
= internal_readline (text
);
204 fclose (rl_instream
);
205 fclose (rl_outstream
);
217 /* We should rather use scm_try_mutex when it becomes available */
218 scm_lock_mutex (reentry_barrier_mutex
);
223 scm_unlock_mutex (reentry_barrier_mutex
);
225 scm_misc_error (s_scm_readline
, "readline is not reentrant", SCM_EOL
);
228 /* This function is only called on nonlocal exit from readline(). */
230 unwind_readline (void *unused
)
232 rl_free_line_state ();
233 rl_cleanup_after_signal ();
234 fputc ('\n', rl_outstream
); /* We don't want next output on this line */
235 fclose (rl_instream
);
236 fclose (rl_outstream
);
241 internal_readline (SCM text
)
245 char *prompt
= SCM_UNBNDP (text
) ? "" : scm_to_locale_string (text
);
248 s
= readline (prompt
);
251 scm_t_port
*pt
= SCM_PTAB_ENTRY (output_port
);
253 ret
= scm_from_stringn (s
, strlen (s
), pt
->encoding
,
254 SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE
);
259 if (!SCM_UNBNDP (text
))
267 stream_from_fport (SCM port
, char *mode
, const char *subr
)
272 fd
= dup (((struct scm_t_fport
*) SCM_STREAM (port
))->fdes
);
279 f
= fdopen (fd
, mode
);
290 scm_readline_init_ports (SCM inp
, SCM outp
)
292 if (SCM_UNBNDP (inp
))
293 inp
= scm_current_input_port ();
295 if (SCM_UNBNDP (outp
))
296 outp
= scm_current_output_port ();
298 if (!SCM_OPINFPORTP (inp
)) {
300 "Input port is not open or not a file port",
304 if (!SCM_OPOUTFPORTP (outp
)) {
306 "Output port is not open or not a file port",
312 rl_instream
= stream_from_fport (inp
, "r", s_scm_readline
);
313 rl_outstream
= stream_from_fport (outp
, "w", s_scm_readline
);
318 SCM_DEFINE (scm_add_history
, "add-history", 1, 0, 0,
321 #define FUNC_NAME s_scm_add_history
325 s
= scm_to_locale_string (text
);
329 return SCM_UNSPECIFIED
;
334 SCM_DEFINE (scm_read_history
, "read-history", 1, 0, 0,
337 #define FUNC_NAME s_scm_read_history
342 filename
= scm_to_locale_string (file
);
343 ret
= scm_from_bool (!read_history (filename
));
350 SCM_DEFINE (scm_write_history
, "write-history", 1, 0, 0,
353 #define FUNC_NAME s_scm_write_history
358 filename
= scm_to_locale_string (file
);
359 ret
= scm_from_bool (!write_history (filename
));
365 SCM_DEFINE (scm_clear_history
, "clear-history", 0, 0, 0,
367 "Clear the history buffer of the readline machinery.")
368 #define FUNC_NAME s_scm_clear_history
371 return SCM_UNSPECIFIED
;
376 SCM_DEFINE (scm_filename_completion_function
, "filename-completion-function", 2, 0, 0,
377 (SCM text
, SCM continuep
),
379 #define FUNC_NAME s_scm_filename_completion_function
383 char *c_text
= scm_to_locale_string (text
);
384 #ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION
385 s
= rl_filename_completion_function (c_text
, scm_is_true (continuep
));
387 s
= filename_completion_function (c_text
, scm_is_true (continuep
));
389 ans
= scm_take_locale_string (s
);
396 * The following has been modified from code contributed by
397 * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
400 SCM scm_readline_completion_function_var
;
403 completion_function (char *text
, int continuep
)
405 SCM compfunc
= SCM_VARIABLE_REF (scm_readline_completion_function_var
);
408 if (scm_is_false (compfunc
))
409 return NULL
; /* #f => completion disabled */
412 SCM t
= scm_from_locale_string (text
);
413 SCM c
= scm_from_bool (continuep
);
414 res
= scm_apply (compfunc
, scm_list_2 (t
, c
), SCM_EOL
);
416 if (scm_is_false (res
))
419 return scm_to_locale_string (res
);
423 #if HAVE_RL_GET_KEYMAP
424 /*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
426 static int match_paren (int x
, int k
);
427 static int find_matching_paren (int k
);
428 static void init_bouncing_parens ();
431 init_bouncing_parens ()
433 if (strncmp (rl_get_keymap_name (rl_get_keymap ()), "vi", 2))
435 rl_bind_key (')', match_paren
);
436 rl_bind_key (']', match_paren
);
437 rl_bind_key ('}', match_paren
);
442 find_matching_paren(int k
)
446 int end_parens_found
= 0;
448 /* Choose the corresponding opening bracket. */
449 if (k
== ')') c
= '(';
450 else if (k
== ']') c
= '[';
451 else if (k
== '}') c
= '{';
453 for (i
=rl_point
-2; i
>=0; i
--)
455 /* Is the current character part of a character literal? */
457 && rl_line_buffer
[i
- 1] == '\\'
458 && rl_line_buffer
[i
- 2] == '#')
460 else if (rl_line_buffer
[i
] == k
)
462 else if (rl_line_buffer
[i
] == '"')
464 /* Skip over a string literal. */
465 for (i
--; i
>= 0; i
--)
466 if (rl_line_buffer
[i
] == '"'
468 && rl_line_buffer
[i
- 1] == '\\'))
471 else if (rl_line_buffer
[i
] == c
)
473 if (end_parens_found
==0)
475 else --end_parens_found
;
482 match_paren (int x
, int k
)
487 struct timeval timeout
;
490 if (!SCM_READLINE_BOUNCE_PARENS
)
493 /* Did we just insert a quoted paren? If so, then don't bounce. */
494 if (rl_point
- 1 >= 1
495 && rl_line_buffer
[rl_point
- 2] == '\\')
498 tmp
= 1000 * SCM_READLINE_BOUNCE_PARENS
;
499 timeout
.tv_sec
= tmp
/ 1000000;
500 timeout
.tv_usec
= tmp
% 1000000;
502 fno
= fileno (rl_instream
);
503 FD_SET (fno
, &readset
);
508 rl_point
= find_matching_paren (k
);
512 select (fno
+ 1, &readset
, NULL
, NULL
, &timeout
);
518 #endif /* HAVE_RL_GET_KEYMAP */
520 #endif /* HAVE_RL_GETC_FUNCTION */
525 #ifdef HAVE_RL_GETC_FUNCTION
526 #include "guile-readline/readline.x"
527 scm_readline_completion_function_var
528 = scm_c_define ("*readline-completion-function*", SCM_BOOL_F
);
529 rl_getc_function
= current_input_getc
;
530 #if defined (_RL_FUNCTION_TYPEDEF)
531 rl_completion_entry_function
= (rl_compentry_func_t
*) completion_function
;
533 rl_completion_entry_function
= (Function
*) completion_function
;
535 rl_basic_word_break_characters
= " \t\n\"'`;()";
536 rl_readline_name
= "Guile";
538 /* Let Guile handle signals. */
539 #if defined (HAVE_DECL_RL_CATCH_SIGNALS) && HAVE_DECL_RL_CATCH_SIGNALS
540 rl_catch_signals
= 0;
543 /* But let readline handle SIGWINCH. */
544 #if defined (HAVE_DECL_RL_CATCH_SIGWINCH) && HAVE_DECL_RL_CATCH_SIGWINCH
545 rl_catch_sigwinch
= 1;
548 reentry_barrier_mutex
= scm_make_mutex ();
549 scm_init_opts (scm_readline_options
,
551 #if HAVE_RL_GET_KEYMAP
552 init_bouncing_parens();
554 scm_add_feature ("readline");
555 #endif /* HAVE_RL_GETC_FUNCTION */