neovm-core 0.0.1

Core runtime structures for NeoVM
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! Image type support builtins.
//!
//! Provides stub/partial implementations of Emacs image builtins:
//! - `image-type-available-p` — check if image type is available
//! - `create-image` — create image descriptor (property list)
//! - `image-size` — return (WIDTH . HEIGHT) cons
//! - `image-mask-p` — check for mask support
//! - `put-image` / `insert-image` / `remove-images` — display stubs
//! - `image-flush` / `clear-image-cache` — cache management stubs
//! - `image-type` — extract type from image spec
//! - `display-images-p` / `image-transforms-p` — capability queries
//!
//! Image specs are property lists: (:type png :file "foo.png" :width 100 ...)

use super::error::{EvalResult, Flow, signal};
use super::intern::resolve_sym;
use super::value::*;
use crate::window::FRAME_ID_BASE;

// ---------------------------------------------------------------------------
// Argument helpers
// ---------------------------------------------------------------------------

fn expect_args(name: &str, args: &[Value], n: usize) -> Result<(), Flow> {
    if args.len() != n {
        Err(signal(
            "wrong-number-of-arguments",
            vec![Value::symbol(name), Value::fixnum(args.len() as i64)],
        ))
    } else {
        Ok(())
    }
}

fn expect_min_args(name: &str, args: &[Value], min: usize) -> Result<(), Flow> {
    if args.len() < min {
        Err(signal(
            "wrong-number-of-arguments",
            vec![Value::symbol(name), Value::fixnum(args.len() as i64)],
        ))
    } else {
        Ok(())
    }
}

fn expect_max_args(name: &str, args: &[Value], max: usize) -> Result<(), Flow> {
    if args.len() > max {
        Err(signal(
            "wrong-number-of-arguments",
            vec![Value::symbol(name), Value::fixnum(args.len() as i64)],
        ))
    } else {
        Ok(())
    }
}

fn expect_range_args(name: &str, args: &[Value], min: usize, max: usize) -> Result<(), Flow> {
    if args.len() < min || args.len() > max {
        Err(signal(
            "wrong-number-of-arguments",
            vec![Value::symbol(name), Value::fixnum(args.len() as i64)],
        ))
    } else {
        Ok(())
    }
}

fn expect_frame_designator(_name: &str, value: &Value) -> Result<(), Flow> {
    match value.kind() {
        ValueKind::Nil => Ok(()),
        ValueKind::Fixnum(id) if id >= 0 && (id as u64) >= FRAME_ID_BASE => Ok(()),
        ValueKind::Veclike(VecLikeType::Frame) if value.as_frame_id().unwrap() >= FRAME_ID_BASE => {
            Ok(())
        }
        _ => Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("frame-live-p"), *value],
        )),
    }
}

fn normalized_keyword_name(value: &Value) -> Option<&str> {
    match value.kind() {
        ValueKind::Symbol(id) => {
            let s = resolve_sym(id);
            Some(s.strip_prefix(':').unwrap_or(s))
        }
        _ => None,
    }
}

fn integer_or_marker_p(value: &Value) -> bool {
    value.as_int().is_some() || value.is_marker()
}

// ---------------------------------------------------------------------------
// Property list helpers
// ---------------------------------------------------------------------------

/// Get a value from a property list by keyword.
/// The plist is a flat list: (:key1 val1 :key2 val2 ...).
#[cfg(test)]
fn plist_get(plist: &Value, key: &Value) -> Value {
    let mut cursor = *plist;
    loop {
        match cursor.kind() {
            ValueKind::Cons => {
                let pair_car = cursor.cons_car();
                let pair_cdr = cursor.cons_cdr();
                if eq_value(&pair_car, key) {
                    // Next element is the value.
                    match pair_cdr.kind() {
                        ValueKind::Cons => {
                            return pair_cdr.cons_car();
                        }
                        _ => return Value::NIL,
                    }
                }
                // Skip the value entry.
                match pair_cdr.kind() {
                    ValueKind::Cons => {
                        cursor = pair_cdr.cons_cdr();
                    }
                    _ => return Value::NIL,
                }
            }
            _ => return Value::NIL,
        }
    }
}

