dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

editorconfig-core.el (7230B)


      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 (eval-when-compile
     72   (require 'subr-x))
     73 
     74 
     75 (defun editorconfig-core--get-handles (dir confname &optional result)
     76   "Get list of EditorConfig handlers for DIR from CONFNAME.
     77 
     78 In the resulting list, the handle for root config file comes first, and the
     79 nearest comes last.
     80 The list may contains nil when no file was found for directories.
     81 RESULT is used internally and normally should not be used."
     82   (setq dir (expand-file-name dir))
     83   (let ((handle (editorconfig-core-handle (concat (file-name-as-directory dir)
     84                                                   confname)))
     85         (parent (file-name-directory (directory-file-name dir))))
     86     (if (or (string= parent dir)
     87             (and handle (editorconfig-core-handle-root-p handle)))
     88         (cl-remove-if-not 'identity (cons handle result))
     89       (editorconfig-core--get-handles parent
     90                                       confname
     91                                       (cons handle result)))))
     92 
     93 ;;;###autoload
     94 (defun editorconfig-core-get-nearest-editorconfig (directory)
     95   "Return path to .editorconfig file that is closest to DIRECTORY."
     96   (when-let* ((handle (car (last
     97                            (editorconfig-core--get-handles directory
     98                                                            ".editorconfig")))))
     99     (editorconfig-core-handle-path handle)))
    100 
    101 ;;;###autoload
    102 (defun editorconfig-core-get-properties (&optional file confname confversion)
    103   "Get EditorConfig properties for FILE.
    104 If FILE is not given, use currently visiting file.
    105 Give CONFNAME for basename of config file other than .editorconfig.
    106 If need to specify config format version, give CONFVERSION.
    107 
    108 This function returns an alist of properties.  Each element will
    109 look like (KEY . VALUE)."
    110   (let ((hash (editorconfig-core-get-properties-hash file confname confversion))
    111         (result nil))
    112     (maphash (lambda (key value)
    113                (add-to-list 'result (cons (symbol-name key) value)))
    114              hash)
    115     result))
    116 
    117 (defun editorconfig-core--hash-merge (into update)
    118   "Merge two hashes INTO and UPDATE.
    119 
    120 This is a destructive function, hash INTO will be modified.
    121 When the same key exists in both two hashes, values of UPDATE takes precedence."
    122   (maphash (lambda (key value) (puthash key value into)) update)
    123   into)
    124 
    125 ;;;###autoload
    126 (defun editorconfig-core-get-properties-hash (&optional file confname confversion)
    127   "Get EditorConfig properties for FILE.
    128 If FILE is not given, use currently visiting file.
    129 Give CONFNAME for basename of config file other than .editorconfig.
    130 If need to specify config format version, give CONFVERSION.
    131 
    132 This function is almost same as `editorconfig-core-get-properties', but returns
    133 hash object instead."
    134   (setq file
    135         (expand-file-name (or file
    136                               buffer-file-name
    137                               (error "FILE is not given and `buffer-file-name' is nil"))))
    138   (setq confname (or confname ".editorconfig"))
    139   (setq confversion (or confversion "0.12.0"))
    140   (let ((result (make-hash-table)))
    141     (dolist (handle (editorconfig-core--get-handles (file-name-directory file)
    142                                                     confname))
    143       (editorconfig-core--hash-merge result
    144                                      (editorconfig-core-handle-get-properties-hash handle
    145                                                                                    file)))
    146 
    147     ;; Downcase known boolean values
    148     (dolist (key '( end_of_line indent_style indent_size insert_final_newline
    149                     trim_trailing_whitespace charset))
    150       (when-let* ((val (gethash key result)))
    151         (puthash key (downcase val) result)))
    152 
    153     ;; Add indent_size property
    154     (let ((v-indent-size (gethash 'indent_size result))
    155           (v-indent-style (gethash 'indent_style result)))
    156       (when (and (not v-indent-size)
    157                  (string= v-indent-style "tab")
    158                  ;; If VERSION < 0.9.0, indent_size should have no default value
    159                  (version<= "0.9.0"
    160                             confversion))
    161         (puthash 'indent_size
    162                  "tab"
    163                  result)))
    164     ;; Add tab_width property
    165     (let ((v-indent-size (gethash 'indent_size result))
    166           (v-tab-width (gethash 'tab_width result)))
    167       (when (and v-indent-size
    168                  (not v-tab-width)
    169                  (not (string= v-indent-size "tab")))
    170         (puthash 'tab_width v-indent-size result)))
    171     ;; Update indent-size property
    172     (let ((v-indent-size (gethash 'indent_size result))
    173           (v-tab-width (gethash 'tab_width result)))
    174       (when (and v-indent-size
    175                  v-tab-width
    176                  (string= v-indent-size "tab"))
    177         (puthash 'indent_size v-tab-width result)))
    178 
    179     result))
    180 
    181 (provide 'editorconfig-core)
    182 ;;; editorconfig-core.el ends here