blob: 9586a3b91da4834e0859bb08da14002fc73fa0c8 [file] [log] [blame]
David Scherer7aced172000-08-15 01:13:23 +00001"""ParenMatch -- An IDLE extension for parenthesis matching.
2
3When you hit a right paren, the cursor should move briefly to the left
4paren. Paren here is used generically; the matching applies to
5parentheses, square brackets, and curly braces.
David Scherer7aced172000-08-15 01:13:23 +00006"""
7
Terry Jan Reedy6fa5bdc2016-05-28 13:22:31 -04008from idlelib.hyperparser import HyperParser
9from idlelib.config import idleConf
David Scherer7aced172000-08-15 01:13:23 +000010
Thomas Wouters0e3f5912006-08-11 14:57:12 +000011_openers = {')':'(',']':'[','}':'{'}
Martin Pantereb995702016-07-28 01:11:04 +000012CHECK_DELAY = 100 # milliseconds
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000013
David Scherer7aced172000-08-15 01:13:23 +000014class ParenMatch:
15 """Highlight matching parentheses
16
17 There are three supported style of paren matching, based loosely
Kurt B. Kaiser4d4d2122001-07-13 19:49:27 +000018 on the Emacs options. The style is select based on the
David Scherer7aced172000-08-15 01:13:23 +000019 HILITE_STYLE attribute; it can be changed used the set_style
20 method.
21
22 The supported styles are:
23
24 default -- When a right paren is typed, highlight the matching
25 left paren for 1/2 sec.
26
27 expression -- When a right paren is typed, highlight the entire
28 expression from the left paren to the right paren.
29
30 TODO:
David Scherer7aced172000-08-15 01:13:23 +000031 - extend IDLE with configuration dialog to change options
32 - implement rest of Emacs highlight styles (see below)
33 - print mismatch warning in IDLE status window
34
35 Note: In Emacs, there are several styles of highlight where the
36 matching paren is highlighted whenever the cursor is immediately
37 to the right of a right paren. I don't know how to do that in Tk,
38 so I haven't bothered.
39 """
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000040 menudefs = [
41 ('edit', [
42 ("Show surrounding parens", "<<flash-paren>>"),
43 ])
44 ]
Kurt B. Kaiser8c11f7e2002-09-14 02:46:19 +000045 STYLE = idleConf.GetOption('extensions','ParenMatch','style',
46 default='expression')
47 FLASH_DELAY = idleConf.GetOption('extensions','ParenMatch','flash-delay',
48 type='int',default=500)
49 HILITE_CONFIG = idleConf.GetHighlight(idleConf.CurrentTheme(),'hilite')
50 BELL = idleConf.GetOption('extensions','ParenMatch','bell',
51 type='bool',default=1)
David Scherer7aced172000-08-15 01:13:23 +000052
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000053 RESTORE_VIRTUAL_EVENT_NAME = "<<parenmatch-check-restore>>"
54 # We want the restore event be called before the usual return and
55 # backspace events.
56 RESTORE_SEQUENCES = ("<KeyPress>", "<ButtonPress>",
57 "<Key-Return>", "<Key-BackSpace>")
58
David Scherer7aced172000-08-15 01:13:23 +000059 def __init__(self, editwin):
60 self.editwin = editwin
61 self.text = editwin.text
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000062 # Bind the check-restore event to the function restore_event,
63 # so that we can then use activate_restore (which calls event_add)
64 # and deactivate_restore (which calls event_delete).
65 editwin.text.bind(self.RESTORE_VIRTUAL_EVENT_NAME,
66 self.restore_event)
Terry Jan Reedy3ff55a82016-08-10 23:44:54 -040067 self.bell = self.text.bell if self.BELL else lambda: None
David Scherer7aced172000-08-15 01:13:23 +000068 self.counter = 0
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000069 self.is_restore_active = 0
David Scherer7aced172000-08-15 01:13:23 +000070 self.set_style(self.STYLE)
71
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000072 def activate_restore(self):
73 if not self.is_restore_active:
74 for seq in self.RESTORE_SEQUENCES:
75 self.text.event_add(self.RESTORE_VIRTUAL_EVENT_NAME, seq)
76 self.is_restore_active = True
77
78 def deactivate_restore(self):
79 if self.is_restore_active:
80 for seq in self.RESTORE_SEQUENCES:
81 self.text.event_delete(self.RESTORE_VIRTUAL_EVENT_NAME, seq)
82 self.is_restore_active = False
83
David Scherer7aced172000-08-15 01:13:23 +000084 def set_style(self, style):
85 self.STYLE = style
86 if style == "default":
87 self.create_tag = self.create_tag_default
88 self.set_timeout = self.set_timeout_last
89 elif style == "expression":
90 self.create_tag = self.create_tag_expression
91 self.set_timeout = self.set_timeout_none
92
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000093 def flash_paren_event(self, event):
Terry Jan Reedy14fbe722014-06-17 16:35:20 -040094 indices = (HyperParser(self.editwin, "insert")
95 .get_surrounding_brackets())
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000096 if indices is None:
Terry Jan Reedy3ff55a82016-08-10 23:44:54 -040097 self.bell()
David Scherer7aced172000-08-15 01:13:23 +000098 return
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000099 self.activate_restore()
100 self.create_tag(indices)
101 self.set_timeout_last()
102
103 def paren_closed_event(self, event):
104 # If it was a shortcut and not really a closing paren, quit.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000105 closer = self.text.get("insert-1c")
106 if closer not in _openers:
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000107 return
108 hp = HyperParser(self.editwin, "insert-1c")
109 if not hp.is_in_code():
110 return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000111 indices = hp.get_surrounding_brackets(_openers[closer], True)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000112 if indices is None:
Terry Jan Reedy3ff55a82016-08-10 23:44:54 -0400113 self.bell()
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000114 return
115 self.activate_restore()
116 self.create_tag(indices)
David Scherer7aced172000-08-15 01:13:23 +0000117 self.set_timeout()
118
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000119 def restore_event(self, event=None):
120 self.text.tag_delete("paren")
121 self.deactivate_restore()
122 self.counter += 1 # disable the last timer, if there is one.
David Scherer7aced172000-08-15 01:13:23 +0000123
124 def handle_restore_timer(self, timer_count):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000125 if timer_count == self.counter:
126 self.restore_event()
David Scherer7aced172000-08-15 01:13:23 +0000127
David Scherer7aced172000-08-15 01:13:23 +0000128 # any one of the create_tag_XXX methods can be used depending on
129 # the style
130
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000131 def create_tag_default(self, indices):
David Scherer7aced172000-08-15 01:13:23 +0000132 """Highlight the single paren that matches"""
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000133 self.text.tag_add("paren", indices[0])
David Scherer7aced172000-08-15 01:13:23 +0000134 self.text.tag_config("paren", self.HILITE_CONFIG)
135
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000136 def create_tag_expression(self, indices):
David Scherer7aced172000-08-15 01:13:23 +0000137 """Highlight the entire expression"""
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000138 if self.text.get(indices[1]) in (')', ']', '}'):
139 rightindex = indices[1]+"+1c"
140 else:
141 rightindex = indices[1]
142 self.text.tag_add("paren", indices[0], rightindex)
David Scherer7aced172000-08-15 01:13:23 +0000143 self.text.tag_config("paren", self.HILITE_CONFIG)
144
145 # any one of the set_timeout_XXX methods can be used depending on
146 # the style
147
148 def set_timeout_none(self):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000149 """Highlight will remain until user input turns it off
150 or the insert has moved"""
151 # After CHECK_DELAY, call a function which disables the "paren" tag
152 # if the event is for the most recent timer and the insert has changed,
153 # or schedules another call for itself.
154 self.counter += 1
155 def callme(callme, self=self, c=self.counter,
156 index=self.text.index("insert")):
157 if index != self.text.index("insert"):
158 self.handle_restore_timer(c)
159 else:
160 self.editwin.text_frame.after(CHECK_DELAY, callme, callme)
161 self.editwin.text_frame.after(CHECK_DELAY, callme, callme)
David Scherer7aced172000-08-15 01:13:23 +0000162
163 def set_timeout_last(self):
164 """The last highlight created will be removed after .5 sec"""
165 # associate a counter with an event; only disable the "paren"
166 # tag if the event is for the most recent timer.
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000167 self.counter += 1
Terry Jan Reedy14fbe722014-06-17 16:35:20 -0400168 self.editwin.text_frame.after(
169 self.FLASH_DELAY,
170 lambda self=self, c=self.counter: self.handle_restore_timer(c))
171
172
173if __name__ == '__main__':
174 import unittest
175 unittest.main('idlelib.idle_test.test_parenmatch', verbosity=2)