/// Check whether a symbol name represents a supported image type.
fn is_supported_image_type(name: &str) -> bool {
    matches!(
        name,
        "png" | "jpeg" | "gif" | "svg" | "webp" | "xpm" | "xbm" | "pbm" | "tiff" | "bmp"
    )
}

fn normalize_image_type_name(name: &str) -> Option<&'static str> {
    let lower = name.to_ascii_lowercase();
    match lower.as_str() {
        "jpg" => Some("jpeg"),
        "jpeg" => Some("jpeg"),
        "png" => Some("png"),
        "gif" => Some("gif"),
        "svg" => Some("svg"),
        "webp" => Some("webp"),
        "xpm" => Some("xpm"),
        "xbm" => Some("xbm"),
        "pbm" => Some("pbm"),
        "tif" | "tiff" => Some("tiff"),
        "bmp" => Some("bmp"),
        "neomacs" => Some("neomacs"),
        _ => None,
    }
}

fn infer_image_type_from_filename(path: &str) -> Option<&'static str> {
    let ext = path.rsplit('.').next()?;
    normalize_image_type_name(ext)
}

/// Validate that a value looks like an image spec.
/// Oracle-compatible shape:
/// - list starts with symbol `image`
/// - plist includes a supported symbolic `:type`
/// - plist includes exactly one source key: `:file` or `:data`
/// - source value is a string
fn is_image_spec(value: &Value) -> bool {
    let items = match list_to_vec(value) {
        Some(v) => v,
        None => return false,
    };

    if items.is_empty() || items[0].as_symbol_name() != Some("image") {
        return false;
    }

    let mut type_seen = false;
    let mut type_ok = false;
    let mut file_seen = false;
    let mut file_ok = false;
    let mut data_seen = false;
    let mut data_ok = false;

    let mut i = 1usize;
    while i + 1 < items.len() {
        if let Some(key) = normalized_keyword_name(&items[i]) {
            let val = &items[i + 1];
            match key {
                "type" if !type_seen => {
                    type_seen = true;
                    type_ok = val.as_symbol_name().is_some_and(is_supported_image_type);
                }
                "file" if !file_seen => {
                    file_seen = true;
                    file_ok = val.as_str().is_some();
                }
                "data" if !data_seen => {
                    data_seen = true;
                    data_ok = val.as_str().is_some();
                }
                _ => {}
            }
        }
        i += 2;
    }

    if !type_seen || !type_ok {
        return false;
    }

    match (file_seen, data_seen) {
        (true, false) => file_ok,
        (false, true) => data_ok,
        _ => false,
    }
}

/// Extract the plist portion of an image spec.
/// If the spec starts with `image`, skip that first element.
#[cfg(test)]
fn image_spec_plist(spec: &Value) -> Value {
    let items = match list_to_vec(spec) {
        Some(v) => v,
        None => return Value::NIL,
    };
    if items.is_empty() {
        return Value::NIL;
    }
    if let Some(name) = items[0].as_symbol_name() {
        if name == "image" {
            // Plist is everything after the `image` symbol.
            return Value::list(items[1..].to_vec());
        }
    }
    // Already a bare plist.
    *spec
}

// ---------------------------------------------------------------------------
// Pure builtins
// ---------------------------------------------------------------------------

/// (image-type-available-p TYPE) -> t or nil
///
/// Return t if image type TYPE is available in this Emacs instance.
/// Supported types: png, jpeg, gif, svg, webp, xpm, xbm, pbm, tiff, bmp.
pub(crate) fn builtin_image_type_available_p(args: Vec<Value>) -> EvalResult {
    expect_args("image-type-available-p", &args, 1)?;
    let type_name = match args[0].as_symbol_name() {
        Some(name) => name.to_string(),
        None => {
            return Err(signal(
                "wrong-type-argument",
                vec![Value::symbol("symbolp"), args[0]],
            ));
        }
    };
    Ok(Value::bool_val(is_supported_image_type(&type_name)))
}

