branch: externals/matlab-mode commit 51bad8954118530629c98d417c2f0a2c1df66d71 Author: John Ciolfi <john.ciolfi...@gmail.com> Commit: John Ciolfi <john.ciolfi...@gmail.com>
matlab-is-matlab-file: fix m-files in archives for Emacs 28 to 30 See: https://github.com/mathworks/Emacs-MATLAB-Mode/issues/56 --- matlab-is-matlab-file.el | 15 +++++- tests/test-matlab-is-matlab-file-files/archive.zip | Bin 0 -> 354 bytes tests/test-matlab-is-matlab-file.el | 59 +++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/matlab-is-matlab-file.el b/matlab-is-matlab-file.el index a706fc7f84..0823c3c83f 100644 --- a/matlab-is-matlab-file.el +++ b/matlab-is-matlab-file.el @@ -43,11 +43,22 @@ This will also enter MATLAB mode for empty files *.m files when (and buffer-file-name ;; have a file? ;; AND a valid MATLAB file name + (string-match "^\\(?:.*/\\)?[a-zA-Z][a-zA-Z0-9_]*\\.m\\'" ;; /path/to/file.m ? + ;; buffer-file-name in archives look like: + ;; /path/to/archive.zip:archive/foo.m + ;; and the ":" causes it too look like an invalid m-file path, so compare against the file + ;; name from the archive. (file-name-sans-versions (if (and (boundp 'archive-subfile-mode) archive-subfile-mode) - (aref archive-subfile-mode 0) ;; Will just be file.m without the directory + ;; When in an archive, the buffer-file-name will look like /path/to/archive.zip:foo.m, + ;; which will not be a valid M-file name because of the ":". Therefore, match against + ;; the file in the archive without the archive path. + (if (fboundp 'archive--file-desc-ext-file-name) ;; Emacs 28+ uses a cl-defstruct. + (archive--file-desc-ext-file-name archive-subfile-mode) + ;; Emacs 27 uses an array + (aref archive-subfile-mode 0)) buffer-file-name))) ;; AND (have MATLAB code OR an empty file that should enter matlab-mode) (or @@ -88,4 +99,4 @@ This will also enter MATLAB mode for empty files *.m files when (provide 'matlab-is-matlab-file) ;;; matlab-is-matlab-file.el ends here -;; LocalWords: alist defcustom mfiles objc defun boundp aref setq cdr +;; LocalWords: alist defcustom mfiles objc defun boundp aref setq cdr fboundp defstruct diff --git a/tests/test-matlab-is-matlab-file-files/archive.zip b/tests/test-matlab-is-matlab-file-files/archive.zip new file mode 100644 index 0000000000..1dabfd2f83 Binary files /dev/null and b/tests/test-matlab-is-matlab-file-files/archive.zip differ diff --git a/tests/test-matlab-is-matlab-file.el b/tests/test-matlab-is-matlab-file.el new file mode 100644 index 0000000000..8ca9559ac1 --- /dev/null +++ b/tests/test-matlab-is-matlab-file.el @@ -0,0 +1,59 @@ +;;; test-matlab-is-matlab-file.el --- -*- lexical-binding: t -*- +;; +;; Copyright 2025 Free Software Foundation, Inc. +;; +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 3, or (at your option) +;; any later version. +;; +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. +;; +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. +;; + +;;; Commentary: +;; +;; Test `matlab-is-matlab-file' +;; + +;;; Code: + +(require 'matlab-is-matlab-file) +(require 'arc-mode) + +(ert-deftest test-matlab-is-matlab-file () + "Test `matlab-is-matlab-file'. +Using ./test-matlab-is-matlab-file-files/archive.zip, +extract the first *.m file in it and validate it enters a MATLAB mode. +Also, validate `matlab-is-matlab-file' returns t." + (let* ((m-file "test-matlab-is-matlab-file-files/archive.zip") + (zip-buf (get-file-buffer m-file)) + m-file-major-mode + is-matlab-file) + (when zip-buf + (kill-buffer zip-buf)) + (setq zip-buf (find-file-noselect m-file)) + + (with-current-buffer zip-buf + (goto-char (point-min)) + (re-search-forward "\\.m$") + (let ((m-buf (archive-extract))) + (setq m-file-major-mode major-mode) + (setq is-matlab-file (matlab-is-matlab-file)) + (kill-buffer m-buf))) + + (kill-buffer zip-buf) + (should (or (eq m-file-major-mode 'matlab-ts-mode) + (eq m-file-major-mode 'matlab-mode))) + (should (eq is-matlab-file t)))) + +(provide 'test-matlab-is-matlab-file) +;;; test-matlab-is-matlab-file.el ends here + +;; LocalWords: buf setq noselect