aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Riefenstahl2019-07-14 17:09:39 +0200
committerEli Zaretskii2019-07-20 12:27:19 +0300
commite6bfc6753cac7d8eaf67ff3adea0087eb19e7f6e (patch)
treee74eb6aada34a6aceb6d6e7284667f2fd18f9915
parent070dd439096c0f72d8f73823649e3c650f31c890 (diff)
downloademacs-e6bfc6753cac7d8eaf67ff3adea0087eb19e7f6e.tar.gz
emacs-e6bfc6753cac7d8eaf67ff3adea0087eb19e7f6e.zip
Make REs in magic-(fallback-)mode-alist case-sensitive.
These variables are used for well-defined file formats where relaxed case matching is not wanted usually. * lisp/files.el (magic-mode-alist, magic-fallback-mode-alist): Update the doc string. (set-auto-mode): Make looking-at for elements of magic-mode-alist and magic-fallback-mode-alist use case-fold-search == nil. * lisp/files.el (files-test-magic-mode-alist-re-baseline) (files-test-magic-mode-alist-re-no-match) (files-test-magic-mode-alist-re-case-diff): Add.
-rw-r--r--lisp/files.el18
-rw-r--r--test/lisp/files-tests.el27
2 files changed, 37 insertions, 8 deletions
diff --git a/lisp/files.el b/lisp/files.el
index e26b4820f5e..34fdc3031a9 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -2964,9 +2964,9 @@ associated with that interpreter in `interpreter-mode-alist'.")
2964 "Alist of buffer beginnings vs. corresponding major mode functions. 2964 "Alist of buffer beginnings vs. corresponding major mode functions.
2965Each element looks like (REGEXP . FUNCTION) or (MATCH-FUNCTION . FUNCTION). 2965Each element looks like (REGEXP . FUNCTION) or (MATCH-FUNCTION . FUNCTION).
2966After visiting a file, if REGEXP matches the text at the beginning of the 2966After visiting a file, if REGEXP matches the text at the beginning of the
2967buffer, or calling MATCH-FUNCTION returns non-nil, `normal-mode' will 2967buffer (respecting case), or calling MATCH-FUNCTION returns non-nil,
2968call FUNCTION rather than allowing `auto-mode-alist' to decide the buffer's 2968`normal-mode' will call FUNCTION rather than allowing `auto-mode-alist' to
2969major mode. 2969decide the buffer's major mode.
2970 2970
2971If FUNCTION is nil, then it is not called. (That is a way of saying 2971If FUNCTION is nil, then it is not called. (That is a way of saying
2972\"allow `auto-mode-alist' to decide for these files.\")") 2972\"allow `auto-mode-alist' to decide for these files.\")")
@@ -2998,9 +2998,9 @@ If FUNCTION is nil, then it is not called. (That is a way of saying
2998 "Like `magic-mode-alist' but has lower priority than `auto-mode-alist'. 2998 "Like `magic-mode-alist' but has lower priority than `auto-mode-alist'.
2999Each element looks like (REGEXP . FUNCTION) or (MATCH-FUNCTION . FUNCTION). 2999Each element looks like (REGEXP . FUNCTION) or (MATCH-FUNCTION . FUNCTION).
3000After visiting a file, if REGEXP matches the text at the beginning of the 3000After visiting a file, if REGEXP matches the text at the beginning of the
3001buffer, or calling MATCH-FUNCTION returns non-nil, `normal-mode' will 3001buffer (respecting case), or calling MATCH-FUNCTION returns non-nil,
3002call FUNCTION, provided that `magic-mode-alist' and `auto-mode-alist' 3002`normal-mode' will call FUNCTION, provided that `magic-mode-alist' and
3003have not specified a mode for this file. 3003`auto-mode-alist' have not specified a mode for this file.
3004 3004
3005If FUNCTION is nil, then it is not called.") 3005If FUNCTION is nil, then it is not called.")
3006(put 'magic-fallback-mode-alist 'risky-local-variable t) 3006(put 'magic-fallback-mode-alist 'risky-local-variable t)
@@ -3117,7 +3117,8 @@ we don't actually set it to the same mode the buffer already has."
3117 ((functionp re) 3117 ((functionp re)
3118 (funcall re)) 3118 (funcall re))
3119 ((stringp re) 3119 ((stringp re)
3120 (looking-at re)) 3120 (let ((case-fold-search nil))
3121 (looking-at re)))
3121 (t 3122 (t
3122 (error 3123 (error
3123 "Problem in magic-mode-alist with element %s" 3124 "Problem in magic-mode-alist with element %s"
@@ -3178,7 +3179,8 @@ we don't actually set it to the same mode the buffer already has."
3178 ((functionp re) 3179 ((functionp re)
3179 (funcall re)) 3180 (funcall re))
3180 ((stringp re) 3181 ((stringp re)
3181 (looking-at re)) 3182 (let ((case-fold-search nil))
3183 (looking-at re)))
3182 (t 3184 (t
3183 (error 3185 (error
3184 "Problem with magic-fallback-mode-alist element: %s" 3186 "Problem with magic-fallback-mode-alist element: %s"
diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el
index aa5dbe7acf9..df2c3f47ae0 100644
--- a/test/lisp/files-tests.el
+++ b/test/lisp/files-tests.el
@@ -1282,5 +1282,32 @@ renaming only, rather than modified in-place."
1282 (should (equal (file-size-human-readable 10000 'si " " "bit") "10 kbit")) 1282 (should (equal (file-size-human-readable 10000 'si " " "bit") "10 kbit"))
1283 (should (equal (file-size-human-readable 10000 'iec " " "bit") "9.8 Kibit"))) 1283 (should (equal (file-size-human-readable 10000 'iec " " "bit") "9.8 Kibit")))
1284 1284
1285(ert-deftest files-test-magic-mode-alist-re-baseline ()
1286 "Test magic-mode-alist with RE, expected behaviour for match."
1287 (let ((magic-mode-alist '(("my-tag" . text-mode))))
1288 (with-temp-buffer
1289 (insert "my-tag")
1290 (normal-mode)
1291 (should (eq major-mode 'text-mode)))))
1292
1293(ert-deftest files-test-magic-mode-alist-re-no-match ()
1294 "Test magic-mode-alist with RE, expected behaviour for no match."
1295 (let ((magic-mode-alist '(("my-tag" . text-mode))))
1296 (with-temp-buffer
1297 (insert "not-my-tag")
1298 (normal-mode)
1299 (should (not (eq major-mode 'text-mode))))))
1300
1301(ert-deftest files-test-magic-mode-alist-re-case-diff ()
1302 "Test that regexps in magic-mode-alist are case-sensitive.
1303See <https://debbugs.gnu.org/36401>."
1304 (let ((case-fold-search t)
1305 (magic-mode-alist '(("my-tag" . text-mode))))
1306 (with-temp-buffer
1307 (goto-char (point-min))
1308 (insert "My-Tag")
1309 (normal-mode)
1310 (should (not (eq major-mode 'text-mode))))))
1311
1285(provide 'files-tests) 1312(provide 'files-tests)
1286;;; files-tests.el ends here 1313;;; files-tests.el ends here