* readline.c: Whitespace changes -- added space after
[bpt/guile.git] / guile-readline / readline.c
1 /* readline.c --- line editing support for Guile */
2
3 /* Copyright (C) 1997,1999 Free Software Foundation, Inc.
4 *
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)
8 * any later version.
9 *
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.
14 *
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
19 *
20 */
21
22 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
23 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
24
25 \f
26
27 #include "libguile/_scm.h"
28 #if defined (HAVE_RL_GETC_FUNCTION)
29 #include "libguile/libguile.h"
30 #include "libguile/gh.h"
31 #include "libguile/iselect.h"
32
33 #include <readline/readline.h>
34 #include <readline/history.h>
35 #include <sys/time.h>
36
37 #include "libguile/scm_validate.h"
38 #include "readline.h"
39
40 scm_option scm_readline_opts[] = {
41 { SCM_OPTION_BOOLEAN, "history-file", 1,
42 "Use history file." },
43 { SCM_OPTION_INTEGER, "history-length", 200,
44 "History length." },
45 { SCM_OPTION_INTEGER, "bounce-parens", 500,
46 "Time (ms) to show matching opening parenthesis (0 = off)."}
47 };
48
49 extern void stifle_history (int max);
50
51 SCM_DEFINE (scm_readline_options, "readline-options-interface", 0, 1, 0,
52 (SCM setting),
53 "")
54 #define FUNC_NAME s_scm_readline_options
55 {
56 SCM ans = scm_options (setting,
57 scm_readline_opts,
58 SCM_N_READLINE_OPTIONS,
59 FUNC_NAME);
60 stifle_history (SCM_HISTORY_LENGTH);
61 return ans;
62 }
63 #undef FUNC_NAME
64
65 #ifndef HAVE_STRDUP
66 static char *
67 strdup (char *s)
68 {
69 int len = strlen (s);
70 char *new = malloc (len + 1);
71 strcpy (new, s);
72 return new;
73 }
74 #endif /* HAVE_STRDUP */
75
76 #ifndef HAVE_RL_CLEANUP_AFTER_SIGNAL
77
78 /* These are readline functions added in release 2.3. They will work
79 * together with readline-2.1 and 2.2. (The readline interface is
80 * disabled for earlier releases.)
81 * They are declared static; if we want to use them elsewhere, then
82 * we need external declarations for them, but at the moment, I don't
83 * think anything else in Guile ought to use these.
84 */
85
86 extern void _rl_clean_up_for_exit ();
87 extern void _rl_kill_kbd_macro ();
88 extern int _rl_init_argument ();
89
90 void
91 rl_cleanup_after_signal ()
92 {
93 #ifdef HAVE_RL_CLEAR_SIGNALS
94 _rl_clean_up_for_exit ();
95 #endif
96 (*rl_deprep_term_function) ();
97 #ifdef HAVE_RL_CLEAR_SIGNALS
98 rl_clear_signals ();
99 #endif
100 rl_pending_input = 0;
101 }
102
103 void
104 rl_free_line_state ()
105 {
106 register HIST_ENTRY *entry;
107
108 free_undo_list ();
109
110 entry = current_history ();
111 if (entry)
112 entry->data = (char *)NULL;
113
114 _rl_kill_kbd_macro ();
115 rl_clear_message ();
116 _rl_init_argument ();
117 }
118
119 #endif /* !HAVE_RL_CLEANUP_AFTER_SIGNAL */
120
121 static int promptp;
122 static SCM input_port;
123 static SCM before_read;
124
125 static int
126 current_input_getc (FILE *in)
127 {
128 SCM ans;
129 if (promptp && SCM_NIMP (before_read))
130 {
131 scm_apply (before_read, SCM_EOL, SCM_EOL);
132 promptp = 0;
133 }
134 ans = scm_getc (input_port);
135 return ans;
136 }
137
138 static void
139 redisplay ()
140 {
141 rl_redisplay ();
142 /* promptp = 1; */
143 }
144
145 static int in_readline = 0;
146 #ifdef USE_THREADS
147 static scm_mutex_t reentry_barrier_mutex;
148 #endif
149
150 static SCM internal_readline (SCM text);
151 static SCM handle_error (void *data, SCM tag, SCM args);
152 static void reentry_barrier ();
153
154
155 SCM_DEFINE (scm_readline, "%readline", 0, 4, 0,
156 (SCM text, SCM inp, SCM outp, SCM read_hook),
157 "")
158 #define FUNC_NAME s_scm_readline
159 {
160 SCM ans;
161
162 reentry_barrier ();
163
164 before_read = SCM_BOOL_F;
165
166 if (!SCM_UNBNDP (text))
167 {
168 if (!(SCM_NIMP (text) && SCM_STRINGP (text)))
169 {
170 --in_readline;
171 scm_wrong_type_arg (s_scm_readline, SCM_ARG1, text);
172 }
173 SCM_COERCE_SUBSTR (text);
174 }
175
176 if (!((SCM_UNBNDP (inp) && SCM_NIMP (scm_cur_inp) && SCM_OPINFPORTP (inp))
177 || SCM_NIMP (inp) && SCM_OPINFPORTP (inp)))
178 {
179 --in_readline;
180 scm_misc_error (s_scm_readline,
181 "Input port is not open or not a file port",
182 SCM_EOL);
183 }
184
185 if (!((SCM_UNBNDP (outp) && SCM_NIMP (scm_cur_outp) && SCM_OPINFPORTP (outp))
186 || (SCM_NIMP (outp) && SCM_OPOUTFPORTP (outp))))
187 {
188 --in_readline;
189 scm_misc_error (s_scm_readline,
190 "Output port is not open or not a file port",
191 SCM_EOL);
192 }
193
194 if (!(SCM_UNBNDP (read_hook) || SCM_FALSEP (read_hook)))
195 {
196 if (!(SCM_NFALSEP (scm_thunk_p (read_hook))))
197 {
198 --in_readline;
199 scm_wrong_type_arg (s_scm_readline, SCM_ARG4, read_hook);
200 }
201 before_read = read_hook;
202 }
203
204 scm_readline_init_ports (inp, outp);
205
206 ans = scm_internal_catch (SCM_BOOL_T,
207 (scm_catch_body_t) internal_readline,
208 (void *) text,
209 handle_error, 0);
210
211 fclose (rl_instream);
212 fclose (rl_outstream);
213
214 --in_readline;
215 return ans;
216 }
217 #undef FUNC_NAME
218
219
220 static void
221 reentry_barrier ()
222 {
223 int reentryp = 0;
224 #ifdef USE_THREADS
225 /* We should rather use scm_mutex_try_lock when it becomes available */
226 scm_mutex_lock (&reentry_barrier_mutex);
227 #endif
228 if (in_readline)
229 reentryp = 1;
230 else
231 ++in_readline;
232 #ifdef USE_THREADS
233 scm_mutex_unlock (&reentry_barrier_mutex);
234 #endif
235 if (reentryp)
236 scm_misc_error (s_scm_readline, "readline is not reentrant", SCM_EOL);
237 }
238
239 static SCM
240 handle_error (void *data, SCM tag, SCM args)
241 {
242 rl_free_line_state ();
243 rl_cleanup_after_signal ();
244 fputc ('\n', rl_outstream); /* We don't want next output on this line */
245 fclose (rl_instream);
246 fclose (rl_outstream);
247 --in_readline;
248 scm_handle_by_throw (data, tag, args);
249 return SCM_UNSPECIFIED; /* never reached */
250 }
251
252 static SCM
253 internal_readline (SCM text)
254 {
255 SCM ret;
256 char *s;
257 char *prompt = SCM_UNBNDP (text) ? "" : SCM_CHARS (text);
258
259 promptp = 1;
260 s = readline (prompt);
261 if (s)
262 ret = scm_makfrom0str (s);
263 else
264 ret = SCM_EOF_VAL;
265
266 free (s);
267
268 return ret;
269 }
270
271 static FILE *
272 stream_from_fport (SCM port, char *mode, const char *subr)
273 {
274 int fd;
275 FILE *f;
276
277 fd = dup (((struct scm_fport *) SCM_STREAM (port))->fdes);
278 if (fd == -1)
279 {
280 --in_readline;
281 scm_syserror (subr);
282 }
283
284 f = fdopen (fd, mode);
285 if (f == NULL)
286 {
287 --in_readline;
288 scm_syserror (subr);
289 }
290
291 return f;
292 }
293
294 void
295 scm_readline_init_ports (SCM inp, SCM outp)
296 {
297 if (SCM_UNBNDP (inp))
298 inp = scm_cur_inp;
299
300 if (SCM_UNBNDP (outp))
301 outp = scm_cur_outp;
302
303 if (!(SCM_NIMP (inp) && SCM_OPINFPORTP (inp))) {
304 scm_misc_error (0,
305 "Input port is not open or not a file port",
306 SCM_EOL);
307 }
308
309 if (!(SCM_NIMP (outp) && SCM_OPOUTFPORTP (outp))) {
310 scm_misc_error (0,
311 "Output port is not open or not a file port",
312 SCM_EOL);
313 }
314
315 input_port = inp;
316 rl_instream = stream_from_fport (inp, "r", s_scm_readline);
317 rl_outstream = stream_from_fport (outp, "w", s_scm_readline);
318 }
319
320
321
322 SCM_DEFINE (scm_add_history, "add-history", 1, 0, 0,
323 (SCM text),
324 "")
325 #define FUNC_NAME s_scm_add_history
326 {
327 char* s;
328 SCM_VALIDATE_STRING (1,text);
329 SCM_COERCE_SUBSTR (text);
330
331 s = SCM_CHARS (text);
332 add_history (strdup (s));
333
334 return SCM_UNSPECIFIED;
335 }
336 #undef FUNC_NAME
337
338
339 SCM_DEFINE (scm_read_history, "read-history", 1, 0, 0,
340 (SCM file),
341 "")
342 #define FUNC_NAME s_scm_read_history
343 {
344 SCM_VALIDATE_STRING (1,file);
345 return SCM_NEGATE_BOOL(read_history (SCM_ROCHARS (file)));
346 }
347 #undef FUNC_NAME
348
349
350 SCM_DEFINE (scm_write_history, "write-history", 1, 0, 0,
351 (SCM file),
352 "")
353 #define FUNC_NAME s_scm_write_history
354 {
355 SCM_VALIDATE_STRING (1,file);
356 return SCM_NEGATE_BOOL(write_history (SCM_ROCHARS (file)));
357 }
358 #undef FUNC_NAME
359
360
361 SCM_DEFINE (scm_filename_completion_function, "filename-completion-function", 2, 0, 0,
362 (SCM text, SCM continuep),
363 "")
364 #define FUNC_NAME s_scm_filename_completion_function
365 {
366 char *s;
367 SCM ans;
368 SCM_VALIDATE_STRING (1,text);
369 SCM_COERCE_SUBSTR (text);
370 s = filename_completion_function (SCM_CHARS (text), SCM_NFALSEP (continuep));
371 ans = scm_makfrom0str (s);
372 free (s);
373 return ans;
374 }
375 #undef FUNC_NAME
376
377 /*
378 * The following has been modified from code contributed by
379 * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
380 */
381
382 SCM scm_readline_completion_function_var;
383
384 static char *
385 completion_function (char *text, int continuep)
386 {
387 SCM compfunc = SCM_CDR (scm_readline_completion_function_var);
388 SCM res;
389
390 if (SCM_FALSEP (compfunc))
391 return NULL; /* #f => completion disabled */
392 else
393 {
394 SCM t = scm_makfrom0str (text);
395 SCM c = continuep ? SCM_BOOL_T : SCM_BOOL_F;
396 res = scm_apply (compfunc, SCM_LIST2 (t, c), SCM_EOL);
397
398 if (SCM_FALSEP (res))
399 return NULL;
400
401 if (!(SCM_NIMP (res) && SCM_STRINGP (res)))
402 scm_misc_error (s_scm_readline,
403 "Completion function returned bogus value: %S",
404 SCM_LIST1 (res));
405 SCM_COERCE_SUBSTR (res);
406 return strdup (SCM_CHARS (res));
407 }
408 }
409
410 /*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
411
412 static void match_paren(int x, int k);
413 static int find_matching_paren(int k);
414 static void init_bouncing_parens();
415
416 static void
417 init_bouncing_parens()
418 {
419 if(strncmp(rl_get_keymap_name(rl_get_keymap()), "vi", 2)) {
420 rl_bind_key(')', match_paren);
421 rl_bind_key(']', match_paren);
422 rl_bind_key('}', match_paren);
423 }
424 }
425
426 static int
427 find_matching_paren(int k)
428 {
429 register int i;
430 register char c = 0;
431 int end_parens_found = 0;
432
433 /* Choose the corresponding opening bracket. */
434 if (k == ')') c = '(';
435 else if (k == ']') c = '[';
436 else if (k == '}') c = '{';
437
438 for (i=rl_point-2; i>=0; i--)
439 {
440 /* Is the current character part of a character literal? */
441 if (i - 2 >= 0
442 && rl_line_buffer[i - 1] == '\\'
443 && rl_line_buffer[i - 2] == '#')
444 ;
445 else if (rl_line_buffer[i] == k)
446 end_parens_found++;
447 else if (rl_line_buffer[i] == '"')
448 {
449 /* Skip over a string literal. */
450 for (i--; i >= 0; i--)
451 if (rl_line_buffer[i] == '"'
452 && ! (i - 1 >= 0
453 && rl_line_buffer[i - 1] == '\\'))
454 break;
455 }
456 else if (rl_line_buffer[i] == c)
457 {
458 if (end_parens_found==0) return i;
459 else --end_parens_found;
460 }
461 }
462 return -1;
463 }
464
465 static void
466 match_paren(int x, int k)
467 {
468 int tmp;
469 fd_set readset;
470 struct timeval timeout;
471
472 rl_insert(x, k);
473 if (!SCM_READLINE_BOUNCE_PARENS)
474 return;
475
476 /* Did we just insert a quoted paren? If so, then don't bounce. */
477 if (rl_point - 1 >= 1
478 && rl_line_buffer[rl_point - 2] == '\\')
479 return;
480
481 tmp = 1000 * SCM_READLINE_BOUNCE_PARENS;
482 timeout.tv_sec = tmp / 1000000;
483 timeout.tv_usec = tmp % 1000000;
484 FD_ZERO(&readset);
485 FD_SET(fileno(rl_instream), &readset);
486
487 if(rl_point > 1) {
488 tmp = rl_point;
489 rl_point = find_matching_paren(k);
490 if(rl_point > -1) {
491 rl_redisplay();
492 scm_internal_select(1, &readset, NULL, NULL, &timeout);
493 }
494 rl_point = tmp;
495 }
496 }
497
498
499 void
500 scm_init_readline ()
501 {
502 #include "readline.x"
503 scm_readline_completion_function_var
504 = scm_sysintern ("*readline-completion-function*", SCM_BOOL_F);
505 rl_getc_function = current_input_getc;
506 rl_redisplay_function = redisplay;
507 rl_completion_entry_function = (Function*) completion_function;
508 rl_basic_word_break_characters = "\t\n\"'`;()";
509 rl_readline_name = "Guile";
510
511 #ifdef USE_THREADS
512 scm_mutex_init (&reentry_barrier_mutex);
513 #endif
514 scm_init_opts (scm_readline_options,
515 scm_readline_opts,
516 SCM_N_READLINE_OPTIONS);
517 init_bouncing_parens();
518 scm_add_feature ("readline");
519 }
520
521 #endif