summaryrefslogtreecommitdiff
path: root/lisp/files.el
diff options
context:
space:
mode:
authorAntero Mejr <antero@mailbox.org>2023-05-09 20:51:14 +0000
committerEli Zaretskii <eliz@gnu.org>2023-05-11 16:33:16 +0300
commitf24ea84f430d5ae6fd3fa5373610551f62ce25be (patch)
tree47901040117df1160679509917493bf6825b9dc7 /lisp/files.el
parent44c2befb4a36391f17ee8f3539dee7be62df89f0 (diff)
downloademacs-f24ea84f430d5ae6fd3fa5373610551f62ce25be.tar.gz
Add safe-local-variable-directories variable.
This variable can be set to automatically load risky dir-local variables from a list of trusted directories. * lisp/emacs-lisp/files.el (safe-local-variable-directories, hack-local-variables-filter, hack-local-variables-confirm): New variable and associated logic. * test/lisp/files-tests.el (files-tests-safe-local-variable-directories): Add tests for same. * doc/emacs/custom.texi (Safe File Variables): Add documentation for same. * doc/lispref/variables.texi (File Local Variables): Add documentation for same. * etc/NEWS (Lisp Changes in Emacs 30.1): Add news entry for same. (Bug#61901)
Diffstat (limited to 'lisp/files.el')
-rw-r--r--lisp/files.el34
1 files changed, 29 insertions, 5 deletions
diff --git a/lisp/files.el b/lisp/files.el
index c6f53e5eaf8..aa01e638c98 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -681,7 +681,8 @@ The command \\[normal-mode], when used interactively,
always obeys file local variable specifications and the -*- line,
and ignores this variable.
-Also see the `permanently-enabled-local-variables' variable."
+Also see the `permanently-enabled-local-variables' and
+`safe-local-variable-directories' variables."
:risky t
:type '(choice (const :tag "Query Unsafe" t)
(const :tag "Safe Only" :safe)
@@ -3696,6 +3697,15 @@ variable to set.")
"A list of file-local variables that are always enabled.
This overrides any `enable-local-variables' setting.")
+(defcustom safe-local-variable-directories '()
+ "A list of directories where local variables are always enabled.
+Directory-local variables loaded from these directories, such as the
+variables in .dir-locals.el, will be enabled even if they are risky."
+ :version "30.1"
+ :type '(repeat string)
+ :risky t
+ :group 'find-file)
+
(defun hack-local-variables-confirm (all-vars unsafe-vars risky-vars dir-name)
"Get confirmation before setting up local variable values.
ALL-VARS is the list of all variables to be set up.
@@ -3734,7 +3744,11 @@ n -- to ignore the local variables list.")
! -- to apply the local variables list, and permanently mark these
values (*) as safe (in the future, they will be set automatically.)
i -- to ignore the local variables list, and permanently mark these
- values (*) as ignored\n\n")
+ values (*) as ignored"
+ (if dir-name "
++ -- to apply the local variables list, and trust all directory-local
+ variables in this directory\n\n"
+ "\n\n"))
(insert "\n\n"))
(dolist (elt all-vars)
(cond ((member elt unsafe-vars)
@@ -3758,7 +3772,11 @@ i -- to ignore the local variables list, and permanently mark these
(pop-to-buffer buf '(display-buffer--maybe-at-bottom))
(let* ((exit-chars '(?y ?n ?\s))
(prompt (format "Please type %s%s: "
- (if offer-save "y, n, ! or i" "y or n")
+ (if offer-save
+ (if dir-name
+ "y, n, !, i, +"
+ "y, n, !, i")
+ "y or n")
(if (< (line-number-at-pos (point-max))
(window-body-height))
""
@@ -3766,8 +3784,13 @@ i -- to ignore the local variables list, and permanently mark these
char)
(when offer-save
(push ?i exit-chars)
- (push ?! exit-chars))
+ (push ?! exit-chars)
+ (when dir-name
+ (push ?+ exit-chars)))
(setq char (read-char-choice prompt exit-chars))
+ (when (and offer-save dir-name (= char ?+))
+ (customize-push-and-save 'safe-local-variable-directories
+ (list dir-name)))
(when (and offer-save
(or (= char ?!) (= char ?i))
unsafe-vars)
@@ -3776,7 +3799,7 @@ i -- to ignore the local variables list, and permanently mark these
'safe-local-variable-values
'ignored-local-variable-values)
unsafe-vars))
- (prog1 (memq char '(?! ?\s ?y))
+ (prog1 (memq char '(?! ?\s ?y ?+))
(quit-window t)))))))
(defconst hack-local-variable-regexp
@@ -3908,6 +3931,7 @@ DIR-NAME is the name of the associated directory. Otherwise it is nil."
(null unsafe-vars)
(null risky-vars))
(memq enable-local-variables '(:all :safe))
+ (member dir-name safe-local-variable-directories)
(hack-local-variables-confirm all-vars unsafe-vars
risky-vars dir-name))
(dolist (elt all-vars)