diff --git a/kittens/diff/config.py b/kittens/diff/config.py index bef86115a..64ac3f390 100644 --- a/kittens/diff/config.py +++ b/kittens/diff/config.py @@ -38,7 +38,18 @@ def set_formats(opts): formats['added_highlight'] = '48' + color_as_sgr(opts.highlight_added_bg) -type_map = {} +def syntax_aliases(raw): + ans = {} + for x in raw.split(): + a, b = x.partition(':')[::2] + if a and b: + ans[a.lower()] = b + return ans + + +type_map = { + 'syntax_aliases': syntax_aliases, +} for name in ( 'foreground background title_fg title_bg margin_bg margin_fg removed_bg removed_margin_bg added_bg added_margin_bg filler_bg hunk_bg hunk_margin_bg' diff --git a/kittens/diff/diff.conf b/kittens/diff/diff.conf index eb125e097..ac4450d9c 100644 --- a/kittens/diff/diff.conf +++ b/kittens/diff/diff.conf @@ -1,3 +1,11 @@ +# vim:fileencoding=utf-8:ft=conf + +# File extension aliases for syntax highlight +# For example, to syntax highlight file.xyz as +# file.abc use a setting of xyz:abc +syntax_aliases pyj:py recipe:py + +# Colors foreground black background white title_fg black diff --git a/kittens/diff/highlight.py b/kittens/diff/highlight.py index ffc0bedd5..52380c198 100644 --- a/kittens/diff/highlight.py +++ b/kittens/diff/highlight.py @@ -73,10 +73,12 @@ def initialize_highlighter(style='default'): formatter = DiffFormatter(style) -def highlight_data(code, filename): - base, ext = os.path.splitext(filename) - if ext.lower() == '.pyj': - filename = base + '.py' +def highlight_data(code, filename, aliases=None): + if aliases: + base, ext = os.path.splitext(filename) + alias = aliases.get(ext[1:]) + if alias is not None: + filename = base + '.' + alias try: lexer = get_lexer_for_filename(filename, stripnl=False) except ClassNotFound: @@ -106,17 +108,17 @@ def highlight_line(line): return ans -def highlight_for_diff(path): +def highlight_for_diff(path, aliases): ans = [] lines = lines_for_path(path) - hd = highlight_data('\n'.join(lines), path) + hd = highlight_data('\n'.join(lines), path, aliases) if hd is not None: for line in hd.splitlines(): ans.append(highlight_line(line)) return ans -def highlight_collection(collection): +def highlight_collection(collection, aliases=None): jobs = {} ans = {} with concurrent.futures.ProcessPoolExecutor(max_workers=os.cpu_count()) as executor: @@ -126,7 +128,7 @@ def highlight_collection(collection): if p: is_binary = isinstance(data_for_path(p), bytes) if not is_binary: - jobs[executor.submit(highlight_for_diff, p)] = p + jobs[executor.submit(highlight_for_diff, p, aliases)] = p for future in concurrent.futures.as_completed(jobs): path = jobs[future] try: @@ -138,11 +140,12 @@ def highlight_collection(collection): def main(): + from .config import defaults # kitty +runpy "from kittens.diff.highlight import main; main()" file import sys initialize_highlighter() with open(sys.argv[-1]) as f: - highlighted = highlight_data(f.read(), f.name) + highlighted = highlight_data(f.read(), f.name, defaults.syntax_aliases) if highlighted is None: raise SystemExit('Unknown filetype: {}'.format(sys.argv[-1])) print(highlighted) diff --git a/kittens/diff/main.py b/kittens/diff/main.py index 9e021fa04..e72d7ac39 100644 --- a/kittens/diff/main.py +++ b/kittens/diff/main.py @@ -243,7 +243,7 @@ class DiffHandler(Handler): if initialize_highlighter is not None and not self.highlighting_done: self.highlighting_done = True initialize_highlighter() - self.start_job('highlight', highlight_collection, self.collection) + self.start_job('highlight', highlight_collection, self.collection, self.opts.syntax_aliases) elif job_id == 'highlight': hdata = job_result['result'] if isinstance(hdata, str):