1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
;;; editorconfig-core-handle.el --- Handle Class for EditorConfig File -*- lexical-binding: t -*-
;; Copyright (C) 2011-2025 Free Software Foundation, Inc.
;; Author: EditorConfig Team <editorconfig@googlegroups.com>
;; Package: editorconfig
;; See
;; https://github.com/editorconfig/editorconfig-emacs/graphs/contributors or
;; https://github.com/editorconfig/editorconfig-emacs/blob/master/CONTRIBUTORS
;; for the list of contributors.
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;; See the GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Handle structures for EditorConfig config file. This library is used
;; internally from editorconfig-core.el .
;;; Code:
(require 'cl-lib)
(require 'editorconfig-fnmatch)
(defvar editorconfig-core-handle--cache-hash
(make-hash-table :test 'equal)
"Hash of EditorConfig filename and its `editorconfig-core-handle' instance.")
(cl-defstruct editorconfig-core-handle-section
"Structure representing one section in a .editorconfig file.
Slots:
`name'
String of section name (glob string).
`props'
Alist of properties: (KEY . VALUE)."
(name nil)
(props nil))
(defun editorconfig-core-handle-section-get-properties (section file)
"Return properties alist when SECTION name match FILE.
FILE should be a relative file name, relative to the directory where
the `.editorconfig' file which has SECTION lives.
If not match, return nil."
(when (editorconfig-core-handle--fnmatch-p
file (editorconfig-core-handle-section-name section))
(editorconfig-core-handle-section-props section)))
(cl-defstruct editorconfig-core-handle
"Structure representing an .editorconfig file.
Slots:
`top-props'
Alist of top properties like ((\"root\" . \"true\"))
`sections'
List of `editorconfig-core-handle-section' structure objects.
`mtime'
Last modified time of .editorconfig file.
`path'
Absolute path to .editorconfig file."
(top-props nil)
(sections nil)
(mtime nil)
(path nil))
(defun editorconfig-core-handle (conf)
"Return EditorConfig handle for CONF, which should be a file path.
If CONF does not exist return nil."
(when (file-readable-p conf)
(let ((cached (gethash conf editorconfig-core-handle--cache-hash))
(mtime (nth 5 (file-attributes conf))))
(if (and cached
(equal (editorconfig-core-handle-mtime cached) mtime))
cached
(let ((parsed (editorconfig-core-handle--parse-file conf)))
(puthash conf parsed editorconfig-core-handle--cache-hash))))))
(defun editorconfig-core-handle-root-p (handle)
"Return non-nil if HANDLE represent root EditorConfig file.
If HANDLE is nil return nil."
(when handle
(string-equal "true"
(downcase (or (cdr (assoc "root"
(editorconfig-core-handle-top-props handle)))
"")))))
(defun editorconfig-core-handle-get-properties (handle file)
"Return list of alist of properties from HANDLE for FILE.
The list returned will be ordered by the lines they appear.
If HANDLE is nil return nil."
(declare (obsolete editorconfig-core-handle-get-properties-hash "0.8.0"))
(when handle
(let* ((dir (file-name-directory (editorconfig-core-handle-path handle)))
(file (file-relative-name file dir)))
(cl-loop for section in (editorconfig-core-handle-sections handle)
for props = (editorconfig-core-handle-section-get-properties
section file)
when props collect (copy-alist props)))))
(defun editorconfig-core-handle-get-properties-hash (handle file)
"Return hash of properties from HANDLE for FILE.
If HANDLE is nil return nil."
(when handle
(let* ((hash (make-hash-table))
(dir (file-name-directory (editorconfig-core-handle-path handle)))
(file (file-relative-name file dir)))
(dolist (section (editorconfig-core-handle-sections handle))
(cl-loop for (key . value) in (editorconfig-core-handle-section-get-properties section file)
do (puthash (intern key) value hash)))
hash)))
(defun editorconfig-core-handle--fnmatch-p (name pattern)
"Return non-nil if NAME match PATTERN.
If pattern has slash, pattern should be relative to DIR.
This function is a fnmatch with a few modification for EditorConfig usage."
(if (string-match-p "/" pattern)
(let ((pattern (replace-regexp-in-string "\\`/" "" pattern)))
(editorconfig-fnmatch-p name pattern))
;; The match is not "anchored" so it can be either in the current dir or
;; in a subdir. Contrary to Zsh patterns, editorconfig's `**/foo' does
;; not match `foo', so we need to split the problem into two matches.
(or (editorconfig-fnmatch-p name pattern)
(editorconfig-fnmatch-p name (concat "**/" pattern)))))
(defun editorconfig-core-handle--parse-file (conf)
"Parse EditorConfig file CONF.
This function returns a `editorconfig-core-handle'.
If CONF is not found return nil."
(when (file-readable-p conf)
(with-temp-buffer
;; NOTE: Use this instead of insert-file-contents-literally to enable
;; code conversion
(insert-file-contents conf)
(goto-char (point-min))
(let ((sections ())
(top-props nil)
;; nil when pattern not appeared yet, "" when pattern is empty ("[]")
(pattern nil)
;; Alist of properties for current PATTERN
(props ())
;; Current line num
(current-line-number 1))
(while (not (eobp))
(skip-chars-forward " \t\f")
(cond
((looking-at "\\(?:[#;].*\\)?$")
nil)
;; Start of section
((looking-at "\\[\\(.*\\)\\][ \t]*$")
(let ((newpattern (match-string 1)))
(when pattern
(push (make-editorconfig-core-handle-section
:name pattern
:props (nreverse props))
sections))
(setq props nil)
(setq pattern newpattern)))
((looking-at "\\([^=: \t]+\\)[ \t]*[=:][ \t]*\\(.*?\\)[ \t]*$")
(let ((key (downcase (match-string 1)))
(value (match-string 2)))
(when (and (< (length key) 51)
(< (length value) 256))
(if pattern
(when (< (length pattern) 4097) ;;FIXME: 4097?
(push `(,key . ,value)
props))
(push `(,key . ,value)
top-props)))))
(t (error "Error while reading config file: %s:%d:\n %s\n"
conf current-line-number
(buffer-substring-no-properties (line-beginning-position)
(line-end-position)))))
(setq current-line-number (1+ current-line-number))
(goto-char (point-min))
(forward-line (1- current-line-number)))
(when pattern
(push (make-editorconfig-core-handle-section
:name pattern
:props (nreverse props))
sections))
(make-editorconfig-core-handle
:top-props (nreverse top-props)
:sections (nreverse sections)
:mtime (nth 5 (file-attributes conf))
:path conf)))))
(provide 'editorconfig-core-handle)
;;; editorconfig-core-handle.el ends here
|