Skip to content

Commit

Permalink
Merge pull request #182 from editorconfig/hackprops
Browse files Browse the repository at this point in the history
Add feature to hack properties before applying
  • Loading branch information
10sr authored Oct 25, 2018
2 parents b9254a0 + ad07b3b commit aaf96c5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ only blocks of `web-mode`: it can be achieved by adding following to your init.e

You can also define your own custom properties and enable them here.

### `editorconfig-hack-properties-functions`

A list of function to alter property values before applying them.

These functions will be run after loading \".editorconfig\" files and before
applying them to current buffer, so that you can alter some properties from
\".editorconfig\" before they take effect.

For example, Makefiles always use tab characters for indentation: you can
overwrite \"indent_style\" property when current `major-mode` is a
`makefile-mode` with following code:

``` emacs-lisp
(add-hook 'editorconfig-hack-properties-functions
'(lambda (props)
(when (derived-mode-p makefile-mode)
(puthash 'indent_style \"tab\" props))))
```

### `editorconfig-indentation-alist`

Alist of indentation setting methods by modes.
Expand Down
21 changes: 21 additions & 0 deletions doc/editorconfig.texi
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ precedence.
@anchor{#customize}
@menu
* editorconfig-after-apply-functions::
* editorconfig-hack-properties-functions::
* editorconfig-indentation-alist::
* editorconfig-exec-path::
* editorconfig-get-properties-function::
Expand All @@ -155,6 +156,26 @@ achieved by adding following to your init.el:

You can also define your own custom properties and enable them here.

@node editorconfig-hack-properties-functions
@subsection @code{editorconfig-hack-properties-functions}
@anchor{#editorconfig-hack-properties-functions}
A list of function to alter property values before applying them.

These functions will be run after loading ".editorconfig" files and
before applying them to current buffer, so that you can alter some
properties from ".editorconfig" before they take effect.

For example, Makefiles always use tab characters for indentation: you
can overwrite "indent_style" property when current @code{major-mode} is
a @code{makefile-mode} with following code:

@verbatim
(add-hook 'editorconfig-hack-properties-functions
'(lambda (props)
(when (derived-mode-p makefile-mode)
(puthash 'indent_style \"tab\" props))))
@end verbatim

@node editorconfig-indentation-alist
@subsection @code{editorconfig-indentation-alist}
@anchor{#editorconfig-indentation-alist}
Expand Down
25 changes: 25 additions & 0 deletions editorconfig.el
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ show line numbers on the left:
'editorconfig-after-apply-functions
"0.7.14")

(defcustom editorconfig-hack-properties-functions ()
"A list of function to alter property values before applying them.
These functions will be run after loading \".editorconfig\" files and before
applying them to current buffer, so that you can alter some properties from
\".editorconfig\" before they take effect.
For example, Makefiles always use tab characters for indentation: you can
overwrite \"indent_style\" property when current `major-mode' is a
`makefile-mode' with following code:
(add-hook 'editorconfig-hack-properties-functions
'(lambda (props)
(when (derived-mode-p makefile-mode)
(puthash 'indent_style \"tab\" props))))"
:type 'hook
:group 'editorconfig)

(defcustom editorconfig-indentation-alist
;; For contributors: Sort modes in alphabetical order, please :)
'((apache-mode apache-indent-level)
Expand Down Expand Up @@ -588,6 +606,13 @@ applies available properties."
(error "Invalid editorconfig-get-properties-function value"))
(let ((props (funcall editorconfig-get-properties-function)))
(progn
(condition-case err
(run-hook-with-args 'editorconfig-hack-properties-functions props)
(error
(display-warning 'editorconfig-hack-properties-functions
(concat (error-message-string err)
". Abort running hook.")
:warning)))
(setq editorconfig-properties-hash props)
(editorconfig-set-indentation (gethash 'indent_style props)
(gethash 'indent_size props)
Expand Down
19 changes: 15 additions & 4 deletions ert-tests/editorconfig.el
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@
(editorconfig-mode 1)

(with-visit-file (concat editorconfig-secondary-ert-dir
"2_space.el")
"2_space.el")
(should (eq lisp-indent-offset 2)))

(let ((editorconfig-lisp-use-default-indent t))
(with-visit-file (concat editorconfig-secondary-ert-dir
"2_space.el")
"2_space.el")
(should (eq lisp-indent-offset nil))))

(let ((editorconfig-lisp-use-default-indent 2))
(with-visit-file (concat editorconfig-secondary-ert-dir
"2_space.el")
"2_space.el")
(should (eq lisp-indent-offset nil))))

(let ((editorconfig-lisp-use-default-indent 4))
(with-visit-file (concat editorconfig-secondary-ert-dir
"2_space.el")
"2_space.el")
(should (eq lisp-indent-offset 2))))
(editorconfig-mode -1))

Expand Down Expand Up @@ -107,3 +107,14 @@
(should (eq major-mode 'perl-mode))
(should (eq perl-indent-level 5)))
(editorconfig-mode -1))

(ert-deftest test-hack-properties-functions nil
(editorconfig-mode 1)
(add-hook 'editorconfig-hack-properties-functions
(lambda (props)
(puthash 'indent_size "5" props)))
(with-visit-file (concat editorconfig-ert-dir
"4_space.py")
(should (eq python-indent-offset 5)))
(setq editorconfig-hack-properties-functions nil)
(editorconfig-mode -1))

0 comments on commit aaf96c5

Please sign in to comment.