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

Đang mở
#74 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
40/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
emacs-lisp

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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).

Ngôn ngữ chính
Emacs Lisp
Star
27
Fork
5
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

  • Không có Dockerfile hay tệp Docker Compose
  • Không có mẫu pull request
  • Đọc hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của mathworks/Emacs-MATLAB-Mode

Tất cả issue của mathworks/Emacs-MATLAB-Mode

Issue tương tự

Thêm issue về DevTools

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.