This commit was manufactured by cvs2svn to create branch 'FSF'.
[external/binutils.git] / readline / parens.c
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
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/time.h>
26 #include "readline.h"
27
28 /* Non-zero means try to blink the matching open parenthesis when the
29    close parenthesis is inserted. */
30 #if defined (FD_SET)
31 int rl_blink_matching_paren = 1;
32 #else /* !FD_SET */
33 int rl_blink_matching_paren = 0;
34 #endif /* !FD_SET */
35
36 static int find_matching_open ();
37
38 rl_insert_close (count, invoking_key)
39      int count, invoking_key;
40 {
41   extern int rl_explicit_arg;
42
43   if (rl_explicit_arg || !rl_blink_matching_paren)
44     rl_insert (count, invoking_key);
45   else
46     {
47 #if defined (FD_SET)
48       int orig_point, match_point, ready;
49       struct timeval timer;
50       fd_set readfds;
51
52       rl_insert (1, invoking_key);
53       rl_redisplay ();
54       match_point =
55         find_matching_open (rl_line_buffer, rl_point - 2, invoking_key);
56
57       /* Emacs might message or ring the bell here, but I don't. */
58       if (match_point < 0)
59         return;
60
61       FD_ZERO (&readfds);
62       FD_SET (fileno (rl_instream), &readfds);
63       timer.tv_sec = 1;
64       timer.tv_usec = 500;
65
66       orig_point = rl_point;
67       rl_point = match_point;
68       rl_redisplay ();
69       ready = select (1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &timer);
70       rl_point = orig_point;
71 #else /* !FD_SET */
72       rl_insert (count, invoking_key);
73 #endif /* !FD_SET */
74     }
75 }
76
77 static int
78 find_matching_open (string, from, closer)
79      char *string;
80      int from, closer;
81 {
82   register int i;
83   int opener, level, delimiter;
84
85   switch (closer)
86     {
87     case ']': opener = '['; break;
88     case '}': opener = '{'; break;
89     case ')': opener = '('; break;
90     default:
91       return (-1);
92     }
93
94   level = 1;                    /* The closer passed in counts as 1. */
95   delimiter = 0;                /* Delimited state unknown. */
96
97   for (i = from; i > -1; i--)
98     {
99       if (delimiter && (string[i] == delimiter))
100         delimiter = 0;
101       else if ((string[i] == '\'') || (string[i] == '"'))
102         delimiter = rl_line_buffer[i];
103       else if (!delimiter && (string[i] == closer))
104         level++;
105       else if (!delimiter && (string[i] == opener))
106         level--;
107
108       if (!level)
109         break;
110     }
111   return (i);
112 }
113
114
115