1 /* readline.c --- line editing support for Guile */
3 /* Copyright (C) 1997,1999,2000,2001 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
25 #include "libguile/_scm.h"
26 #ifdef HAVE_RL_GETC_FUNCTION
28 #include "libguile/gh.h"
29 #include "libguile/iselect.h"
35 #include <readline/readline.h>
36 #include <readline/history.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)."}
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
,
61 SCM_N_READLINE_OPTIONS
,
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 before_read
;
129 current_input_getc (FILE *in SCM_UNUSED
)
131 if (promptp
&& !SCM_FALSEP (before_read
))
133 scm_apply (before_read
, SCM_EOL
, SCM_EOL
);
136 return scm_getc (input_port
);
146 static int in_readline
= 0;
148 static scm_t_mutex reentry_barrier_mutex
;
151 static SCM
internal_readline (SCM text
);
152 static SCM
handle_error (void *data
, SCM tag
, SCM args
);
153 static void reentry_barrier (void);
156 SCM_DEFINE (scm_readline
, "%readline", 0, 4, 0,
157 (SCM text
, SCM inp
, SCM outp
, SCM read_hook
),
159 #define FUNC_NAME s_scm_readline
165 before_read
= SCM_BOOL_F
;
167 if (!SCM_UNBNDP (text
))
169 if (!SCM_STRINGP (text
))
172 scm_wrong_type_arg (s_scm_readline
, SCM_ARG1
, text
);
174 SCM_STRING_COERCE_0TERMINATION_X (text
);
177 if (!((SCM_UNBNDP (inp
) && SCM_OPINFPORTP (scm_cur_inp
))
178 || SCM_OPINFPORTP (inp
)))
181 scm_misc_error (s_scm_readline
,
182 "Input port is not open or not a file port",
186 if (!((SCM_UNBNDP (outp
) && SCM_OPINFPORTP (scm_cur_outp
))
187 || SCM_OPOUTFPORTP (outp
)))
190 scm_misc_error (s_scm_readline
,
191 "Output port is not open or not a file port",
195 if (!(SCM_UNBNDP (read_hook
) || SCM_FALSEP (read_hook
)))
197 if (!(SCM_NFALSEP (scm_thunk_p (read_hook
))))
200 scm_wrong_type_arg (s_scm_readline
, SCM_ARG4
, read_hook
);
202 before_read
= read_hook
;
205 scm_readline_init_ports (inp
, outp
);
207 ans
= scm_internal_catch (SCM_BOOL_T
,
208 (scm_t_catch_body
) internal_readline
,
209 (void *) SCM_UNPACK (text
),
212 fclose (rl_instream
);
213 fclose (rl_outstream
);
226 /* We should rather use scm_t_mutexry_lock when it becomes available */
227 scm_mutex_lock (&reentry_barrier_mutex
);
234 scm_mutex_unlock (&reentry_barrier_mutex
);
237 scm_misc_error (s_scm_readline
, "readline is not reentrant", SCM_EOL
);
241 handle_error (void *data
, SCM tag
, SCM args
)
243 rl_free_line_state ();
244 rl_cleanup_after_signal ();
245 fputc ('\n', rl_outstream
); /* We don't want next output on this line */
246 fclose (rl_instream
);
247 fclose (rl_outstream
);
249 scm_handle_by_throw (data
, tag
, args
);
250 return SCM_UNSPECIFIED
; /* never reached */
254 internal_readline (SCM text
)
258 char *prompt
= SCM_UNBNDP (text
) ? "" : SCM_STRING_CHARS (text
);
261 s
= readline (prompt
);
263 ret
= scm_makfrom0str (s
);
273 stream_from_fport (SCM port
, char *mode
, const char *subr
)
278 fd
= dup (((struct scm_t_fport
*) SCM_STREAM (port
))->fdes
);
285 f
= fdopen (fd
, mode
);
296 scm_readline_init_ports (SCM inp
, SCM outp
)
298 if (SCM_UNBNDP (inp
))
301 if (SCM_UNBNDP (outp
))
304 if (!SCM_OPINFPORTP (inp
)) {
306 "Input port is not open or not a file port",
310 if (!SCM_OPOUTFPORTP (outp
)) {
312 "Output port is not open or not a file port",
317 rl_instream
= stream_from_fport (inp
, "r", s_scm_readline
);
318 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
);
330 SCM_STRING_COERCE_0TERMINATION_X (text
);
332 s
= SCM_STRING_CHARS (text
);
333 add_history (strdup (s
));
335 return SCM_UNSPECIFIED
;
340 SCM_DEFINE (scm_read_history
, "read-history", 1, 0, 0,
343 #define FUNC_NAME s_scm_read_history
345 SCM_VALIDATE_STRING (1,file
);
346 return SCM_NEGATE_BOOL (read_history (SCM_STRING_CHARS (file
)));
351 SCM_DEFINE (scm_write_history
, "write-history", 1, 0, 0,
354 #define FUNC_NAME s_scm_write_history
356 SCM_VALIDATE_STRING (1,file
);
357 return SCM_NEGATE_BOOL (write_history (SCM_STRING_CHARS (file
)));
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 SCM_VALIDATE_STRING (1,text
);
380 SCM_STRING_COERCE_0TERMINATION_X (text
);
381 #ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION
382 s
= rl_filename_completion_function (SCM_STRING_CHARS (text
), SCM_NFALSEP (continuep
));
384 s
= filename_completion_function (SCM_STRING_CHARS (text
), SCM_NFALSEP (continuep
));
386 ans
= scm_makfrom0str (s
);
393 * The following has been modified from code contributed by
394 * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
397 SCM scm_readline_completion_function_var
;
400 completion_function (char *text
, int continuep
)
402 SCM compfunc
= SCM_VARIABLE_REF (scm_readline_completion_function_var
);
405 if (SCM_FALSEP (compfunc
))
406 return NULL
; /* #f => completion disabled */
409 SCM t
= scm_makfrom0str (text
);
410 SCM c
= continuep
? SCM_BOOL_T
: SCM_BOOL_F
;
411 res
= scm_apply (compfunc
, scm_list_2 (t
, c
), SCM_EOL
);
413 if (SCM_FALSEP (res
))
416 if (!SCM_STRINGP (res
))
417 scm_misc_error (s_scm_readline
,
418 "Completion function returned bogus value: %S",
420 SCM_STRING_COERCE_0TERMINATION_X (res
);
421 return strdup (SCM_STRING_CHARS (res
));
425 /*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
427 static int match_paren (int x
, int k
);
428 static int find_matching_paren (int k
);
429 static void init_bouncing_parens ();
432 init_bouncing_parens ()
434 if (strncmp (rl_get_keymap_name (rl_get_keymap ()), "vi", 2))
436 rl_bind_key (')', match_paren
);
437 rl_bind_key (']', match_paren
);
438 rl_bind_key ('}', match_paren
);
443 find_matching_paren(int k
)
447 int end_parens_found
= 0;
449 /* Choose the corresponding opening bracket. */
450 if (k
== ')') c
= '(';
451 else if (k
== ']') c
= '[';
452 else if (k
== '}') c
= '{';
454 for (i
=rl_point
-2; i
>=0; i
--)
456 /* Is the current character part of a character literal? */
458 && rl_line_buffer
[i
- 1] == '\\'
459 && rl_line_buffer
[i
- 2] == '#')
461 else if (rl_line_buffer
[i
] == k
)
463 else if (rl_line_buffer
[i
] == '"')
465 /* Skip over a string literal. */
466 for (i
--; i
>= 0; i
--)
467 if (rl_line_buffer
[i
] == '"'
469 && rl_line_buffer
[i
- 1] == '\\'))
472 else if (rl_line_buffer
[i
] == c
)
474 if (end_parens_found
==0)
476 else --end_parens_found
;
483 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 scm_internal_select (fno
+ 1, &readset
, NULL
, NULL
, &timeout
);
519 #if defined (HAVE_RL_PRE_INPUT_HOOK) && defined (GUILE_SIGWINCH_SA_RESTART_CLEARED)
520 /* Readline disables SA_RESTART on SIGWINCH.
521 * This code turns it back on.
524 sigwinch_enable_restart (void)
526 #ifdef HAVE_SIGINTERRUPT
527 siginterrupt (SIGWINCH
, 0);
529 struct sigaction action
;
531 sigaction (SIGWINCH
, NULL
, &action
);
532 action
.sa_flags
|= SA_RESTART
;
533 sigaction (SIGWINCH
, &action
, NULL
);
539 #endif /* HAVE_RL_GETC_FUNCTION */
544 #ifdef HAVE_RL_GETC_FUNCTION
545 #include "guile-readline/readline.x"
546 scm_readline_completion_function_var
547 = scm_c_define ("*readline-completion-function*", SCM_BOOL_F
);
548 rl_getc_function
= current_input_getc
;
549 rl_redisplay_function
= redisplay
;
550 #if defined (_RL_FUNCTION_TYPEDEF)
551 rl_completion_entry_function
= (rl_compentry_func_t
*) completion_function
;
553 rl_completion_entry_function
= (Function
*) completion_function
;
555 rl_basic_word_break_characters
= "\t\n\"'`;()";
556 rl_readline_name
= "Guile";
557 #if defined (HAVE_RL_PRE_INPUT_HOOK) && defined (GUILE_SIGWINCH_SA_RESTART_CLEARED)
558 rl_pre_input_hook
= sigwinch_enable_restart
;
562 scm_mutex_init (&reentry_barrier_mutex
);
564 scm_init_opts (scm_readline_options
,
566 SCM_N_READLINE_OPTIONS
);
567 init_bouncing_parens();
568 scm_add_feature ("readline");
569 #endif /* HAVE_RL_GETC_FUNCTION */