Non-ASCII characters in file paths break matlab-shell-save-and-go and matlab-shell-run-region (emacsrun / emacsrunregion)

オープン
#74 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
40/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
emacs-lisp

調査の方向性

The payload names no source file, so start from the two MATLAB commands the report quotes, emacsrun for matlab-shell-save-and-go and emacsrunregion for matlab-shell-run-region, and find where the Emacs side builds them. Reproduce with a path containing é and compare the string sent to the matlab-shell process with what MATLAB receives, checking the process coding system. Done means both commands pass accented paths intact under the UTF-8 locale. The reporter's advice on comint-send-string is a workaround to read, not a fix to copy.

索引モデルが issue の本文から書いたものです。

説明

Description

When a file's path contains non-ASCII characters (e.g. é, è), commands that send the file path to MATLAB fail. Paths with only ASCII characters work. I see two different symptoms, depending on the command:

  1. matlab-shell-save-and-go (C-c C-s): the accented characters are replaced by char(NNN) calls that are sent as separate arguments to emacsrun.
  2. matlab-shell-run-region (C-c C-r): the accented characters are dropped entirely from the path sent to emacsrunregion.

This happens even though MATLAB itself reports a UTF-8 locale and encoding (see "Locale check" below), so it doesn't seem to be a simple locale misconfiguration.

Environment

  • matlab-mode version: 20260419.1224
  • Emacs version: GNU Emacs 30.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.52, cairo version 1.18.4)
  • OS: NixOS, MATLAB R2025a running inside an Ubuntu distrobox container, launched through a wrapper script (below)
  • Major mode: matlab-ts-mode

Configuration

