summaryrefslogtreecommitdiffstats
path: root/Tools/idle/AutoIndent.py
blob: d800589bc51fda4adfd1ec3cff4ad9470b953358 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import string

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)

    def config(self, **options):
        for key, value in options.items():
            if key == 'prefertabs':
                self.prefertabs = value
            elif key == 'spaceindent':
                self.spaceindent = value
            else:
                raise KeyError, "bad option name: %s" % `key`

    def autoindent(self, event):
        text = self.text
        line = text.get("insert linestart", "insert")
        i, n = 0, len(line)
        while i < n and line[i] in " \t":
            i = i+1
        indent = line[:i]
        lastchar = text.get("insert -1c")
        if lastchar == ":":
            if not indent:
                if self.prefertabs:
                    indent = "\t"
                else:
                    indent = self.spaceindent
            elif indent[-1] == "\t":
                indent = indent + "\t"
            else:
                indent = indent + self.spaceindent
        text.insert("insert", "\n" + indent)
        text.see("insert")
        return "break"

    def indentregion(self, event):
        head, tail, chars, lines = self.getregion()
        for pos in range(len(lines)):
            line = lines[pos]
            if line:
                i, n = 0, len(line)
                while i < n and line[i] in " \t":
                    i = i+1
                line = line[:i] + "    " + line[i:]
                lines[pos] = line
        self.setregion(head, tail, chars, lines)
        return "break"

    def dedentregion(self, event):
        head, tail, chars, lines = self.getregion()
        for pos in range(len(lines)):
            line = lines[pos]
            if line:
                i, n = 0, len(line)
                while i < n and line[i] in " \t":
                    i = i+1
                indent, line = line[:i], line[i:]
                if indent:
                    if indent == "\t" or indent[-2:] == "\t\t":
                        indent = indent[:-1] + "    "
                    elif indent[-4:] == "    ":
                        indent = indent[:-4]
                    else:
                        indent = string.expandtabs(indent, 8)
                        indent = indent[:-4]
                    line = indent + line
                lines[pos] = line
        self.setregion(head, tail, chars, lines)
        return "break"

    def commentregion(self, event):
        head, tail, chars, lines = self.getregion()
        for pos in range(len(lines)):
            line = lines[pos]
            if not line:
                continue
            lines[pos] = '##' + line
        self.setregion(head, tail, chars, lines)

    def uncommentregion(self, event):
        head, tail, chars, lines = self.getregion()
        for pos in range(len(lines)):
            line = lines[pos]
            if not line:
                continue
            if line[:2] == '##':
                line = line[2:]
            elif line[:1] == '#':
                line = line[1:]
            lines[pos] = line
        self.setregion(head, tail, chars, lines)

    def getregion(self):
        text = self.text
        head = text.index("sel.first linestart")
        tail = text.index("sel.last -1c lineend +1c")
        if not (head and tail):
            head = text.index("insert linestart")
            tail = text.index("insert lineend +1c")
        chars = text.get(head, tail)
        lines = string.split(chars, "\n")
        return head, tail, chars, lines

    def setregion(self, head, tail, chars, lines):
        text = self.text
        newchars = string.join(lines, "\n")
        if newchars == chars:
            text.bell()
            return
        text.tag_remove("sel", "1.0", "end")
        text.mark_set("insert", head)
        text.delete(head, tail)
        text.insert(head, newchars)
        text.tag_add("sel", head, "insert")