[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#33400: [PATCH] Let dir locals for more specific modes override those

From: Neil Roberts
Subject: bug#33400: [PATCH] Let dir locals for more specific modes override those from less
Date: Thu, 15 Nov 2018 19:01:17 +0100

The list of dir local variables to apply is now sorted by the number
of parent modes of the mode used as the key in the association
list. That way when the variables are applied in order the variables
from more specific modes will override those from less specific modes.

If there are directory entries in the list then they are sorted in
order of name length. The list of modes for that dir is then
recursively sorted with the same mechanism. That way variables tied to
a particular subdirectory override those in in a parent directory.

Previously the behaviour didn’t seem to be well defined anyway and was
dependent on the order they appeared in the file. However this order
was changed in version 26.1 and it probably also depended on the
number of dir-local files that are merged.

 lisp/files.el | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/lisp/files.el b/lisp/files.el
index dbac6f614f..09bee6a5f9 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -4093,6 +4093,51 @@ dir-locals-find-file
 (declare-function map-merge-with "map" (type function &rest maps))
 (declare-function map-merge "map" (type &rest maps))
+(defun dir-locals-get-sort-score (node)
+  "Return a number used for sorting the definitions of dir locals.
+NODE is assumed to be a cons cell where the car is either a
+string or a symbol representing a mode name.
+If it is a mode then the the depth of the mode (ie, how many
+parents that mode has) will be returned.
+If it is a string then the length of the string plus 1000 will be
+Otherwise it returns -1.
+That way the value can be used to sort the list such that deeper
+modes will be after the other modes. This will be followed by
+directory entries in order of length. If the entries are all
+applied in order then that means the more specific modes will
+override the values specified by the earlier modes and directory
+variables will override modes."
+  (let ((key (car node)))
+    (cond ((null key) -1)
+          ((symbolp key)
+           (let ((mode key)
+                 (depth 0))
+             (while (setq mode (get mode 'derived-mode-parent))
+               (setq depth (1+ depth)))
+             depth))
+          ((stringp key)
+           (+ 1000 (length key)))
+          (t -2))))
+(defun dir-locals-sort-variables (variables)
+  "Sorts VARIABLES so that applying them in order has the right effect.
+The variables are compared by dir-locals-get-sort-score. Directory entries
+are then recursively sorted using the same criteria."
+  (setq variables (sort variables
+                        (lambda (a b)
+                          (< (dir-locals-get-sort-score a)
+                             (dir-locals-get-sort-score b)))))
+  (dolist (n variables)
+    (when (stringp (car n))
+      (setcdr n (dir-locals-sort-variables (cdr n)))))
+  variables)
 (defun dir-locals-read-from-dir (dir)
   "Load all variables files in DIR and register a new class and instance.
 DIR is the absolute name of a directory which must contain at
@@ -4130,6 +4175,7 @@ dir-locals-read-from-dir
       (setq success latest))
+    (setq variables (dir-locals-sort-variables variables))
     (dir-locals-set-class-variables class-name variables)
     (dir-locals-set-directory-class dir class-name success)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]