1 /* readline.c --- line editing support for Guile */
3 /* Copyright (C) 1997,1999, 2000 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
22 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
23 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
27 #include "libguile/_scm.h"
28 #ifdef HAVE_RL_GETC_FUNCTION
30 #include "libguile/gh.h"
31 #include "libguile/iselect.h"
36 #include <readline/readline.h>
37 #include <readline/history.h>
41 #include "libguile/validate.h"
42 #include "guile-readline/readline.h"
44 scm_option scm_readline_opts
[] = {
45 { SCM_OPTION_BOOLEAN
, "history-file", 1,
46 "Use history file." },
47 { SCM_OPTION_INTEGER
, "history-length", 200,
49 { SCM_OPTION_INTEGER
, "bounce-parens", 500,
50 "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
,
62 SCM_N_READLINE_OPTIONS
,
64 stifle_history (SCM_HISTORY_LENGTH
);
74 char *new = malloc (len
+ 1);
78 #endif /* HAVE_STRDUP */
80 #ifndef HAVE_RL_CLEANUP_AFTER_SIGNAL
82 /* These are readline functions added in release 2.3. They will work
83 * together with readline-2.1 and 2.2. (The readline interface is
84 * disabled for earlier releases.)
85 * They are declared static; if we want to use them elsewhere, then
86 * we need external declarations for them, but at the moment, I don't
87 * think anything else in Guile ought to use these.
90 extern void _rl_clean_up_for_exit ();
91 extern void _rl_kill_kbd_macro ();
92 extern int _rl_init_argument ();
95 rl_cleanup_after_signal ()
97 #ifdef HAVE_RL_CLEAR_SIGNALS
98 _rl_clean_up_for_exit ();
100 (*rl_deprep_term_function
) ();
101 #ifdef HAVE_RL_CLEAR_SIGNALS
104 rl_pending_input
= 0;
108 rl_free_line_state ()
110 register HIST_ENTRY
*entry
;
114 entry
= current_history ();
116 entry
->data
= (char *)NULL
;
118 _rl_kill_kbd_macro ();
120 _rl_init_argument ();
123 #endif /* !HAVE_RL_CLEANUP_AFTER_SIGNAL */
126 static SCM input_port
;
127 static SCM before_read
;
130 current_input_getc (FILE *in
)
132 if (promptp
&& SCM_NIMP (before_read
))
134 scm_apply (before_read
, SCM_EOL
, SCM_EOL
);
137 return scm_getc (input_port
);
147 static int in_readline
= 0;
149 static scm_mutex_t reentry_barrier_mutex
;
152 static SCM
internal_readline (SCM text
);
153 static SCM
handle_error (void *data
, SCM tag
, SCM args
);
154 static void reentry_barrier (void);
157 SCM_DEFINE (scm_readline
, "%readline", 0, 4, 0,
158 (SCM text
, SCM inp
, SCM outp
, SCM read_hook
),
160 #define FUNC_NAME s_scm_readline
166 before_read
= SCM_BOOL_F
;
168 if (!SCM_UNBNDP (text
))
170 if (!(SCM_NIMP (text
) && SCM_STRINGP (text
)))
173 scm_wrong_type_arg (s_scm_readline
, SCM_ARG1
, text
);
175 SCM_STRING_COERCE_0TERMINATION_X (text
);
178 if (!((SCM_UNBNDP (inp
) && SCM_NIMP (scm_cur_inp
) && SCM_OPINFPORTP (inp
))
179 || (SCM_NIMP (inp
) && SCM_OPINFPORTP (inp
))))
182 scm_misc_error (s_scm_readline
,
183 "Input port is not open or not a file port",
187 if (!((SCM_UNBNDP (outp
) && SCM_NIMP (scm_cur_outp
) && SCM_OPINFPORTP (outp
))
188 || (SCM_NIMP (outp
) && SCM_OPOUTFPORTP (outp
))))
191 scm_misc_error (s_scm_readline
,
192 "Output port is not open or not a file port",
196 if (!(SCM_UNBNDP (read_hook
) || SCM_FALSEP (read_hook
)))
198 if (!(SCM_NFALSEP (scm_thunk_p (read_hook
))))
201 scm_wrong_type_arg (s_scm_readline
, SCM_ARG4
, read_hook
);
203 before_read
= read_hook
;
206 scm_readline_init_ports (inp
, outp
);
208 ans
= scm_internal_catch (SCM_BOOL_T
,
209 (scm_catch_body_t
) internal_readline
,
210 (void *) SCM_UNPACK (text
),
213 fclose (rl_instream
);
214 fclose (rl_outstream
);
227 /* We should rather use scm_mutex_try_lock when it becomes available */
228 scm_mutex_lock (&reentry_barrier_mutex
);
235 scm_mutex_unlock (&reentry_barrier_mutex
);
238 scm_misc_error (s_scm_readline
, "readline is not reentrant", SCM_EOL
);
242 handle_error (void *data
, SCM tag
, SCM args
)
244 rl_free_line_state ();
245 rl_cleanup_after_signal ();
246 fputc ('\n', rl_outstream
); /* We don't want next output on this line */
247 fclose (rl_instream
);
248 fclose (rl_outstream
);
250 scm_handle_by_throw (data
, tag
, args
);
251 return SCM_UNSPECIFIED
; /* never reached */
255 internal_readline (SCM text
)
259 char *prompt
= SCM_UNBNDP (text
) ? "" : SCM_STRING_CHARS (text
);
262 s
= readline (prompt
);
264 ret
= scm_makfrom0str (s
);
274 stream_from_fport (SCM port
, char *mode
, const char *subr
)
279 fd
= dup (((struct scm_fport
*) SCM_STREAM (port
))->fdes
);
286 f
= fdopen (fd
, mode
);
297 scm_readline_init_ports (SCM inp
, SCM outp
)
299 if (SCM_UNBNDP (inp
))
302 if (SCM_UNBNDP (outp
))
305 if (!(SCM_NIMP (inp
) && SCM_OPINFPORTP (inp
))) {
307 "Input port is not open or not a file port",
311 if (!(SCM_NIMP (outp
) && SCM_OPOUTFPORTP (outp
))) {
313 "Output port is not open or not a file port",
318 rl_instream
= stream_from_fport (inp
, "r", s_scm_readline
);
319 rl_outstream
= stream_from_fport (outp
, "w", s_scm_readline
);
324 SCM_DEFINE (scm_add_history
, "add-history", 1, 0, 0,
327 #define FUNC_NAME s_scm_add_history
330 SCM_VALIDATE_STRING (1,text
);
331 SCM_STRING_COERCE_0TERMINATION_X (text
);
333 s
= SCM_STRING_CHARS (text
);
334 add_history (strdup (s
));
336 return SCM_UNSPECIFIED
;
341 SCM_DEFINE (scm_read_history
, "read-history", 1, 0, 0,
344 #define FUNC_NAME s_scm_read_history
346 SCM_VALIDATE_STRING (1,file
);
347 return SCM_NEGATE_BOOL (read_history (SCM_STRING_CHARS (file
)));
352 SCM_DEFINE (scm_write_history
, "write-history", 1, 0, 0,
355 #define FUNC_NAME s_scm_write_history
357 SCM_VALIDATE_STRING (1,file
);
358 return SCM_NEGATE_BOOL (write_history (SCM_STRING_CHARS (file
)));
363 SCM_DEFINE (scm_filename_completion_function
, "filename-completion-function", 2, 0, 0,
364 (SCM text
, SCM continuep
),
366 #define FUNC_NAME s_scm_filename_completion_function
370 SCM_VALIDATE_STRING (1,text
);
371 SCM_STRING_COERCE_0TERMINATION_X (text
);
372 s
= filename_completion_function (SCM_STRING_CHARS (text
), SCM_NFALSEP (continuep
));
373 ans
= scm_makfrom0str (s
);
380 * The following has been modified from code contributed by
381 * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
384 SCM scm_readline_completion_function_var
;
387 completion_function (char *text
, int continuep
)
389 SCM compfunc
= SCM_CDR (scm_readline_completion_function_var
);
392 if (SCM_FALSEP (compfunc
))
393 return NULL
; /* #f => completion disabled */
396 SCM t
= scm_makfrom0str (text
);
397 SCM c
= continuep
? SCM_BOOL_T
: SCM_BOOL_F
;
398 res
= scm_apply (compfunc
, SCM_LIST2 (t
, c
), SCM_EOL
);
400 if (SCM_FALSEP (res
))
403 if (!(SCM_NIMP (res
) && SCM_STRINGP (res
)))
404 scm_misc_error (s_scm_readline
,
405 "Completion function returned bogus value: %S",
407 SCM_STRING_COERCE_0TERMINATION_X (res
);
408 return strdup (SCM_STRING_CHARS (res
));
412 /*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
414 static int match_paren (int x
, int k
);
415 static int find_matching_paren (int k
);
416 static void init_bouncing_parens ();
419 init_bouncing_parens ()
421 if (strncmp (rl_get_keymap_name (rl_get_keymap ()), "vi", 2))
423 rl_bind_key (')', match_paren
);
424 rl_bind_key (']', match_paren
);
425 rl_bind_key ('}', match_paren
);
430 find_matching_paren(int k
)
434 int end_parens_found
= 0;
436 /* Choose the corresponding opening bracket. */
437 if (k
== ')') c
= '(';
438 else if (k
== ']') c
= '[';
439 else if (k
== '}') c
= '{';
441 for (i
=rl_point
-2; i
>=0; i
--)
443 /* Is the current character part of a character literal? */
445 && rl_line_buffer
[i
- 1] == '\\'
446 && rl_line_buffer
[i
- 2] == '#')
448 else if (rl_line_buffer
[i
] == k
)
450 else if (rl_line_buffer
[i
] == '"')
452 /* Skip over a string literal. */
453 for (i
--; i
>= 0; i
--)
454 if (rl_line_buffer
[i
] == '"'
456 && rl_line_buffer
[i
- 1] == '\\'))
459 else if (rl_line_buffer
[i
] == c
)
461 if (end_parens_found
==0)
463 else --end_parens_found
;
470 match_paren (int x
, int k
)
474 struct timeval timeout
;
477 if (!SCM_READLINE_BOUNCE_PARENS
)
480 /* Did we just insert a quoted paren? If so, then don't bounce. */
481 if (rl_point
- 1 >= 1
482 && rl_line_buffer
[rl_point
- 2] == '\\')
485 tmp
= 1000 * SCM_READLINE_BOUNCE_PARENS
;
486 timeout
.tv_sec
= tmp
/ 1000000;
487 timeout
.tv_usec
= tmp
% 1000000;
489 fno
= fileno (rl_instream
);
490 FD_SET (fno
, &readset
);
495 rl_point
= find_matching_paren (k
);
499 scm_internal_select (fno
+ 1, &readset
, NULL
, NULL
, &timeout
);
506 #if defined (HAVE_RL_PRE_INPUT_HOOK) && defined (GUILE_SIGWINCH_SA_RESTART_CLEARED)
507 /* Readline disables SA_RESTART on SIGWINCH.
508 * This code turns it back on.
511 sigwinch_enable_restart (void)
513 #ifdef HAVE_SIGINTERRUPT
514 siginterrupt (SIGWINCH
, 0);
516 struct sigaction action
;
518 sigaction (SIGWINCH
, NULL
, &action
);
519 action
.sa_flags
|= SA_RESTART
;
520 sigaction (SIGWINCH
, &action
, NULL
);
526 #endif /* HAVE_RL_GETC_FUNCTION */
531 #ifdef HAVE_RL_GETC_FUNCTION
532 #include "guile-readline/readline.x"
533 scm_readline_completion_function_var
534 = scm_sysintern ("*readline-completion-function*", SCM_BOOL_F
);
535 rl_getc_function
= current_input_getc
;
536 rl_redisplay_function
= redisplay
;
537 rl_completion_entry_function
= (Function
*) completion_function
;
538 rl_basic_word_break_characters
= "\t\n\"'`;()";
539 rl_readline_name
= "Guile";
540 #if defined (HAVE_RL_PRE_INPUT_HOOK) && defined (GUILE_SIGWINCH_SA_RESTART_CLEARED)
541 rl_pre_input_hook
= sigwinch_enable_restart
;
545 scm_mutex_init (&reentry_barrier_mutex
);
547 scm_init_opts (scm_readline_options
,
549 SCM_N_READLINE_OPTIONS
);
550 init_bouncing_parens();
551 scm_add_feature ("readline");
552 #endif /* HAVE_RL_GETC_FUNCTION */