File: indent.py

package info (click to toggle)
frescobaldi 1.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,240 kB
  • ctags: 2,434
  • sloc: python: 15,614; lisp: 28; sh: 25; makefile: 2
file content (261 lines) | stat: -rw-r--r-- 9,566 bytes parent folder | download
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
#
# Copyright (c) 2008, 2009, 2010 by Wilbert Berendsen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# See http://www.gnu.org/licenses/ for more information.

from __future__ import unicode_literals

"""
Indent LilyPond input.

Recognizes common LilyPond mode and Scheme mode.

This module is not dependent on any other module,
besides the Python standard re module.
"""

import re

# tokens to look for in LilyPond mode
lily_re = (
    r"(?P<indent>\{|<<)"
    r"|(?P<dedent>>>|\})"
    r'|(?P<string>"(\\[\\"]|[^"])*")'
    r"|(?P<newline>\n[^\S\n]*)"
    r"|(?P<space>[^\S\n]+)"
    r"|(?P<scheme>#)"
    r"|(?P<blockcomment>%\{.*?%\})"
    r"|(?P<longcomment>%%%[^\n]*)"
    r"|(?P<comment>%[^\n]*)"
    )

# tokens to look for in Scheme mode
scheme_re = (
    r"(?P<indent>\()"
    r"|(?P<dedent>\))"
    r'|(?P<string>"(\\[\\"]|[^"])*")'
    r"|(?P<newline>\n[^\S\n]*)"
    r"|(?P<space>[^\S\n]+)"
    r"|(?P<lilypond>#\{)"
    r"|(?P<longcomment>;;;[^\n]*)"
    r"|(?P<blockcomment>#!.*?!#)"
    r"|(?P<comment>;[^\n]*)"
    )

# tokens to look for in LilyPond-inside-Scheme mode
schemelily_re = r"(?P<backtoscheme>#\})|" + lily_re


# Parse LilyPond text
lily = re.compile(lily_re, re.DOTALL)

# Parse LilyPond-in-Scheme text
schemelily = re.compile(schemelily_re, re.DOTALL)

# Parse Scheme text, instantiate to keep state (depth)
class scheme:
    search = re.compile(scheme_re, re.DOTALL).search
    depth = 0


