From 1c6a76ae9c436fe89bb9ab638696e4509a100a96 Mon Sep 17 00:00:00 2001 From: Tom Gillespie Date: Sun, 15 Aug 2021 14:31:43 -0700 Subject: [PATCH] Backport hack-local-variables for find-file-literally with dos * lisp/files.el (hack-local-variables--find-variables): Update hack-local-variables--find-variables to check whether a buffer has find-file-literally set before converting ?\^m to ?\n. * test/lisp/files-tests.el (files-tests-hack-local-literal-dos): Added test for calling hack-local-variables on files with dos encoding opened with find-file-literally. This fixes a bug where hack-local-variables would fail to find a matching suffix for local variables when files with dos line endings were opened literally using find-file-literally. The unless branch is conditioned on buffer-file-coding-system because the find-file-literally local variable is set after the call to set-buffer-major-mode in find-file-noselect-1. If find-file-literally was set before calling set-buffer-major-mode then it could be use directly. --- lisp/files.el | 6 ++++-- test/lisp/files-tests.el | 11 +++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lisp/files.el b/lisp/files.el index 0a00b8b828..86c592e480 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -3742,7 +3742,8 @@ hack-local-variables (forward-line 1) (let ((startpos (point)) endpos - (thisbuf (current-buffer))) + (thisbuf (current-buffer)) + (noconv (eq buffer-file-coding-system 'no-conversion))) (save-excursion (unless (let ((case-fold-search t)) (re-search-forward @@ -3758,7 +3759,8 @@ hack-local-variables (with-temp-buffer (insert-buffer-substring thisbuf startpos endpos) (goto-char (point-min)) - (subst-char-in-region (point) (point-max) ?\^m ?\n) + (unless noconv + (subst-char-in-region (point) (point-max) ?\^m ?\n)) (while (not (eobp)) ;; Discard the prefix. (if (looking-at prefix) diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el index 1fc8007352..d24a4c5a23 100644 --- a/test/lisp/files-tests.el +++ b/test/lisp/files-tests.el @@ -150,6 +150,17 @@ files-tests-local-variables (dolist (subtest (cdr test)) (should (file-test--do-local-variables-test str subtest))))))) +(ert-deftest files-tests-hack-local-literal-dos () + (let ((tempfile (make-temp-file "files-tests-test-hack-local-literal-dos" nil ".el"))) + (unwind-protect + (progn + (with-temp-buffer + (insert ";; -*- mode: Emacs-Lisp -*-\^m\n;; Local Variables:\^m\n;; lol: t\^m\n;; End:\^m\n") + (write-file tempfile)) + (with-current-buffer (find-file-literally tempfile) + (hack-local-variables))) + (delete-file tempfile)))) + (defvar files-test-bug-18141-file (expand-file-name "data/files-bug18141.el.gz" (getenv "EMACS_TEST_DIRECTORY")) "Test file for bug#18141.") -- 2.31.1