198 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 8956c5abb5 mruby.h: include mruby/presym.h for all source files
Since presym is now mandatory, mruby.h includes presym.h so that
MRB_SYM() macros are available everywhere without explicit include.
Remove redundant #include <mruby/presym.h> from all source files.

Co-authored-by: Claude <noreply@anthropic.com>
2026-03-09 16:50:58 +09:00
Yukihiro "Matz" Matsumoto 9a0e8e33ed mirb: use ISSPACE/ISALNUM from mruby.h, remove stale ctype.h
Remove redundant local ISSPACE/ISALNUM definitions from
mirb_completion.c and unused ctype.h includes from both
mirb_completion.c and mirb.c. The locale-independent macros
from mruby.h are already available via <mruby.h>.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-23 15:49:36 +09:00
Yukihiro "Matz" Matsumoto 48c38c822f mirb_editor.c: table-drive block keywords in calc_indent_level()
Replace the strncmp() if-else chain for block-opening and closing
keywords with a data-driven indent_table, matching the existing
dedent_table pattern. Also use mirb_is_word_char() for the word
boundary check.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-23 15:46:28 +09:00
Yukihiro "Matz" Matsumoto f3dc8d4ae3 mirb_buffer.c: extract buffer_join_line_up() helper
Replace duplicate line-joining logic in mirb_buffer_delete_back()
and mirb_buffer_delete_forward() with a shared helper that appends
a line's content to the previous line, then removes it.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-23 15:35:09 +09:00
Yukihiro "Matz" Matsumoto 0b07cc1381 mirb_buffer.c: extract buffer_ensure_line_cap() helper
Replace three identical 7-line blocks that grow the lines array
with a single buffer_ensure_line_cap() helper function, matching
the existing line_ensure_cap() naming pattern.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-23 14:05:38 +09:00
Yukihiro "Matz" Matsumoto c23536daf9 mirb_completion.c: unify global completion contexts
Replace three separate global contexts (g_readline_ctx,
g_linenoise_ctx, g_editor_ctx) with a single g_ctx and shared
init_completion_ctx() helper. Consolidate the three identical
cleanup functions into mirb_cleanup_completion().

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-23 09:59:02 +09:00
Yukihiro "Matz" Matsumoto 9b24e120bc mirb_editor.c: table-drive is_dedent_keyword()
Replace the if-else chain with a data table that encodes each
dedent keyword, its valid delimiters, and whether it can appear
at end of line.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-23 09:57:06 +09:00
Yukihiro "Matz" Matsumoto d6b06634ec mirb: share is_word_char() and COLOR_RESET across modules
Move is_word_char() to mirb_buffer.h as mirb_is_word_char() static
inline, removing duplicate definitions from mirb_buffer.c and
mirb_highlight.c. Move COLOR_RESET to mirb_highlight.h, removing
the duplicate from mirb_editor.c.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-23 09:53:09 +09:00
Yukihiro "Matz" Matsumoto 9feb47f334 mirb.c: unify cleanup paths in main() using goto
Replace the cleanup() function and duplicated end-of-main cleanup
with a single goto cleanup label. This also fixes a minor resource
leak where cxt was not freed when library loading failed.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-23 08:32:26 +09:00
Yukihiro "Matz" Matsumoto 5394b71a60 mruby-bin-mirb: consolidate duplicate keyword lists
Move the Ruby keyword array from static definitions in both
mirb_highlight.c and mirb_completion.c to a single shared
mirb_keywords[] defined in mirb_highlight.c and declared in
mirb_highlight.h.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-23 08:09:33 +09:00
Yukihiro "Matz" Matsumoto aafa05522d mirb_editor.c: refactor duplicated indent calculation logic
Extract three helpers (calc_expected_indent, adjust_line_indent,
insert_indent_spaces) to eliminate repeated indent computation and
whitespace adjustment code in perform_dedent, reindent_line,
handle_tab_indent, and the Enter key handler.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-23 08:05:04 +09:00
Yukihiro "Matz" Matsumoto c06a11912c mruby-bin-mirb: fix uninitialized editor struct causing bintest failures
Zero-initialize the mirb_editor struct to prevent highlight.enabled
from containing garbage values when stdin is not a tty (e.g. in
bintest). Without this, ANSI color codes could be emitted in
non-interactive mode, breaking output string matching in tests.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-10 15:14:53 +09:00
Yukihiro "Matz" Matsumoto 1713d4a2e7 mruby-bin-mirb: syntax highlight result values and hash key symbols
Use syntax highlighter for result values instead of single color.
Add support for hash key symbol syntax (e.g., `a:` in `{a: 1}`).

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-10 11:12:26 +09:00
Yukihiro "Matz" Matsumoto e8e2e76fd6 mruby-bin-mirb: add colored output for results and errors
Result values are shown in cyan, errors in bold red.
The arrow " => " uses gray for subtle appearance.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-10 11:12:26 +09:00
Yukihiro "Matz" Matsumoto db4c8d91ea mruby-bin-mirb: add OSC 11 terminal background color detection
Automatically detect terminal background color using OSC 11 escape
sequence to select appropriate syntax highlighting theme (dark/light).