# searches for indent inside a string
indent_rx = re.compile(r'\n([^\S\n]*)')

    
def indent(text,
        start = None,
        indentwidth = 2,
        tabwidth = 8,
        usetabs = None,
        startscheme = False
        ):
    """
    Properly indents the LilyPond input in text.
    
    If start is an integer value, use that value as the indentwidth to start
    with, disregarding the current indent of the first line.
    If it is None, use the indent of the first line.
    
    indentwidth: how many positions to indent (default 2)
    tabwidth: width of a tab character
    usetabs: whether to use tab characters in the indent:
        - None = determine from document
        - True = use tabs for the parts of the indent that exceed the tab width
        - False = don't use tabs.
    startscheme: start in scheme mode (not very robust)
    """
    
    # record length of indent of first line
    space = re.match(r'[^\S\n]*', text).group()
    if start is None:
        start = len(space.expandtabs(tabwidth))
    if usetabs is None:
        usetabs = '\t' in space or '\n\t' in text
    
    mode = [lily]       # the mode to parse in
    indent = [start]    # stack with indent history
    pos = len(space)    # start position in text
    output = []         # list of output lines
    
    if startscheme:
        mode.append(scheme())
    if usetabs:
        makeindent = lambda i: '\t' * int(i / tabwidth) + ' ' * (i % tabwidth)
    else:
        makeindent = lambda i: ' ' * i
    
    line = []           # list to build the output, per line
    curindent = -1      # current indent in count of spaces, -1 : not yet set
    
    # Search the text from the previous position
    # (very fast: does not alter the string in text)
    m = mode[-1].search(text, pos)
    while m:
        # also append text before the found token
        more = pos < m.start()
        if more:
            line.append(text[pos:m.start()])
        
        # type, text, and new position for next search
        item, token, pos = m.lastgroup, m.group(), m.end()

        # If indent not yet determined, set it to 0 if we found a long comment
        # (with three or more %%% or ;;; characters). Was any other text found,
        # keep the current indent level for the current line.
        # (Our current indent can change if our line starts with dedent tokens.)
        if curindent == -1:
            if item == 'longcomment':
                curindent = 0
            elif (more or item not in ('dedent', 'space', 'backtoscheme')):
                curindent = indent[-1]
        
        # Check if we found a multiline block comment.
        # Thoses are handled specially. Indents inside the block comment are
        # preserved but positioned as close as possible to the current indent.
        # So the algorithm cuts the shortest indent off from all lines and then
        # adds the current indent.
        if item == 'blockcomment' and '\n' in token:
            # Find the shortest indent inside the block comment
            shortest = min(len(n.group(1).expandtabs(tabwidth))
                for n in indent_rx.finditer(token))
            # Remove that indent from all lines
            fixindent = lambda n: '\n' + makeindent(
                curindent - shortest + len(n.group(1).expandtabs(tabwidth)))
            token = indent_rx.sub(fixindent, token)
        
        elif mode[-1] in (lily, schemelily):
            # we are parsing in LilyPond mode.
            if item == 'indent':
                indent.append(indent[-1] + indentwidth)
            elif item == 'dedent' and len(indent) > 1:
                indent.pop()
            elif item == 'scheme':
                mode.append(scheme())       # enter scheme mode
            elif item == 'backtoscheme':
                indent.pop()
                mode.pop()                  # leave lilypond mode, back to scheme
        else:
            # we are parsing in Scheme mode.
            if item == 'indent':
                mode[-1].depth += 1         # count parentheses
                # look max 10 characters ahead to vertically align opening
                # parentheses, but stop at closing parenthesis, quote or newline.
                n = re.search(r'[()"\n]', text[pos:pos+10])
                if n and n.group() == '(':
                    indent.append(indent[-1] + n.start() + 1)
                else:
                    indent.append(indent[-1] + indentwidth)
                
            elif item == 'dedent':
                if mode[-1].depth:
                    indent.pop()
                if mode[-1].depth <= 1:
                    mode.pop()              # leave scheme mode
                else:
                    mode[-1].depth -= 1     # count parentheses backwards
            elif item == 'lilypond':
                mode.append(schemelily)     # enter lilypond-in-scheme mode
                indent.append(indent[-1] + indentwidth)
            elif mode[-1].depth == 0:
                # jump out if we got one atom or are at a space or end of line
                # and still no opening parenthesis. But stay if we only just
                # had a hash(#).
                if (item in ('string', 'comment', 'longcomment')
                    or (more and item in ('newline', 'space'))):
                    mode.pop()
        
        if item == 'newline':
            # Write out the line
            output.append(makeindent(curindent) + ''.join(line))
            line = []
            curindent = -1
        else:
            line.append(token)
        
        # On to the next token
        m = mode[-1].search(text, pos)
    
    # Still some text left?
    if pos < len(text):
        line.append(text[pos:])
    if line:
        if curindent == -1:
            curindent = indent[-1]
        output.append(makeindent(curindent) + ''.join(line))
    else:
        output.append(makeindent(start))
    # Return formatted output
    return '\n'.join(output)

    
if __name__ == '__main__':
    
    import sys, optparse
    
    op = optparse.OptionParser(usage='usage: %prog [options] [filename]')
    op.add_option('-o', '--output',
        help='write to this file instead of standard output')
    op.add_option('-i', '--indent-width', type='int', default=2,
        help='indent width in characters to use [default: %default]')
    op.add_option('-t', '--tab-width', type='int', default=8,
        help='tab width to assume [default: %default]')
    op.add_option('-s', '--start-indent', type='int', default=0,
        help='start indent [default: %default]')
    op.add_option('--scheme', action='store_true',
        help='start indenting in Scheme mode')
    op.add_option('-u', '--use-tabs', action='store_true',
        help='use tabs instead of spaces for indent')
    options, args = op.parse_args()
    # TODO: encoding
    # TODO: error handling
    infile = args and open(args[0]) or sys.stdin
    text = infile.read()
    text = indent(text,
        start=options.start_indent,
        indentwidth=options.indent_width,
        tabwidth=options.tab_width,
        usetabs=options.use_tabs,
        startscheme=options.scheme
        )
    outfile = options.output and (options.output) or sys.stdout
    outfile.write(text)
    
    if infile is not sys.stdin:
        infile.close()
    if outfile is not sys.stdout:    
        outfile.close()
    sys.exit(0)