Skip to content

Commit 491dd44

Browse files
author
Paolo Prete
committed
add autoformat/indent
1 parent b5094ac commit 491dd44

31 files changed

+473
-52
lines changed

NEWS.txt

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
--------------
2+
Version 1.30
3+
--------------
4+
* add autoformat/indent
5+
16
--------------
27
Version 1.25
38
--------------

documentation/miscellaneous.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ You can add them by setting "compile-additional-opts" configuration parameter:
2323
click on "TOOLS--->Set configuration parameter", then insert "compile-additional-opts" as parameter, then insert all the additional options you need in the same entry. For example: "-I /my/include/directory -l WARN".
2424
Don't forget to restart the SpontiniServer after the parameter is set.
2525

26+
### AUTOFORMAT/INDENT
27+
28+
It is possible to format or simply auto-indent LilyPond code through various options:
29+
30+
* By selecting a code snippet and clicking on "TOOLS--->Format/Indent selection"
31+
* By enabling/disabling the auto-format/auto-indent option from the "TOOLS" menu ("TOOLS--->Enable/Disable autoformat", "TOOLS--->Enable/Disable autoindent"). Each time the file being edited is saved, the code will automatically be auto-formatted/auto-indented.
32+
* By configuring the editor to have auto-format/auto-indent enabled/disabled by default: click on "TOOLS->Set configuration parameter", and enter "default-autoformat" (or "default-autoindent") as the parameter to configure and "on" or "off" as the value. Remember to restart SpontiniServer.
33+
* If auto-format is enabled by default, it can be avoided for one or more sections of code that are between two lines starting with "% set autoformat off" and "% set autoformat on" (or "% set autoindent off" and "% set autoindent on"). If a line containing "% set autoformat off" (or "% set autoindent off") is placed at the beginning of the file, then auto-formatting/auto-indenting will be avoided for the entire file without needing to use the closing line ("% set autoformat on" or "% set autoindent on").
34+
Be careful to insert a space between "%" and "set" for each of these lines.
35+
2636
### AXES
2737

2838
Once a score is loaded and rendered, four draggable axes, useful for graphically aligning objects, can be activated by selecting "TOOLS--->Toggle axes"
@@ -105,7 +115,7 @@ Then the svg drop-down menu is displayed after a double click on the left mouse
105115

106116
You can check the LilyPond compiling output in two ways:
107117

108-
* On the Javascript console of your browser (open "Web developer" ---> "Web console" on Firefox and "More tools" ---> "Developer tools" on Chrome/Chromium).
118+
* On the Javascript console of your browser (open "Web developer" ---> "Web console" on Firefox and "More tools" ---> "Developer tools" on Chrome/Chromium)
109119
* On the Spontini-Server's window or on the Spontini-Server's output (if the server is launched with "nogui" option)
110120

111121
### PAN-ZOOM

examples/easy-cross-staff-example-2.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (24/01/2020) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

examples/easy-cross-staff-example-3.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (24/01/2020) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

examples/easy-cross-staff-example-4.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (24/06/2020) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

examples/easy-cross-staff-example-5.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (24/06/2020) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

examples/easy-cross-staff-example-6.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (24/06/2020) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

examples/easy-cross-staff-example-7.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (24/06/2020) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

examples/easy-cross-staff-example-8.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (24/01/2020) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

examples/easy-cross-staff-example-9.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (24/01/2020) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

examples/filter-example-1.ly

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (16/10/2021) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.
@@ -72,4 +73,4 @@ r1 r \break
7273
c'4 d' e' f' \break
7374
\SECTIONEND
7475

75-
}
76+
}