Detection priority: MIRB_THEME env > OSC 11 > COLORFGBG env > dark default.

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-10 11:12:26 +09:00
Paweł Świątkowski f856cf811f Require sys/socket.h (for Cosmopolitan)
Compilation of mirb with Cosmopolitan fails because of missing include
(Cosmopolitan seems to be more strict than "traditional" compilers.
2026-01-04 19:52:11 +01:00
Yukihiro "Matz" Matsumoto 624272b15d mruby-bin-mirb: add syntax highlighting for keywords and strings
Add syntax highlighting to mirb's multi-line editor with support for:
- keywords (def, if, class, end, etc.) in magenta
- strings ("...", '...', %q{...}) in green
- comments (#...) in gray
- numbers (42, 3.14, 0xff) in cyan
- symbols (:foo) in yellow
- constants (Array, Foo) in bold yellow
- instance variables (@var) in blue
- global variables ($var) in bold blue

Features:
- auto-detects light/dark theme via COLORFGBG env var
- MIRB_THEME=light/dark for explicit override
- method calls like obj.class correctly not highlighted as keywords
- enabled automatically when terminal supports color

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-29 11:56:58 +09:00
Yukihiro "Matz" Matsumoto 4a97da33c3 mruby-bin-mirb: add UTF-8 multibyte character support
When MRB_UTF8_STRING is defined, mirb editor now properly handles
UTF-8 multibyte characters:
- cursor movement skips entire UTF-8 characters
- backspace/delete removes entire UTF-8 characters
- display cursor positioning accounts for wide characters (CJK)
- accepts UTF-8 byte sequences from terminal input

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-29 11:56:57 +09:00
Yukihiro "Matz" Matsumoto 95ae434710 mirb_completion.c: fix strndup for MinGW
Use _WIN32 instead of _MSC_VER to provide strndup implementation
for all Windows compilers including MinGW/MSYS.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-26 21:21:20 +09:00
Yukihiro "Matz" Matsumoto d51a3504e8 mruby-bin-mirb: fix Windows/MSVC compilation warnings
- Define strdup as _strdup on MSVC to avoid deprecation warning
- Add strndup implementation for Windows (not available in MSVC)

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-26 17:21:33 +09:00
Yukihiro "Matz" Matsumoto 58604ba1d5 mruby-bin-mirb: extract is_line_blank() helper function
Consolidate duplicated blank line check logic from two places in the
ENTER key handler into a single helper function.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-22 11:24:12 +09:00
Yukihiro "Matz" Matsumoto c777762c9a mruby-bin-mirb: remove unused cursor save/restore functions
mirb_term_save_cursor() and mirb_term_restore_cursor() were never
called anywhere in the codebase.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-22 11:22:22 +09:00
Yukihiro "Matz" Matsumoto c418fc90a8 mruby-bin-mirb: add mirb_buffer_to_string_upto_line()
move buffer_to_string_upto_line() from mirb_editor.c to mirb_buffer.c
as a public API. mirb_buffer_to_string() now delegates to this function.
this eliminates code duplication and provides proper module encapsulation.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-22 10:28:03 +09:00
Yukihiro "Matz" Matsumoto c09fbbd262 mirb_editor.c: extract is_dedent_keyword() helper
consolidate duplicated dedenting keyword detection logic that was
repeated in reindent_line(), handle_tab_indent(), and handle_key().
the helper checks for end, else, elsif, when, in, rescue, ensure, and }.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-22 09:13:51 +09:00
Yukihiro "Matz" Matsumoto e4b8ae5156 mirb_completion.h: fix double semicolon typo
Co-authored-by: Claude <noreply@anthropic.com>
2025-12-21 17:43:51 +09:00
Yukihiro "Matz" Matsumoto 814b0cccd9 mruby-bin-mirb: add auto-dedent for 'in' keyword
add support for automatic dedentation when typing 'in' at the
beginning of a line, matching the behavior of 'when' for pattern
matching case/in expressions.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-18 16:27:27 +09:00
Yukihiro "Matz" Matsumoto d8045dc5bc mruby-bin-mirb: re-indent current line on Enter
Before inserting a newline, re-indent the current line to match
the expected indent level. This fixes cases where the user typed
with incorrect indentation.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-18 16:27:25 +09:00
Yukihiro "Matz" Matsumoto ac306ae3e5 mruby-bin-mirb: preserve cursor position during TAB auto-indent
When TAB triggers auto-indentation, preserve the cursor's relative
position within the line instead of moving it to the indent boundary.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-18 16:27:25 +09:00
Yukihiro "Matz" Matsumoto f585cc2eff mruby-bin-mirb: align dedent to proper indent level
Instead of just removing 2 spaces, perform_dedent() now calculates
the expected indent level from previous lines and aligns to that.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-18 16:27:24 +09:00
Yukihiro "Matz" Matsumoto e901b6d1a3 mruby-bin-mirb: auto-dedent for else, elsif, when, rescue, ensure
Extend auto-dedent to trigger when typing dedent keywords, not just
end and }. Now dedent occurs when completing: else, elsif, when,
rescue, ensure.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-18 16:27:24 +09:00
Yukihiro "Matz" Matsumoto 1c07ac4892 mruby-bin-mirb: fix indentation when splitting line at dedent keyword
When splitting a line with Enter, check if the new line starts with
a dedenting keyword (end, else, elsif, when, rescue, ensure, }) and
reduce indentation by one level.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-18 16:27:24 +09:00
Yukihiro "Matz" Matsumoto fa74186b51 mruby-bin-mirb: add TAB auto-indentation
TAB now performs auto-indentation instead of completion when:
- cursor is at start of line
- cursor is at end of line
- character before cursor is whitespace

