| 1 | n/a | """ParenMatch -- An IDLE extension for parenthesis matching. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | When you hit a right paren, the cursor should move briefly to the left |
|---|
| 4 | n/a | paren. Paren here is used generically; the matching applies to |
|---|
| 5 | n/a | parentheses, square brackets, and curly braces. |
|---|
| 6 | n/a | """ |
|---|
| 7 | n/a | from idlelib.hyperparser import HyperParser |
|---|
| 8 | n/a | from idlelib.config import idleConf |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | _openers = {')':'(',']':'[','}':'{'} |
|---|
| 11 | n/a | CHECK_DELAY = 100 # milliseconds |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | class ParenMatch: |
|---|
| 14 | n/a | """Highlight matching parentheses |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | There are three supported style of paren matching, based loosely |
|---|
| 17 | n/a | on the Emacs options. The style is select based on the |
|---|
| 18 | n/a | HILITE_STYLE attribute; it can be changed used the set_style |
|---|
| 19 | n/a | method. |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | The supported styles are: |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | default -- When a right paren is typed, highlight the matching |
|---|
| 24 | n/a | left paren for 1/2 sec. |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | expression -- When a right paren is typed, highlight the entire |
|---|
| 27 | n/a | expression from the left paren to the right paren. |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | TODO: |
|---|
| 30 | n/a | - extend IDLE with configuration dialog to change options |
|---|
| 31 | n/a | - implement rest of Emacs highlight styles (see below) |
|---|
| 32 | n/a | - print mismatch warning in IDLE status window |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | Note: In Emacs, there are several styles of highlight where the |
|---|
| 35 | n/a | matching paren is highlighted whenever the cursor is immediately |
|---|
| 36 | n/a | to the right of a right paren. I don't know how to do that in Tk, |
|---|
| 37 | n/a | so I haven't bothered. |
|---|
| 38 | n/a | """ |
|---|
| 39 | n/a | menudefs = [ |
|---|
| 40 | n/a | ('edit', [ |
|---|
| 41 | n/a | ("Show surrounding parens", "<<flash-paren>>"), |
|---|
| 42 | n/a | ]) |
|---|
| 43 | n/a | ] |
|---|
| 44 | n/a | STYLE = idleConf.GetOption('extensions','ParenMatch','style', |
|---|
| 45 | n/a | default='expression') |
|---|
| 46 | n/a | FLASH_DELAY = idleConf.GetOption('extensions','ParenMatch','flash-delay', |
|---|
| 47 | n/a | type='int',default=500) |
|---|
| 48 | n/a | HILITE_CONFIG = idleConf.GetHighlight(idleConf.CurrentTheme(),'hilite') |
|---|
| 49 | n/a | BELL = idleConf.GetOption('extensions','ParenMatch','bell', |
|---|
| 50 | n/a | type='bool',default=1) |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | RESTORE_VIRTUAL_EVENT_NAME = "<<parenmatch-check-restore>>" |
|---|
| 53 | n/a | # We want the restore event be called before the usual return and |
|---|
| 54 | n/a | # backspace events. |
|---|
| 55 | n/a | RESTORE_SEQUENCES = ("<KeyPress>", "<ButtonPress>", |
|---|
| 56 | n/a | "<Key-Return>", "<Key-BackSpace>") |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | def __init__(self, editwin): |
|---|
| 59 | n/a | self.editwin = editwin |
|---|
| 60 | n/a | self.text = editwin.text |
|---|
| 61 | n/a | # Bind the check-restore event to the function restore_event, |
|---|
| 62 | n/a | # so that we can then use activate_restore (which calls event_add) |
|---|
| 63 | n/a | # and deactivate_restore (which calls event_delete). |
|---|
| 64 | n/a | editwin.text.bind(self.RESTORE_VIRTUAL_EVENT_NAME, |
|---|
| 65 | n/a | self.restore_event) |
|---|
| 66 | n/a | self.bell = self.text.bell if self.BELL else lambda: None |
|---|
| 67 | n/a | self.counter = 0 |
|---|
| 68 | n/a | self.is_restore_active = 0 |
|---|
| 69 | n/a | self.set_style(self.STYLE) |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | def activate_restore(self): |
|---|
| 72 | n/a | if not self.is_restore_active: |
|---|
| 73 | n/a | for seq in self.RESTORE_SEQUENCES: |
|---|
| 74 | n/a | self.text.event_add(self.RESTORE_VIRTUAL_EVENT_NAME, seq) |
|---|
| 75 | n/a | self.is_restore_active = True |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | def deactivate_restore(self): |
|---|
| 78 | n/a | if self.is_restore_active: |
|---|
| 79 | n/a | for seq in self.RESTORE_SEQUENCES: |
|---|
| 80 | n/a | self.text.event_delete(self.RESTORE_VIRTUAL_EVENT_NAME, seq) |
|---|
| 81 | n/a | self.is_restore_active = False |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | def set_style(self, style): |
|---|
| 84 | n/a | self.STYLE = style |
|---|
| 85 | n/a | if style == "default": |
|---|
| 86 | n/a | self.create_tag = self.create_tag_default |
|---|
| 87 | n/a | self.set_timeout = self.set_timeout_last |
|---|
| 88 | n/a | elif style == "expression": |
|---|
| 89 | n/a | self.create_tag = self.create_tag_expression |
|---|
| 90 | n/a | self.set_timeout = self.set_timeout_none |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | def flash_paren_event(self, event): |
|---|
| 93 | n/a | indices = (HyperParser(self.editwin, "insert") |
|---|
| 94 | n/a | .get_surrounding_brackets()) |
|---|
| 95 | n/a | if indices is None: |
|---|
| 96 | n/a | self.bell() |
|---|
| 97 | n/a | return |
|---|
| 98 | n/a | self.activate_restore() |
|---|
| 99 | n/a | self.create_tag(indices) |
|---|
| 100 | n/a | self.set_timeout_last() |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | def paren_closed_event(self, event): |
|---|
| 103 | n/a | # If it was a shortcut and not really a closing paren, quit. |
|---|
| 104 | n/a | closer = self.text.get("insert-1c") |
|---|
| 105 | n/a | if closer not in _openers: |
|---|
| 106 | n/a | return |
|---|
| 107 | n/a | hp = HyperParser(self.editwin, "insert-1c") |
|---|
| 108 | n/a | if not hp.is_in_code(): |
|---|
| 109 | n/a | return |
|---|
| 110 | n/a | indices = hp.get_surrounding_brackets(_openers[closer], True) |
|---|
| 111 | n/a | if indices is None: |
|---|
| 112 | n/a | self.bell() |
|---|
| 113 | n/a | return |
|---|
| 114 | n/a | self.activate_restore() |
|---|
| 115 | n/a | self.create_tag(indices) |
|---|
| 116 | n/a | self.set_timeout() |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | def restore_event(self, event=None): |
|---|
| 119 | n/a | self.text.tag_delete("paren") |
|---|
| 120 | n/a | self.deactivate_restore() |
|---|
| 121 | n/a | self.counter += 1 # disable the last timer, if there is one. |
|---|
| 122 | n/a | |
|---|
| 123 | n/a | def handle_restore_timer(self, timer_count): |
|---|
| 124 | n/a | if timer_count == self.counter: |
|---|
| 125 | n/a | self.restore_event() |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | # any one of the create_tag_XXX methods can be used depending on |
|---|
| 128 | n/a | # the style |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | def create_tag_default(self, indices): |
|---|
| 131 | n/a | """Highlight the single paren that matches""" |
|---|
| 132 | n/a | self.text.tag_add("paren", indices[0]) |
|---|
| 133 | n/a | self.text.tag_config("paren", self.HILITE_CONFIG) |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | def create_tag_expression(self, indices): |
|---|
| 136 | n/a | """Highlight the entire expression""" |
|---|
| 137 | n/a | if self.text.get(indices[1]) in (')', ']', '}'): |
|---|
| 138 | n/a | rightindex = indices[1]+"+1c" |
|---|
| 139 | n/a | else: |
|---|
| 140 | n/a | rightindex = indices[1] |
|---|
| 141 | n/a | self.text.tag_add("paren", indices[0], rightindex) |
|---|
| 142 | n/a | self.text.tag_config("paren", self.HILITE_CONFIG) |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | # any one of the set_timeout_XXX methods can be used depending on |
|---|
| 145 | n/a | # the style |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | def set_timeout_none(self): |
|---|
| 148 | n/a | """Highlight will remain until user input turns it off |
|---|
| 149 | n/a | or the insert has moved""" |
|---|
| 150 | n/a | # After CHECK_DELAY, call a function which disables the "paren" tag |
|---|
| 151 | n/a | # if the event is for the most recent timer and the insert has changed, |
|---|
| 152 | n/a | # or schedules another call for itself. |
|---|
| 153 | n/a | self.counter += 1 |
|---|
| 154 | n/a | def callme(callme, self=self, c=self.counter, |
|---|
| 155 | n/a | index=self.text.index("insert")): |
|---|
| 156 | n/a | if index != self.text.index("insert"): |
|---|
| 157 | n/a | self.handle_restore_timer(c) |
|---|
| 158 | n/a | else: |
|---|
| 159 | n/a | self.editwin.text_frame.after(CHECK_DELAY, callme, callme) |
|---|
| 160 | n/a | self.editwin.text_frame.after(CHECK_DELAY, callme, callme) |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | def set_timeout_last(self): |
|---|
| 163 | n/a | """The last highlight created will be removed after .5 sec""" |
|---|
| 164 | n/a | # associate a counter with an event; only disable the "paren" |
|---|
| 165 | n/a | # tag if the event is for the most recent timer. |
|---|
| 166 | n/a | self.counter += 1 |
|---|
| 167 | n/a | self.editwin.text_frame.after( |
|---|
| 168 | n/a | self.FLASH_DELAY, |
|---|
| 169 | n/a | lambda self=self, c=self.counter: self.handle_restore_timer(c)) |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | if __name__ == '__main__': |
|---|
| 173 | n/a | import unittest |
|---|
| 174 | n/a | unittest.main('idlelib.idle_test.test_parenmatch', verbosity=2) |
|---|