examples/filter-example-2.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (28/11//2021) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

examples/filter-example-3.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (16/10/2021) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

examples/oso-padding-example-1.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (24/01/2020) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

examples/oso-padding-example-2.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (24/01/2020) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

examples/padding-on-brackets-example.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (24/01/2020) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

examples/pdf-manipulate-example.ly

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
% pdf-manipulate-example-postengraving.py, inside the example's directory, will be executed
2121
% and it will add a string + a vector image + a raster image on the generated PDF page.
2222
%
23-
% NOTE: don't engrave this example in SVG MODE, otherwise the python script will fail.
23+
% NOTE: don't engrave this example in SVG MODE, otherwise the python script will fail.
2424
%
2525
% In general, given a FILENAME.ly file, You can associate to it two scripts that will be executed
2626
% before and after the creation of the PDF: their filename must be in the form FILENAME-beforepdf.suffix

examples/python-to-spontini-example.ly

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ scriptOutput = argv[1]
3232
expr = ' '
3333
for i in range (8):
3434
expr +='a '
35-
35+
3636
scriptOutput[0] = expr
3737
3838
%}
@@ -44,5 +44,5 @@ scriptOutput[0] = expr
4444
\version "2.19.84"
4545

4646
{
47-
%{__BLOCK__1%} a a a a a a a a %{%}
47+
%{__BLOCK__1%} a a a a a a a a %{%}
4848
}

examples/set-properties-example-1.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (24/01/2020) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

examples/set-properties-example-2.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (24/01/2020) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

examples/spanned-pedals-example.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (24/01/2020) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

examples/string-quartet-template-example.ly

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
% set autoformat off
12
%
23
% Created (10/10/2020) by Paolo-Prete.
34
% This file is part of Spontini-Editor project.

lib/python/python_ly_utils.py

+112
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import ly.words
2323
import ly.lex.lilypond
2424
import ly.pitch.transpose
25+
import ly.indent
26+
import ly.reformat
27+
import re
2528

2629
def removeUnwantedSpacesInChord(text):
2730

@@ -72,6 +75,115 @@ def removeChordOnSingleNote(text, lang):
7275

7376
return ret
7477

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+
75187
def orderPitchesInChord(text, lang):
76188

77189
ret = text

lib/python/spontini_server_core.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
from lib.python.python_ly_utils import translatePitches
6262
from lib.python.python_ly_utils import orderPitchesInChord
6363
from lib.python.python_ly_utils import convertPitchesToEnharmonic
64+
from lib.python.python_ly_utils import formatOrIndentOnlyScore
6465
from lib.python.spontini_server_utils import *
6566
except:
6667
# if the above imports fail, we are calling the ASGI server trhough CLI, then the following imports are valid:
@@ -69,6 +70,7 @@
6970
from python_ly_utils import translatePitches
7071
from python_ly_utils import orderPitchesInChord
7172
from python_ly_utils import convertPitchesToEnharmonic
73+
from python_ly_utils import formatOrIndentOnlyScore
7274
from spontini_server_utils import *
7375

7476
os.chdir(os.path.join(getLibPythonPath(), '..'))
@@ -96,7 +98,10 @@
9698
DEFAULT_MODE_PARAM = "default-mode"
9799
COMPILE_ADDITIONAL_OPTS_PARAM = "compile-additional-opts"
98100
DEFAULT_MIDI_INPUT_CHANNEL_PARAM = "default-midi-input-channel"
99-
configurableParams = [WORKSPACE_PARAM, VERSION_PARAM, CAN_CONFIG_FROM_NON_LOCALHOST_PARAM, FORK_ACCESS_ONLY_PARAM, DEBUG_PARAM, LILYPOND_EXEC_PARAM, INKSCAPE_EXEC_PARAM, MIDI_ENABLED_PARAM, DEFAULT_MIDI_INPUT_CHANNEL_PARAM, SOUNDFONT_URL_PARAM, DEFAULT_MODE_PARAM, COMPILE_ADDITIONAL_OPTS_PARAM]
101+
DEFAULT_AUTOFORMAT_PARAM = "default-autoformat"
102+
DEFAULT_AUTOINDENT_PARAM = "default-autoindent"
103+
configurableParams = [WORKSPACE_PARAM, VERSION_PARAM, CAN_CONFIG_FROM_NON_LOCALHOST_PARAM, FORK_ACCESS_ONLY_PARAM, DEBUG_PARAM,
104+
LILYPOND_EXEC_PARAM, INKSCAPE_EXEC_PARAM, MIDI_ENABLED_PARAM, DEFAULT_MIDI_INPUT_CHANNEL_PARAM, SOUNDFONT_URL_PARAM, DEFAULT_MODE_PARAM, COMPILE_ADDITIONAL_OPTS_PARAM, DEFAULT_AUTOFORMAT_PARAM, DEFAULT_AUTOINDENT_PARAM]
100105