Auto-indent calculates expected indent level from previous lines
and adjusts current line. Dedenting keywords (end, else, elsif,
when, rescue, ensure, }) reduce indent by one level.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-18 16:27:24 +09:00
Yukihiro "Matz" Matsumoto cda57fcf03 mruby-bin-mirb: make Ctrl+K delete empty lines
when pressing Ctrl+K on an empty line, delete the entire line instead
of doing nothing. this makes it easier to clean up empty lines while
editing multi-line input.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-18 16:27:24 +09:00
Yukihiro "Matz" Matsumoto 53e78b8353 mruby-bin-mirb: only evaluate on Enter at end of last line
pressing Enter in the middle of multi-line input now always inserts
a new line instead of evaluating, even if the code is syntactically
complete. evaluation only occurs when cursor is at the end of the
last line.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-18 16:27:24 +09:00
Yukihiro "Matz" Matsumoto d3fcb176bf mruby-bin-mirb: restore tab completion for custom editor
- restore mirb_completion.c/h from before readline removal
- add editor adapter for tab completion (mirb_setup_editor_completion,
  mirb_get_completions, mirb_free_completions)
- add TAB key handling in mirb_editor.c
- fix string literal completion: properly detect when cursor is outside
  a string by scanning forward, allow string/array/hash literals as
  safe receivers for method completion

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-18 16:27:24 +09:00
Yukihiro "Matz" Matsumoto d47dedd17d mruby-bin-mirb: improve Enter key behavior and auto-indentation
- fix Enter in middle of line with trailing blank continuation line:
  now properly splits the line and removes redundant blank line