/// (create-image FILE-OR-DATA &optional TYPE DATA-P &rest PROPS) -> image descriptor
///
/// Create an image descriptor (a list starting with `image`).
/// FILE-OR-DATA is a file name string or raw data string.
/// TYPE is a symbol like `png`, `jpeg`, etc.
/// DATA-P if non-nil means FILE-OR-DATA is raw image data, not a file name.
/// PROPS are additional property-list pairs (e.g. :width 100 :height 200).
///
/// Returns: (image :type TYPE :file FILE-OR-DATA ... PROPS)
pub(crate) fn builtin_create_image(args: Vec<Value>) -> EvalResult {
    expect_min_args("create-image", &args, 1)?;

    let file_or_data = args[0];
    let data_p = args.len() > 2 && args[2].is_truthy();

    // TYPE argument (optional).
    let image_type = if args.len() > 1 && !args[1].is_nil() {
        match args[1].as_symbol_name() {
            Some(name) => {
                let normalized = normalize_image_type_name(name).unwrap_or(name);
                Value::symbol(normalized)
            }
            None => {
                let rendered = super::print::print_value(&args[1]);
                return Err(signal(
                    "error",
                    vec![Value::string(format!("Invalid image type `{rendered}`"))],
                ));
            }
        }
    } else {
        let inferred = if data_p {
            None
        } else {
            file_or_data
                .as_str()
                .and_then(infer_image_type_from_filename)
                .map(str::to_string)
        };
        match inferred {
            Some(name) => Value::symbol(name),
            None => Value::NIL,
        }
    };

    // Build the image spec property list.
    let mut spec_items: Vec<Value> = Vec::new();
    spec_items.push(Value::symbol("image"));
    spec_items.push(Value::keyword("type"));
    spec_items.push(image_type);

    if data_p {
        spec_items.push(Value::keyword("data"));
        spec_items.push(file_or_data);
    } else {
        spec_items.push(Value::keyword("file"));
        spec_items.push(file_or_data);
    }

    // Emacs adds :scale default on freshly created image specs.
    spec_items.push(Value::keyword("scale"));
    spec_items.push(Value::symbol("default"));

    // Append any extra PROPS (starting from index 3).
    if args.len() > 3 {
        for prop in &args[3..] {
            spec_items.push(*prop);
        }
    }

    Ok(Value::list(spec_items))
}

/// (image-size SPEC &optional PIXELS FRAME) -> (WIDTH . HEIGHT)
///
/// Batch/no-window semantics:
/// - invalid SPEC -> `(error "Invalid image specification")`
/// - valid SPEC in batch -> `(error "Window system frame should be used")`
pub(crate) fn builtin_image_size(args: Vec<Value>) -> EvalResult {
    expect_min_args("image-size", &args, 1)?;
    expect_max_args("image-size", &args, 3)?;

    if !is_image_spec(&args[0]) {
        return Err(signal(
            "error",
            vec![Value::string("Invalid image specification")],
        ));
    }
    Err(signal(
        "error",
        vec![Value::string("Window system frame should be used")],
    ))
}

/// (image-mask-p SPEC &optional FRAME) -> nil
///
/// Batch/no-window semantics:
/// - invalid SPEC -> `(error "Invalid image specification")`
/// - valid SPEC in batch -> `(error "Window system frame should be used")`
pub(crate) fn builtin_image_mask_p(args: Vec<Value>) -> EvalResult {
    expect_min_args("image-mask-p", &args, 1)?;
    expect_max_args("image-mask-p", &args, 2)?;

    if !is_image_spec(&args[0]) {
        return Err(signal(
            "error",
            vec![Value::string("Invalid image specification")],
        ));
    }
    Err(signal(
        "error",
        vec![Value::string("Window system frame should be used")],
    ))
}

