blob: 9c9848838fdfd6c38f22a8cace78308b54ac6b42 [file] [log] [blame]
Jari Aalto31859422009-01-12 13:36:28 +00001/* parens.c -- implementation of matching parentheses feature. */
Jari Aalto726f6381996-08-26 18:22:31 +00002
Jari Aalto31859422009-01-12 13:36:28 +00003/* Copyright (C) 1987, 1989, 1992-2009 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00004
Jari Aalto31859422009-01-12 13:36:28 +00005 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
Jari Aalto726f6381996-08-26 18:22:31 +00007
Jari Aalto31859422009-01-12 13:36:28 +00008 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
Jari Aalto726f6381996-08-26 18:22:31 +000011 (at your option) any later version.
12
Jari Aalto31859422009-01-12 13:36:28 +000013 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Jari Aalto726f6381996-08-26 18:22:31 +000016 GNU General Public License for more details.
17
Jari Aalto31859422009-01-12 13:36:28 +000018 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
20*/
21
Jari Aalto726f6381996-08-26 18:22:31 +000022#define READLINE_LIBRARY
23
Jari Aaltob80f6442004-07-27 13:29:18 +000024#if defined (__TANDEM)
25# include <floss.h>
26#endif
27
Jari Aalto726f6381996-08-26 18:22:31 +000028#include "rlconf.h"
29
Jari Aaltoccc6cda1996-12-23 17:02:34 +000030#if defined (HAVE_CONFIG_H)
31# include <config.h>
32#endif
33
Jari Aalto726f6381996-08-26 18:22:31 +000034#include <stdio.h>
35#include <sys/types.h>
Jari Aaltoccc6cda1996-12-23 17:02:34 +000036
Jari Aalto7117c2d2002-07-17 14:10:11 +000037#if defined (HAVE_UNISTD_H)
38# include <unistd.h>
39#endif
40
Chet Ramey00018032011-11-21 20:51:19 -050041#include "posixselect.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000042
43#if defined (HAVE_STRING_H)
44# include <string.h>
45#else /* !HAVE_STRING_H */
46# include <strings.h>
47#endif /* !HAVE_STRING_H */
48
49#if !defined (strchr) && !defined (__STDC__)
50extern char *strchr (), *strrchr ();
51#endif /* !strchr && !__STDC__ */
52
Jari Aalto726f6381996-08-26 18:22:31 +000053#include "readline.h"
Jari Aaltobb706242000-03-17 21:46:59 +000054#include "rlprivate.h"
Jari Aalto726f6381996-08-26 18:22:31 +000055
Jari Aaltof73dda02001-11-13 17:56:06 +000056static int find_matching_open PARAMS((char *, int, int));
Jari Aalto726f6381996-08-26 18:22:31 +000057
58/* Non-zero means try to blink the matching open parenthesis when the
59 close parenthesis is inserted. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000060#if defined (HAVE_SELECT)
Jari Aalto726f6381996-08-26 18:22:31 +000061int rl_blink_matching_paren = 1;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000062#else /* !HAVE_SELECT */
Jari Aalto726f6381996-08-26 18:22:31 +000063int rl_blink_matching_paren = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000064#endif /* !HAVE_SELECT */
Jari Aalto726f6381996-08-26 18:22:31 +000065
Jari Aalto28ef6c32001-04-06 19:14:31 +000066static int _paren_blink_usec = 500000;
67
Jari Aaltobb706242000-03-17 21:46:59 +000068/* Change emacs_standard_keymap to have bindings for paren matching when
69 ON_OR_OFF is 1, change them back to self_insert when ON_OR_OFF == 0. */
70void
71_rl_enable_paren_matching (on_or_off)
72 int on_or_off;
73{
74 if (on_or_off)
75 { /* ([{ */
76 rl_bind_key_in_map (')', rl_insert_close, emacs_standard_keymap);
77 rl_bind_key_in_map (']', rl_insert_close, emacs_standard_keymap);
78 rl_bind_key_in_map ('}', rl_insert_close, emacs_standard_keymap);
79 }
80 else
81 { /* ([{ */
82 rl_bind_key_in_map (')', rl_insert, emacs_standard_keymap);
83 rl_bind_key_in_map (']', rl_insert, emacs_standard_keymap);
84 rl_bind_key_in_map ('}', rl_insert, emacs_standard_keymap);
85 }
86}
Jari Aalto726f6381996-08-26 18:22:31 +000087
Jari Aaltoccc6cda1996-12-23 17:02:34 +000088int
Jari Aalto28ef6c32001-04-06 19:14:31 +000089rl_set_paren_blink_timeout (u)
90 int u;
91{
92 int o;
93
94 o = _paren_blink_usec;
95 if (u > 0)
96 _paren_blink_usec = u;
97 return (o);
98}
99
100int
Jari Aalto726f6381996-08-26 18:22:31 +0000101rl_insert_close (count, invoking_key)
102 int count, invoking_key;
103{
104 if (rl_explicit_arg || !rl_blink_matching_paren)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000105 _rl_insert_char (count, invoking_key);
Jari Aalto726f6381996-08-26 18:22:31 +0000106 else
107 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000108#if defined (HAVE_SELECT)
Jari Aalto726f6381996-08-26 18:22:31 +0000109 int orig_point, match_point, ready;
110 struct timeval timer;
111 fd_set readfds;
112
Jari Aalto7117c2d2002-07-17 14:10:11 +0000113 _rl_insert_char (1, invoking_key);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000114 (*rl_redisplay_function) ();
Jari Aalto726f6381996-08-26 18:22:31 +0000115 match_point =
116 find_matching_open (rl_line_buffer, rl_point - 2, invoking_key);
117
118 /* Emacs might message or ring the bell here, but I don't. */
119 if (match_point < 0)
120 return -1;
121
122 FD_ZERO (&readfds);
123 FD_SET (fileno (rl_instream), &readfds);
Chet Ramey00018032011-11-21 20:51:19 -0500124 USEC_TO_TIMEVAL (_paren_blink_usec, timer);
Jari Aalto726f6381996-08-26 18:22:31 +0000125
126 orig_point = rl_point;
127 rl_point = match_point;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000128 (*rl_redisplay_function) ();
Jari Aalto726f6381996-08-26 18:22:31 +0000129 ready = select (1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &timer);
130 rl_point = orig_point;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000131#else /* !HAVE_SELECT */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000132 _rl_insert_char (count, invoking_key);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000133#endif /* !HAVE_SELECT */
Jari Aalto726f6381996-08-26 18:22:31 +0000134 }
135 return 0;
136}
137
138static int
139find_matching_open (string, from, closer)
140 char *string;
141 int from, closer;
142{
143 register int i;
144 int opener, level, delimiter;
145
146 switch (closer)
147 {
148 case ']': opener = '['; break;
149 case '}': opener = '{'; break;
150 case ')': opener = '('; break;
151 default:
152 return (-1);
153 }
154
155 level = 1; /* The closer passed in counts as 1. */
156 delimiter = 0; /* Delimited state unknown. */
157
158 for (i = from; i > -1; i--)
159 {
160 if (delimiter && (string[i] == delimiter))
161 delimiter = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000162 else if (rl_basic_quote_characters && strchr (rl_basic_quote_characters, string[i]))
163 delimiter = string[i];
Jari Aalto726f6381996-08-26 18:22:31 +0000164 else if (!delimiter && (string[i] == closer))
165 level++;
166 else if (!delimiter && (string[i] == opener))
167 level--;
168
169 if (!level)
170 break;
171 }
172 return (i);
173}