1 /* readline.c --- line editing support for Guile */
3 /* Copyright (C) 1997,1999,2000,2001, 2002, 2003 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 2, 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., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307 USA
29 #include "libguile/_scm.h"
30 #ifdef HAVE_RL_GETC_FUNCTION
32 #include "libguile/gh.h"
33 #include "libguile/iselect.h"
39 #include <readline/readline.h>
40 #include <readline/history.h>
48 #include "libguile/validate.h"
49 #include "guile-readline/readline.h"
51 scm_t_option scm_readline_opts
[] = {
52 { SCM_OPTION_BOOLEAN
, "history-file", 1,
53 "Use history file." },
54 { SCM_OPTION_INTEGER
, "history-length", 200,
56 { SCM_OPTION_INTEGER
, "bounce-parens", 500,
57 "Time (ms) to show matching opening parenthesis (0 = off)."}
60 extern void stifle_history (int max
);
62 SCM_DEFINE (scm_readline_options
, "readline-options-interface", 0, 1, 0,
65 #define FUNC_NAME s_scm_readline_options
67 SCM ans
= scm_options (setting
,
69 SCM_N_READLINE_OPTIONS
,
71 stifle_history (SCM_HISTORY_LENGTH
);
80 size_t len
= strlen (s
);
81 char *new = malloc (len
+ 1);
85 #endif /* HAVE_STRDUP */
87 #ifndef HAVE_RL_CLEANUP_AFTER_SIGNAL
89 /* These are readline functions added in release 2.3. They will work
90 * together with readline-2.1 and 2.2. (The readline interface is
91 * disabled for earlier releases.)
92 * They are declared static; if we want to use them elsewhere, then
93 * we need external declarations for them, but at the moment, I don't
94 * think anything else in Guile ought to use these.
97 extern void _rl_clean_up_for_exit ();
98 extern void _rl_kill_kbd_macro ();
99 extern int _rl_init_argument ();
102 rl_cleanup_after_signal ()
104 #ifdef HAVE_RL_CLEAR_SIGNALS
105 _rl_clean_up_for_exit ();
107 (*rl_deprep_term_function
) ();
108 #ifdef HAVE_RL_CLEAR_SIGNALS
111 rl_pending_input
= 0;
115 rl_free_line_state ()
117 register HIST_ENTRY
*entry
;
121 entry
= current_history ();
123 entry
->data
= (char *)NULL
;
125 _rl_kill_kbd_macro ();
127 _rl_init_argument ();
130 #endif /* !HAVE_RL_CLEANUP_AFTER_SIGNAL */
133 static SCM input_port
;
134 static SCM before_read
;
137 current_input_getc (FILE *in SCM_UNUSED
)
139 if (promptp
&& scm_is_true (before_read
))
141 scm_apply (before_read
, SCM_EOL
, SCM_EOL
);
144 return scm_getc (input_port
);
147 static int in_readline
= 0;
148 static SCM reentry_barrier_mutex
;
150 static SCM
internal_readline (SCM text
);
151 static SCM
handle_error (void *data
, SCM tag
, SCM args
);
152 static void reentry_barrier (void);
155 SCM_DEFINE (scm_readline
, "%readline", 0, 4, 0,
156 (SCM text
, SCM inp
, SCM outp
, SCM read_hook
),
158 #define FUNC_NAME s_scm_readline
164 before_read
= SCM_BOOL_F
;
166 if (!SCM_UNBNDP (text
))
168 if (!SCM_STRINGP (text
))
171 scm_wrong_type_arg (s_scm_readline
, SCM_ARG1
, text
);
175 if (!((SCM_UNBNDP (inp
) && SCM_OPINFPORTP (scm_cur_inp
))
176 || SCM_OPINFPORTP (inp
)))
179 scm_misc_error (s_scm_readline
,
180 "Input port is not open or not a file port",
184 if (!((SCM_UNBNDP (outp
) && SCM_OPOUTFPORTP (scm_cur_outp
))
185 || SCM_OPOUTFPORTP (outp
)))
188 scm_misc_error (s_scm_readline
,
189 "Output port is not open or not a file port",
193 if (!(SCM_UNBNDP (read_hook
) || scm_is_false (read_hook
)))
195 if (scm_is_false (scm_thunk_p (read_hook
)))
198 scm_wrong_type_arg (s_scm_readline
, SCM_ARG4
, read_hook
);
200 before_read
= read_hook
;
203 scm_readline_init_ports (inp
, outp
);
205 ans
= scm_internal_catch (SCM_BOOL_T
,
206 (scm_t_catch_body
) internal_readline
,
207 (void *) SCM_UNPACK (text
),
211 fclose (rl_instream
);
212 fclose (rl_outstream
);
225 /* We should rather use scm_try_mutex when it becomes available */
226 scm_lock_mutex (reentry_barrier_mutex
);
231 scm_unlock_mutex (reentry_barrier_mutex
);
233 scm_misc_error (s_scm_readline
, "readline is not reentrant", SCM_EOL
);
237 handle_error (void *data
, SCM tag
, SCM args
)
239 rl_free_line_state ();
240 rl_cleanup_after_signal ();
241 fputc ('\n', rl_outstream
); /* We don't want next output on this line */
243 fclose (rl_instream
);
244 fclose (rl_outstream
);
247 scm_handle_by_throw (data
, tag
, args
);
248 return SCM_UNSPECIFIED
; /* never reached */
252 internal_readline (SCM text
)
256 char *prompt
= SCM_UNBNDP (text
) ? "" : SCM_STRING_CHARS (text
);
259 s
= readline (prompt
);
261 ret
= scm_makfrom0str (s
);
271 stream_from_fport (SCM port
, char *mode
, const char *subr
)
276 fd
= dup (((struct scm_t_fport
*) SCM_STREAM (port
))->fdes
);
283 f
= fdopen (fd
, mode
);
294 scm_readline_init_ports (SCM inp
, SCM outp
)
296 if (SCM_UNBNDP (inp
))
299 if (SCM_UNBNDP (outp
))
302 if (!SCM_OPINFPORTP (inp
)) {
304 "Input port is not open or not a file port",
308 if (!SCM_OPOUTFPORTP (outp
)) {
310 "Output port is not open or not a file port",
316 rl_instream
= stream_from_fport (inp
, "r", s_scm_readline
);
317 rl_outstream
= stream_from_fport (outp
, "w", s_scm_readline
);
323 SCM_DEFINE (scm_add_history
, "add-history", 1, 0, 0,
326 #define FUNC_NAME s_scm_add_history
329 SCM_VALIDATE_STRING (1,text
);
331 s
= SCM_STRING_CHARS (text
);
332 add_history (strdup (s
));
334 return SCM_UNSPECIFIED
;
339 SCM_DEFINE (scm_read_history
, "read-history", 1, 0, 0,
342 #define FUNC_NAME s_scm_read_history
344 SCM_VALIDATE_STRING (1,file
);
345 return scm_from_bool (!read_history (SCM_STRING_CHARS (file
)));
350 SCM_DEFINE (scm_write_history
, "write-history", 1, 0, 0,
353 #define FUNC_NAME s_scm_write_history
355 SCM_VALIDATE_STRING (1,file
);
356 return scm_from_bool (!write_history (SCM_STRING_CHARS (file
)));
360 SCM_DEFINE (scm_clear_history
, "clear-history", 0, 0, 0,
362 "Clear the history buffer of the readline machinery.")
363 #define FUNC_NAME s_scm_clear_history
366 return SCM_UNSPECIFIED
;
371 SCM_DEFINE (scm_filename_completion_function
, "filename-completion-function", 2, 0, 0,
372 (SCM text
, SCM continuep
),
374 #define FUNC_NAME s_scm_filename_completion_function
378 SCM_VALIDATE_STRING (1,text
);
379 #ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION
380 s
= rl_filename_completion_function (SCM_STRING_CHARS (text
), scm_is_true (continuep
));
382 s
= filename_completion_function (SCM_STRING_CHARS (text
), scm_is_true (continuep
));
384 ans
= scm_makfrom0str (s
);
391 * The following has been modified from code contributed by
392 * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
395 SCM scm_readline_completion_function_var
;
398 completion_function (char *text
, int continuep
)
400 SCM compfunc
= SCM_VARIABLE_REF (scm_readline_completion_function_var
);
403 if (scm_is_false (compfunc
))
404 return NULL
; /* #f => completion disabled */
407 SCM t
= scm_makfrom0str (text
);
408 SCM c
= scm_from_bool (continuep
);
409 res
= scm_apply (compfunc
, scm_list_2 (t
, c
), SCM_EOL
);
411 if (scm_is_false (res
))
414 if (!SCM_STRINGP (res
))
415 scm_misc_error (s_scm_readline
,
416 "Completion function returned bogus value: %S",
418 return strdup (SCM_STRING_CHARS (res
));
422 /*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
424 static int match_paren (int x
, int k
);
425 static int find_matching_paren (int k
);
426 static void init_bouncing_parens ();
429 init_bouncing_parens ()
431 if (strncmp (rl_get_keymap_name (rl_get_keymap ()), "vi", 2))
433 rl_bind_key (')', match_paren
);
434 rl_bind_key (']', match_paren
);
435 rl_bind_key ('}', match_paren
);
440 find_matching_paren(int k
)
444 int end_parens_found
= 0;
446 /* Choose the corresponding opening bracket. */
447 if (k
== ')') c
= '(';
448 else if (k
== ']') c
= '[';
449 else if (k
== '}') c
= '{';
451 for (i
=rl_point
-2; i
>=0; i
--)
453 /* Is the current character part of a character literal? */
455 && rl_line_buffer
[i
- 1] == '\\'
456 && rl_line_buffer
[i
- 2] == '#')
458 else if (rl_line_buffer
[i
] == k
)
460 else if (rl_line_buffer
[i
] == '"')
462 /* Skip over a string literal. */
463 for (i
--; i
>= 0; i
--)
464 if (rl_line_buffer
[i
] == '"'
466 && rl_line_buffer
[i
- 1] == '\\'))
469 else if (rl_line_buffer
[i
] == c
)
471 if (end_parens_found
==0)
473 else --end_parens_found
;
480 match_paren (int x
, int k
)
486 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] == '\\')
499 tmp
= 1000 * SCM_READLINE_BOUNCE_PARENS
;
500 timeout
.tv_sec
= tmp
/ 1000000;
501 timeout
.tv_usec
= tmp
% 1000000;
503 fno
= fileno (rl_instream
);
504 FD_SET (fno
, &readset
);
510 rl_point
= find_matching_paren (k
);
515 scm_internal_select (fno
+ 1, &readset
, NULL
, NULL
, &timeout
);
517 WaitForSingleObject (GetStdHandle(STD_INPUT_HANDLE
),
518 SCM_READLINE_BOUNCE_PARENS
);
526 #if defined (HAVE_RL_PRE_INPUT_HOOK) && defined (GUILE_SIGWINCH_SA_RESTART_CLEARED)
527 /* Readline disables SA_RESTART on SIGWINCH.
528 * This code turns it back on.
531 sigwinch_enable_restart (void)
533 #ifdef HAVE_SIGINTERRUPT
534 siginterrupt (SIGWINCH
, 0);
536 struct sigaction action
;
538 sigaction (SIGWINCH
, NULL
, &action
);
539 action
.sa_flags
|= SA_RESTART
;
540 sigaction (SIGWINCH
, &action
, NULL
);
546 #endif /* HAVE_RL_GETC_FUNCTION */
551 #ifdef HAVE_RL_GETC_FUNCTION
552 #include "guile-readline/readline.x"
553 scm_readline_completion_function_var
554 = scm_c_define ("*readline-completion-function*", SCM_BOOL_F
);
556 rl_getc_function
= current_input_getc
;
558 #if defined (_RL_FUNCTION_TYPEDEF)
559 rl_completion_entry_function
= (rl_compentry_func_t
*) completion_function
;
561 rl_completion_entry_function
= (Function
*) completion_function
;
563 rl_basic_word_break_characters
= "\t\n\"'`;()";
564 rl_readline_name
= "Guile";
565 #if defined (HAVE_RL_PRE_INPUT_HOOK) && defined (GUILE_SIGWINCH_SA_RESTART_CLEARED)
566 rl_pre_input_hook
= sigwinch_enable_restart
;
569 reentry_barrier_mutex
= scm_permanent_object (scm_make_mutex ());
570 scm_init_opts (scm_readline_options
,
572 SCM_N_READLINE_OPTIONS
);
573 init_bouncing_parens();
574 scm_add_feature ("readline");
575 #endif /* HAVE_RL_GETC_FUNCTION */