Debian Codegraph @

src/Scripts/Editor/Indent.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # This script indents the selection on the current document
3 # or the whole document if nothing is selected
4
5 import PythonQt.QtGui as pqt
6
7 selection = q.getSelectedText() # Get current selection
8 # Careful, you must select a whole instrument block, or you will get strange indentation
9 if selection == "": # If no selection
10 orcText = q.getOrc() # use complete orc section
11 else:
12 orcText = selection
13
14 orcLines = orcText.splitlines() # split lines
15
16 newOrcText = ""
17 level = 0 # indentation level for the current line
18 newlevel = 0 # indentation level for following lines
19
20 for line in orcLines: # go through all the lines
21 line = line.strip() # remove initial and trailing whitespace
22
23 if line.find("instr") == 0:
24 level = 0
25 newlevel = 1
26 elif line.find("endin") == 0:
27 level = 0
28 elif line.find("if ") == 0:
29 newlevel = level + 1
30 elif line.find("elseif ") == 0:
31 level -= 1
32 newlevel = level + 1
33 elif line.find("endif") == 0:
34 level -= 1
35 newlevel = level
36 newOrcText += "\t" * level # add necessary tabs
37 newOrcText += line + "\n"
38 level = newlevel
39
40 if (q.getFileName().endswith('.csd')):
41 if (selection == ""):
42 q.setOrc(newOrcText) # Write all the orchestra section
43 pqt.QMessageBox.information(0, 'Indent script',
44 'Selection has been indented')
45 else:
46 if selection[-1] != "\n": # If selection doesn't end with line ending
47 newOrcText = newOrcText[0:-2] # Remove extra line ending
48 q.insertText(newOrcText) # Peplaces the current selection
49 pqt.QMessageBox.information(0, 'Indent script',
50 'All document indented')
51 else:
52 pqt.QMessageBox.information(0, 'Indent script',
53 'Not a csd file!')
54