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

未关闭
#74 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
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 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

mathworks/Emacs-MATLAB-Mode 的其他 Issue

查看 mathworks/Emacs-MATLAB-Mode 的全部 Issue

相似的 Issue

更多 DevTools Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。