diff --git a/dot_config/mimeapps.list.tmpl b/dot_config/mimeapps.list.tmpl deleted file mode 100644 index 6519478..0000000 --- a/dot_config/mimeapps.list.tmpl +++ /dev/null @@ -1,27 +0,0 @@ -[Added Associations] -application/json=nvim.desktop; -application/pdf=firefox.desktop;libreoffice-draw.desktop;chromium-browser.desktop;org.inkscape.Inkscape.desktop;krita_pdf.desktop;display-im7.q16.desktop; -application/x-docbook+xml=nvim.desktop; -application/x-yaml=nvim.desktop; -text/csv=libreoffice-calc.desktop;krita_csv.desktop;nvim.desktop;vim.desktop;libreoffice-writer.desktop;okularApplication_txt.desktop; -text/markdown=nvim.desktop; -text/plain=wine-Programs-Kvaser Database Editor 3-Kvaser Database Editor 3.desktop;nvim.desktop;libreoffice-writer.desktop;vim.desktop; -text/x-cmake=nvim.desktop; -x-scheme-handler/geo=openstreetmap-geo-handler.desktop; -x-scheme-handler/http=firefox.desktop; -x-scheme-handler/https=firefox.desktop; - -[Default Applications] -application/json=nvim.desktop; -application/pdf=firefox.desktop; -application/x-docbook+xml=nvim.desktop; -application/x-yaml=nvim.desktop; -text/csv=libreoffice-calc.desktop; -text/html=firefox.desktop -text/markdown=nvim.desktop; -text/plain=wine-Programs-Kvaser Database Editor 3-Kvaser Database Editor 3.desktop; -text/x-cmake=nvim.desktop; -x-scheme-handler/geo=openstreetmap-geo-handler.desktop; -x-scheme-handler/http=firefox.desktop; -x-scheme-handler/https=firefox.desktop; -x-scheme-handler/opencode=ai.opencode.desktop.desktop diff --git a/dot_config/modify_mimeapps.list.tmpl b/dot_config/modify_mimeapps.list.tmpl new file mode 100644 index 0000000..5a9a60a --- /dev/null +++ b/dot_config/modify_mimeapps.list.tmpl @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 +{{ $browser := "firefox.desktop" }}{{ if hasPrefix "UGC" .chezmoi.hostname }}{{ $browser = "default-browser.desktop" }}{{ end }} +# chezmoi modify_ script: merge a curated set of default associations into the +# machine's existing mimeapps.list (read from stdin) instead of replacing the +# whole file, so each machine keeps its own entries. +import sys + +BROWSER = "{{ $browser }}" +SECTION = "[Default Applications]" + +# Associations enforced on every machine, in [Default Applications]. +ENFORCED = { + "application/json": "nvim.desktop", + "application/x-docbook+xml": "nvim.desktop", + "application/x-yaml": "nvim.desktop", + "text/markdown": "nvim.desktop", + "text/x-cmake": "nvim.desktop", + "text/html": BROWSER, + "x-scheme-handler/http": BROWSER, + "x-scheme-handler/https": BROWSER, +} + + +def main(): + lines = sys.stdin.read().splitlines() + + # Split into sections, preserving header lines and body order. The first + # (headerless) section holds any preamble before the first [Header]. + sections = [[None, []]] + for line in lines: + stripped = line.strip() + if stripped.startswith("[") and stripped.endswith("]"): + sections.append([stripped, []]) + else: + sections[-1][1].append(line) + + target = next((s for s in sections if s[0] == SECTION), None) + if target is None: + target = [SECTION, []] + sections.append(target) + + remaining = dict(ENFORCED) + body = [] + for line in target[1]: + key = line.split("=", 1)[0].strip() if "=" in line else None + if key in remaining: + body.append("{}={};".format(key, remaining.pop(key))) + else: + body.append(line) + + # Append any still-missing enforced keys before trailing blank lines. + trailing = [] + while body and body[-1].strip() == "": + trailing.insert(0, body.pop()) + for key, val in ENFORCED.items(): + if key in remaining: + body.append("{}={};".format(key, val)) + body.extend(trailing) + target[1] = body + + out = [] + for header, section_body in sections: + if header is not None: + out.append(header) + out.extend(section_body) + sys.stdout.write("\n".join(out) + "\n") + + +main()