Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to hack properties before applying #182

Merged
merged 4 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))