/// (put-image IMAGE POINT &optional STRING AREA) -> nil
///
/// Display IMAGE at POINT in the current buffer as an overlay.
/// Stub: does nothing, returns nil.
pub(crate) fn builtin_put_image(args: Vec<Value>) -> EvalResult {
    expect_min_args("put-image", &args, 2)?;
    expect_max_args("put-image", &args, 4)?;

    // Validate that first arg looks like an image spec.
    if !is_image_spec(&args[0]) {
        let rendered = super::print::print_value(&args[0]);
        return Err(signal(
            "error",
            vec![Value::string(format!("Not an image: {rendered}"))],
        ));
    }

    // Validate POINT is integer-or-marker in batch.
    if !integer_or_marker_p(&args[1]) {
        return Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("integer-or-marker-p"), args[1]],
        ));
    }

    // Optional AREA must be nil, left-margin, or right-margin.
    if args.len() > 3 && !args[3].is_nil() {
        let valid = matches!(
            args[3].as_symbol_name(),
            Some("left-margin") | Some("right-margin")
        );
        if !valid {
            let rendered = super::print::print_value(&args[3]);
            return Err(signal(
                "error",
                vec![Value::string(format!("Invalid area {rendered}"))],
            ));
        }
    }

    // Batch compatibility: return a truthy placeholder for inserted overlay.
    Ok(Value::T)
}

/// (insert-image IMAGE &optional STRING AREA SLICE) -> nil
///
/// Insert IMAGE into the current buffer at point.
/// Batch stub: validates IMAGE and returns t.
pub(crate) fn builtin_insert_image(args: Vec<Value>) -> EvalResult {
    expect_min_args("insert-image", &args, 1)?;
    expect_max_args("insert-image", &args, 5)?;

    if !is_image_spec(&args[0]) {
        let rendered = super::print::print_value(&args[0]);
        return Err(signal(
            "error",
            vec![Value::string(format!("Not an image: {rendered}"))],
        ));
    }

    // Optional AREA must be nil, left-margin, or right-margin.
    if args.len() > 2 && !args[2].is_nil() {
        let valid = matches!(
            args[2].as_symbol_name(),
            Some("left-margin") | Some("right-margin")
        );
        if !valid {
            let rendered = super::print::print_value(&args[2]);
            return Err(signal(
                "error",
                vec![Value::string(format!("Invalid area {rendered}"))],
            ));
        }
    }

    Ok(Value::T)
}

/// (remove-images START END &optional BUFFER) -> nil
///
/// Remove images between START and END in BUFFER.
/// Stub: does nothing, returns nil.
pub(crate) fn builtin_remove_images(args: Vec<Value>) -> EvalResult {
    expect_min_args("remove-images", &args, 2)?;
    expect_max_args("remove-images", &args, 3)?;

    // Validate START and END are integer-or-marker in batch.
    if !integer_or_marker_p(&args[0]) {
        return Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("integer-or-marker-p"), args[0]],
        ));
    }
    if !integer_or_marker_p(&args[1]) {
        return Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("integer-or-marker-p"), args[1]],
        ));
    }

    // Stub: no-op.
    Ok(Value::NIL)
}

/// (image-flush SPEC &optional FRAME) -> nil
///
/// Flush the image cache for image SPEC.
/// Batch semantics:
/// - invalid SPEC -> `(error "Invalid image specification")`
/// - FRAME = t -> nil (all-frames path)
/// - otherwise -> `(error "Window system frame should be used")`
pub(crate) fn builtin_image_flush(args: Vec<Value>) -> EvalResult {
    expect_min_args("image-flush", &args, 1)?;
    expect_max_args("image-flush", &args, 2)?;

    if !is_image_spec(&args[0]) {
        return Err(signal(
            "error",
            vec![Value::string("Invalid image specification")],
        ));
    }

    if let Some(frame) = args.get(1) {
        if frame.is_t() {
            return Ok(Value::NIL);
        }
        if !frame.is_nil() {
            expect_frame_designator("image-flush", frame)?;
        }
    }

    Err(signal(
        "error",
        vec![Value::string("Window system frame should be used")],
    ))
}

/// (clear-image-cache &optional FILTER) -> nil
///
/// Clear the image cache.  FILTER can be nil (clear all), a frame,
/// or t (clear all frames).
/// Stub: does nothing, returns nil.
pub(crate) fn builtin_clear_image_cache(args: Vec<Value>) -> EvalResult {
    if args.len() > 2 {
        return Err(signal(
            "wrong-number-of-arguments",
            vec![
                Value::symbol("clear-image-cache"),
                Value::fixnum(args.len() as i64),
            ],
        ));
    }

    if args.len() == 2 {
        let animation_cache = &args[1];
        if !animation_cache.is_nil() && !animation_cache.is_cons() {
            return Err(signal(
                "wrong-type-argument",
                vec![Value::symbol("listp"), *animation_cache],
            ));
        }
        // When animation-cache is non-nil, Emacs does not validate `filter`.
    }

    if args.is_empty() {
        return Err(signal(
            "error",
            vec![Value::string("Window system frame should be used")],
        ));
    }

    if args[0].is_nil() {
        return Err(signal(
            "error",
            vec![Value::string("Window system frame should be used")],
        ));
    }

    Ok(Value::NIL)
}

