|
22 | 22 | import ly.words
|
23 | 23 | import ly.lex.lilypond
|
24 | 24 | import ly.pitch.transpose
|
| 25 | +import ly.indent |
| 26 | +import ly.reformat |
| 27 | +import re |
25 | 28 |
|
26 | 29 | def removeUnwantedSpacesInChord(text):
|
27 | 30 |
|
@@ -72,6 +75,115 @@ def removeChordOnSingleNote(text, lang):
|
72 | 75 |
|
73 | 76 | return ret
|
74 | 77 |
|
| 78 | +def formatOrIndentOnlyScore(scoreToFormat, editorCurs, justIndent=False): |
| 79 | + |
| 80 | + def updateEditorCurs(i, editorCurs, editorCursOffs): |
| 81 | + editorCursInside = "" |
| 82 | + if i == editorCurs[0]: |
| 83 | + editorCursInside = [editorCursOffs, editorCurs[1]] |
| 84 | + editorCursOffs += 1 |
| 85 | + return editorCursInside, editorCursOffs |
| 86 | + |
| 87 | + errorLine = -1 |
| 88 | + if scoreToFormat.strip().startswith("% set autoformat off"): |
| 89 | + return [scoreToFormat, editorCurs, errorLine] |
| 90 | + |
| 91 | + lines = scoreToFormat.split('\n') |
| 92 | + |
| 93 | + blocks = [] |
| 94 | + modScoreLines = [] |
| 95 | + editorCursOffs = 0 |
| 96 | + obscureKey = "% __oOo__DONOTTOUCH__oOo__ %" |
| 97 | + i = 0 |
| 98 | + modBlocks = 0 |
| 99 | + |
| 100 | + editorCursInside = "" |
| 101 | + |
| 102 | + while i < len(lines): |
| 103 | + line = lines[i] |
| 104 | + if re.match(r'^\s*\\easyCrossStaff', line) or\ |
| 105 | + re.match(r'^\s*\% set autoformat off', line) or\ |
| 106 | + re.match(r'^\s*\% set autoindent off', line): |
| 107 | + endReg = r'^\s*#\'\(' |
| 108 | + if re.match(r'^\s*\% set autoformat off', line): |
| 109 | + endReg = r'^\s*\% set autoformat on' |
| 110 | + if re.match(r'^\s*\% set autoindent off', line): |
| 111 | + endReg = r'^\s*\% set autoindent on' |
| 112 | + iTemp = i |
| 113 | + endRegFound = False |
| 114 | + while i < len(lines): |
| 115 | + if re.match(endReg, lines[i]): |
| 116 | + endRegFound = True |
| 117 | + i += 1 |
| 118 | + if not endRegFound: |
| 119 | + return [scoreToFormat, editorCurs, iTemp + 1] |
| 120 | + |
| 121 | + i = iTemp |
| 122 | + blockStart = i |
| 123 | + if i == editorCurs[0]: |
| 124 | + editorCursInside = [editorCursOffs, editorCurs[1]] |
| 125 | + editorCursOffs += 1 |
| 126 | + # Trovare il blocco che termina con la riga che inizia con "#'(" |
| 127 | + while i < len(lines) and not re.match(endReg, lines[i]): |
| 128 | + editorCursInside, editorCursOffs = updateEditorCurs(i, editorCurs, editorCursOffs) |
| 129 | + i += 1 |
| 130 | + if i < len(lines): |
| 131 | + blockEnd = i |
| 132 | + editorCursInside, editorCursOffs = updateEditorCurs(i, editorCurs, editorCursOffs) |
| 133 | + blocks.append((blockStart, blockEnd, lines[blockStart:blockEnd + 1], obscureKey, editorCursInside)) |
| 134 | + if editorCursInside: |
| 135 | + editorCursInside = "" |
| 136 | + modScoreLines.append([obscureKey, False]) |
| 137 | + modBlocks += 1 |
| 138 | + elif re.match(r'^\s*\\tabularTwoStavesPoly', line): |
| 139 | + blockStart = i |
| 140 | + editorCursInside, editorCursOffs = updateEditorCurs(i, editorCurs, editorCursOffs) |
| 141 | + i += 1 |
| 142 | + while i < len(lines) and re.match(r'^\s*\{', lines[i]): |
| 143 | + editorCursInside, editorCursOffs = updateEditorCurs(i, editorCurs, editorCursOffs) |
| 144 | + i += 1 |
| 145 | + i -= 1 |
| 146 | + if i < len(lines): |
| 147 | + blockEnd = i |
| 148 | + blocks.append((blockStart, blockEnd, lines[blockStart:blockEnd + 1], obscureKey, editorCursInside)) |
| 149 | + if editorCursInside: |
| 150 | + editorCursInside = "" |
| 151 | + modScoreLines.append([obscureKey, False]) |
| 152 | + modBlocks += 1 |
| 153 | + else: |
| 154 | + modScoreLines.append([line, False]) |
| 155 | + i += 1 |
| 156 | + |
| 157 | + modScore = '\n'.join([item[0] for item in modScoreLines]) |
| 158 | + |
| 159 | + doc = ly.document.Document(modScore) |
| 160 | + curs = ly.document.Cursor(doc) |
| 161 | + indenter = ly.indent.Indenter() |
| 162 | + if justIndent: |
| 163 | + indenter.indent(curs) |
| 164 | + else: |
| 165 | + ly.reformat.reformat(curs, indenter) |
| 166 | + |
| 167 | + restoredLines = doc.plaintext().split('\n') |
| 168 | + |
| 169 | + blockCtr = 0 |
| 170 | + for i in range(0, len(restoredLines)): |
| 171 | + if restoredLines[i].strip().startswith(obscureKey): |
| 172 | + |
| 173 | + if blocks[blockCtr][4][0:]: |
| 174 | + editorCurs = [blocks[blockCtr][4][0:][0] + i, editorCurs[1]] |
| 175 | + |
| 176 | + restoredLines[i] = '\n'.join(blocks[blockCtr][2][0:]) |
| 177 | + blockCtr += 1 |
| 178 | + |
| 179 | + formattedScore = '\n'.join(restoredLines) |
| 180 | + |
| 181 | + # guard |
| 182 | + if formattedScore.strip() == "" or obscureKey in formattedScore: |
| 183 | + formattedScore = scoreToFormat |
| 184 | + |
| 185 | + return [formattedScore, editorCurs, errorLine] |
| 186 | + |
75 | 187 | def orderPitchesInChord(text, lang):
|
76 | 188 |
|
77 | 189 | ret = text
|
|
0 commit comments