(use-package matlab-mode
  :hook
  (matlab-ts-mode . matlab-sections-minor-mode)
  :bind
  (:map matlab-ts-mode-map
        ("C-c C-c" . matlab-sections-run-section)
        ("C-c C-r" . matlab-shell-run-region))
  :custom
  (matlab-indent-level 2)
  (matlab-verify-on-save nil)
  (matlab-shell-command "matlab")
  (matlab-shell-command-switches '("-nodesktop" "-nosplash")))

matlab is a wrapper script that runs MATLAB inside the container. It exports a UTF-8 locale before entering the container:

#!/bin/sh
# distrobox_binary
# name: matlab-box
MATLAB_BIN="/home/user/.local/share/distrobox-homes/matlab-box/.local/share/MATLAB/R2025a/bin/matlab"
export LANG=C.UTF-8
export LC_ALL=C.UTF-8
if [ -z "${CONTAINER_ID}" ]; then
    exec "/run/current-system/sw/bin/distrobox-enter" -n matlab-box -- bash -l -c 'exec "$@"' _ "$MATLAB_BIN" "$@"
elif [ -n "${CONTAINER_ID}" ] && [ "${CONTAINER_ID}" != "matlab-box" ]; then
    exec distrobox-host-exec '/home/user/.local/bin/matlab' "$@"
else
    exec "$MATLAB_BIN" "$@"
fi

Steps to reproduce

  1. Open a .m file whose path contains non-ASCII characters, e.g. /home/user/Documents/Université/Systèmes linéaires/exercice_1_q1.m
  2. Press C-c C-s, or select a region and press C-c C-r

Actual behavior

Symptom 1: matlab-shell-save-and-go (C-c C-s)

The non-ASCII characters become char(NNN) calls passed as separate arguments:

>> emacsrun('/home/user/Documents/Universit', char(233), '/2026-2027/M1/S1/Syst', char(232), 'mes lin', char(233), 'aires multivariables/.../exercice_1_q1')
Error using emacsrun (line 44)
Error: Invalid text character. Check for unsupported symbol, invisible
character, or pasting of non-ASCII characters.

Symptom 2: matlab-shell-run-region (C-c C-r)

The non-ASCII characters are missing from the path (Universit, Systmes linaires), so MATLAB can't find the file:

>> emacsrunregion('/home/user/Documents/Universit/2026-2027/M1/S1/Systmes linaires multivariables/.../exercice_1_q1.m',14,189)
Error using emacsrunregion (line 24)
You must save your region into a file accessible by MATLAB process.

Expected behavior

The full path, accents included, reaches MATLAB. For symptom 1, this works when typed by hand, with the arguments concatenated in [...]:

>> emacsrun(['/home/user/Documents/Universit', char(233), '/2026-2027/M1/S1/Syst', char(232), 'mes lin', char(233), 'aires multivariables/.../exercice_1_q1'])

Locale check

I initially suspected the container's locale (LANG is often unset in a distrobox login shell), so I added export LANG=C.UTF-8 and export LC_ALL=C.UTF-8 to the wrapper above. This did not change anything: both symptoms still occur.

feature('locale') run in the matlab-shell buffer inside Emacs reports a UTF-8 encoding everywhere:

>> feature('locale')

ans = 

  struct with fields:

               ctype: 'en_US_POSIX.UTF-8'
             collate: 'en_US_POSIX.UTF-8'
                time: 'en_US_POSIX.UTF-8'
             numeric: 'en_US_POSIX.UTF-8'
            monetary: 'en_US_POSIX.UTF-8'
            messages: 'en_US.UTF-8'
            encoding: 'UTF-8'
    terminalEncoding: 'UTF-8'
         jvmEncoding: 'UTF-8'
              status: 'MathWorks locale management system initialized.'
             warning: ''

So MATLAB's side is already UTF-8, and the failures happen anyway.

Possible cause

I haven't confirmed any of this in the source, so these are guesses:

  • For symptom 1, the code that builds the emacsrun(...) command seems to escape non-ASCII characters as char(NNN) without wrapping the result in [], so MATLAB receives several arguments. Since MATLAB reports UTF-8, the char(NNN) escaping may be applied unconditionally rather than depending on the locale.
  • For symptom 2, the characters are lost somewhere between building the command and MATLAB receiving it, possibly when Emacs encodes the string for the process (the coding system of the matlab-shell process), or in the code that builds the emacsrunregion(...) command. I haven't verified where.

Temporary workaround

An advice on comint-send-string that rewrites emacsrun* commands before they are sent. If a quoted string still contains non-ASCII characters, it turns that string into an ASCII-only MATLAB expression. If the command is already split into char(NNN) pieces, it wraps them in [].

(defun my/matlab--escape-literal (inner)
  "Return a MATLAB expression for the string INNER with non-ASCII chars as char(N)."
  (let (parts (run ""))
    (dolist (c (string-to-list inner))
      (if (< c 128)
          (setq run (concat run (string c)))
        (when (> (length run) 0)
          (push (format "'%s'" run) parts)
          (setq run ""))
        (push (if (< c 256)
                  (format "char(%d)" c)
                (format "native2unicode(uint8([%s]),'UTF-8')"
                        (mapconcat #'number-to-string
                                   (string-to-list (encode-coding-string (string c) 'utf-8))
                                   ",")))
              parts)))
    (when (> (length run) 0)
      (push (format "'%s'" run) parts))
    (concat "[" (mapconcat #'identity (nreverse parts) ", ") "]")))

(defun my/matlab-fix-emacsrun-args (args)
  "Make emacsrun* commands safe for paths with non-ASCII characters."
  (let ((proc (nth 0 args))
        (str  (nth 1 args)))
    (cond
     ((and (stringp str)
           (string-prefix-p "emacsrun" str)
           (string-match-p "[^[:ascii:]]" str))
      (list proc
            (replace-regexp-in-string
             "'\\(?:[^']\\|''\\)*'"
             (lambda (m)
               (if (string-match-p "[^[:ascii:]]" m)
                   (my/matlab--escape-literal (substring m 1 -1))
                 m))
             str t t)))
     ((and (stringp str)
           (string-match "\\`emacsrun(\\(.*\\))\\(\n?\\)\\'" str)
           (string-match-p "char([0-9]+)" str)
           (not (string-match-p "\\`emacsrun(\\[" str)))
      (list proc
            (concat "emacsrun([" (match-string 1 str) "])" (match-string 2 str))))
     (t args))))

(advice-add 'comint-send-string :filter-args #'my/matlab-fix-emacsrun-args)

With this advice, both C-c C-s (emacsrun) and C-c C-r (emacsrunregion) work on paths containing non-ASCII characters.

This is only a workaround. The proper fix would be in the code that builds these command strings and/or in how the shell process is started (encoding).

主要言語
Emacs Lisp
スター
27
フォーク
5
PR マージ指標
30日以内にマージされた PR はありません

環境構築

  • Dockerfile・Docker Compose ファイルなし
  • プルリクエストのテンプレートなし
  • コントリビューションガイドを読む

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

mathworks/Emacs-MATLAB-Mode のほかの issue

mathworks/Emacs-MATLAB-Mode の issue をすべて見る

似ている issue

DevTools の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。