101106
debug = True
102107
savedConFilename = "saved-config.txt"
@@ -198,6 +203,8 @@ def readConfigParams():
198203
global INKSCAPE_EXEC_PARAM
199204
global MIDI_ENABLED_PARAM
200205
global DEFAULT_MIDI_INPUT_CHANNEL_PARAM
206+
global DEFAULT_AUTOFORMAT_PARAM
207+
global DEFAULT_AUTOINDENT_PARAM
201208
global DEFAULT_MODE_PARAM
202209
global SOUNDFONT_URL_PARAM
203210
global COMPILE_ADDITIONAL_OPTS_PARAM
@@ -223,6 +230,8 @@ def readConfigParams():
223230
setConfigParam(INKSCAPE_EXEC_PARAM, "")
224231
setConfigParam(DEFAULT_MODE_PARAM, "svg")
225232
setConfigParam(DEFAULT_MIDI_INPUT_CHANNEL_PARAM, "-1")
233+
setConfigParam(DEFAULT_AUTOFORMAT_PARAM, "off")
234+
setConfigParam(DEFAULT_AUTOINDENT_PARAM, "off")
226235
confFile.close()
227236

228237
with open(savedConFilenameWithPath) as fp:
@@ -1521,6 +1530,21 @@ def doPostSync(message, request):
15211530
pass
15221531
return sendCompleteResponse("OK", ret)
15231532

1533+
if message['cmd'] == 'FORMAT' or message['cmd'] == 'INDENT':
1534+
ret = ""
1535+
if not checkMsgStructure(message, 2):
1536+
return sendMalformedMsgResponse()
1537+
try:
1538+
indentOnly = False
1539+
if message['cmd'] == "INDENT":
1540+
indentOnly = True
1541+
editorCurs = editorCursorList = [int(x) for x in message['param2'].split(',')]
1542+
ret, curs, badLine = formatOrIndentOnlyScore(message['param1'], editorCursorList, indentOnly)
1543+
except:
1544+
log(clientInfo + traceback.format_exc(), "E")
1545+
1546+
return sendCompleteResponse("OK", ret + sepTkn + str(curs[0])+","+str(curs[1]) + sepTkn + str(badLine))
1547+
15241548
if message['cmd'] == 'MERGE_TABLE_CELLS':
15251549
ret = ""
15261550
status = "OK"

lib/version.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.25_alfa
1+
1.30_alfa

lib/webgui/js/global-stuff.js

+2
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ var currMouseCoordsOnSVG = {
121121
}
122122

123123
var isHTML = false
124+
var autoFormat = false
125+
var autoIndent = false
124126

125127
function replaceAll(string, search, replace) {
126128
return string.split(search).join(replace);

lib/webgui/js/main.js

+18
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ else
8383
}
8484

8585
var data = {
86+
cmd: 'GET_CONFIG_PARAM',
87+
param1: "default-autoformat"
88+
}
89+
sendMsgToSpontiniServer(data, function(statusTxt, responseTxt) {
90+
if (responseTxt == "on")
91+
autoFormat = true
92+
})
93+
94+
data = {
95+
cmd: 'GET_CONFIG_PARAM',
96+
param1: "default-autoindent"
97+
}
98+
sendMsgToSpontiniServer(data, function(statusTxt, responseTxt) {
99+
if (responseTxt == "on")
100+
autoIndent = true
101+
})
102+
103+
data = {
86104
cmd: 'GET_CONFIG_PARAM',
87105
param1: "default-mode"
88106
}

0 commit comments

Comments
 (0)