editorconfig-core.el (7187B)
1 ;;; editorconfig-core.el --- EditorConfig Core library in Emacs Lisp -*- lexical-binding: t -*- 2 3 ;; Copyright (C) 2011-2023 EditorConfig Team 4 5 ;; Author: EditorConfig Team <editorconfig@googlegroups.com> 6 7 ;; See 8 ;; https://github.com/editorconfig/editorconfig-emacs/graphs/contributors 9 ;; or the CONTRIBUTORS file for the list of contributors. 10 11 ;; This file is part of EditorConfig Emacs Plugin. 12 13 ;; EditorConfig Emacs Plugin is free software: you can redistribute it and/or 14 ;; modify it under the terms of the GNU General Public License as published by 15 ;; the Free Software Foundation, either version 3 of the License, or (at your 16 ;; option) any later version. 17 18 ;; EditorConfig Emacs Plugin is distributed in the hope that it will be useful, 19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 21 ;; Public License for more details. 22 23 ;; You should have received a copy of the GNU General Public License along with 24 ;; EditorConfig Emacs Plugin. If not, see <https://www.gnu.org/licenses/>. 25 26 ;;; Commentary: 27 28 ;; This library is one implementation of EditorConfig Core, which parses 29 ;; .editorconfig files and returns properties for given files. 30 ;; This can be used in place of, for example, editorconfig-core-c. 31 32 33 ;; Use from EditorConfig Emacs Plugin 34 35 ;; Emacs plugin (v0.5 or later) can utilize this implementation. 36 ;; By default, the plugin first search for any EditorConfig executable, 37 ;; and fallback to this library if not found. 38 ;; If you always want to use this library, add following lines to your init.el: 39 40 ;; (setq editorconfig-get-properties-function 41 ;; 'editorconfig-core-get-properties-hash) 42 43 44 ;; Functions 45 46 ;; editorconfig-core-get-properties (&optional file confname confversion) 47 48 ;; Get EditorConfig properties for FILE. 49 50 ;; If FILE is not given, use currently visiting file. 51 ;; Give CONFNAME for basename of config file other than .editorconfig. 52 ;; If need to specify config format version, give CONFVERSION. 53 54 ;; This functions returns alist of properties. Each element will look like 55 ;; (KEY . VALUE) . 56 57 58 ;; editorconfig-core-get-properties-hash (&optional file confname confversion) 59 60 ;; Get EditorConfig properties for FILE. 61 62 ;; This function is almost same as `editorconfig-core-get-properties', but 63 ;; returns hash object instead. 64 65 ;;; Code: 66 67 (require 'cl-lib) 68 69 (require 'editorconfig-core-handle) 70 71 72 (defun editorconfig-core--get-handles (dir confname &optional result) 73 "Get list of EditorConfig handlers for DIR from CONFNAME. 74 75 In the resulting list, the handle for root config file comes first, and the 76 nearest comes last. 77 The list may contains nil when no file was found for directories. 78 RESULT is used internally and normally should not be used." 79 (setq dir (expand-file-name dir)) 80 (let ((handle (editorconfig-core-handle (concat (file-name-as-directory dir) 81 confname))) 82 (parent (file-name-directory (directory-file-name dir)))) 83 (if (or (string= parent dir) 84 (and handle (editorconfig-core-handle-root-p handle))) 85 (cl-remove-if-not 'identity (cons handle result)) 86 (editorconfig-core--get-handles parent 87 confname 88 (cons handle result))))) 89 90 ;;;###autoload 91 (defun editorconfig-core-get-nearest-editorconfig (directory) 92 "Return path to .editorconfig file that is closest to DIRECTORY." 93 (when-let ((handle (car (last 94 (editorconfig-core--get-handles directory 95 ".editorconfig"))))) 96 (editorconfig-core-handle-path handle))) 97 98 ;;;###autoload 99 (defun editorconfig-core-get-properties (&optional file confname confversion) 100 "Get EditorConfig properties for FILE. 101 If FILE is not given, use currently visiting file. 102 Give CONFNAME for basename of config file other than .editorconfig. 103 If need to specify config format version, give CONFVERSION. 104 105 This function returns an alist of properties. Each element will 106 look like (KEY . VALUE)." 107 (let ((hash (editorconfig-core-get-properties-hash file confname confversion)) 108 (result nil)) 109 (maphash (lambda (key value) 110 (add-to-list 'result (cons (symbol-name key) value))) 111 hash) 112 result)) 113 114 (defun editorconfig-core--hash-merge (into update) 115 "Merge two hashes INTO and UPDATE. 116 117 This is a destructive function, hash INTO will be modified. 118 When the same key exists in both two hashes, values of UPDATE takes precedence." 119 (maphash (lambda (key value) (puthash key value into)) update) 120 into) 121 122 ;;;###autoload 123 (defun editorconfig-core-get-properties-hash (&optional file confname confversion) 124 "Get EditorConfig properties for FILE. 125 If FILE is not given, use currently visiting file. 126 Give CONFNAME for basename of config file other than .editorconfig. 127 If need to specify config format version, give CONFVERSION. 128 129 This function is almost same as `editorconfig-core-get-properties', but returns 130 hash object instead." 131 (setq file 132 (expand-file-name (or file 133 buffer-file-name 134 (error "FILE is not given and `buffer-file-name' is nil")))) 135 (setq confname (or confname ".editorconfig")) 136 (setq confversion (or confversion "0.12.0")) 137 (let ((result (make-hash-table))) 138 (dolist (handle (editorconfig-core--get-handles (file-name-directory file) 139 confname)) 140 (editorconfig-core--hash-merge result 141 (editorconfig-core-handle-get-properties-hash handle 142 file))) 143 144 ;; Downcase known boolean values 145 (dolist (key '( end_of_line indent_style indent_size insert_final_newline 146 trim_trailing_whitespace charset)) 147 (when-let ((val (gethash key result))) 148 (puthash key (downcase val) result))) 149 150 ;; Add indent_size property 151 (let ((v-indent-size (gethash 'indent_size result)) 152 (v-indent-style (gethash 'indent_style result))) 153 (when (and (not v-indent-size) 154 (string= v-indent-style "tab") 155 ;; If VERSION < 0.9.0, indent_size should have no default value 156 (version<= "0.9.0" 157 confversion)) 158 (puthash 'indent_size 159 "tab" 160 result))) 161 ;; Add tab_width property 162 (let ((v-indent-size (gethash 'indent_size result)) 163 (v-tab-width (gethash 'tab_width result))) 164 (when (and v-indent-size 165 (not v-tab-width) 166 (not (string= v-indent-size "tab"))) 167 (puthash 'tab_width v-indent-size result))) 168 ;; Update indent-size property 169 (let ((v-indent-size (gethash 'indent_size result)) 170 (v-tab-width (gethash 'tab_width result))) 171 (when (and v-indent-size 172 v-tab-width 173 (string= v-indent-size "tab")) 174 (puthash 'indent_size v-tab-width result))) 175 176 result)) 177 178 (provide 'editorconfig-core) 179 ;;; editorconfig-core.el ends here