/// (image-cache-size) -> integer
///
/// NeoVM currently has no persistent image cache, so this is always 0.
pub(crate) fn builtin_image_cache_size(args: Vec<Value>) -> EvalResult {
    expect_args("image-cache-size", &args, 0)?;
    Ok(Value::fixnum(0))
}

/// (image-metadata SPEC &optional FRAME) -> metadata object or nil
///
/// Returns nil for non-image specifications. For valid image specs on
/// non-window-system frames, this signals the same error shape as GNU Emacs.
pub(crate) fn builtin_image_metadata(args: Vec<Value>) -> EvalResult {
    expect_range_args("image-metadata", &args, 1, 2)?;

    if !is_image_spec(&args[0]) {
        return Ok(Value::NIL);
    }

    if let Some(frame) = args.get(1) {
        expect_frame_designator("image-metadata", frame)?;
    }

    Err(signal(
        "error",
        vec![Value::string("Window system frame should be used")],
    ))
}

/// (imagep OBJECT) -> t if OBJECT looks like an image descriptor.
pub(crate) fn builtin_imagep(args: Vec<Value>) -> EvalResult {
    expect_args("imagep", &args, 1)?;
    Ok(Value::bool_val(is_image_spec(&args[0])))
}

/// (image-type SOURCE &optional TYPE DATA-P) -> symbol
///
/// Compatibility behavior:
/// - SOURCE must be a file name string.
/// - TYPE, when non-nil, must be a symbol and is returned (normalized aliases).
/// - Without TYPE, type is inferred from file extension.
/// - If type inference fails, signal `unknown-image-type`.
pub(crate) fn builtin_image_type(args: Vec<Value>) -> EvalResult {
    expect_min_args("image-type", &args, 1)?;
    expect_max_args("image-type", &args, 3)?;

    let source = &args[0];
    let explicit_type = args.get(1).cloned().unwrap_or(Value::NIL);
    let data_p = args.get(2).cloned().unwrap_or(Value::NIL);

    if source.as_str().is_none() {
        let rendered = super::print::print_value(source);
        return Err(signal(
            "error",
            vec![Value::string(format!(
                "Invalid image file name `{rendered}`"
            ))],
        ));
    }

    let resolved = if explicit_type.is_nil() {
        if data_p.is_truthy() {
            None
        } else {
            source
                .as_str()
                .and_then(infer_image_type_from_filename)
                .map(str::to_string)
        }
    } else {
        let rendered = super::print::print_value(&explicit_type);
        let sym_name = explicit_type.as_symbol_name().ok_or_else(|| {
            signal(
                "error",
                vec![Value::string(format!("Invalid image type `{rendered}`"))],
            )
        })?;
        Some(
            normalize_image_type_name(sym_name)
                .unwrap_or(sym_name)
                .to_string(),
        )
    };

    let Some(resolved) = resolved else {
        return Err(signal(
            "unknown-image-type",
            vec![Value::list(vec![Value::string(
                "Cannot determine image type",
            )])],
        ));
    };

    Ok(Value::symbol(resolved))
}

/// (image-transforms-p &optional FRAME) -> bool
///
/// Return nil if FRAME does not match an active frame designator in Neovm.
/// (Compatibility layer keeps this conservative and follows official Emacs semantics
/// observed for common callers.)
pub(crate) fn builtin_image_transforms_p(args: Vec<Value>) -> EvalResult {
    expect_max_args("image-transforms-p", &args, 1)?;
    if let Some(frame_or_display) = args.first() {
        expect_frame_designator("image-transforms-p", frame_or_display)?;
    }
    Ok(Value::NIL)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
#[path = "image_test.rs"]
mod tests;