1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
.pragma library
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Pure helpers for the default apps page, tested by test_mimeapps.js under
// node. Every edit works on the text of mimeapps.list and touches only the
// one line it means: other sections ([Added Associations] included),
// comments and blank lines survive byte for byte. The file is walked line by
// line, never cut "up to the next [" (AGENTS.md, the INI note).
const SECTION = "[Default Applications]";
// The role rows, each setting its whole group of types at once.
const ROLES = [
{ label: "Browser", mimes: ["text/html", "application/xhtml+xml", "x-scheme-handler/http", "x-scheme-handler/https"] },
{ label: "Mail", mimes: ["x-scheme-handler/mailto"] },
{ label: "Text", mimes: ["text/plain"] },
{ label: "PDF", mimes: ["application/pdf"] },
{ label: "Image", mimes: ["image/png", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"] },
{ label: "Video", mimes: ["video/mp4", "video/x-matroska", "video/webm"] },
{ label: "Audio", mimes: ["audio/mpeg", "audio/flac", "audio/ogg", "audio/x-wav"] },
{ label: "Files", mimes: ["inode/directory"] },
{ label: "Archive", mimes: ["application/zip", "application/x-tar", "application/x-7z-compressed", "application/gzip"] },
];
// The trailing newline is dropped on split and always put back on join.
function lines(text) { return text === "" ? [] : text.replace(/\n$/, "").split("\n"); }
function join(ls) { return ls.join("\n") + "\n"; }
// The key of a "key=value" line, null for a comment, header or blank.
function keyOf(line) {
const m = /^\s*([^#=\s[][^=]*?)\s*=/.exec(line);
return m ? m[1] : null;
}
// [header index, end index (exclusive)] of the section, or null.
function section(ls) {
const start = ls.findIndex(l => l.trim() === SECTION);
if (start < 0) return null;
let end = start + 1;
while (end < ls.length && !/^\s*\[/.test(ls[end])) end++;
return [start, end];
}
// { mime: desktopId } from [Default Applications]. A value is a ;-list and
// its first item is the default.
function parse(text) {
const ls = lines(text), s = section(ls), out = {};
if (!s) return out;
for (let i = s[0] + 1; i < s[1]; i++) {
const k = keyOf(ls[i]);
if (k === null) continue;
const id = ls[i].slice(ls[i].indexOf("=") + 1).split(";").map(x => x.trim()).find(x => x);
if (id) out[k] = id;
}
return out;
}
function set(text, mime, id) {
const ls = lines(text);
let s = section(ls);
if (!s) {
if (ls.length > 0 && ls[ls.length - 1].trim() !== "") ls.push("");
ls.push(SECTION);
s = [ls.length - 1, ls.length];
}
const line = mime + "=" + id + ";";
for (let i = s[0] + 1; i < s[1]; i++)
if (keyOf(ls[i]) === mime) { ls[i] = line; return join(ls); }
// After the last non-blank line, so a blank line before the next header stays.
let at = s[1];
while (at > s[0] + 1 && ls[at - 1].trim() === "") at--;
ls.splice(at, 0, line);
return join(ls);
}
// The header stays even when its last entry goes, so the text written is
// never empty (FileView.setText("") writes nothing, AGENTS.md).
function remove(text, mime) {
const ls = lines(text), s = section(ls);
if (!s) return text;
for (let i = s[0] + 1; i < s[1]; i++)
if (keyOf(ls[i]) === mime) { ls.splice(i, 1); return join(ls); }
return text;
}
// The applications directories in XDG order, the user's first.
function dirs(home, dataHome, dataDirs) {
return [dataHome || home + "/.local/share"]
.concat((dataDirs || "/usr/local/share:/usr/share").split(":"))
.filter(d => d)
.map(d => d.replace(/\/+$/, "") + "/applications");
}
// grep -rH '^MimeType=' output -> { desktopId: [mimes] }. The id is the path
// under applications/ with "/" as "-" (desktop entry spec); the first
// directory to list an id wins, which is the user's when it has one.
function scan(out, appDirs) {
const apps = {};
for (const line of out.split("\n")) {
const i = line.indexOf(":MimeType=");
if (i < 0) continue;
const path = line.slice(0, i);
const dir = appDirs.find(d => path.startsWith(d + "/"));
if (!dir) continue;
const id = path.slice(dir.length + 1).replace(/\//g, "-");
if (id in apps) continue;
apps[id] = line.slice(i + ":MimeType=".length).split(";").filter(m => m);
}
return apps;
}
// The app most of a role's types point at ("" when none is set), and how
// many distinct apps they point at.
function role(defaults, mimes) {
const n = {};
for (const m of mimes) if (defaults[m]) n[defaults[m]] = (n[defaults[m]] || 0) + 1;
const ids = Object.keys(n).sort((a, b) => n[b] - n[a]);
return { id: ids[0] || "", apps: ids.length };
}
// The ids declaring at least one of the types.
function handlers(apps, mimes) {
return Object.keys(apps).filter(id => apps[id].some(m => mimes.includes(m)));
}
// Every type any installed app declares, sorted.
function allMimes(apps) {
const seen = {};
for (const id in apps) for (const m of apps[id]) seen[m] = true;
return Object.keys(seen).sort();
}
|