From a3a16b319faa413a2b8767fb155c014d2be83a0d Mon Sep 17 00:00:00 2001 From: 10sr <8.slashes@gmail.com> Date: Mon, 7 Oct 2019 22:34:03 +0900 Subject: [PATCH 1/3] Disallow setting mode when it is not listed in whitelist --- editorconfig.el | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/editorconfig.el b/editorconfig.el index 6b966223..17bca3ef 100644 --- a/editorconfig.el +++ b/editorconfig.el @@ -488,6 +488,10 @@ to non-nil when FINAL-NEWLINE is true." (> (string-to-number length) 0)) (setq fill-column (string-to-number length)))) +(defvar editorconfig-file-type-emacs-whitelist + '() + "List of known `major-mode' that can be used for file_type_emacs value.") + (defun editorconfig-set-major-mode-from-name (filetype) "Set buffer `major-mode' by FILETYPE. @@ -499,7 +503,12 @@ FILETYPE should be s string like `\"ini\"`, if not nil or empty string." "-mode"))))) (when mode (if (fboundp mode) - (editorconfig-apply-major-mode-safely mode) + (if (apply 'provided-mode-derived-p mode + editorconfig-file-type-emacs-whitelist) + (editorconfig-apply-major-mode-safely mode) + (display-warning :error (format "Major-mode `%S' is not listed in `%S'" + mode + 'editorconfig-file-type-emacs-whitelist))) (display-warning :error (format "Major-mode `%S' not found" mode)) nil)))) From c9de2a46979102e3cc21d2d63619b088bacd2a5c Mon Sep 17 00:00:00 2001 From: 10sr <8.slashes@gmail.com> Date: Tue, 8 Oct 2019 13:40:49 +0900 Subject: [PATCH 2/3] Add editorconfig-file-type-emacs-whitelist value --- editorconfig.el | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/editorconfig.el b/editorconfig.el index 17bca3ef..64ab20e2 100644 --- a/editorconfig.el +++ b/editorconfig.el @@ -489,7 +489,9 @@ to non-nil when FINAL-NEWLINE is true." (setq fill-column (string-to-number length)))) (defvar editorconfig-file-type-emacs-whitelist - '() + (append (mapcar 'car + editorconfig-indentation-alist) + '(conf-mode)) "List of known `major-mode' that can be used for file_type_emacs value.") (defun editorconfig-set-major-mode-from-name (filetype) From aefcc0be4189c500570043569c142b096d9eef9d Mon Sep 17 00:00:00 2001 From: 10sr <8.slashes@gmail.com> Date: Tue, 8 Oct 2019 13:55:05 +0900 Subject: [PATCH 3/3] Fix support for Emacs<26 --- editorconfig.el | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/editorconfig.el b/editorconfig.el index 64ab20e2..b38a3aa8 100644 --- a/editorconfig.el +++ b/editorconfig.el @@ -494,6 +494,18 @@ to non-nil when FINAL-NEWLINE is true." '(conf-mode)) "List of known `major-mode' that can be used for file_type_emacs value.") +;; Emacs<26 does not have provided-mode-derived-p +(defun editorconfig--provided-mode-derived-p (mode &rest modes) + "Non-nil if MODE is derived from one of MODES. +Uses the `derived-mode-parent' property of the symbol to trace backwards. +If you just want to check `major-mode', use `derived-mode-p'." + (if (fboundp 'provided-mode-derived-p) + (apply 'provided-mode-derived-p mode modes) + (while (and (not (memq mode modes)) + (setq mode (get mode 'derived-mode-parent)))) + mode)) + + (defun editorconfig-set-major-mode-from-name (filetype) "Set buffer `major-mode' by FILETYPE. @@ -505,7 +517,7 @@ FILETYPE should be s string like `\"ini\"`, if not nil or empty string." "-mode"))))) (when mode (if (fboundp mode) - (if (apply 'provided-mode-derived-p mode + (if (apply 'editorconfig--provided-mode-derived-p mode editorconfig-file-type-emacs-whitelist) (editorconfig-apply-major-mode-safely mode) (display-warning :error (format "Major-mode `%S' is not listed in `%S'"