Debian Codegraph @

libsrc/readline/parens.c

raw
1 /* parens.c -- Implemenation of matching parenthesis feature. */
2
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
4
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
7
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 1, or
11 (at your option) any later version.
12
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #define READLINE_LIBRARY
23
24 #include "rlconf.h"
25
26 #if !defined (PAREN_MATCHING)
27
28 rl_insert_close (count, invoking_key)
29 int count, invoking_key;
30 {
31 return (rl_insert (count, invoking_key));
32 }
33
34 #else /* PAREN_MATCHING */
35
36 #include <stdio.h>
37 #include <sys/types.h>
38 #if defined (FD_SET)
39 # include <sys/time.h>
40 #endif /* FD_SET */
41 #include "readline.h"
42
43 extern int rl_explicit_arg;
44
45 /* Non-zero means try to blink the matching open parenthesis when the
46 close parenthesis is inserted. */
47 #if defined (FD_SET)
48 int rl_blink_matching_paren = 1;
49 #else /* !FD_SET */
50 int rl_blink_matching_paren = 0;
51 #endif /* !FD_SET */
52
53 static int find_matching_open ();
54
55 rl_insert_close (count, invoking_key)
56 int count, invoking_key;
57 {
58 if (rl_explicit_arg || !rl_blink_matching_paren)
59 rl_insert (count, invoking_key);
60 else
61 {
62 #if defined (FD_SET)
63 int orig_point, match_point, ready;
64 struct timeval timer;
65 fd_set readfds;
66
67 rl_insert (1, invoking_key);
68 rl_redisplay ();
69 match_point =
70 find_matching_open (rl_line_buffer, rl_point - 2, invoking_key);
71
72 /* Emacs might message or ring the bell here, but I don't. */
73 if (match_point < 0)
74 return -1;
75
76 FD_ZERO (&readfds);
77 FD_SET (fileno (rl_instream), &readfds);
78 timer.tv_sec = 1;
79 timer.tv_usec = 500;
80
81 orig_point = rl_point;
82 rl_point = match_point;
83 rl_redisplay ();
84 ready = select (1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &timer);
85 rl_point = orig_point;
86 #else /* !FD_SET */
87 rl_insert (count, invoking_key);
88 #endif /* !FD_SET */
89 }
90 return 0;
91 }
92
93 static int
94 find_matching_open (string, from, closer)
95 char *string;
96 int from, closer;
97 {
98 register int i;
99 int opener, level, delimiter;
100
101 switch (closer)
102 {
103 case ']': opener = '['; break;
104 case '}': opener = '{'; break;
105 case ')': opener = '('; break;
106 default:
107 return (-1);
108 }
109
110 level = 1; /* The closer passed in counts as 1. */
111 delimiter = 0; /* Delimited state unknown. */
112
113 for (i = from; i > -1; i--)
114 {
115 if (delimiter && (string[i] == delimiter))
116 delimiter = 0;
117 else if ((string[i] == '\'') || (string[i] == '"'))
118 delimiter = rl_line_buffer[i];
119 else if (!delimiter && (string[i] == closer))
120 level++;
121 else if (!delimiter && (string[i] == opener))
122 level--;
123
124 if (!level)
125 break;
126 }
127 return (i);
128 }
129
130 #endif /* PAREN_MATCHING */