Function: editorconfig-core-handle--parse-file

editorconfig-core-handle--parse-file is a byte-compiled function defined in editorconfig-core-handle.el.gz.

Signature

(editorconfig-core-handle--parse-file CONF)

Documentation

Parse EditorConfig file CONF.

This function returns a editorconfig-core-handle. If CONF is not found return nil.

Source Code

;; Defined in /usr/src/emacs/lisp/editorconfig-core-handle.el.gz
(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 ()))
        (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 "\\([^=: \n\t][^=:\n]*\\)[ \t]*[=:][ \t]*\\(.*?\\)[ \t]*$")
            (let ((key (downcase (string-trim (match-string 1))))
                  (value (match-string 2)))
              (if pattern
                  (push `(,key . ,value)
                        props)
                (push `(,key . ,value)
                      top-props))))

           (t (error "Error while reading config file: %s:%d:\n    %s\n"
                     conf (line-number-at-pos)
                     (buffer-substring-no-properties (line-beginning-position)
                                                     (line-end-position)))))
          (forward-line 1))
        (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)))))