From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Neil Roberts Newsgroups: gmane.emacs.bugs Subject: bug#33400: [PATCH v3] Let dir locals for more specific modes override those from less Date: Fri, 10 May 2019 12:57:28 +0200 Message-ID: <20190510105728.7143-1-bpeeluk@yahoo.co.uk> References: <83tve2g660.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="238926"; mail-complaints-to="usenet@blaine.gmane.org" To: 33400@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Fri May 10 13:08:55 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hP3OZ-000zws-3w for geb-bug-gnu-emacs@m.gmane.org; Fri, 10 May 2019 13:08:55 +0200 Original-Received: from localhost ([127.0.0.1]:41261 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hP3OY-00024E-00 for geb-bug-gnu-emacs@m.gmane.org; Fri, 10 May 2019 07:08:54 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:33993) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hP3G0-00034l-4i for bug-gnu-emacs@gnu.org; Fri, 10 May 2019 07:00:05 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hP3Fy-00050a-Pi for bug-gnu-emacs@gnu.org; Fri, 10 May 2019 07:00:04 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:52005) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hP3Fy-0004zY-LT for bug-gnu-emacs@gnu.org; Fri, 10 May 2019 07:00:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1hP3Fy-0000QC-Fs for bug-gnu-emacs@gnu.org; Fri, 10 May 2019 07:00:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Neil Roberts Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 10 May 2019 11:00:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 33400 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 33400-submit@debbugs.gnu.org id=B33400.15574859761564 (code B ref 33400); Fri, 10 May 2019 11:00:02 +0000 Original-Received: (at 33400) by debbugs.gnu.org; 10 May 2019 10:59:36 +0000 Original-Received: from localhost ([127.0.0.1]:37315 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hP3FX-0000P9-HA for submit@debbugs.gnu.org; Fri, 10 May 2019 06:59:35 -0400 Original-Received: from fanzine.igalia.com ([91.117.99.155]:46163) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hP3FS-0000Ol-4r for 33400@debbugs.gnu.org; Fri, 10 May 2019 06:59:33 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:To:From; bh=XU7BzNknPboAOdapUu66K379XEbWakooYjjt1Ozads4=; b=sccy0WB//RuOFhQ7GG+3lQIamdtL06LWC3ApQNS9j0LlBwkvI1EdC0qlyc4zh/bgany2FP6N7wci/FrTpColn66gdjSFjCo+jYFdB5THoxWnu2mnkRO5ahoNejFgtO75HjZ+lStmlMkRSeoEqCu+B9ruCLYKs6YaTfRjYVbBh+xaSIkbevNwpwo3ng+OPREbSfeyLmh6qX5hdJXQKxo15ZiSUqwjMTChKE36LHYA2Uifk8bWmtP8o26DhTmG+nBhE5zSzLIWYFQ8cfMepw1O6BtzG+5SBRLtDysxMdRCzDV229b4QH8/Cumt2Nl3DrOKMM6gXV8n3wzB/7eMIbDInA==; Original-Received: from [37.173.106.249] (helo=localhost.localdomain) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1hP3FG-0004ec-UR for <33400@debbugs.gnu.org>; Fri, 10 May 2019 12:59:21 +0200 X-Mailer: git-send-email 2.17.2 In-Reply-To: <83tve2g660.fsf@gnu.org> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:159035 Archived-At: 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. Bug#33400 * lisp/files.el (dir-locals-get-sort-score, dir-locals-sort-variables, dir-locals-read-from-dir): Sort the dir locals so that more precise modes and directory-specific entries have override lesser ones. * doc/emacs/custom.texi (Directory Variables): Document the priority. --- doc/emacs/custom.texi | 22 ++++++++++++++++++++ lisp/files.el | 47 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/doc/emacs/custom.texi b/doc/emacs/custom.texi index 22e352ef9f..3a85907b45 100644 --- a/doc/emacs/custom.texi +++ b/doc/emacs/custom.texi @@ -1377,6 +1377,28 @@ Directory Variables Finally, it specifies a different @file{ChangeLog} file name for any file in the @file{src/imported} subdirectory. +If the @file{.dir-locals.el} file contains multiple different values +for a variable using different mode names or directories, the values +will be applied in an order such that the values for more specific +modes take priority over more generic modes. Values specified under a +directory have even more priority. For example: + +@example +((nil . ((fill-column . 40))) + (c-mode . ((fill-column . 50))) + (prog-mode . ((fill-column . 60))) + ("narrow-files" . ((nil . (fill-column 20))))) +@end example + +Files that use @code{c-mode} also match @code{prog-mode} because the +former inherits from the latter. The value used for +@code{fill-column} in C files will however be @code{50} because the +mode name is more specific than @code{prog-mode}. Files using other +modes inheriting from @code{prog-mode} will use @code{60}. Any file +under the directory @file{narrow-files} will use the value @code{20} +even if they use @code{c-mode} because directory entries have priority +over mode entries. + You can specify the variables @code{mode}, @code{eval}, and @code{unibyte} in your @file{.dir-locals.el}, and they have the same meanings as they would have in file local variables. @code{coding} diff --git a/lisp/files.el b/lisp/files.el index 8477c227bc..ae52fff730 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -4110,6 +4110,52 @@ 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 +returned. + +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 @@ -4147,6 +4193,7 @@ dir-locals-read-from-dir variables newvars)))))) (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) class-name)) -- 2.17.2