From unknown Tue Jun 17 20:16:07 2025 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Mailer: MIME-tools 5.509 (Entity 5.509) Content-Type: text/plain; charset=utf-8 From: bug#35522 <35522@debbugs.gnu.org> To: bug#35522 <35522@debbugs.gnu.org> Subject: Status: [PATCH v2] Let dir locals for more specific modes override those from less Reply-To: bug#35522 <35522@debbugs.gnu.org> Date: Wed, 18 Jun 2025 03:16:07 +0000 retitle 35522 [PATCH v2] Let dir locals for more specific modes override th= ose from less reassign 35522 emacs submitter 35522 Neil Roberts severity 35522 normal tag 35522 fixed patch thanks From debbugs-submit-bounces@debbugs.gnu.org Wed May 01 07:49:57 2019 Received: (at submit) by debbugs.gnu.org; 1 May 2019 11:49:57 +0000 Received: from localhost ([127.0.0.1]:43600 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hLnkK-0000H5-No for submit@debbugs.gnu.org; Wed, 01 May 2019 07:49:57 -0400 Received: from eggs.gnu.org ([209.51.188.92]:39833) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hLnkI-0000Gs-BC for submit@debbugs.gnu.org; Wed, 01 May 2019 07:49:55 -0400 Received: from lists.gnu.org ([209.51.188.17]:48177) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hLnkC-0004n6-UK for submit@debbugs.gnu.org; Wed, 01 May 2019 07:49:48 -0400 Received: from eggs.gnu.org ([209.51.188.92]:54212) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hLnk9-0007u6-Pi for bug-gnu-emacs@gnu.org; Wed, 01 May 2019 07:49:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hLnk6-0004kz-OC for bug-gnu-emacs@gnu.org; Wed, 01 May 2019 07:49:45 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:37683) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hLnk6-0004jw-3X for bug-gnu-emacs@gnu.org; Wed, 01 May 2019 07:49:42 -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=CFl4u+Nbv8fWsuvj2Amk9QhIa1R7RsLEHUrnzSRNZes=; b=l4A/EBU3kCiZRjO2qYH+yqTgX2smloInVp6fxgQ7v9FsCqMlOiOiwSJZ2I53OmOa7dYamH7JNQ1c/er/rFPPikwKiIpIgPZzzYzWSPSbfznZoiXIjPG/B3O7wGRmPLmt6YeiIwDYKeM3M8NOin14s5L4ESzv+XqrY6FDpzNdE9HGW/9whY0w0sEd4h7W460+QpaLdtjM5xqWGKQi5gki9vfZqvPHRraQLrhIiZa23DSJ5m/YkNVbPIgw4BAPct2tqytAWV6HHryR3mLNldsoYQYPkakPwMSAmcovGo4GE7IHRr4bYzlsi0Dftm+Zl0Nz+DH6z91kFbGTHp8NaqljNA==; Received: from lns-bzn-38-82-253-104-2.adsl.proxad.net ([82.253.104.2] helo=localhost.localdomain) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1hLnk2-00052b-ST for ; Wed, 01 May 2019 13:49:39 +0200 From: Neil Roberts To: bug-gnu-emacs@gnu.org Subject: [PATCH v2] Let dir locals for more specific modes override those from less Date: Wed, 1 May 2019 13:49:13 +0200 Message-Id: <20190501114913.513-1-bpeeluk@yahoo.co.uk> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20181115180117.10423-1-bpeeluk@yahoo.co.uk> References: <20181115180117.10423-1-bpeeluk@yahoo.co.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Spam-Score: -1.6 (-) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -2.6 (--) 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 c05d70a00e..e104e49472 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -4108,6 +4108,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 @@ -4145,6 +4191,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 From debbugs-submit-bounces@debbugs.gnu.org Wed May 01 12:46:21 2019 Received: (at control) by debbugs.gnu.org; 1 May 2019 16:46:21 +0000 Received: from localhost ([127.0.0.1]:44739 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hLsNB-0007dH-8X for submit@debbugs.gnu.org; Wed, 01 May 2019 12:46:21 -0400 Received: from eggs.gnu.org ([209.51.188.92]:41348) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hLsN9-0007d2-80 for control@debbugs.gnu.org; Wed, 01 May 2019 12:46:19 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:55939) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hLsN4-0003eX-2a for control@debbugs.gnu.org; Wed, 01 May 2019 12:46:14 -0400 Received: from rgm by fencepost.gnu.org with local (Exim 4.82) (envelope-from ) id 1hLsN3-0002Ya-M0 for control@debbugs.gnu.org; Wed, 01 May 2019 12:46:13 -0400 Subject: control message for bug 35522 To: X-Mailer: mail (GNU Mailutils 2.99.98) Message-Id: From: Glenn Morris Date: Wed, 01 May 2019 12:46:13 -0400 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: control X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) merge 33400 35522 From debbugs-submit-bounces@debbugs.gnu.org Sun May 12 10:03:52 2019 Received: (at control) by debbugs.gnu.org; 12 May 2019 14:03:52 +0000 Received: from localhost ([127.0.0.1]:43011 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hPp4x-0008Rb-UB for submit@debbugs.gnu.org; Sun, 12 May 2019 10:03:52 -0400 Received: from mail-it1-f170.google.com ([209.85.166.170]:35863) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hPp4v-0008RI-Hj; Sun, 12 May 2019 10:03:50 -0400 Received: by mail-it1-f170.google.com with SMTP id e184so4055962ite.1; Sun, 12 May 2019 07:03:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=cGyLgOxduaLjrWBx+h7nGNWADAxzPAlk/5SyanUc+AM=; b=HrENZpmBM3GRTieLRebggNrJkgkojl2c4A0bsifMYaH9qbRf6Aqzsi8Uwsew+MPZH7 oCt7Ulapp0+6RyI/3srQ3zU22ASwwxTpMZDl2rW8ejIlUT7YPs+dvg9K1WD8w5EAliTo eIr2KCIwQVFSAF/8bburg6DHtrDgkPWHCRqvPCOWGtVls8IkU8J0m2nlFaQMcSIuqija G0kNgsRYrflzKj8WiRB+KGSvv2JpqShPxMn/ZW4qmhJZsOCjoFxeSd3KPm2lFa3wX7jG 10su1wDBDMXwO0FF2Y6kVZp/V/66xWOaTpJioI1faOHGAzGa44IeOC5gubldz12wKszB 3iyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=cGyLgOxduaLjrWBx+h7nGNWADAxzPAlk/5SyanUc+AM=; b=LVKVbmKoJ7kwsS/kEV9uCt1ddDGQBxlCykecs26RHAci3iC+OWorbnbJ+H0ux9skdx HqUP0nNXJ9K2HbFJmjQ7Dk8HznKSEdia5FBR2lbL9rmDsZa581/nh29X4jV+k+kdNbDN buJAnyd6ZJcvrK+mRfyN/hH8OPqmdSY3+JTrJ7uNnpNNI4pNkGISdACLyscxzh6DLhFx JSYjrwapWCyPTV4GdqmfHuwVsPZfT4jinnK7spzq/7KTrKMC7QasFoKrQApo+VnaVd5n pUXXZPANtyhJr+U3HPHr3HhtewS5Lc92LJju/JWnSKWtVq9cXO07ON+FxojJHK/dnSx+ x4vA== X-Gm-Message-State: APjAAAVfUZsQcGvjMNtSqJgjptgd3Nkl+ZEx/yRF7yZYWO9PkLwcpa4U CvjbipeIgYriDWCou9wmQPFYiUq3 X-Google-Smtp-Source: APXvYqwBvepk61bNn8HjGusa/ZceIwY+dFSoSjCySrjevilt+FWO8XGR9mupaye6v5v/gQLtsK/gxw== X-Received: by 2002:a24:4ace:: with SMTP id k197mr15381863itb.34.1557669823641; Sun, 12 May 2019 07:03:43 -0700 (PDT) Received: from minid (cbl-45-2-119-34.yyz.frontiernetworks.ca. [45.2.119.34]) by smtp.gmail.com with ESMTPSA id 14sm5287061itl.1.2019.05.12.07.03.42 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 12 May 2019 07:03:42 -0700 (PDT) From: Noam Postavsky To: Neil Roberts Subject: Re: bug#33400: [PATCH v3] Let dir locals for more specific modes override those from less References: <83tve2g660.fsf@gnu.org> <20190510105728.7143-1-bpeeluk@yahoo.co.uk> Date: Sun, 12 May 2019 10:03:42 -0400 In-Reply-To: <20190510105728.7143-1-bpeeluk@yahoo.co.uk> (Neil Roberts's message of "Fri, 10 May 2019 12:57:28 +0200") Message-ID: <877eavixc1.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: control Cc: 33400@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) tags 33400 fixed close 33400 26.3 quit Pushed to emacs-26, with a couple of minor fixes: > * 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 We close the parens at end of line for ChangeLog entries: * 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 > +@example > +((nil . ((fill-column . 40))) > + (c-mode . ((fill-column . 50))) > + (prog-mode . ((fill-column . 60))) > + ("narrow-files" . ((nil . (fill-column 20))))) > +@end example The last line in the example needs to be + ("narrow-files" . ((nil . ((fill-column . 20)))))) 02bee7860f 2019-05-12T09:59:55-04:00 "Let dir locals for more specific modes override those from less" https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=02bee7860f7e650ef13e00fe1a7f9a362e3eb001 From unknown Tue Jun 17 20:16:07 2025 Received: (at fakecontrol) by fakecontrolmessage; To: internal_control@debbugs.gnu.org From: Debbugs Internal Request Subject: Internal Control Message-Id: bug archived. Date: Mon, 10 Jun 2019 11:24:04 +0000 User-Agent: Fakemail v42.6.9 # This is a fake control message. # # The action: # bug archived. thanks # This fakemail brought to you by your local debbugs # administrator