branch: scratch/editorconfig-cc commit cf6786403ac486bcea5e9feb9eb419852564d407 Author: 10sr <8slashes+...@gmail.com> Commit: Stefan Monnier <monn...@iro.umontreal.ca>
Change symbol prefix: edconf -> editorconfig --- editorconfig.el | 141 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 102 insertions(+), 39 deletions(-) diff --git a/editorconfig.el b/editorconfig.el index 488b6aa882..3058144c0c 100644 --- a/editorconfig.el +++ b/editorconfig.el @@ -38,14 +38,14 @@ ;;; Code: -(defcustom edconf-exec-path +(defcustom editorconfig-exec-path "editorconfig" "EditorConfig command" :type 'string :group 'editorconfig) -(defcustom edconf-get-properties-function - 'edconf-get-properties-from-exec +(defcustom editorconfig-get-properties-function + 'editorconfig-get-properties-from-exec "Function to get EditorConofig properties for current buffer. This function will be called with no argument and should return a hash object containing properties, or nil if any core program is not available. @@ -54,7 +54,7 @@ property values as values." :type 'function :group 'editorconfig) -(defcustom edconf-custom-hooks () +(defcustom editorconfig-custom-hooks () "A list of custom hooks after loading common EditorConfig settings Each element in this list is a hook function. This hook function takes one @@ -65,7 +65,7 @@ The hook does not have to be coding style related; you can add whatever functionality you want. For example, the following is an example to add a new property emacs_linum to decide whether to show line numbers on the left -(add-to-list 'edconf-custom-hooks +(add-to-list 'editorconfig-custom-hooks '(lambda (props) (let ((show-line-num (gethash 'emacs_linum props))) (cond ((equal show-line-num \"true\") (linum-mode 1)) @@ -75,73 +75,136 @@ property emacs_linum to decide whether to show line numbers on the left :type '(lambda (properties) (body)) :group 'editorconfig) -(defun edconf-string-integer-p (string) +(defcustom editorconfig-indentation-alist + '((awk-mode c-basic-offset) + (c++-mode c-basic-offset) + (c-mode c-basic-offset) + (cmake-mode cmake-tab-width) + (coffee-mode coffee-tab-width) + (cperl-mode cperl-indent-level) + (css-mode css-indent-offset) + (emacs-lisp-mode lisp-indent-offset) + (erlang-mode erlang-indent-level) + (groovy-mode c-basic-offset) + (haskell-mode haskell-indent-spaces + haskell-indent-offset + shm-indent-spaces) + (idl-mode c-basic-offset) + (java-mode c-basic-offset) + (js-mode js-indent-level) + (js2-mode js2-basic-offset) + (js3-mode js3-indent-level) + (json-mode js-indent-level) + (latex-mode . editorconfig-set-indentation/latex-mode) + (lisp-mode lisp-indent-offset) + (livescript-mode livescript-tab-width) + (mustache-mode mustache-basic-offset) + (nxml-mode nxml-child-indent (nxml-attribute-indent . 2)) + (objc-mode c-basic-offset) + (perl-mode perl-indent-level) + (pike-mode c-basic-offset) + (puppet-mode puppet-indent-level) + (python-mode . editorconfig-set-indentation/python-mode) + (ruby-mode ruby-indent-level) + (scala-mode scala-indent:step) + (sgml-mode sgml-basic-offset) + (sh-mode sh-basic-offset sh-indentation) + (web-mode (web-mode-indent-style . (lambda (size) 2)) + web-mode-markup-indent-offset + web-mode-css-indent-offset + web-mode-code-indent-offset + web-mode-script-padding + web-mode-style-padding) + (yaml-mode yaml-indent-offset)) + "Alist of indentation setting methods by modes. + +Each element looks like (MODE . FUNCTION) or (MODE . INDENT-SPEC-LIST). + +If FUNCTION is provided, it will be called when setting the indentation. The +indent size will be passed. + +If INDENT-SPEC-LIST is provided, each element of it must have one of the +following forms: + + 1. VARIABLE + + It means (VARIABLE . 1). + + 2. (VARIABLE . SPEC) + + Setting VARIABLE according to the type of SPEC: + + - Integer + + The value is (* SPEC INDENT-SIZE); + + - Function + + The value is (funcall SPEC INDENT-SIZE); + + - Any other type. + + The value is SPEC. + +NOTE: Only the **buffer local** value of VARIABLE will be set." + :type '(alist :key-type symbol :value-type sexp) + :risky t + :group 'editorconfig) + +(defun editorconfig-string-integer-p (string) "Whether a string representing integer" (if (stringp string) (string-match-p "\\`[0-9]+\\'" string) nil)) -(defun edconf-set-indentation (style &optional size tab_width) - (if (edconf-string-integer-p size) +(defun editorconfig-set-indentation (style &optional size tab_width) + (if (editorconfig-string-integer-p size) (setq size (string-to-number size)) (when (not (equal size "tab")) (setq size nil))) - (setq web-mode-indent-style 2) - LaTeX-indent-level size - LaTeX-item-indent size - TeX-brace-indent-level size - cmake-tab-width size - js-indent-level size - lisp-indent-offset size - ruby-indent-level size - sh-indentation size - web-mode-markup-indent-offset size - web-mode-css-indent-offset size - web-mode-code-indent-offset size - ;(make-local-variable 'sgml-basic-offset) size ) -(defun edconf-set-line-ending (end-of-line) +(defun editorconfig-set-line-ending (end-of-line) ) -(defun edconf-set-line-length (length) +(defun editorconfig-set-line-length (length) "set the max line length (fill-column)" - (when (edconf-string-integer-p length) + (when (editorconfig-string-integer-p length) (set-fill-column (string-to-number length)))) -(defun edconf-get-properties () +(defun editorconfig-get-properties () ) -(defun edconf-parse-properties (props-string) +(defun editorconfig-parse-properties (props-string) ) -(defun edconf-get-properties-from-exec () - "Get EditorConfig properties of current buffer by calling `edconf-exec-path'." - (if (executable-find edconf-exec-path) - (edconf-parse-properties (edconf-get-properties)) +(defun editorconfig-get-properties-from-exec () + "Get EditorConfig properties of current buffer by calling `editorconfig-exec-path'." + (if (executable-find editorconfig-exec-path) + (editorconfig-parse-properties (editorconfig-get-properties)) (display-warning :error "Unable to find editorconfig executable.") nil)) -(defun edconf-find-file-hook () - (let ((props (and (functionp edconf-get-properties-function) - (funcall edconf-get-properties-function)))) +(defun editorconfig-find-file-hook () + (let ((props (and (functionp editorconfig-get-properties-function) + (funcall editorconfig-get-properties-function)))) (if props (progn - (edconf-set-line-length (gethash 'max_line_length props)) - (dolist (hook edconf-custom-hooks) + (editorconfig-set-line-length (gethash 'max_line_length props)) + (dolist (hook editorconfig-custom-hooks) (funcall hook props))) (display-warning :error "EditorConfig core program is not available. Styles will not be applied.")))) ;;;###autoload -(define-minor-mode edconf-mode +(define-minor-mode editorconfig-mode "Toggle EditorConfig feature." :global t :lighter "" - (if edconf-mode + (if editorconfig-mode (add-hook 'find-file-hook - 'edconf-find-file-hook) + 'editorconfig-find-file-hook) (remove-hook 'find-file-hook - 'edconf-find-file-hook))) + 'editorconfig-find-file-hook))) ;;;###autoload (add-to-list 'auto-mode-alist '("/\\.editorconfig\\'" . conf-unix-mode))