Code-Ersetzung mit Teams (TXT-Import, nur neue Spieler nachladen, Auto-Reload), Codes ohne Trennzeichen, Cursor bleibt stehen; Auswerfen per diskutil mit Wiederholung direkt nach dem Kopieren

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
This commit is contained in:
Lichtkasten DevandClaude Opus 5.5 committed 2026-11-04 00:19:45 +00:00
1 parent d39db536a0
commit 25729ebfe3
10 files changed
+615 -134

No files matched your search

@@ -88,8 +88,12 @@ public struct CodeReplacements: Codable, Sendable {
public var delimiter: Character = "\\"
public var table: [String: [String]] = [:]
public var caseSensitive = false
/// PM "Default replacement": used when a \code\ is not found (empty = leave the code as typed).
public var defaultReplacement = ""
/// Codes without delimiter: "w10" + space/comma/… expands (only while typing).
public var bareCodes = true
enum CodingKeys: String, CodingKey { case delimiter, table, caseSensitive }
enum CodingKeys: String, CodingKey { case delimiter, table, caseSensitive, defaultReplacement, bareCodes }
public init(table: [String: [String]] = [:]) { self.table = table }
@@ -99,6 +103,8 @@ public struct CodeReplacements: Codable, Sendable {
delimiter = d.first ?? "\\"
table = try c.decodeIfPresent([String: [String]].self, forKey: .table) ?? [:]
caseSensitive = try c.decodeIfPresent(Bool.self, forKey: .caseSensitive) ?? false
defaultReplacement = try c.decodeIfPresent(String.self, forKey: .defaultReplacement) ?? ""
bareCodes = try c.decodeIfPresent(Bool.self, forKey: .bareCodes) ?? true
}
public func encode(to encoder: Encoder) throws {
@@ -106,6 +112,8 @@ public struct CodeReplacements: Codable, Sendable {
try c.encode(String(delimiter), forKey: .delimiter)
try c.encode(table, forKey: .table)
try c.encode(caseSensitive, forKey: .caseSensitive)
try c.encode(defaultReplacement, forKey: .defaultReplacement)
try c.encode(bareCodes, forKey: .bareCodes)
}
/// Loads a tab-delimited PM code replacement file. Lines starting with '#' are ignored.
@@ -123,7 +131,7 @@ public struct CodeReplacements: Codable, Sendable {
return CodeReplacements(table: table)
}
func lookup(_ code: String) -> String? {
public func lookup(_ code: String) -> String? {
var name = code
var column = 1
if let hash = code.lastIndex(of: "#"), let n = Int(code[code.index(after: hash)...]) {
@@ -136,6 +144,48 @@ public struct CodeReplacements: Codable, Sendable {
return row[column - 1]
}
static let terminators: Set<Character> = [" ", "\t", "\n", "\r", ",", ";", ":", ")", "!", "?", ".", "/"]
static func isTokenChar(_ c: Character) -> Bool { c.isLetter || c.isNumber || c == "_" || c == "-" || c == "#" }
/// Live expansion while typing (PM behaviour): looks at the text just before the cursor and returns the
/// range (UTF-16, for NSTextView) to replace plus the replacement, or nil.
/// – "\\w10\\" (delimiter mode, as soon as the closing delimiter is typed)
/// – "w10" followed by space/comma/… (bare codes, the terminator itself is kept)
public func pendingExpansion(in text: String, cursor: Int) -> (range: NSRange, replacement: String)? {
guard !table.isEmpty else { return nil }
let ns = text as NSString
guard cursor > 0, cursor <= ns.length else { return nil }
let before = ns.substring(to: cursor)
guard let last = before.last else { return nil }
if last == delimiter {
let body = before.dropLast()
if let open = body.lastIndex(of: delimiter) {
let code = String(body[body.index(after: open)...])
if !code.isEmpty, !code.contains(where: { $0.isWhitespace }) {
let rep = lookup(code) ?? (defaultReplacement.isEmpty ? nil : defaultReplacement)
if let rep {
let start = (String(body[..<open]) as NSString).length
return (NSRange(location: start, length: cursor - start), rep)
}
}
}
}
if bareCodes, CodeReplacements.terminators.contains(last) {
let body = before.dropLast()
var start = body.endIndex
while start > body.startIndex, CodeReplacements.isTokenChar(body[body.index(before: start)]) {
start = body.index(before: start)
}
let token = String(body[start...])
guard !token.isEmpty, let rep = lookup(token) else { return nil }
let loc = (String(body[..<start]) as NSString).length
return (NSRange(location: loc, length: (token as NSString).length), rep)
}
return nil
}
public func expand(_ text: String) -> String {
guard !table.isEmpty, text.contains(delimiter) else { return text }
var out = ""