- fix auto-indentation when inserting in middle of existing code:
  calculate indent from lines up to cursor, not entire buffer
- add mirb_buffer_delete_line() for removing lines from buffer

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-18 16:27:23 +09:00
Yukihiro "Matz" Matsumoto 23c49ad026 mruby-bin-mirb: show incrementing line numbers in multi-line prompts
Previously, all continuation lines showed the same line number (e.g.,
"1*" for every line). Now each line shows its actual line number:

  1> class Foo
  2*   def bar
  3*   end
  4* end

Add mirb_editor_set_prompt_format() which accepts printf-style format
strings (e.g., "%d> ", "%d* ") and calculates the correct prompt length
for each line to ensure proper cursor positioning.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-18 16:27:23 +09:00
Yukihiro "Matz" Matsumoto 5f85c1beae mruby-bin-mirb: add command history with Up/Down navigation
Add in-memory command history for mirb sessions:
- Up arrow on first line: navigate to older history entries
- Down arrow on last line: navigate to newer history entries
- Current input is preserved when browsing and restored when
  navigating past the newest entry
- History uses a circular buffer (100 entries max)
- Duplicate consecutive entries are not added

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-18 16:27:22 +09:00
Yukihiro "Matz" Matsumoto 527018cf07 mruby-bin-mirb: replace readline with custom multi-line editor
Remove readline/linenoise dependency and implement custom multi-line
editor with:
- Terminal raw mode handling (POSIX termios)
- Multi-line buffer with cursor navigation
- Auto-indentation for Ruby blocks
- Auto-dedentation when typing 'end' or '}'
- Natural terminal scrolling behavior
- Emacs-style keybindings (Ctrl+A/E/K/U/W/Y, Alt+B/F/D)

This eliminates GPL licensing concerns from readline while providing
better multi-line editing than the previous single-line implementation.

The MRUBY_MIRB_READLINE environment variable is no longer supported
as readline integration has been completely removed; ref #6626

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:10 +09:00
Yukihiro "Matz" Matsumoto b36e0b4090 mruby-bin-mirb: add colored output for prompts and errors
Add ANSI color support to mirb for better visual distinction:
- green prompts (both ready '>' and continuation '*')
- red error messages (syntax errors, runtime errors, warnings)
- bold result indicator ('=>')

Colors are automatically disabled when:
- output is not a TTY
- TERM is unset or "dumb"
- NO_COLOR environment variable is set

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:10 +09:00
Yukihiro "Matz" Matsumoto d52f3189d8 mruby-bin-mirb: add auto-indent for continuation lines
- automatically indent continuation lines based on block depth
- detect block-opening keywords (def, class, if, do, etc.) and braces
- use ANSI escape sequences to fix indentation for:
  - block-closing keywords (end, })
  - mid-block keywords (else, elsif, rescue, ensure, when)
- only active for interactive TTY input with ANSI support
- requires GNU readline (not available with linenoise)

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:10 +09:00
Yukihiro "Matz" Matsumoto f55bb0857a mruby-bin-mirb: add README with tab completion documentation
Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:10 +09:00
Yukihiro "Matz" Matsumoto 4cf75fb035 mruby-bin-mirb: restrict evaluation to simple receivers
fixed a bug where tab completion on complex expressions like "d.new(1).a"
would corrupt local variables, causing them to become nil.

the root cause was that evaluating complex receiver expressions during tab
completion ran mrb_vm_run() without proper stack management (stack_keep)
and environment adjustment that mirb's main REPL loop performs. this
corrupted the local variable storage.

the fix restricts tab completion to only evaluate simple receiver
expressions (variable/constant names without operators or method calls).
complex expressions are skipped for completion. this means:
- works: d.<tab> completes methods of variable d
- works: String.<tab> completes methods of constant String
- skipped: d.new(1).<tab> provides no completion

this is a reasonable trade-off that prevents the corruption bug while
still supporting the most common completion scenarios.

also updated mirb_eval_receiver() to use the compiler context for proper
local variable resolution, with argument order matching mrb_parse_string.

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:05 +09:00
Yukihiro "Matz" Matsumoto 2f152823f3 mruby-bin-mirb: add tab completion support
implements context-aware tab completion for mirb supporting all readline
variants (GNU readline, libedit, linenoise) with graceful degradation
when no readline library is available.

