mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-27 05:13:10 +00:00
Changes to make completions internalized scripts.
This about doubles the size of fish. These things are huge. We may not want to ship it like this.
This commit is contained in:
parent
60d1ac4fec
commit
1b3427acd9
3 changed files with 57 additions and 25 deletions
|
@ -168,7 +168,7 @@ GENERATED_INTERN_SCRIPT_FILES := builtin_scripts.h builtin_scripts.cpp
|
||||||
# Use a pattern rule so that Make knows to only issue one invocation
|
# Use a pattern rule so that Make knows to only issue one invocation
|
||||||
# per http://www.gnu.org/software/make/manual/make.html#Pattern-Intro
|
# per http://www.gnu.org/software/make/manual/make.html#Pattern-Intro
|
||||||
builtin%scripts.h builtin%scripts.cpp: internalize_scripts.py
|
builtin%scripts.h builtin%scripts.cpp: internalize_scripts.py
|
||||||
./internalize_scripts.py share/functions/*.fish
|
./internalize_scripts.py share/functions/*.fish share/completions/*.fish
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -889,6 +889,7 @@ distclean: clean
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *.o doc.h doc.tmp doc_src/*.doxygen doc_src/*.cpp doc_src/*.o doc_src/commands.hdr
|
rm -f *.o doc.h doc.tmp doc_src/*.doxygen doc_src/*.cpp doc_src/*.o doc_src/commands.hdr
|
||||||
|
rm -f $(GENERATED_INTERN_SCRIPT_FILES)
|
||||||
rm -f tests/tmp.err tests/tmp.out tests/tmp.status tests/foo.txt
|
rm -f tests/tmp.err tests/tmp.out tests/tmp.status tests/foo.txt
|
||||||
rm -f $(PROGRAMS) fish_tests tokenizer_test key_reader
|
rm -f $(PROGRAMS) fish_tests tokenizer_test key_reader
|
||||||
rm -f share/config.fish etc/config.fish doc_src/index.hdr doc_src/commands.hdr
|
rm -f share/config.fish etc/config.fish doc_src/index.hdr doc_src/commands.hdr
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
|
||||||
import string, sys, os.path
|
import string, sys, os.path
|
||||||
|
|
||||||
escapes = {}
|
escapes = {}
|
||||||
|
@ -11,7 +12,7 @@ escapes['\r'] = r'\r'
|
||||||
#escapes['\t'] = r'\t'
|
#escapes['\t'] = r'\t'
|
||||||
# Let's replace tabs with four spaces
|
# Let's replace tabs with four spaces
|
||||||
# so the text looks nicely indented in the C source
|
# so the text looks nicely indented in the C source
|
||||||
escapes['\t'] = r' '
|
escapes['\t'] = r' '
|
||||||
escapes['\v'] = r'\v'
|
escapes['\v'] = r'\v'
|
||||||
# escapes['\''] = r'\''
|
# escapes['\''] = r'\''
|
||||||
escapes['\"'] = r'\"'
|
escapes['\"'] = r'\"'
|
||||||
|
@ -19,21 +20,28 @@ escapes['\\'] = r'\\'
|
||||||
|
|
||||||
def escape(c):
|
def escape(c):
|
||||||
if c in escapes:
|
if c in escapes:
|
||||||
return escapes[c]
|
return (escapes[c], False)
|
||||||
elif c not in string.printable:
|
elif c not in string.printable:
|
||||||
return "\\x%x" % ord(c)
|
return ("\\x%x" % ord(c), True)
|
||||||
else:
|
else:
|
||||||
return c
|
return (c, False)
|
||||||
|
|
||||||
def stringize(line):
|
def stringize(line):
|
||||||
newline = '"'
|
newline = '"'
|
||||||
|
was_escape = False
|
||||||
for c in line:
|
for c in line:
|
||||||
newline += escape(c)
|
# Avoid an issue where characters after a hexadecimal escape are treated as part of that escape
|
||||||
|
# by adding two quotes
|
||||||
|
if was_escape and c in string.hexdigits:
|
||||||
|
newline += '""'
|
||||||
|
chars, was_escape = escape(c)
|
||||||
|
newline += chars
|
||||||
newline += '"'
|
newline += '"'
|
||||||
return newline
|
return newline
|
||||||
|
|
||||||
class cfunc:
|
class cfunc:
|
||||||
def __init__(self, name, lines):
|
def __init__(self, type, name, lines):
|
||||||
|
self.type = type
|
||||||
self.name = name
|
self.name = name
|
||||||
self.lines = lines
|
self.lines = lines
|
||||||
|
|
||||||
|
@ -45,22 +53,36 @@ class cfunc:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def cfunc_name(self):
|
def cfunc_name(self):
|
||||||
munged_name = string.replace(self.name, '-', '_')
|
# Translate - and . to underscore
|
||||||
return "function_%s" % munged_name
|
translator = string.maketrans('-.', '__')
|
||||||
|
munged_name = string.translate(self.name, translator)
|
||||||
|
return "%s_%s" % (self.type, munged_name)
|
||||||
|
|
||||||
funcs = []
|
TYPES = ['function', 'completion']
|
||||||
|
type_to_funcs = dict((t, []) for t in TYPES)
|
||||||
for file in sys.argv[1:]:
|
for file in sys.argv[1:]:
|
||||||
fd = open(file, 'r')
|
fd = open(file, 'r')
|
||||||
newlines = []
|
newlines = []
|
||||||
for line in fd:
|
for line in fd:
|
||||||
newlines.append(stringize(line))
|
newlines.append(stringize(line))
|
||||||
fd.close()
|
fd.close()
|
||||||
|
dirname = os.path.dirname(file)
|
||||||
|
|
||||||
|
# Try to figure out the file type (completion or function)
|
||||||
|
matches = [dir in dirname for dir in TYPES]
|
||||||
|
if matches.count(True) is not 1:
|
||||||
|
print "Cannot determine the type of the file at path %s" % file
|
||||||
|
sys.exit(-1)
|
||||||
|
type = TYPES[matches.index(True)]
|
||||||
|
|
||||||
name = os.path.basename(file)
|
name = os.path.basename(file)
|
||||||
name, ext = os.path.splitext(name)
|
name, ext = os.path.splitext(name)
|
||||||
funcs.append(cfunc(name, newlines))
|
newfunc = cfunc(type, name, newlines)
|
||||||
|
type_to_funcs[type].append(newfunc)
|
||||||
|
|
||||||
# Sort our functions by name
|
# Sort our functions by name
|
||||||
funcs.sort(key=cfunc.cfunc_name)
|
for funcs in type_to_funcs.itervalues():
|
||||||
|
funcs.sort(key=cfunc.cfunc_name)
|
||||||
|
|
||||||
# Output our header
|
# Output our header
|
||||||
fd = open('builtin_scripts.h', 'w')
|
fd = open('builtin_scripts.h', 'w')
|
||||||
|
@ -71,22 +93,29 @@ fd.write("""struct builtin_script_t {
|
||||||
};""")
|
};""")
|
||||||
|
|
||||||
fd.write('\n')
|
fd.write('\n')
|
||||||
fd.write('\n')
|
|
||||||
fd.write('extern const struct builtin_script_t internal_function_scripts[%d];\n' % len(funcs))
|
for type in TYPES:
|
||||||
fd.write('\n')
|
funcs = type_to_funcs[type]
|
||||||
|
fd.write('\n')
|
||||||
|
fd.write('extern const struct builtin_script_t internal_%s_scripts[%d];' % (type, len(funcs)))
|
||||||
|
fd.write('\n')
|
||||||
fd.close()
|
fd.close()
|
||||||
|
|
||||||
# Output the function definitions
|
# Output the function definitions
|
||||||
fd = open('builtin_scripts.cpp', 'w')
|
fd = open('builtin_scripts.cpp', 'w')
|
||||||
fd.write('/* This file is generated by internalize_scripts.py */\n\n')
|
fd.write('/* This file is generated by internalize_scripts.py */\n\n')
|
||||||
fd.write('#include "builtin_scripts.h"\n\n')
|
fd.write('#include "builtin_scripts.h"\n\n')
|
||||||
for func in funcs:
|
for type in TYPES:
|
||||||
fd.write(func.cdef())
|
for func in type_to_funcs[type]:
|
||||||
fd.write('\n')
|
fd.write(func.cdef())
|
||||||
|
fd.write('\n')
|
||||||
|
|
||||||
# Output the refs
|
# Output the refs
|
||||||
func_refs = ["{L%s, %s}" % (stringize(func.name), func.cfunc_name()) for func in funcs]
|
for type in TYPES:
|
||||||
fd.write('const struct builtin_script_t internal_function_scripts[%d] =\n' % len(funcs))
|
funcs = type_to_funcs[type]
|
||||||
fd.write('{\n\t')
|
func_refs = ["{L%s, %s}" % (stringize(func.name), func.cfunc_name()) for func in funcs]
|
||||||
fd.write(',\n\t'.join(func_refs))
|
fd.write('const struct builtin_script_t internal_%s_scripts[%d] =\n' % (type, len(funcs)))
|
||||||
fd.write('\n};\n')
|
fd.write('{\n\t')
|
||||||
|
fd.write(',\n\t'.join(func_refs))
|
||||||
|
fd.write('\n};\n')
|
||||||
|
fd.close()
|
||||||
|
|
|
@ -870,10 +870,12 @@ int parse_util_load( const wcstring &cmd,
|
||||||
/* Figure out which builtin-scripts array to search (if any) */
|
/* Figure out which builtin-scripts array to search (if any) */
|
||||||
const builtin_script_t *builtins = NULL;
|
const builtin_script_t *builtins = NULL;
|
||||||
size_t builtin_count = 0;
|
size_t builtin_count = 0;
|
||||||
if (path_var_name == L"fish_function_path")
|
if (path_var_name == L"fish_function_path") {
|
||||||
{
|
|
||||||
builtins = internal_function_scripts;
|
builtins = internal_function_scripts;
|
||||||
builtin_count = sizeof internal_function_scripts / sizeof *internal_function_scripts;
|
builtin_count = sizeof internal_function_scripts / sizeof *internal_function_scripts;
|
||||||
|
} else if (path_var_name == L"fish_complete_path") {
|
||||||
|
builtins = internal_completion_scripts;
|
||||||
|
builtin_count = sizeof internal_completion_scripts / sizeof *internal_completion_scripts;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in a new issue