| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
a |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import PythonQt.QtGui as pqt |
| 10 |
|
| 11 |
|
| 12 |
excpList = ('instr', 'endin', 'if', 'elseif', 'endif', 'opcode', 'endop', 'sr', 'ksmps', 'kr', 'nchnls', '0dbfs', 'until') |
| 13 |
|
| 14 |
def exceptions(line, excptlis): |
| 15 |
"""returns t if line starts with a word from excptlis""" |
| 16 |
firstWord = line.split()[0] |
| 17 |
if firstWord in excptlis: |
| 18 |
return True |
| 19 |
else: |
| 20 |
return False |
| 21 |
|
| 22 |
def long_comment(line, prevState): |
| 23 |
"""returns 1 if a '/* ...*/' comment is active, 0 otherwise""" |
| 24 |
if line.rstrip()[-2:] == '*/': |
| 25 |
return 0 |
| 26 |
elif line.lstrip()[0:2] == '/*': |
| 27 |
return 1 |
| 28 |
else: |
| 29 |
return prevState |
| 30 |
|
| 31 |
def comment(line, longComment): |
| 32 |
"""returns t if line is a comment""" |
| 33 |
if longComment == 1 or line.lstrip()[0] == ';' or line.rstrip()[-2:] == '*/': |
| 34 |
return True |
| 35 |
else: |
| 36 |
return False |
| 37 |
|
| 38 |
def listUdos(orcText): |
| 39 |
"""list all udo names in orcText""" |
| 40 |
res = [] |
| 41 |
prev = 0 |
| 42 |
for line in orcText.splitlines(): |
| 43 |
long = long_comment(line, prev) |
| 44 |
if line.strip() and not comment(line, long): |
| 45 |
|
| 46 |
if line.split()[0] == "instr": |
| 47 |
break |
| 48 |
|
| 49 |
elif line.split()[0] == "opcode": |
| 50 |
udonam = line.split()[1].rstrip(',') |
| 51 |
res.append(udonam) |
| 52 |
prev = long |
| 53 |
return res |
| 54 |
|
| 55 |
|
| 56 |
def indent(): |
| 57 |
selection = q.getSelectedText() |
| 58 |
udoList = listUdos(q.getOrc()) |
| 59 |
if selection == "": |
| 60 |
orcText = q.getOrc() |
| 61 |
else: |
| 62 |
orcText = selection |
| 63 |
|
| 64 |
newOrcText = "" |
| 65 |
prev = 0 |
| 66 |
for line in orcText.splitlines(): |
| 67 |
|
| 68 |
longComment = long_comment(line, prev) |
| 69 |
if line.strip() == "" or comment(line, longComment) or exceptions(line, excpList): |
| 70 |
newline = line |
| 71 |
else: |
| 72 |
words = line.split() |
| 73 |
pos = 0 |
| 74 |
opcdpos = 0 |
| 75 |
firstarg = 0 |
| 76 |
opcd = 0 |
| 77 |
newline = "" |
| 78 |
for word in words: |
| 79 |
|
| 80 |
if q.opcodeExists(word) or word in udoList: |
| 81 |
|
| 82 |
if opcdpos < space_left.value: |
| 83 |
format = '%%-%ds%%s ' % space_left.value |
| 84 |
newline = format % (newline, word) |
| 85 |
opcdpos = space_left.value |
| 86 |
|
| 87 |
else: |
| 88 |
newline = '%s%s ' % (newline, word) |
| 89 |
pos = opcdpos + len(word) + 1 |
| 90 |
firstarg = 1 |
| 91 |
opcd = 1 |
| 92 |
|
| 93 |
elif firstarg == 1: |
| 94 |
if pos - opcdpos < space_left.value and pos < space_right.value: |
| 95 |
format = '%%-%ds%%s ' % space_right.value |
| 96 |
newline = format % (newline, word) |
| 97 |
pos = space_right.value + len(word) + 1 |
| 98 |
else: |
| 99 |
newline = '%s%s ' % (newline, word) |
| 100 |
pos = pos + len(word) + 1 |
| 101 |
firstarg = 0 |
| 102 |
|
| 103 |
else: |
| 104 |
newline = '%s%s ' % (newline, word) |
| 105 |
pos = pos + len(word) + 1 |
| 106 |
if opcd == 0: |
| 107 |
opcdpos = pos |
| 108 |
newOrcText = "%s\n%s" % (newOrcText, newline.rstrip()) |
| 109 |
prev = longComment |
| 110 |
|
| 111 |
|
| 112 |
newOrcText = newOrcText[1:] |
| 113 |
|
| 114 |
if (selection == ""): |
| 115 |
q.setOrc(newOrcText) |
| 116 |
else: |
| 117 |
q.insertText(newOrcText) |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
w = pqt.QWidget() |
| 122 |
w.setGeometry(50,50, 400,450) |
| 123 |
l = pqt.QGridLayout(w) |
| 124 |
w.setLayout(l) |
| 125 |
w.setWindowTitle("Csound Code Indentation") |
| 126 |
|
| 127 |
space_left = pqt.QSpinBox(w) |
| 128 |
space_left.setValue(11) |
| 129 |
l.addWidget(space_left, 2, 0) |
| 130 |
|
| 131 |
space_right = pqt.QSpinBox(w) |
| 132 |
space_right.setValue(22) |
| 133 |
l.addWidget(space_right, 3, 0) |
| 134 |
|
| 135 |
renderButton = pqt.QPushButton("Indent!",w) |
| 136 |
text = pqt.QTextBrowser(w) |
| 137 |
l.addWidget(renderButton, 4, 0) |
| 138 |
l.addWidget(text, 1, 0) |
| 139 |
renderButton.connect("clicked()", indent) |
| 140 |
|
| 141 |
info = """Indents the opcode name at 11 spaces from the left |
| 142 |
and the first argument at 22 spaces, if possible: |
| 143 |
output1 opcode1 arg1, arg2 |
| 144 |
output2 opcode2 arg3 |
| 145 |
(Instead of 11 / 22, you can choose other values in the spin boxes below.) |
| 146 |
|
| 147 |
Lines starting with instr / endin, opcode / endop, if / elseif / endif will be left untouched. The same for comment lines or the statements in the orchestra header. |
| 148 |
|
| 149 |
Select any part of your orchestra code and click on the button above. If you do not select anything, the whole orchestra code will be cleaned up. |
| 150 |
|
| 151 |
Joachim Heintz 2012, using code by Andres Cabrera""" |
| 152 |
text.setText(info) |
| 153 |
|
| 154 |
w.show() |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|