completion features:
- method names on objects (e.g., "hello".re<Tab> completes to reverse, replace)
- local variables from compiler context
- global variables via Ruby introspection
- constants and class names
- Ruby keywords

architecture:
- core completion engine is library-agnostic
- thin adapters for readline/libedit and linenoise
- context detection based on cursor position analysis
- safe receiver evaluation with exception handling
- proper word break characters so "String.new" works correctly

implementation adds:
- mirb_completion.h: interface definitions and data structures
- mirb_completion.c: complete implementation (~670 lines)
- mirb.c: integration with setup/cleanup calls

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:05 +09:00
Yukihiro "Matz" Matsumoto 0aafb83374 mruby-bin-mirb: add MRUBY_MIRB_READLINE to control readline library selection
addresses #6626 where users building portable binaries need explicit control
over readline detection instead of auto-detection.

MRUBY_MIRB_READLINE values:
  auto (default) - auto-detect: try readline, then edit, then linenoise
  readline, gnu  - force GNU readline only
  edit, libedit  - force libedit only
  linenoise      - force linenoise only
  none, off, false, disabled - use plain input mode (no readline)

close #6626

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:05 +09:00
Yukihiro "Matz" Matsumoto 9aa6f28eab mirb: show method context for errors from previous code
runtime errors now distinguish between:
- errors in current input: show relative line number
- errors from previously defined methods: show method context

examples:
1> a.foo
line 1: undefined method 'a' for Object (NoMethodError)

1> def foo
2*   bar
3* end
1> foo
(mirb):in foo: undefined method 'bar' for Object (NoMethodError)

this provides better context since method name is more useful
than line number for errors in previously defined code

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:05 +09:00
Yukihiro "Matz" Matsumoto ac39b7d8ad mruby-bin-mirb: enhanced syntax error display
syntax errors now show:
- line:column format with relative line numbers (matching prompt)
- source line from user input
- caret indicator pointing to error position

example:
1> x = @@@
line 1:6: syntax error, unexpected invalid token
  x = @@@
        ^

multi-line example:
1> class Foo
2*   def bar
3*     x = @@@
line 3:6: syntax error, unexpected invalid token
      x = @@@
        ^

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:05 +09:00
Yukihiro "Matz" Matsumoto 5a3f0e2e0b mruby-bin-mirb: add line numbers to multi-line prompts
add minimal line number decoration to prompts to help track position
within multi-line code blocks. format is 'N>' for initial line and
'N*' for continuation lines. line counter resets after each complete
evaluation for clarity and minimal visual noise.

example:
1> def foo
2*   x = 1
3* end
 => :foo
1> 1 + 1
 => 2

Co-authored-by: Claude <noreply@anthropic.com>
2025-12-11 18:09:05 +09:00
Yukihiro "Matz" Matsumoto 40b0cb98f7 mruby.h: add MRB_OPEN_FAILURE() macro and refactor MRB_OPEN_SUCCESS()
since all current uses check for failure (!MRB_OPEN_SUCCESS), add
MRB_OPEN_FAILURE() as the primary macro for better readability. define
MRB_OPEN_SUCCESS() in terms of MRB_OPEN_FAILURE() to avoid duplication
and optimize the common case. update all usage sites to use the clearer
MRB_OPEN_FAILURE() form.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-16 06:53:02 +09:00
Yukihiro "Matz" Matsumoto 8e50a45f3e mrb_print_error: handle NULL gracefully to simplify error checking
made mrb_print_error() handle NULL by printing "Failed to allocate
mrb_state" when mrb is NULL. since mrb_close() already handles NULL,
this allows simplified error checking pattern:

  if (!MRB_OPEN_SUCCESS(mrb)) {
    mrb_print_error(mrb);  // handles NULL
    mrb_close(mrb);        // handles NULL
    return EXIT_FAILURE;
  }

updated all binary tools (mruby, mirb, mrdb, mrbtest) to use this
simplified pattern, removing nested if checks.

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-14 00:49:55 +09:00