summaryrefslogtreecommitdiffstats
path: root/Tools/idle/AutoIndent.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1999-01-02 21:28:54 (GMT)
committerGuido van Rossum <guido@python.org>1999-01-02 21:28:54 (GMT)
commit504b0bf066e4fddb21646331e89c2f6836c5c638 (patch)
treef5454648430eb4818810305325561aabb02cf035 /Tools/idle/AutoIndent.py
parentf07c328c072e62ada8671ec30392572add22d904 (diff)
downloadcpython-504b0bf066e4fddb21646331e89c2f6836c5c638.zip
cpython-504b0bf066e4fddb21646331e89c2f6836c5c638.tar.gz
cpython-504b0bf066e4fddb21646331e89c2f6836c5c638.tar.bz2
Checking in IDLE 0.2.
Much has changed -- too much, in fact, to write down. The big news is that there's a standard way to write IDLE extensions; see extend.txt. Some sample extensions have been provided, and some existing code has been converted to extensions. Probably the biggest new user feature is a new search dialog with more options, search and replace, and even search in files (grep). This is exactly as downloaded from my laptop after returning from the holidays -- it hasn't even been tested on Unix yet.
Diffstat (limited to 'Tools/idle/AutoIndent.py')
-rw-r--r--Tools/idle/AutoIndent.py142
1 files changed, 118 insertions, 24 deletions
diff --git a/Tools/idle/AutoIndent.py b/Tools/idle/AutoIndent.py
index d800589..329f492 100644
--- a/Tools/idle/AutoIndent.py
+++ b/Tools/idle/AutoIndent.py
@@ -1,16 +1,81 @@
import string
+from Tkinter import TclError
+
+###$ event <<newline-and-indent>>
+###$ win <Key-Return>
+###$ win <KP_Enter>
+###$ unix <Key-Return>
+###$ unix <KP_Enter>
+
+###$ event <<indent-region>>
+###$ win <Control-bracketright>
+###$ unix <Alt-bracketright>
+###$ unix <Control-bracketright>
+
+###$ event <<dedent-region>>
+###$ win <Control-bracketleft>
+###$ unix <Alt-bracketleft>
+###$ unix <Control-bracketleft>
+
+###$ event <<comment-region>>
+###$ win <Alt-Key-3>
+###$ unix <Alt-Key-3>
+
+###$ event <<uncomment-region>>
+###$ win <Alt-Key-4>
+###$ unix <Alt-Key-4>
+
+###$ event <<tabify-region>>
+###$ win <Alt-Key-5>
+###$ unix <Alt-Key-5>
+
+###$ event <<untabify-region>>
+###$ win <Alt-Key-6>
+###$ unix <Alt-Key-6>
class AutoIndent:
- def __init__(self, text, prefertabs=0, spaceindent=4*" "):
- self.text = text
- self.prefertabs = prefertabs
- self.spaceindent = spaceindent
- text.bind("<<newline-and-indent>>", self.autoindent)
- text.bind("<<indent-region>>", self.indentregion)
- text.bind("<<dedent-region>>", self.dedentregion)
- text.bind("<<comment-region>>", self.commentregion)
- text.bind("<<uncomment-region>>", self.uncommentregion)
+ menudefs = [
+ ('edit', [
+ None,
+ ('_Indent region', '<<indent-region>>'),
+ ('_Dedent region', '<<dedent-region>>'),
+ ('Comment _out region', '<<comment-region>>'),
+ ('U_ncomment region', '<<uncomment-region>>'),
+ ('Tabify region', '<<tabify-region>>'),
+ ('Untabify region', '<<untabify-region>>'),
+ ]),
+ ]
+
+ windows_keydefs = {
+ '<<newline-and-indent>>': ['<Key-Return>', '<KP_Enter>'],
+ '<<indent-region>>': ['<Control-bracketright>'],
+ '<<dedent-region>>': ['<Control-bracketleft>'],
+ '<<comment-region>>': ['<Alt-Key-3>'],
+ '<<uncomment-region>>': ['<Alt-Key-4>'],
+ '<<tabify-region>>': ['<Alt-Key-5>'],
+ '<<untabify-region>>': ['<Alt-Key-6>'],
+ }
+
+ unix_keydefs = {
+ '<<newline-and-indent>>': ['<Key-Return>', '<KP_Enter>'],
+ '<<indent-region>>': ['<Alt-bracketright>',
+ '<Meta-bracketright>',
+ '<Control-bracketright>'],
+ '<<dedent-region>>': ['<Alt-bracketleft>',
+ '<Meta-bracketleft>',
+ '<Control-bracketleft>'],
+ '<<comment-region>>': ['<Alt-Key-3>', '<Meta-Key-3>'],
+ '<<uncomment-region>>': ['<Alt-Key-4>', '<Meta-Key-4>'],
+ '<<tabify-region>>': ['<Alt-Key-5>', '<Meta-Key-5>'],
+ '<<untabify-region>>': ['<Alt-Key-6>', '<Meta-Key-6>'],
+ }
+
+ prefertabs = 0
+ spaceindent = 4*" "
+
+ def __init__(self, editwin):
+ self.text = editwin.text
def config(self, **options):
for key, value in options.items():
@@ -21,8 +86,16 @@ class AutoIndent:
else:
raise KeyError, "bad option name: %s" % `key`
- def autoindent(self, event):
+ def newline_and_indent_event(self, event):
text = self.text
+ try:
+ first = text.index("sel.first")
+ last = text.index("sel.last")
+ except TclError:
+ first = last = None
+ if first and last:
+ text.delete(first, last)
+ text.mark_set("insert", first)
line = text.get("insert linestart", "insert")
i, n = 0, len(line)
while i < n and line[i] in " \t":
@@ -43,8 +116,10 @@ class AutoIndent:
text.see("insert")
return "break"
- def indentregion(self, event):
- head, tail, chars, lines = self.getregion()
+ auto_indent = newline_and_indent_event
+
+ def indent_region_event(self, event):
+ head, tail, chars, lines = self.get_region()
for pos in range(len(lines)):
line = lines[pos]
if line:
@@ -53,11 +128,11 @@ class AutoIndent:
i = i+1
line = line[:i] + " " + line[i:]
lines[pos] = line
- self.setregion(head, tail, chars, lines)
+ self.set_region(head, tail, chars, lines)
return "break"
- def dedentregion(self, event):
- head, tail, chars, lines = self.getregion()
+ def dedent_region_event(self, event):
+ head, tail, chars, lines = self.get_region()
for pos in range(len(lines)):
line = lines[pos]
if line:
@@ -75,20 +150,20 @@ class AutoIndent:
indent = indent[:-4]
line = indent + line
lines[pos] = line
- self.setregion(head, tail, chars, lines)
+ self.set_region(head, tail, chars, lines)
return "break"
- def commentregion(self, event):
- head, tail, chars, lines = self.getregion()
+ def comment_region_event(self, event):
+ head, tail, chars, lines = self.get_region()
for pos in range(len(lines)):
line = lines[pos]
if not line:
continue
lines[pos] = '##' + line
- self.setregion(head, tail, chars, lines)
+ self.set_region(head, tail, chars, lines)
- def uncommentregion(self, event):
- head, tail, chars, lines = self.getregion()
+ def uncomment_region_event(self, event):
+ head, tail, chars, lines = self.get_region()
for pos in range(len(lines)):
line = lines[pos]
if not line:
@@ -98,9 +173,19 @@ class AutoIndent:
elif line[:1] == '#':
line = line[1:]
lines[pos] = line
- self.setregion(head, tail, chars, lines)
+ self.set_region(head, tail, chars, lines)
- def getregion(self):
+ def tabify_region_event(self, event):
+ head, tail, chars, lines = self.get_region()
+ lines = map(tabify, lines)
+ self.set_region(head, tail, chars, lines)
+
+ def untabify_region_event(self, event):
+ head, tail, chars, lines = self.get_region()
+ lines = map(string.expandtabs, lines)
+ self.set_region(head, tail, chars, lines)
+
+ def get_region(self):
text = self.text
head = text.index("sel.first linestart")
tail = text.index("sel.last -1c lineend +1c")
@@ -111,7 +196,7 @@ class AutoIndent:
lines = string.split(chars, "\n")
return head, tail, chars, lines
- def setregion(self, head, tail, chars, lines):
+ def set_region(self, head, tail, chars, lines):
text = self.text
newchars = string.join(lines, "\n")
if newchars == chars:
@@ -122,3 +207,12 @@ class AutoIndent:
text.delete(head, tail)
text.insert(head, newchars)
text.tag_add("sel", head, "insert")
+
+def tabify(line, tabsize=8):
+ spaces = tabsize * ' '
+ for i in range(0, len(line), tabsize):
+ if line[i:i+tabsize] != spaces:
+ break
+ else:
+ i = len(line)
+ return '\t' * (i/tabsize) + line[i:]