all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH] Fix hack-local-variables for find-file-literally with dos encoding
@ 2021-08-15 21:23 Tom Gillespie
  2021-08-15 21:35 ` Tom Gillespie
  2021-08-15 21:52 ` Lars Ingebrigtsen
  0 siblings, 2 replies; 17+ messages in thread
From: Tom Gillespie @ 2021-08-15 21:23 UTC (permalink / raw)
  To: Emacs developers

[-- Attachment #1: Type: text/plain, Size: 261 bytes --]

Hi,
    This patch provides a test and a fix for an edge case when opening
files literally that have dos crlf line endings. The patch was made
against master, but a similar fix could be issued for a potential
Emacs 27.3 release if one is forthcoming. Best!
Tom

[-- Attachment #2: 0001-Fix-hack-local-variables-for-find-file-literally-wit.patch --]
[-- Type: text/x-patch, Size: 2996 bytes --]

From bcbc9af3ddce5e07afc42bddb09e5ff43baf219f Mon Sep 17 00:00:00 2001
From: Tom Gillespie <tgbugs@gmail.com>
Date: Sun, 15 Aug 2021 14:17:42 -0700
Subject: [PATCH] Fix hack-local-variables for find-file-literally with dos
 encoding

* 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 875ac55316..c3518d59b9 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -3931,7 +3931,8 @@ hack-local-variables--find-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
@@ -3947,7 +3948,8 @@ hack-local-variables--find-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 fb24b98595..83659d900e 100644
--- a/test/lisp/files-tests.el
+++ b/test/lisp/files-tests.el
@@ -164,6 +164,17 @@ files-tests-permanent-local-variables
       (hack-local-variables)
       (should (eq lexical-binding nil)))))
 
+(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
   (ert-resource-file "files-bug18141.el.gz")
   "Test file for bug#18141.")
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2021-08-16 18:09 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-08-15 21:23 [PATCH] Fix hack-local-variables for find-file-literally with dos encoding Tom Gillespie
2021-08-15 21:35 ` Tom Gillespie
2021-08-15 21:52 ` Lars Ingebrigtsen
2021-08-15 22:14   ` Lars Ingebrigtsen
2021-08-15 22:31     ` Tom Gillespie
2021-08-15 22:21   ` Tom Gillespie
2021-08-15 22:39     ` Tom Gillespie
2021-08-15 23:09       ` Tom Gillespie
2021-08-16 11:32         ` Eli Zaretskii
2021-08-16 12:01     ` Lars Ingebrigtsen
2021-08-16 12:06       ` Lars Ingebrigtsen
2021-08-16 12:29       ` Eli Zaretskii
2021-08-16 12:53         ` Lars Ingebrigtsen
2021-08-16 13:22           ` Eli Zaretskii
2021-08-16 17:44             ` Tom Gillespie
2021-08-16 17:49               ` Stefan Monnier
2021-08-16 18:09                 ` Eli Zaretskii

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.