mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-02 02:38:30 -06:00
**What**
Typing a command with the Greek keyboard layout active (ΛΙΝΕ for LINE, ΜΙΡΡΟΡ for MIRROR) now works like typing the Latin name, in four places: starting a command, answering a prompt keyword (ΘΝΔΟ = Undo), the selection words at "Select objects" (ΑΛΛ = ALL), and command-line AutoComplete. The layout puts each Greek letter on the key of the Latin letter it sounds like (θ on U, ψ on C, ω on V, Q types `;`, W the final sigma), so `keyboard::latin_keys` maps letter by letter and ignores accents, including a combining tonos or dialytika straight after its letter. It returns `None` for a word with no Greek letter, so Latin input is never touched. ASCII characters in a Greek word are kept, in lower case (the dash of `-ΛΑΥΕΡ`), and a word with any other character is not read as Latin keys.
A command the interface handles itself answers to its Greek-typed name too: ΨΛΟΣΕ asks about unsaved changes as CLOSE does, `;ΣΑΩΕ` opens the Save picker for a drawing without a file, ΛΑΥΕΡ opens the Layers dialog.
What keeps existing behaviour:
- A word that is already a command, alias or LISP function keeps its own meaning, as typed or in another case or accent (a Greek `(defun c:ΛΙΝΕ …)` runs as itself, also when typed λινε).
- A keyword a prompt offers in its own Greek spelling (a LISP program's list) wins over its Latin reading, with Greek case folded.
- A selection word wins over a keyword on the same key, as it does for Latin typing: at COUNT's "Select object to count or [List/Spaces]", Λ is Last, like L.
- A name that exists wins over the keyword or selection word its keys spell: an existing block at BEDIT's "block name or select" and at BLOCKREPLACE's two name prompts (a block called Μ opens at BEDIT, it is not Multiple), and a loaded hatch pattern at HATCH's pattern prompt (a pattern called ΣΟΛΙΔ is that pattern, not Solid). The name is found in any case or accent and the command gets it as stored.
- Only the lookup reads the Greek keys: text that is no selection word reaches the command as typed (a Greek block name at BEDIT's "block name or select"), and an unknown Greek command is reported as typed.
Known and accepted:
- At a prompt that takes a name and offers a keyword, a Greek word whose keys spell the keyword is the keyword while no block has that name (Σ is Select at BLOCKREPLACE), as the Latin `S` is there.
- Two aliases whose Greek names differ only in case or accent can both exist (the alias editor folds ASCII only). Each runs when typed as stored; another spelling of the word runs the first of them in the store's order.
- Object snap overrides and the other point modifiers (END, FROM, M2P, TT, TK, `.X`) are not read from Greek-layout typing. They need the Latin layout, as before.
- Calls by command id are unchanged: a `cmd {json}` line and `execute` (control channel, MCP) do not read a Greek-typed name.
- In a script a line that starts with `;` is still a comment, so a line `;ΣΑΩΕ` there is not QSAVE.
Also adds 46 `el.tsv` rows for strings that were in the source but missing from the catalog when the rows were written (alias editor, save-on-close and units dialogs, a few menu and command labels). On this head the i18n report lists 39 strings that fall back to English for `el` (mostly keyboard shortcuts, right-click customization and alias import); they are not in this PR. Four command labels keep the English name in parentheses, like the rest of the catalog. The `el.tsv` row of `ATTRIBUTION.md` credits these rows (AI-assisted, read by the contributor, a native Greek speaker), and `contributors/people.toml` has the author's entry.
**Tests**
- `keyboard::tests`: the mapping, non-Greek words left alone, combining marks only straight after a Greek letter, one Greek word in any case, accent or final sigma, a character the layout does not type is no spelling of a Greek letter (the micro sign is not μ), 100 000-letter and malformed input do not panic
- `engine/tests/it/greek_keyboard.rs`: ΛΙΝΕ draws a line (lower case and accents too), `;ΣΑΩΕ`, an unknown Greek word is reported as typed, the selection word ΑΛΛ, Λ, λ and ΛΑΣΤ at COUNT's prompt count like L (Last, not the List option), keywords at MIRROR's prompt (Ν is read as No and keeps the sources, Υ erases them), a Greek LISP command runs as itself and in any case or accent, a Greek block name at BEDIT reaches the command as typed, a block called Μ opens at BEDIT (in either case), a block called Σ at BLOCKREPLACE, a loaded pattern called ΣΟΛΙΔ at HATCH's pattern prompt (upper case, lower case, accented)
- `engine/tests/greek_keyboard_aliases.rs` (its own binary, the alias store is process-global): a Greek alias keeps its meaning, as typed and in any case or accent; a word of 1 000 000 Greek letters is looked up among 5 000 Greek aliases within 5 s (170 s before the word was folded once per lookup)
- `prompt::tests`: a Greek keyword is matched as spelled, before its Latin reading
- `ui-egui`: ΨΛΟΣΕ asks before it closes a drawing with unsaved changes, and a LISP `c:ΨΛΟΣΕ` still runs as itself; ΛΑΥΕΡ (also λάυερ) opens the Layers dialog and `;ΣΑΩΕ` the Save picker; AutoComplete reads a Greek prefix as Latin keys, offers the Greek alias that Enter would run, and leaves a Greek word the drawing's LISP defines as typed
- Each fix after review landed with a test that failed before it.
**What ran** on e171e083, `cargo xtask ci` step by step: `cargo fmt --all -- --check` and `cargo clippy --workspace --all-targets -- -D warnings` clean; `cargo test --workspace --lib --bins --tests --no-fail-fast`: 3759 passed, 0 failed, 28 ignored; the doctest step (the workspace has no doctests), the i18n report, `cargo xtask assets`, `cargo xtask layers` and `cargo xtask wasm` passed. The same steps passed on e171e083 merged with main at e1d379ad, a local merge that is not pushed: 3761 passed, 0 failed, 28 ignored.
Main is merged in. The merge commit e3b3a2e0 had kept only this branch's side of `cmd/file.rs` and `cmd/file/pgp.rs`; e8a79d19 restores main's alias-persistence helpers and exports next to `greek_alias`, so those two files are main's again plus this PR's own lines.
**Docs (not edited here, per AGENTS.md)**: `docs/localization.md` "Greek (`el`) interface" and `docs/localization-parity.md` row "Greek | el" (its row count) could mention that commands typed with the Greek layout are accepted.
Nothing here is Autodesk-derived: the mapping is the standard Greek layout, written from the layout itself.
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit
* **New Features**
* Greek keyboard-layout input is recognized as Latin keystrokes when entering commands, aliases, autocomplete prefixes, selection keywords, and prompt options.
* Greek commands and aliases can be matched across case and accent variants. Existing commands, aliases, and matching block names take precedence over Greek-layout interpretations; unrecognized input retains its original text.
* At prompts that accept either a block name or a selection keyword, Greek input is treated as a name when a matching block exists; otherwise, it can be interpreted as a selection keyword.
* Greek-layout input can also match loaded or built-in hatch pattern names, which take precedence over prompt keyword interpretation.
* In BLOCKREPLACE prompts, you can select a block reference to choose a block.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
291 lines
12 KiB
Rust
291 lines
12 KiB
Rust
//! The user's command alias file (the publicly documented `.pgp` format: `ALIAS, *COMMAND` lines,
|
|
//! `;` comments): ALIASEDIT and REINIT.
|
|
//!
|
|
//! A loaded file replaces the user aliases, which add to or override the built-in ones for every
|
|
//! drawing; command names themselves always win. Lines that define external commands
|
|
//! (programs the file would have the application start) are never run: they are skipped with a
|
|
//! note. CADCraft ships no alias file; the aliases built into the command catalog stay the
|
|
//! defaults.
|
|
//!
|
|
//! The aliases are user settings, never part of a drawing: [`alias_profile`] gives them (and the
|
|
//! file REINIT reads) to the host's user profile, and [`restore_aliases`] brings them back on the
|
|
//! next start. The desktop and web hosts keep that profile; the headless CLI has no profile, so
|
|
//! aliases set in a script last for that session only.
|
|
|
|
use std::collections::BTreeMap;
|
|
use std::sync::{Mutex, MutexGuard, OnceLock, PoisonError};
|
|
|
|
use serde_json::{Value, json};
|
|
|
|
use super::super::always;
|
|
use super::super::helpers::support_text;
|
|
use super::{CommandSpec, Result, Session, bad, bool_or, find_command};
|
|
|
|
/// Most user aliases held.
|
|
pub const MAX_ALIASES: usize = 10_000;
|
|
/// Longest alias name.
|
|
const MAX_ALIAS_CHARS: usize = 64;
|
|
/// Longest command id (the catalog's longest is `texttofront.dimensions`; a test keeps it true).
|
|
const MAX_COMMAND_ID_CHARS: usize = 32;
|
|
/// Longest line read.
|
|
const MAX_LINE_CHARS: usize = 4096;
|
|
/// Most warnings reported.
|
|
const MAX_WARNINGS: usize = 100;
|
|
/// Longest alias file path kept in the user profile.
|
|
const MAX_ALIAS_FILE_CHARS: usize = 1024;
|
|
|
|
/// The largest alias section [`alias_profile`] can produce: [`MAX_ALIASES`] entries, each a
|
|
/// [`MAX_ALIAS_CHARS`]-character name (up to four UTF-8 bytes per character, and JSON adds its
|
|
/// quotes) mapped to a command id, plus the alias file path and the section's keys. A host that
|
|
/// keeps the section in its user profile allows at least this much, so a full set always comes
|
|
/// back after a restart.
|
|
pub const MAX_ALIAS_PROFILE_BYTES: usize = MAX_ALIASES * (MAX_ALIAS_CHARS * 4 + MAX_COMMAND_ID_CHARS + 6) + MAX_ALIAS_FILE_CHARS * 4 + 64;
|
|
|
|
#[derive(Default)]
|
|
struct Aliases {
|
|
/// Lower-case alias → command id.
|
|
map: BTreeMap<String, String>,
|
|
/// The file last loaded (REINIT reads it again).
|
|
file: Option<String>,
|
|
}
|
|
|
|
fn store() -> MutexGuard<'static, Aliases> {
|
|
static USER: OnceLock<Mutex<Aliases>> = OnceLock::new();
|
|
USER.get_or_init(|| Mutex::new(Aliases::default())).lock().unwrap_or_else(PoisonError::into_inner)
|
|
}
|
|
|
|
/// The command a user alias stands for (`name` in lower case).
|
|
pub(crate) fn user_alias(name: &str) -> Option<String> {
|
|
store().map.get(name).cloned()
|
|
}
|
|
|
|
/// The user alias that is the Greek word `word` in another case or accent.
|
|
pub(crate) fn greek_alias(word: &str) -> Option<String> {
|
|
let same = crate::keyboard::same_greek_word_as(word)?;
|
|
store().map.keys().find(|alias| same(alias)).cloned()
|
|
}
|
|
|
|
/// The user aliases and the file REINIT reads, as the host's user profile keeps them:
|
|
/// `{"aliases": {alias: command}, "file": path or null}`. Not drawing data.
|
|
pub fn alias_profile() -> Value {
|
|
let st = store();
|
|
// A path [`restore_aliases`] would refuse is left out, so it can't grow the profile either.
|
|
json!({ "aliases": st.map, "file": st.file.as_deref().filter(|f| keepable_path(f)) })
|
|
}
|
|
|
|
/// Whether the profile keeps an alias file path: short enough, and no control characters.
|
|
fn keepable_path(f: &str) -> bool {
|
|
f.chars().count() <= MAX_ALIAS_FILE_CHARS && !f.chars().any(char::is_control)
|
|
}
|
|
|
|
/// Restore aliases saved by [`alias_profile`], replacing the user aliases and the file.
|
|
///
|
|
/// The saved set is untrusted (a hand-edited or older profile): entries that are not aliases of
|
|
/// real commands are skipped, command names are never aliased, at most [`MAX_ALIASES`] are kept,
|
|
/// and a malformed section changes nothing.
|
|
pub fn restore_aliases(saved: &Value) {
|
|
let Some(obj) = saved.as_object() else { return };
|
|
let file = match obj.get("file") {
|
|
None | Some(Value::Null) => None,
|
|
Some(Value::String(f)) if keepable_path(f) => Some(f.clone()),
|
|
Some(Value::String(_)) => None,
|
|
// A file entry that is not a path makes the section malformed: keep the aliases as they are.
|
|
Some(_) => return,
|
|
};
|
|
let Some(map) = obj.get("aliases").and_then(Value::as_object) else { return };
|
|
let mut aliases = BTreeMap::new();
|
|
// Entries that are skipped don't use up the cap.
|
|
for (alias, target) in map {
|
|
if aliases.len() >= MAX_ALIASES {
|
|
break;
|
|
}
|
|
let Ok(name) = alias_name(alias) else { continue };
|
|
let Some(id) = target.as_str().and_then(command_id) else { continue };
|
|
aliases.insert(name, id);
|
|
}
|
|
let mut st = store();
|
|
st.map = aliases;
|
|
st.file = file;
|
|
}
|
|
|
|
/// The command id `target` names (`*LINE`, `_.line`, `-LAYER`…), if there is such a command.
|
|
pub fn command_id(target: &str) -> Option<String> {
|
|
let t = target.trim().trim_start_matches('*').trim().trim_start_matches(['_', '.']).to_ascii_lowercase();
|
|
find_command(&t).map(|c| c.id.to_string())
|
|
}
|
|
|
|
/// Whether `a` can be typed as an alias: one word of printable characters, not a command name.
|
|
pub fn alias_name(a: &str) -> std::result::Result<String, String> {
|
|
let a = a.trim();
|
|
if a.is_empty() || a.chars().count() > MAX_ALIAS_CHARS || a.chars().any(|c| c.is_whitespace() || c.is_control() || c == '\'' || c == '{') {
|
|
return Err(format!("`{a}` can't be an alias"));
|
|
}
|
|
let l = a.to_ascii_lowercase();
|
|
if find_command(&l).is_some() {
|
|
return Err(format!("{a} is a command name; it stays the command"));
|
|
}
|
|
Ok(l)
|
|
}
|
|
|
|
/// The aliases of a `.pgp` file's text, and its problems.
|
|
fn parse(text: &str) -> (Vec<(String, String)>, Vec<String>) {
|
|
let mut out: Vec<(String, String)> = Vec::new();
|
|
let mut warnings = Vec::new();
|
|
let mut warn = |no: usize, msg: String| {
|
|
if warnings.len() < MAX_WARNINGS {
|
|
warnings.push(format!("line {no}: {msg}"));
|
|
}
|
|
};
|
|
for (i, line) in text.trim_start_matches('\u{feff}').lines().enumerate() {
|
|
let no = i.saturating_add(1);
|
|
let line = line.split(';').next().unwrap_or("").trim();
|
|
if line.is_empty() {
|
|
continue;
|
|
}
|
|
if line.chars().count() > MAX_LINE_CHARS {
|
|
warn(no, "line too long; skipped".into());
|
|
continue;
|
|
}
|
|
let Some((alias, target)) = line.split_once(',') else {
|
|
warn(no, format!("`{line}` is not ALIAS, *COMMAND; skipped"));
|
|
continue;
|
|
};
|
|
if !target.trim_start().starts_with('*') {
|
|
warn(no, format!("{} is an external command; CADCraft doesn't run programs from alias files", alias.trim()));
|
|
continue;
|
|
}
|
|
let name = match alias_name(alias) {
|
|
Ok(n) => n,
|
|
Err(e) => {
|
|
warn(no, format!("{e}; skipped"));
|
|
continue;
|
|
}
|
|
};
|
|
let Some(id) = command_id(target) else {
|
|
warn(no, format!("{}: unknown command {}; skipped", alias.trim(), target.trim()));
|
|
continue;
|
|
};
|
|
if out.len() >= MAX_ALIASES {
|
|
warn(no, format!("more than {MAX_ALIASES} aliases; the rest are skipped"));
|
|
break;
|
|
}
|
|
// A later line for the same alias wins, as it would when typed into the file twice.
|
|
out.retain(|(a, _)| *a != name);
|
|
out.push((name, id));
|
|
}
|
|
(out, warnings)
|
|
}
|
|
|
|
fn listing(warnings: &[String], added: usize) -> Value {
|
|
let st = store();
|
|
json!({ "aliases": st.map, "file": st.file, "loaded": added, "warnings": warnings })
|
|
}
|
|
|
|
/// Replace the user aliases with a file's (`path` is remembered for REINIT).
|
|
fn load(text: &str, path: Option<String>) -> (usize, Vec<String>) {
|
|
let (aliases, warnings) = parse(text);
|
|
let mut st = store();
|
|
st.map = aliases.into_iter().collect();
|
|
if path.is_some() {
|
|
st.file = path;
|
|
}
|
|
(st.map.len(), warnings)
|
|
}
|
|
|
|
/// ALIASEDIT `{file? | text?, set?: {alias: command}, remove?: [alias], clear?: bool}`: load an
|
|
/// alias file (replacing the user aliases), clear them, or add and remove single aliases.
|
|
fn run_aliasedit(_s: &mut Session, p: &Value) -> Result<Value> {
|
|
let mut warnings = Vec::new();
|
|
let mut added = 0;
|
|
if bool_or(p, "clear", false) {
|
|
let mut st = store();
|
|
st.map.clear();
|
|
st.file = None;
|
|
}
|
|
if let Some((text, source)) = support_text("aliasedit", p, "file", "text")? {
|
|
let path = p.get("file").and_then(Value::as_str).map(|_| source);
|
|
(added, warnings) = load(&text, path);
|
|
}
|
|
match p.get("set") {
|
|
None | Some(Value::Null) => {}
|
|
Some(Value::Object(m)) => {
|
|
for (alias, target) in m {
|
|
let name = alias_name(alias).map_err(|e| bad("aliasedit", e))?;
|
|
let target = target.as_str().ok_or_else(|| bad("aliasedit", format!("{alias}: the command must be a name")))?;
|
|
let id = command_id(target).ok_or_else(|| bad("aliasedit", format!("{alias}: unknown command {target}")))?;
|
|
let mut st = store();
|
|
if !st.map.contains_key(&name) && st.map.len() >= MAX_ALIASES {
|
|
return Err(bad("aliasedit", format!("more than {MAX_ALIASES} aliases")));
|
|
}
|
|
st.map.insert(name, id);
|
|
added += 1;
|
|
}
|
|
}
|
|
Some(_) => return Err(bad("aliasedit", "`set` is an object {alias: command}")),
|
|
}
|
|
match p.get("remove") {
|
|
None | Some(Value::Null) => {}
|
|
Some(Value::Array(a)) => {
|
|
let mut st = store();
|
|
for v in a {
|
|
if let Some(n) = v.as_str() {
|
|
st.map.remove(&n.trim().to_ascii_lowercase());
|
|
}
|
|
}
|
|
}
|
|
Some(_) => return Err(bad("aliasedit", "`remove` is a list of aliases")),
|
|
}
|
|
Ok(listing(&warnings, added))
|
|
}
|
|
|
|
/// REINIT `{file? | text?}`: read the alias file again (the one last loaded, or the given one).
|
|
fn run_reinit(s: &mut Session, p: &Value) -> Result<Value> {
|
|
if let Some((text, source)) = support_text("reinit", p, "file", "text")? {
|
|
let path = p.get("file").and_then(Value::as_str).map(|_| source);
|
|
let (n, warnings) = load(&text, path);
|
|
s.echo(format!("{n} command alias(es) loaded."));
|
|
return Ok(listing(&warnings, n));
|
|
}
|
|
let Some(path) = store().file.clone() else {
|
|
s.echo("No command alias file is loaded; ALIASEDIT {file} loads one.");
|
|
return Ok(listing(&[], 0));
|
|
};
|
|
let (text, _) = support_text("reinit", &json!({ "file": path }), "file", "text")?.ok_or_else(|| bad("reinit", "no alias file"))?;
|
|
let (n, warnings) = load(&text, Some(path.clone()));
|
|
s.echo(format!("{n} command alias(es) loaded from {path}."));
|
|
Ok(listing(&warnings, n))
|
|
}
|
|
|
|
pub(crate) fn specs() -> Vec<CommandSpec> {
|
|
vec![
|
|
CommandSpec::new("aliasedit", "Command Aliases", run_aliasedit)
|
|
.menu(&["Tools", "Customize", "Edit Command Aliases (PGP)..."])
|
|
.params("{file?: path to a .pgp alias file | text?: its contents, set?: {alias: command}, remove?: [alias], clear?: bool} → {aliases, file, loaded, warnings}")
|
|
.enabled(always)
|
|
.noundo(),
|
|
CommandSpec::new("reinit", "Reinitialize", run_reinit).params("{file? | text?} (default: the alias file loaded last)").enabled(always).noundo(),
|
|
]
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::MAX_COMMAND_ID_CHARS;
|
|
|
|
/// [`super::MAX_ALIAS_PROFILE_BYTES`] bounds a saved alias's command id by the longest id in
|
|
/// the catalog: keep that true as commands are added.
|
|
#[test]
|
|
fn every_command_id_fits_the_saved_alias_bound() {
|
|
for spec in crate::cmd::command_specs() {
|
|
assert!(spec.id.chars().count() <= MAX_COMMAND_ID_CHARS, "`{}` exceeds the alias profile bound", spec.id);
|
|
}
|
|
}
|
|
|
|
/// An alias file path the profile couldn't restore isn't saved in it.
|
|
#[test]
|
|
fn only_paths_the_profile_restores_are_saved() {
|
|
assert!(super::keepable_path("C:\\support\\mine.pgp"));
|
|
assert!(!super::keepable_path(&"x".repeat(super::MAX_ALIAS_FILE_CHARS + 1)));
|
|
assert!(!super::keepable_path("mine\n.pgp"));
|
|
}
|
|
}
|