diff options
| -rw-r--r-- | doc/emacs/custom.texi | 22 | ||||
| -rw-r--r-- | lisp/files.el | 47 |
2 files changed, 69 insertions, 0 deletions
diff --git a/doc/emacs/custom.texi b/doc/emacs/custom.texi index b07362f3cea..3fd655048b4 100644 --- a/doc/emacs/custom.texi +++ b/doc/emacs/custom.texi | |||
| @@ -1375,6 +1375,28 @@ be applied in the current directory, not in any subdirectories. | |||
| 1375 | Finally, it specifies a different @file{ChangeLog} file name for any | 1375 | Finally, it specifies a different @file{ChangeLog} file name for any |
| 1376 | file in the @file{src/imported} subdirectory. | 1376 | file in the @file{src/imported} subdirectory. |
| 1377 | 1377 | ||
| 1378 | If the @file{.dir-locals.el} file contains multiple different values | ||
| 1379 | for a variable using different mode names or directories, the values | ||
| 1380 | will be applied in an order such that the values for more specific | ||
| 1381 | modes take priority over more generic modes. Values specified under a | ||
| 1382 | directory have even more priority. For example: | ||
| 1383 | |||
| 1384 | @example | ||
| 1385 | ((nil . ((fill-column . 40))) | ||
| 1386 | (c-mode . ((fill-column . 50))) | ||
| 1387 | (prog-mode . ((fill-column . 60))) | ||
| 1388 | ("narrow-files" . ((nil . ((fill-column . 20)))))) | ||
| 1389 | @end example | ||
| 1390 | |||
| 1391 | Files that use @code{c-mode} also match @code{prog-mode} because the | ||
| 1392 | former inherits from the latter. The value used for | ||
| 1393 | @code{fill-column} in C files will however be @code{50} because the | ||
| 1394 | mode name is more specific than @code{prog-mode}. Files using other | ||
| 1395 | modes inheriting from @code{prog-mode} will use @code{60}. Any file | ||
| 1396 | under the directory @file{narrow-files} will use the value @code{20} | ||
| 1397 | even if they use @code{c-mode} because directory entries have priority | ||
| 1398 | over mode entries. | ||
| 1399 | |||
| 1378 | You can specify the variables @code{mode}, @code{eval}, and | 1400 | You can specify the variables @code{mode}, @code{eval}, and |
| 1379 | @code{unibyte} in your @file{.dir-locals.el}, and they have the same | 1401 | @code{unibyte} in your @file{.dir-locals.el}, and they have the same |
| 1380 | meanings as they would have in file local variables. @code{coding} | 1402 | meanings as they would have in file local variables. @code{coding} |
diff --git a/lisp/files.el b/lisp/files.el index d7ed2487862..f3b502095dd 100644 --- a/lisp/files.el +++ b/lisp/files.el | |||
| @@ -4026,6 +4026,52 @@ This function returns either: | |||
| 4026 | ;; No cache entry. | 4026 | ;; No cache entry. |
| 4027 | locals-dir))) | 4027 | locals-dir))) |
| 4028 | 4028 | ||
| 4029 | (defun dir-locals--get-sort-score (node) | ||
| 4030 | "Return a number used for sorting the definitions of dir locals. | ||
| 4031 | NODE is assumed to be a cons cell where the car is either a | ||
| 4032 | string or a symbol representing a mode name. | ||
| 4033 | |||
| 4034 | If it is a mode then the the depth of the mode (ie, how many | ||
| 4035 | parents that mode has) will be returned. | ||
| 4036 | |||
| 4037 | If it is a string then the length of the string plus 1000 will be | ||
| 4038 | returned. | ||
| 4039 | |||
| 4040 | Otherwise it returns -1. | ||
| 4041 | |||
| 4042 | That way the value can be used to sort the list such that deeper | ||
| 4043 | modes will be after the other modes. This will be followed by | ||
| 4044 | directory entries in order of length. If the entries are all | ||
| 4045 | applied in order then that means the more specific modes will | ||
| 4046 | override the values specified by the earlier modes and directory | ||
| 4047 | variables will override modes." | ||
| 4048 | (let ((key (car node))) | ||
| 4049 | (cond ((null key) -1) | ||
| 4050 | ((symbolp key) | ||
| 4051 | (let ((mode key) | ||
| 4052 | (depth 0)) | ||
| 4053 | (while (setq mode (get mode 'derived-mode-parent)) | ||
| 4054 | (setq depth (1+ depth))) | ||
| 4055 | depth)) | ||
| 4056 | ((stringp key) | ||
| 4057 | (+ 1000 (length key))) | ||
| 4058 | (t -2)))) | ||
| 4059 | |||
| 4060 | (defun dir-locals--sort-variables (variables) | ||
| 4061 | "Sorts VARIABLES so that applying them in order has the right effect. | ||
| 4062 | The variables are compared by dir-locals--get-sort-score. | ||
| 4063 | Directory entries are then recursively sorted using the same | ||
| 4064 | criteria." | ||
| 4065 | (setq variables (sort variables | ||
| 4066 | (lambda (a b) | ||
| 4067 | (< (dir-locals--get-sort-score a) | ||
| 4068 | (dir-locals--get-sort-score b))))) | ||
| 4069 | (dolist (n variables) | ||
| 4070 | (when (stringp (car n)) | ||
| 4071 | (setcdr n (dir-locals--sort-variables (cdr n))))) | ||
| 4072 | |||
| 4073 | variables) | ||
| 4074 | |||
| 4029 | (defun dir-locals-read-from-dir (dir) | 4075 | (defun dir-locals-read-from-dir (dir) |
| 4030 | "Load all variables files in DIR and register a new class and instance. | 4076 | "Load all variables files in DIR and register a new class and instance. |
| 4031 | DIR is the absolute name of a directory which must contain at | 4077 | DIR is the absolute name of a directory which must contain at |
| @@ -4054,6 +4100,7 @@ Return the new class name, which is a symbol named DIR." | |||
| 4054 | (read (current-buffer)))) | 4100 | (read (current-buffer)))) |
| 4055 | (end-of-file nil)))) | 4101 | (end-of-file nil)))) |
| 4056 | (setq success latest)) | 4102 | (setq success latest)) |
| 4103 | (setq variables (dir-locals--sort-variables variables)) | ||
| 4057 | (dir-locals-set-class-variables class-name variables) | 4104 | (dir-locals-set-class-variables class-name variables) |
| 4058 | (dir-locals-set-directory-class dir class-name success) | 4105 | (dir-locals-set-directory-class dir class-name success) |
| 4059 | class-name)) | 4106 | class-name)) |