Guido van Rossum | 808fa49 | 1999-06-02 11:05:19 +0000 | [diff] [blame] | 1 | #from Tkinter import TclError |
| 2 | #import tkMessageBox |
| 3 | #import tkSimpleDialog |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 5 | ###$ event <<newline-and-indent>> |
| 6 | ###$ win <Key-Return> |
| 7 | ###$ win <KP_Enter> |
| 8 | ###$ unix <Key-Return> |
| 9 | ###$ unix <KP_Enter> |
| 10 | |
| 11 | ###$ event <<indent-region>> |
| 12 | ###$ win <Control-bracketright> |
| 13 | ###$ unix <Alt-bracketright> |
| 14 | ###$ unix <Control-bracketright> |
| 15 | |
| 16 | ###$ event <<dedent-region>> |
| 17 | ###$ win <Control-bracketleft> |
| 18 | ###$ unix <Alt-bracketleft> |
| 19 | ###$ unix <Control-bracketleft> |
| 20 | |
| 21 | ###$ event <<comment-region>> |
| 22 | ###$ win <Alt-Key-3> |
| 23 | ###$ unix <Alt-Key-3> |
| 24 | |
| 25 | ###$ event <<uncomment-region>> |
| 26 | ###$ win <Alt-Key-4> |
| 27 | ###$ unix <Alt-Key-4> |
| 28 | |
| 29 | ###$ event <<tabify-region>> |
| 30 | ###$ win <Alt-Key-5> |
| 31 | ###$ unix <Alt-Key-5> |
| 32 | |
| 33 | ###$ event <<untabify-region>> |
| 34 | ###$ win <Alt-Key-6> |
| 35 | ###$ unix <Alt-Key-6> |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | a6be387 | 1999-06-01 19:52:34 +0000 | [diff] [blame] | 37 | import PyParse |
| 38 | |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 39 | class AutoIndent: |
| 40 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 41 | menudefs = [ |
| 42 | ('edit', [ |
| 43 | None, |
| 44 | ('_Indent region', '<<indent-region>>'), |
| 45 | ('_Dedent region', '<<dedent-region>>'), |
| 46 | ('Comment _out region', '<<comment-region>>'), |
| 47 | ('U_ncomment region', '<<uncomment-region>>'), |
| 48 | ('Tabify region', '<<tabify-region>>'), |
| 49 | ('Untabify region', '<<untabify-region>>'), |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 50 | ('Toggle tabs', '<<toggle-tabs>>'), |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 51 | ('New indent width', '<<change-indentwidth>>'), |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 52 | ]), |
| 53 | ] |
| 54 | |
Guido van Rossum | 33f2b7b | 1999-01-03 00:47:35 +0000 | [diff] [blame] | 55 | keydefs = { |
| 56 | '<<smart-backspace>>': ['<Key-BackSpace>'], |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 57 | '<<newline-and-indent>>': ['<Key-Return>', '<KP_Enter>'], |
Guido van Rossum | 17c516e | 1999-04-19 16:23:15 +0000 | [diff] [blame] | 58 | '<<smart-indent>>': ['<Key-Tab>'] |
Guido van Rossum | 33f2b7b | 1999-01-03 00:47:35 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | windows_keydefs = { |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 62 | '<<indent-region>>': ['<Control-bracketright>'], |
| 63 | '<<dedent-region>>': ['<Control-bracketleft>'], |
| 64 | '<<comment-region>>': ['<Alt-Key-3>'], |
| 65 | '<<uncomment-region>>': ['<Alt-Key-4>'], |
| 66 | '<<tabify-region>>': ['<Alt-Key-5>'], |
| 67 | '<<untabify-region>>': ['<Alt-Key-6>'], |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 68 | '<<toggle-tabs>>': ['<Alt-Key-t>'], |
Guido van Rossum | d93f739 | 1999-06-01 19:47:56 +0000 | [diff] [blame] | 69 | '<<change-indentwidth>>': ['<Alt-Key-u>'], |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | unix_keydefs = { |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 73 | '<<indent-region>>': ['<Alt-bracketright>', |
| 74 | '<Meta-bracketright>', |
| 75 | '<Control-bracketright>'], |
| 76 | '<<dedent-region>>': ['<Alt-bracketleft>', |
| 77 | '<Meta-bracketleft>', |
| 78 | '<Control-bracketleft>'], |
| 79 | '<<comment-region>>': ['<Alt-Key-3>', '<Meta-Key-3>'], |
| 80 | '<<uncomment-region>>': ['<Alt-Key-4>', '<Meta-Key-4>'], |
| 81 | '<<tabify-region>>': ['<Alt-Key-5>', '<Meta-Key-5>'], |
| 82 | '<<untabify-region>>': ['<Alt-Key-6>', '<Meta-Key-6>'], |
Guido van Rossum | a954ba1 | 1999-06-01 20:06:44 +0000 | [diff] [blame] | 83 | '<<toggle-tabs>>': ['<Alt-Key-t>'], |
| 84 | '<<change-indentwidth>>': ['<Alt-Key-u>'], |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 87 | # usetabs true -> literal tab characters are used by indent and |
| 88 | # dedent cmds, possibly mixed with spaces if |
| 89 | # indentwidth is not a multiple of tabwidth |
| 90 | # false -> tab characters are converted to spaces by indent |
| 91 | # and dedent cmds, and ditto TAB keystrokes |
Guido van Rossum | d93f739 | 1999-06-01 19:47:56 +0000 | [diff] [blame] | 92 | # indentwidth is the number of characters per logical indent level. |
| 93 | # tabwidth is the display width of a literal tab character. |
| 94 | # CAUTION: telling Tk to use anything other than its default |
| 95 | # tab setting causes it to use an entirely different tabbing algorithm, |
| 96 | # treating tab stops as fixed distances from the left margin. |
| 97 | # Nobody expects this, so for now tabwidth should never be changed. |
Guido van Rossum | 0fcd635 | 1999-06-08 12:54:23 +0000 | [diff] [blame] | 98 | usetabs = 1 |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 99 | indentwidth = 4 |
Guido van Rossum | 1320560 | 1999-06-11 15:03:00 +0000 | [diff] [blame] | 100 | tabwidth = 8 # for IDLE use, must remain 8 until Tk is fixed |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 101 | |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 102 | # If context_use_ps1 is true, parsing searches back for a ps1 line; |
Guido van Rossum | f4a1508 | 1999-06-03 14:32:16 +0000 | [diff] [blame] | 103 | # else searches for a popular (if, def, ...) Python stmt. |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 104 | context_use_ps1 = 0 |
| 105 | |
Guido van Rossum | f4a1508 | 1999-06-03 14:32:16 +0000 | [diff] [blame] | 106 | # When searching backwards for a reliable place to begin parsing, |
Guido van Rossum | a6be387 | 1999-06-01 19:52:34 +0000 | [diff] [blame] | 107 | # first start num_context_lines[0] lines back, then |
| 108 | # num_context_lines[1] lines back if that didn't work, and so on. |
| 109 | # The last value should be huge (larger than the # of lines in a |
| 110 | # conceivable file). |
| 111 | # Making the initial values larger slows things down more often. |
Guido van Rossum | a6be387 | 1999-06-01 19:52:34 +0000 | [diff] [blame] | 112 | num_context_lines = 50, 500, 5000000 |
| 113 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 114 | def __init__(self, editwin): |
Guido van Rossum | 808fa49 | 1999-06-02 11:05:19 +0000 | [diff] [blame] | 115 | self.editwin = editwin |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 116 | self.text = editwin.text |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 117 | |
| 118 | def config(self, **options): |
| 119 | for key, value in options.items(): |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 120 | if key == 'usetabs': |
| 121 | self.usetabs = value |
| 122 | elif key == 'indentwidth': |
| 123 | self.indentwidth = value |
| 124 | elif key == 'tabwidth': |
| 125 | self.tabwidth = value |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 126 | elif key == 'context_use_ps1': |
| 127 | self.context_use_ps1 = value |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 128 | else: |
| 129 | raise KeyError, "bad option name: %s" % `key` |
| 130 | |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 131 | # If ispythonsource and guess are true, guess a good value for |
| 132 | # indentwidth based on file content (if possible), and if |
| 133 | # indentwidth != tabwidth set usetabs false. |
| 134 | # In any case, adjust the Text widget's view of what a tab |
| 135 | # character means. |
| 136 | |
| 137 | def set_indentation_params(self, ispythonsource, guess=1): |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 138 | if guess and ispythonsource: |
| 139 | i = self.guess_indent() |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 140 | if 2 <= i <= 8: |
| 141 | self.indentwidth = i |
| 142 | if self.indentwidth != self.tabwidth: |
| 143 | self.usetabs = 0 |
| 144 | |
Guido van Rossum | 1320560 | 1999-06-11 15:03:00 +0000 | [diff] [blame] | 145 | self.editwin.set_tabwidth(self.tabwidth) |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 146 | |
Guido van Rossum | 33f2b7b | 1999-01-03 00:47:35 +0000 | [diff] [blame] | 147 | def smart_backspace_event(self, event): |
| 148 | text = self.text |
Guido van Rossum | 1320560 | 1999-06-11 15:03:00 +0000 | [diff] [blame] | 149 | first, last = self.editwin.get_selection_indices() |
Guido van Rossum | 33f2b7b | 1999-01-03 00:47:35 +0000 | [diff] [blame] | 150 | if first and last: |
| 151 | text.delete(first, last) |
| 152 | text.mark_set("insert", first) |
| 153 | return "break" |
Guido van Rossum | 1320560 | 1999-06-11 15:03:00 +0000 | [diff] [blame] | 154 | # Delete whitespace left, until hitting a real char or closest |
| 155 | # preceding virtual tab stop. |
Guido van Rossum | 33f2b7b | 1999-01-03 00:47:35 +0000 | [diff] [blame] | 156 | chars = text.get("insert linestart", "insert") |
Guido van Rossum | 1320560 | 1999-06-11 15:03:00 +0000 | [diff] [blame] | 157 | if chars == '': |
| 158 | if text.compare("insert", ">", "1.0"): |
| 159 | # easy: delete preceding newline |
| 160 | text.delete("insert-1c") |
| 161 | else: |
| 162 | text.bell() # at start of buffer |
| 163 | return "break" |
| 164 | if chars[-1] not in " \t": |
| 165 | # easy: delete preceding real char |
| 166 | text.delete("insert-1c") |
| 167 | return "break" |
| 168 | # Ick. It may require *inserting* spaces if we back up over a |
| 169 | # tab character! This is written to be clear, not fast. |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 170 | tabwidth = self.tabwidth |
| 171 | have = len(chars.expandtabs(tabwidth)) |
Guido van Rossum | 1320560 | 1999-06-11 15:03:00 +0000 | [diff] [blame] | 172 | assert have > 0 |
Tim Peters | bd8ce79 | 2002-01-23 16:57:55 +0000 | [diff] [blame] | 173 | want = ((have - 1) // self.indentwidth) * self.indentwidth |
Guido van Rossum | 1320560 | 1999-06-11 15:03:00 +0000 | [diff] [blame] | 174 | ncharsdeleted = 0 |
| 175 | while 1: |
| 176 | chars = chars[:-1] |
| 177 | ncharsdeleted = ncharsdeleted + 1 |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 178 | have = len(chars.expandtabs(tabwidth)) |
Guido van Rossum | 1320560 | 1999-06-11 15:03:00 +0000 | [diff] [blame] | 179 | if have <= want or chars[-1] not in " \t": |
| 180 | break |
| 181 | text.undo_block_start() |
| 182 | text.delete("insert-%dc" % ncharsdeleted, "insert") |
| 183 | if have < want: |
| 184 | text.insert("insert", ' ' * (want - have)) |
| 185 | text.undo_block_stop() |
Guido van Rossum | 33f2b7b | 1999-01-03 00:47:35 +0000 | [diff] [blame] | 186 | return "break" |
| 187 | |
Guido van Rossum | 17c516e | 1999-04-19 16:23:15 +0000 | [diff] [blame] | 188 | def smart_indent_event(self, event): |
| 189 | # if intraline selection: |
| 190 | # delete it |
| 191 | # elif multiline selection: |
| 192 | # do indent-region & return |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 193 | # indent one level |
Guido van Rossum | 17c516e | 1999-04-19 16:23:15 +0000 | [diff] [blame] | 194 | text = self.text |
Guido van Rossum | 1320560 | 1999-06-11 15:03:00 +0000 | [diff] [blame] | 195 | first, last = self.editwin.get_selection_indices() |
Guido van Rossum | 318a70d | 1999-05-03 15:49:52 +0000 | [diff] [blame] | 196 | text.undo_block_start() |
| 197 | try: |
| 198 | if first and last: |
| 199 | if index2line(first) != index2line(last): |
| 200 | return self.indent_region_event(event) |
| 201 | text.delete(first, last) |
| 202 | text.mark_set("insert", first) |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 203 | prefix = text.get("insert linestart", "insert") |
| 204 | raw, effective = classifyws(prefix, self.tabwidth) |
| 205 | if raw == len(prefix): |
| 206 | # only whitespace to the left |
| 207 | self.reindent_to(effective + self.indentwidth) |
Guido van Rossum | 318a70d | 1999-05-03 15:49:52 +0000 | [diff] [blame] | 208 | else: |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 209 | if self.usetabs: |
| 210 | pad = '\t' |
| 211 | else: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 212 | effective = len(prefix.expandtabs(self.tabwidth)) |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 213 | n = self.indentwidth |
| 214 | pad = ' ' * (n - effective % n) |
| 215 | text.insert("insert", pad) |
Guido van Rossum | 318a70d | 1999-05-03 15:49:52 +0000 | [diff] [blame] | 216 | text.see("insert") |
| 217 | return "break" |
| 218 | finally: |
| 219 | text.undo_block_stop() |
Guido van Rossum | 17c516e | 1999-04-19 16:23:15 +0000 | [diff] [blame] | 220 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 221 | def newline_and_indent_event(self, event): |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 222 | text = self.text |
Guido van Rossum | 1320560 | 1999-06-11 15:03:00 +0000 | [diff] [blame] | 223 | first, last = self.editwin.get_selection_indices() |
Guido van Rossum | 318a70d | 1999-05-03 15:49:52 +0000 | [diff] [blame] | 224 | text.undo_block_start() |
| 225 | try: |
| 226 | if first and last: |
| 227 | text.delete(first, last) |
| 228 | text.mark_set("insert", first) |
| 229 | line = text.get("insert linestart", "insert") |
| 230 | i, n = 0, len(line) |
| 231 | while i < n and line[i] in " \t": |
| 232 | i = i+1 |
Guido van Rossum | a6be387 | 1999-06-01 19:52:34 +0000 | [diff] [blame] | 233 | if i == n: |
| 234 | # the cursor is in or at leading indentation; just inject |
| 235 | # an empty line at the start |
| 236 | text.insert("insert linestart", '\n') |
| 237 | return "break" |
Guido van Rossum | 318a70d | 1999-05-03 15:49:52 +0000 | [diff] [blame] | 238 | indent = line[:i] |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 239 | # strip whitespace before insert point |
Guido van Rossum | 318a70d | 1999-05-03 15:49:52 +0000 | [diff] [blame] | 240 | i = 0 |
| 241 | while line and line[-1] in " \t": |
| 242 | line = line[:-1] |
Guido van Rossum | a6be387 | 1999-06-01 19:52:34 +0000 | [diff] [blame] | 243 | i = i+1 |
Guido van Rossum | 318a70d | 1999-05-03 15:49:52 +0000 | [diff] [blame] | 244 | if i: |
| 245 | text.delete("insert - %d chars" % i, "insert") |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 246 | # strip whitespace after insert point |
| 247 | while text.get("insert") in " \t": |
| 248 | text.delete("insert") |
| 249 | # start new line |
Guido van Rossum | a6be387 | 1999-06-01 19:52:34 +0000 | [diff] [blame] | 250 | text.insert("insert", '\n') |
Guido van Rossum | f4a1508 | 1999-06-03 14:32:16 +0000 | [diff] [blame] | 251 | |
Guido van Rossum | 1320560 | 1999-06-11 15:03:00 +0000 | [diff] [blame] | 252 | # adjust indentation for continuations and block |
| 253 | # open/close first need to find the last stmt |
Guido van Rossum | a6be387 | 1999-06-01 19:52:34 +0000 | [diff] [blame] | 254 | lno = index2line(text.index('insert')) |
| 255 | y = PyParse.Parser(self.indentwidth, self.tabwidth) |
| 256 | for context in self.num_context_lines: |
| 257 | startat = max(lno - context, 1) |
Guido van Rossum | f4a1508 | 1999-06-03 14:32:16 +0000 | [diff] [blame] | 258 | startatindex = `startat` + ".0" |
| 259 | rawtext = text.get(startatindex, "insert") |
Guido van Rossum | a6be387 | 1999-06-01 19:52:34 +0000 | [diff] [blame] | 260 | y.set_str(rawtext) |
Guido van Rossum | f4a1508 | 1999-06-03 14:32:16 +0000 | [diff] [blame] | 261 | bod = y.find_good_parse_start( |
| 262 | self.context_use_ps1, |
| 263 | self._build_char_in_string_func(startatindex)) |
Guido van Rossum | a6be387 | 1999-06-01 19:52:34 +0000 | [diff] [blame] | 264 | if bod is not None or startat == 1: |
| 265 | break |
| 266 | y.set_lo(bod or 0) |
| 267 | c = y.get_continuation_type() |
| 268 | if c != PyParse.C_NONE: |
| 269 | # The current stmt hasn't ended yet. |
| 270 | if c == PyParse.C_STRING: |
| 271 | # inside a string; just mimic the current indent |
| 272 | text.insert("insert", indent) |
| 273 | elif c == PyParse.C_BRACKET: |
| 274 | # line up with the first (if any) element of the |
| 275 | # last open bracket structure; else indent one |
Guido van Rossum | 1320560 | 1999-06-11 15:03:00 +0000 | [diff] [blame] | 276 | # level beyond the indent of the line with the |
| 277 | # last open bracket |
Guido van Rossum | a6be387 | 1999-06-01 19:52:34 +0000 | [diff] [blame] | 278 | self.reindent_to(y.compute_bracket_indent()) |
| 279 | elif c == PyParse.C_BACKSLASH: |
| 280 | # if more than one line in this stmt already, just |
Guido van Rossum | 1320560 | 1999-06-11 15:03:00 +0000 | [diff] [blame] | 281 | # mimic the current indent; else if initial line |
| 282 | # has a start on an assignment stmt, indent to |
| 283 | # beyond leftmost =; else to beyond first chunk of |
| 284 | # non-whitespace on initial line |
Guido van Rossum | a6be387 | 1999-06-01 19:52:34 +0000 | [diff] [blame] | 285 | if y.get_num_lines_in_stmt() > 1: |
| 286 | text.insert("insert", indent) |
| 287 | else: |
| 288 | self.reindent_to(y.compute_backslash_indent()) |
Guido van Rossum | d93f739 | 1999-06-01 19:47:56 +0000 | [diff] [blame] | 289 | else: |
Guido van Rossum | a6be387 | 1999-06-01 19:52:34 +0000 | [diff] [blame] | 290 | assert 0, "bogus continuation type " + `c` |
| 291 | return "break" |
| 292 | |
| 293 | # This line starts a brand new stmt; indent relative to |
Guido van Rossum | 1320560 | 1999-06-11 15:03:00 +0000 | [diff] [blame] | 294 | # indentation of initial line of closest preceding |
| 295 | # interesting stmt. |
Guido van Rossum | a6be387 | 1999-06-01 19:52:34 +0000 | [diff] [blame] | 296 | indent = y.get_base_indent_string() |
| 297 | text.insert("insert", indent) |
| 298 | if y.is_block_opener(): |
| 299 | self.smart_indent_event(event) |
| 300 | elif indent and y.is_block_closer(): |
Guido van Rossum | 318a70d | 1999-05-03 15:49:52 +0000 | [diff] [blame] | 301 | self.smart_backspace_event(event) |
Guido van Rossum | 318a70d | 1999-05-03 15:49:52 +0000 | [diff] [blame] | 302 | return "break" |
| 303 | finally: |
Guido van Rossum | a6be387 | 1999-06-01 19:52:34 +0000 | [diff] [blame] | 304 | text.see("insert") |
Guido van Rossum | 318a70d | 1999-05-03 15:49:52 +0000 | [diff] [blame] | 305 | text.undo_block_stop() |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 306 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 307 | auto_indent = newline_and_indent_event |
| 308 | |
Guido van Rossum | 1320560 | 1999-06-11 15:03:00 +0000 | [diff] [blame] | 309 | # Our editwin provides a is_char_in_string function that works |
| 310 | # with a Tk text index, but PyParse only knows about offsets into |
| 311 | # a string. This builds a function for PyParse that accepts an |
| 312 | # offset. |
Guido van Rossum | f4a1508 | 1999-06-03 14:32:16 +0000 | [diff] [blame] | 313 | |
| 314 | def _build_char_in_string_func(self, startindex): |
| 315 | def inner(offset, _startindex=startindex, |
| 316 | _icis=self.editwin.is_char_in_string): |
| 317 | return _icis(_startindex + "+%dc" % offset) |
| 318 | return inner |
| 319 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 320 | def indent_region_event(self, event): |
| 321 | head, tail, chars, lines = self.get_region() |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 322 | for pos in range(len(lines)): |
| 323 | line = lines[pos] |
| 324 | if line: |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 325 | raw, effective = classifyws(line, self.tabwidth) |
| 326 | effective = effective + self.indentwidth |
| 327 | lines[pos] = self._make_blanks(effective) + line[raw:] |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 328 | self.set_region(head, tail, chars, lines) |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 329 | return "break" |
| 330 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 331 | def dedent_region_event(self, event): |
| 332 | head, tail, chars, lines = self.get_region() |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 333 | for pos in range(len(lines)): |
| 334 | line = lines[pos] |
| 335 | if line: |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 336 | raw, effective = classifyws(line, self.tabwidth) |
| 337 | effective = max(effective - self.indentwidth, 0) |
| 338 | lines[pos] = self._make_blanks(effective) + line[raw:] |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 339 | self.set_region(head, tail, chars, lines) |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 340 | return "break" |
| 341 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 342 | def comment_region_event(self, event): |
| 343 | head, tail, chars, lines = self.get_region() |
Guido van Rossum | e2571f2 | 1999-06-10 14:44:48 +0000 | [diff] [blame] | 344 | for pos in range(len(lines) - 1): |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 345 | line = lines[pos] |
Guido van Rossum | 0fcd635 | 1999-06-08 12:54:23 +0000 | [diff] [blame] | 346 | lines[pos] = '##' + line |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 347 | self.set_region(head, tail, chars, lines) |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 348 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 349 | def uncomment_region_event(self, event): |
| 350 | head, tail, chars, lines = self.get_region() |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 351 | for pos in range(len(lines)): |
| 352 | line = lines[pos] |
| 353 | if not line: |
| 354 | continue |
| 355 | if line[:2] == '##': |
| 356 | line = line[2:] |
| 357 | elif line[:1] == '#': |
| 358 | line = line[1:] |
| 359 | lines[pos] = line |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 360 | self.set_region(head, tail, chars, lines) |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 361 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 362 | def tabify_region_event(self, event): |
| 363 | head, tail, chars, lines = self.get_region() |
Guido van Rossum | d93f739 | 1999-06-01 19:47:56 +0000 | [diff] [blame] | 364 | tabwidth = self._asktabwidth() |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 365 | for pos in range(len(lines)): |
| 366 | line = lines[pos] |
| 367 | if line: |
Guido van Rossum | d93f739 | 1999-06-01 19:47:56 +0000 | [diff] [blame] | 368 | raw, effective = classifyws(line, tabwidth) |
| 369 | ntabs, nspaces = divmod(effective, tabwidth) |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 370 | lines[pos] = '\t' * ntabs + ' ' * nspaces + line[raw:] |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 371 | self.set_region(head, tail, chars, lines) |
| 372 | |
| 373 | def untabify_region_event(self, event): |
| 374 | head, tail, chars, lines = self.get_region() |
Guido van Rossum | d93f739 | 1999-06-01 19:47:56 +0000 | [diff] [blame] | 375 | tabwidth = self._asktabwidth() |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 376 | for pos in range(len(lines)): |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 377 | lines[pos] = lines[pos].expandtabs(tabwidth) |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 378 | self.set_region(head, tail, chars, lines) |
| 379 | |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 380 | def toggle_tabs_event(self, event): |
Guido van Rossum | 808fa49 | 1999-06-02 11:05:19 +0000 | [diff] [blame] | 381 | if self.editwin.askyesno( |
Guido van Rossum | d93f739 | 1999-06-01 19:47:56 +0000 | [diff] [blame] | 382 | "Toggle tabs", |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 383 | "Turn tabs " + ("on", "off")[self.usetabs] + "?", |
| 384 | parent=self.text): |
| 385 | self.usetabs = not self.usetabs |
| 386 | return "break" |
| 387 | |
Guido van Rossum | d93f739 | 1999-06-01 19:47:56 +0000 | [diff] [blame] | 388 | # XXX this isn't bound to anything -- see class tabwidth comments |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 389 | def change_tabwidth_event(self, event): |
Guido van Rossum | d93f739 | 1999-06-01 19:47:56 +0000 | [diff] [blame] | 390 | new = self._asktabwidth() |
| 391 | if new != self.tabwidth: |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 392 | self.tabwidth = new |
| 393 | self.set_indentation_params(0, guess=0) |
| 394 | return "break" |
| 395 | |
| 396 | def change_indentwidth_event(self, event): |
Guido van Rossum | 808fa49 | 1999-06-02 11:05:19 +0000 | [diff] [blame] | 397 | new = self.editwin.askinteger( |
Guido van Rossum | d93f739 | 1999-06-01 19:47:56 +0000 | [diff] [blame] | 398 | "Indent width", |
| 399 | "New indent width (1-16)", |
| 400 | parent=self.text, |
| 401 | initialvalue=self.indentwidth, |
| 402 | minvalue=1, |
| 403 | maxvalue=16) |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 404 | if new and new != self.indentwidth: |
| 405 | self.indentwidth = new |
| 406 | return "break" |
| 407 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 408 | def get_region(self): |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 409 | text = self.text |
Guido van Rossum | 1320560 | 1999-06-11 15:03:00 +0000 | [diff] [blame] | 410 | first, last = self.editwin.get_selection_indices() |
| 411 | if first and last: |
| 412 | head = text.index(first + " linestart") |
| 413 | tail = text.index(last + "-1c lineend +1c") |
| 414 | else: |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 415 | head = text.index("insert linestart") |
| 416 | tail = text.index("insert lineend +1c") |
| 417 | chars = text.get(head, tail) |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 418 | lines = chars.split("\n") |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 419 | return head, tail, chars, lines |
| 420 | |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 421 | def set_region(self, head, tail, chars, lines): |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 422 | text = self.text |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 423 | newchars = "\n".join(lines) |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 424 | if newchars == chars: |
| 425 | text.bell() |
| 426 | return |
| 427 | text.tag_remove("sel", "1.0", "end") |
| 428 | text.mark_set("insert", head) |
Guido van Rossum | 318a70d | 1999-05-03 15:49:52 +0000 | [diff] [blame] | 429 | text.undo_block_start() |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 430 | text.delete(head, tail) |
| 431 | text.insert(head, newchars) |
Guido van Rossum | 318a70d | 1999-05-03 15:49:52 +0000 | [diff] [blame] | 432 | text.undo_block_stop() |
Guido van Rossum | 3b4ca0d | 1998-10-10 18:48:31 +0000 | [diff] [blame] | 433 | text.tag_add("sel", head, "insert") |
Guido van Rossum | 504b0bf | 1999-01-02 21:28:54 +0000 | [diff] [blame] | 434 | |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 435 | # Make string that displays as n leading blanks. |
| 436 | |
| 437 | def _make_blanks(self, n): |
| 438 | if self.usetabs: |
| 439 | ntabs, nspaces = divmod(n, self.tabwidth) |
| 440 | return '\t' * ntabs + ' ' * nspaces |
| 441 | else: |
| 442 | return ' ' * n |
| 443 | |
| 444 | # Delete from beginning of line to insert point, then reinsert |
| 445 | # column logical (meaning use tabs if appropriate) spaces. |
| 446 | |
| 447 | def reindent_to(self, column): |
| 448 | text = self.text |
| 449 | text.undo_block_start() |
Guido van Rossum | a6be387 | 1999-06-01 19:52:34 +0000 | [diff] [blame] | 450 | if text.compare("insert linestart", "!=", "insert"): |
| 451 | text.delete("insert linestart", "insert") |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 452 | if column: |
| 453 | text.insert("insert", self._make_blanks(column)) |
| 454 | text.undo_block_stop() |
| 455 | |
Guido van Rossum | d93f739 | 1999-06-01 19:47:56 +0000 | [diff] [blame] | 456 | def _asktabwidth(self): |
Guido van Rossum | 808fa49 | 1999-06-02 11:05:19 +0000 | [diff] [blame] | 457 | return self.editwin.askinteger( |
Guido van Rossum | d93f739 | 1999-06-01 19:47:56 +0000 | [diff] [blame] | 458 | "Tab width", |
| 459 | "Spaces per tab?", |
| 460 | parent=self.text, |
| 461 | initialvalue=self.tabwidth, |
| 462 | minvalue=1, |
| 463 | maxvalue=16) or self.tabwidth |
| 464 | |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 465 | # Guess indentwidth from text content. |
| 466 | # Return guessed indentwidth. This should not be believed unless |
| 467 | # it's in a reasonable range (e.g., it will be 0 if no indented |
| 468 | # blocks are found). |
| 469 | |
| 470 | def guess_indent(self): |
| 471 | opener, indented = IndentSearcher(self.text, self.tabwidth).run() |
| 472 | if opener and indented: |
| 473 | raw, indentsmall = classifyws(opener, self.tabwidth) |
| 474 | raw, indentlarge = classifyws(indented, self.tabwidth) |
| 475 | else: |
| 476 | indentsmall = indentlarge = 0 |
| 477 | return indentlarge - indentsmall |
Guido van Rossum | 17c516e | 1999-04-19 16:23:15 +0000 | [diff] [blame] | 478 | |
| 479 | # "line.col" -> line, as an int |
| 480 | def index2line(index): |
| 481 | return int(float(index)) |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 482 | |
| 483 | # Look at the leading whitespace in s. |
| 484 | # Return pair (# of leading ws characters, |
| 485 | # effective # of leading blanks after expanding |
| 486 | # tabs to width tabwidth) |
| 487 | |
| 488 | def classifyws(s, tabwidth): |
| 489 | raw = effective = 0 |
| 490 | for ch in s: |
| 491 | if ch == ' ': |
| 492 | raw = raw + 1 |
| 493 | effective = effective + 1 |
| 494 | elif ch == '\t': |
| 495 | raw = raw + 1 |
Guido van Rossum | 64e9d61 | 2002-01-23 15:15:13 +0000 | [diff] [blame] | 496 | effective = (effective // tabwidth + 1) * tabwidth |
Guido van Rossum | def2c96 | 1999-05-21 04:38:27 +0000 | [diff] [blame] | 497 | else: |
| 498 | break |
| 499 | return raw, effective |
| 500 | |
| 501 | import tokenize |
| 502 | _tokenize = tokenize |
| 503 | del tokenize |
| 504 | |
| 505 | class IndentSearcher: |
| 506 | |
| 507 | # .run() chews over the Text widget, looking for a block opener |
| 508 | # and the stmt following it. Returns a pair, |
| 509 | # (line containing block opener, line containing stmt) |
| 510 | # Either or both may be None. |
| 511 | |
| 512 | def __init__(self, text, tabwidth): |
| 513 | self.text = text |
| 514 | self.tabwidth = tabwidth |
| 515 | self.i = self.finished = 0 |
| 516 | self.blkopenline = self.indentedline = None |
| 517 | |
| 518 | def readline(self): |
| 519 | if self.finished: |
| 520 | return "" |
| 521 | i = self.i = self.i + 1 |
| 522 | mark = `i` + ".0" |
| 523 | if self.text.compare(mark, ">=", "end"): |
| 524 | return "" |
| 525 | return self.text.get(mark, mark + " lineend+1c") |
| 526 | |
| 527 | def tokeneater(self, type, token, start, end, line, |
| 528 | INDENT=_tokenize.INDENT, |
| 529 | NAME=_tokenize.NAME, |
| 530 | OPENERS=('class', 'def', 'for', 'if', 'try', 'while')): |
| 531 | if self.finished: |
| 532 | pass |
| 533 | elif type == NAME and token in OPENERS: |
| 534 | self.blkopenline = line |
| 535 | elif type == INDENT and self.blkopenline: |
| 536 | self.indentedline = line |
| 537 | self.finished = 1 |
| 538 | |
| 539 | def run(self): |
| 540 | save_tabsize = _tokenize.tabsize |
| 541 | _tokenize.tabsize = self.tabwidth |
| 542 | try: |
| 543 | try: |
| 544 | _tokenize.tokenize(self.readline, self.tokeneater) |
| 545 | except _tokenize.TokenError: |
| 546 | # since we cut off the tokenizer early, we can trigger |
| 547 | # spurious errors |
| 548 | pass |
| 549 | finally: |
| 550 | _tokenize.tabsize = save_tabsize |
| 551 | return self.blkopenline, self.indentedline |