#!/usr/bin/env python # vimpdb.py: Generates a Vim syntax highlighting file for the PDB file format. # Copyright ©2022-2024 Evan Pretti. All rights reserved. # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # 1. Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # This software is provided by the author and contributors "as is" and any # express or implied warranties, including, but not limited to, the implied # warranties of merchantability and fitness for a particular purpose are # disclaimed. In no event shall the author or contributors be liable for any # direct, indirect, incidental, special, exemplary, or consequential damages # (including, but not limited to, procurement of substitute goods or services; # loss of use, data, or profits; or business interruption) however caused and on # any theory of liability, whether in contract, strict liability, or tort # (including negligence or otherwise) arising in any way out of the use of this # software, even if advised of the possibility of such damage. records = [] colors = { "ATOM": "red", "CODE": "green", "COMM": "darkgray", "CONT": "lightcyan", "DATE": "lightblue", "DESC": "lightyellow", "INTG": "cyan", "PDID": "lightgreen", "REAL": "blue", "RECO": "lightred", "RESI": "yellow", "TITL": "white", } with open("pdb.dat", "r") as format_file: for record_line in format_file: record_name = record_line[:6].rstrip() record_items = [] offset = 7 while record_line[offset:offset + 10]: record_items.append(( int(record_line[offset:offset + 2]), int(record_line[offset + 3:offset + 5]), record_line[offset + 6:offset + 10], )) offset += 11 records.append((record_name, record_items)) with open("pdb.vim", "w") as vim_file: print("if exists(\"b:current_syntax\")\n finish\nendif", file=vim_file) for record_index, (record_name, record_items) in enumerate(records): if record_items: print(f"sy match P{record_index} /^{record_name}/ nextgroup=P{record_index}F0 skipwhite", file=vim_file) else: print(f"sy match P{record_index} /^{record_name} *$/", file=vim_file) for record_index, (record_name, record_items) in enumerate(records): for field_index, (i, j, field_kind) in enumerate(record_items): next_index = field_index + 1 if next_index < len(record_items): print(f"sy match P{record_index}F{field_index} /\\%{i}v.*\\%<{j + 2}v/ contained nextgroup=P{record_index}F{next_index} skipwhite", file=vim_file) else: print(f"sy match P{record_index}F{field_index} /\\%{i}v.*\\%<{j + 2}v/ contained", file=vim_file) for record_index, (record_name, record_items) in enumerate(records): print(f"hi link P{record_index} HRECO", file=vim_file) for record_index, (record_name, record_items) in enumerate(records): for field_index, (i, j, field_kind) in enumerate(record_items): print(f"hi link P{record_index}F{field_index} H{field_kind}", file=vim_file) for field_kind, color in colors.items(): print(f"hi H{field_kind} ctermfg={color} guifg={color}", file=vim_file) print("let b:current_syntax = \"pdb\"", file=vim_file) with open("test.pdb", "w") as test_file: for record_index, (record_name, record_items) in enumerate(records): line = list(record_name + " " * (80 - len(record_name))) for field_index, (i, j, field_kind) in enumerate(record_items): line[i - 1:j] = ["#"] * (j + 1 - i) print("".join(line), file=test_file)