--- /dev/null
+/*
+ file-name-map.cc -- implement map_file_name()
+
+ source file of the GNU LilyPond music typesetter
+
+ (c) 2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
+
+*/
+
+#include <map>
+
+#include "file-name-map.hh"
+#include "lily-guile.hh"
+
+std::map<String, String> file_name_map_global;
+
+String
+map_file_name (String s)
+{
+ if (file_name_map_global.find (s) != file_name_map_global.end ())
+ {
+ s = file_name_map_global[s];
+ }
+ return s;
+}
+
+LY_DEFINE (ly_add_file_name_alist, "ly:add-file-name-alist",
+ 1, 0, 0, (SCM alist),
+ "Add mappings for error messages from specified alist")
+{
+ for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
+ {
+ SCM key = scm_caar (s);
+ SCM val = scm_cdar (s);
+
+ file_name_map_global[ly_scm2string (key)] = ly_scm2string (val);
+ }
+ return SCM_UNSPECIFIED;
+}
+
return 0
class Substring (Chunk):
- def __init__ (self, source, start, end):
+ def __init__ (self, source, start, end, line_number):
self.source = source
self.start = start
self.end = end
+ self.line_number = line_number
def replacement_text (self):
return self.source[self.start:self.end]
class Snippet (Chunk):
- def __init__ (self, type, match, format):
+ def __init__ (self, type, match, format, line_number):
self.type = type
self.match = match
self.hash = 0
self.options = []
self.format = format
+ self.line_number = line_number
def replacement_text (self):
return self.match.group ('match')
return re.sub (f, self.processed_filename (), s)
class Lilypond_snippet (Snippet):
- def __init__ (self, type, match, format):
- Snippet.__init__ (self, type, match, format)
+ def __init__ (self, type, match, format, line_number):
+ Snippet.__init__ (self, type, match, format, line_number)
os = match.group ('options')
if os:
self.options = split_options (os)
'include': Include_snippet,
}
+def find_linestarts (s):
+ nls = [0]
+ start = 0
+ end = len (s)
+ while 1:
+ i = s.find ('\n', start)
+ if i < 0:
+ break
+
+ i = i + 1
+ nls.append (i)
+ start = i
+
+ nls.append (len (s))
+ return nls
+
def find_toplevel_snippets (s, types):
res = {}
for i in types:
map (lambda x, f = found: f.setdefault (x, None),
types)
+ line_starts = find_linestarts (s)
+ line_start_idx = 0
# We want to search for multiple regexes, without searching
# the string multiple times for one regex.
# Hence, we use earlier results to limit the string portion
cl = Snippet
if snippet_type_to_class.has_key (type):
cl = snippet_type_to_class[type]
- snip = cl (type, m, format)
+
+ line_number = line_start_idx
+ while (line_starts[line_number] < index):
+ line_number += 1
+
+ line_number ++
+ snip = cl (type, m, format, line_number)
start = index + m.start ('match')
found[type] = (start, snip)
endex = found[first][0]
if not first:
- snippets.append (Substring (s, index, len (s)))
+ snippets.append (Substring (s, index, len (s), line_start_idx))
break
+ while (start > line_starts[line_start_idx+1]):
+ line_start_idx += 1
+
(start, snip) = found[first]
- snippets.append (Substring (s, index, start))
+ snippets.append (Substring (s, index, start, line_start_idx + 1))
snippets.append (snip)
found[first] = None
index = start + len (snip.match.group ('match'))
# it is too generic for lilypond-book.
if texstr_names and re.search ('^[0-9A-Za-z/]*lilypond', cmd):
- my_system (string.join ([cmd + ' --backend texstr ' ] + texstr_names))
+ my_system (string.join ([cmd,'--backend texstr', 'snippet-map.ly'] + texstr_names))
for l in texstr_names:
my_system ('latex %s.texstr' % l)
if ly_names:
- my_system (string.join ([cmd] + ly_names))
+ my_system (string.join ([cmd, 'snippet-map.ly'] + ly_names))
LATEX_DOCUMENT = r'''
%(preamble)s
class Compile_error:
pass
-def do_process_cmd (chunks):
+def write_file_map (lys, name):
+ snippet_map = open ('snippet-map.ly', 'w')
+ snippet_map.write ("\n#(ly:add-file-name-alist '(")
+ for ly in lys:
+ snippet_map.write ('("%s" . "%s:%d (%s.ly)")\n' % (ly.basename(),
+ name,
+ ly.line_number,
+ ly.basename()))
+
+ snippet_map.write ('))\n')
+
+def do_process_cmd (chunks, input_name):
+ all_lys = filter(lambda x: is_derived_class (x.__class__, Lilypond_snippet),
+ chunks)
+
+ write_file_map (all_lys, input_name)
ly_outdated = \
filter (lambda x: is_derived_class (x.__class__,
Lilypond_snippet)
for c in chunks])
elif process_cmd:
- do_process_cmd (chunks)
+ do_process_cmd (chunks, input_fullname)
ly.progress (_ ("Compiling %s...") % output_filename)
output_file.writelines ([s.replacement_text () \
for s in chunks])