blob: ccec708f31f43685c7bf9181347b8f61b9dcb647 [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"""
Terry Jan Reedy6fa5bdc2016-05-28 13:22:31 -04007from idlelib.hyperparser import HyperParser
8from idlelib.config import idleConf
David Scherer7aced172000-08-15 01:13:23 +00009
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010_openers = {')':'(',']':'[','}':'{'}
Martin Pantereb995702016-07-28 01:11:04 +000011CHECK_DELAY = 100 # milliseconds
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000012
David Scherer7aced172000-08-15 01:13:23 +000013class ParenMatch:
14 """Highlight matching parentheses
15
16 There are three supported style of paren matching, based loosely
Kurt B. Kaiser4d4d2122001-07-13 19:49:27 +000017 on the Emacs options. The style is select based on the
David Scherer7aced172000-08-15 01:13:23 +000018 HILITE_STYLE attribute; it can be changed used the set_style
19 method.
20
21 The supported styles are:
22
23 default -- When a right paren is typed, highlight the matching
24 left paren for 1/2 sec.
25
26 expression -- When a right paren is typed, highlight the entire
27 expression from the left paren to the right paren.
28
29 TODO:
David Scherer7aced172000-08-15 01:13:23 +000030 - extend IDLE with configuration dialog to change options
31 - implement rest of Emacs highlight styles (see below)
32 - print mismatch warning in IDLE status window
33
34 Note: In Emacs, there are several styles of highlight where the
35 matching paren is highlighted whenever the cursor is immediately
36 to the right of a right paren. I don't know how to do that in Tk,
37 so I haven't bothered.
38 """
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000039 menudefs = [
40 ('edit', [
41 ("Show surrounding parens", "<<flash-paren>>"),
42 ])
43 ]
Kurt B. Kaiser8c11f7e2002-09-14 02:46:19 +000044 STYLE = idleConf.GetOption('extensions','ParenMatch','style',
45 default='expression')
46 FLASH_DELAY = idleConf.GetOption('extensions','ParenMatch','flash-delay',
47 type='int',default=500)
48 HILITE_CONFIG = idleConf.GetHighlight(idleConf.CurrentTheme(),'hilite')
49 BELL = idleConf.GetOption('extensions','ParenMatch','bell',
50 type='bool',default=1)
David Scherer7aced172000-08-15 01:13:23 +000051
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000052 RESTORE_VIRTUAL_EVENT_NAME = "<<parenmatch-check-restore>>"
53 # We want the restore event be called before the usual return and
54 # backspace events.
55 RESTORE_SEQUENCES = ("<KeyPress>", "<ButtonPress>",
56 "<Key-Return>", "<Key-BackSpace>")
57
David Scherer7aced172000-08-15 01:13:23 +000058 def __init__(self, editwin):
59 self.editwin = editwin
60 self.text = editwin.text
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000061 # Bind the check-restore event to the function restore_event,
62 # so that we can then use activate_restore (which calls event_add)
63 # and deactivate_restore (which calls event_delete).
64 editwin.text.bind(self.RESTORE_VIRTUAL_EVENT_NAME,
65 self.restore_event)
Terry Jan Reedy3ff55a82016-08-10 23:44:54 -040066 self.bell = self.text.bell if self.BELL else lambda: None
David Scherer7aced172000-08-15 01:13:23 +000067 self.counter = 0
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000068 self.is_restore_active = 0
David Scherer7aced172000-08-15 01:13:23 +000069 self.set_style(self.STYLE)
70
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000071 def activate_restore(self):
72 if not self.is_restore_active:
73 for seq in self.RESTORE_SEQUENCES:
74 self.text.event_add(self.RESTORE_VIRTUAL_EVENT_NAME, seq)
75 self.is_restore_active = True
76
77 def deactivate_restore(self):
78 if self.is_restore_active:
79 for seq in self.RESTORE_SEQUENCES:
80 self.text.event_delete(self.RESTORE_VIRTUAL_EVENT_NAME, seq)
81 self.is_restore_active = False
82
David Scherer7aced172000-08-15 01:13:23 +000083 def set_style(self, style):
84 self.STYLE = style
85 if style == "default":
86 self.create_tag = self.create_tag_default
87 self.set_timeout = self.set_timeout_last
88 elif style == "expression":
89 self.create_tag = self.create_tag_expression
90 self.set_timeout = self.set_timeout_none
91
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000092 def flash_paren_event(self, event):
Terry Jan Reedy14fbe722014-06-17 16:35:20 -040093 indices = (HyperParser(self.editwin, "insert")
94 .get_surrounding_brackets())
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000095 if indices is None:
Terry Jan Reedy3ff55a82016-08-10 23:44:54 -040096 self.bell()
David Scherer7aced172000-08-15 01:13:23 +000097 return
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000098 self.activate_restore()
99 self.create_tag(indices)
100 self.set_timeout_last()
101
102 def paren_closed_event(self, event):
103 # If it was a shortcut and not really a closing paren, quit.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000104 closer = self.text.get("insert-1c")
105 if closer not in _openers:
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000106 return
107 hp = HyperParser(self.editwin, "insert-1c")
108 if not hp.is_in_code():
109 return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000110 indices = hp.get_surrounding_brackets(_openers[closer], True)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000111 if indices is None:
Terry Jan Reedy3ff55a82016-08-10 23:44:54 -0400112 self.bell()
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000113 return
114 self.activate_restore()
115 self.create_tag(indices)
David Scherer7aced172000-08-15 01:13:23 +0000116 self.set_timeout()
117
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000118 def restore_event(self, event=None):
119 self.text.tag_delete("paren")
120 self.deactivate_restore()
121 self.counter += 1 # disable the last timer, if there is one.
David Scherer7aced172000-08-15 01:13:23 +0000122
123 def handle_restore_timer(self, timer_count):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000124 if timer_count == self.counter:
125 self.restore_event()
David Scherer7aced172000-08-15 01:13:23 +0000126
David Scherer7aced172000-08-15 01:13:23 +0000127 # any one of the create_tag_XXX methods can be used depending on
128 # the style
129
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000130 def create_tag_default(self, indices):
David Scherer7aced172000-08-15 01:13:23 +0000131 """Highlight the single paren that matches"""
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000132 self.text.tag_add("paren", indices[0])
David Scherer7aced172000-08-15 01:13:23 +0000133 self.text.tag_config("paren", self.HILITE_CONFIG)
134
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000135 def create_tag_expression(self, indices):
David Scherer7aced172000-08-15 01:13:23 +0000136 """Highlight the entire expression"""
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000137 if self.text.get(indices[1]) in (')', ']', '}'):
138 rightindex = indices[1]+"+1c"
139 else:
140 rightindex = indices[1]
141 self.text.tag_add("paren", indices[0], rightindex)
David Scherer7aced172000-08-15 01:13:23 +0000142 self.text.tag_config("paren", self.HILITE_CONFIG)
143
144 # any one of the set_timeout_XXX methods can be used depending on
145 # the style
146
147 def set_timeout_none(self):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000148 """Highlight will remain until user input turns it off
149 or the insert has moved"""
150 # After CHECK_DELAY, call a function which disables the "paren" tag
151 # if the event is for the most recent timer and the insert has changed,
152 # or schedules another call for itself.
153 self.counter += 1
154 def callme(callme, self=self, c=self.counter,
155 index=self.text.index("insert")):
156 if index != self.text.index("insert"):
157 self.handle_restore_timer(c)
158 else:
159 self.editwin.text_frame.after(CHECK_DELAY, callme, callme)
160 self.editwin.text_frame.after(CHECK_DELAY, callme, callme)
David Scherer7aced172000-08-15 01:13:23 +0000161
162 def set_timeout_last(self):
163 """The last highlight created will be removed after .5 sec"""
164 # associate a counter with an event; only disable the "paren"
165 # tag if the event is for the most recent timer.
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000166 self.counter += 1
Terry Jan Reedy14fbe722014-06-17 16:35:20 -0400167 self.editwin.text_frame.after(
168 self.FLASH_DELAY,
169 lambda self=self, c=self.counter: self.handle_restore_timer(c))
170
171
172if __name__ == '__main__':
173 import unittest
174 unittest.main('idlelib.idle_test.test_parenmatch', verbosity=2)