forked from editorconfig/editorconfig-emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editorconfig.el
393 lines (341 loc) · 14.5 KB
/
editorconfig.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
;;; editorconfig.el --- EditorConfig Emacs Plugin
;; Copyright (C) 2011-2016 EditorConfig Team
;; Author: EditorConfig Team <editorconfig@googlegroups.com>
;; Version: 0.7.2
;; URL: https://github.com/editorconfig/editorconfig-emacs#readme
;; Package-Requires: ((editorconfig-core "0.6.2"))
;; See
;; https://github.com/editorconfig/editorconfig-emacs/graphs/contributors
;; or the CONTRIBUTORS file for the list of contributors.
;; This file is part of EditorConfig Emacs Plugin.
;; EditorConfig Emacs Plugin is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or (at your
;; option) any later version.
;; EditorConfig Emacs Plugin is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
;; Public License for more details.
;; You should have received a copy of the GNU General Public License along with
;; EditorConfig Emacs Plugin. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; EditorConfig helps developers define and maintain consistent
;; coding styles between different editors and IDEs.
;; The EditorConfig project consists of a file format for defining
;; coding styles and a collection of text editor plugins that enable
;; editors to read the file format and adhere to defined styles.
;; EditorConfig files are easily readable and they work nicely with
;; version control systems.
;;; Code:
(declare-function editorconfig-core-get-properties-hash
"editorconfig-core"
nil)
(defcustom editorconfig-exec-path
"editorconfig"
"EditorConfig executable name.
This executable is invoked by `editorconfig-call-editorconfig-exec'."
:type 'string
:group 'editorconfig)
(define-obsolete-variable-alias
'edconf-exec-path
'editorconfig-exec-path
"0.5")
(defcustom editorconfig-get-properties-function
'editorconfig-get-properties
"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.
The hash object should have symbols of property names as keys and strings of
property values as values."
:type 'function
:group 'editorconfig)
(define-obsolete-variable-alias
'edconf-get-properties-function
'editorconfig-get-properties-function
"0.5")
(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
parameter, which is a property hash table. The value of properties can be
obtained through gethash function.
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-hook 'editorconfig-custom-hooks
'(lambda (props)
(let ((show-line-num (gethash 'emacs_linum props)))
(cond ((equal show-line-num \"true\") (linum-mode 1))
((equal show-line-num \"false\") (linum-mode 0))))))"
:type 'hook
:group 'editorconfig)
(define-obsolete-variable-alias
'edconf-custom-hooks
'editorconfig-custom-hooks
"0.5")
(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)
(ess-mode ess-indent-offset)
(groovy-mode c-basic-offset)
(haskell-mode haskell-indent-spaces
haskell-indent-offset
shm-indent-spaces)
(idl-mode c-basic-offset)
(jade-mode jade-tab-width)
(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)
(lua-mode lua-indent-level)
(matlab-mode matlab-indent-level)
(mustache-mode mustache-basic-offset)
(nginx-mode nginx-indent-level)
(nxml-mode nxml-child-indent (nxml-attribute-indent . 2))
(objc-mode c-basic-offset)
(octave-mode octave-block-offset)
(perl-mode perl-indent-level)
(pike-mode c-basic-offset)
(ps-mode ps-mode-tab)
(puppet-mode puppet-indent-level)
(python-mode . editorconfig-set-indentation/python-mode)
(ruby-mode ruby-indent-level)
(scala-mode scala-indent:step)
(scss-mode css-indent-offset)
(sgml-mode sgml-basic-offset)
(sh-mode sh-basic-offset sh-indentation)
(slim-mode slim-indent-offset)
(tcl-mode tcl-indent-level
tcl-continued-indent-level)
(typescript-mode typescript-indent-level)
(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)
(define-obsolete-variable-alias
'edconf-indentation-alist
'editorconfig-indentation-alist
"0.5")
(defun editorconfig-string-integer-p (string)
"Return non-nil if STRING represents integer."
(if (stringp string)
(string-match-p "\\`[0-9]+\\'" string)
nil))
(defun editorconfig-set-indentation/python-mode (size)
"Set `python-mode' indent size to SIZE."
(set (make-local-variable (if (or (> emacs-major-version 24)
(and (= emacs-major-version 24)
(>= emacs-minor-version 3)))
'python-indent-offset
'python-indent))
size)
;; For https://launchpad.net/python-mode
(when (boundp 'py-indent-offset)
(set (make-local-variable 'py-indent-offset) size)))
(defun editorconfig-set-indentation/latex-mode (size)
"Set `latex-mode' indent size to SIZE."
(set (make-local-variable 'tex-indent-basic) size)
(set (make-local-variable 'tex-indent-item) size)
(set (make-local-variable 'tex-indent-arg) (* 2 size))
;; For AUCTeX
(when (boundp 'TeX-brace-indent-level)
(set (make-local-variable 'TeX-brace-indent-level) size))
(when (boundp 'LaTeX-indent-level)
(set (make-local-variable 'LaTeX-indent-level) size))
(when (boundp 'LaTeX-item-indent)
(set (make-local-variable 'LaTeX-item-indent) (- size))))
(defun editorconfig-set-indentation (style &optional size tab_width)
"Set indentation type from STYLE, SIZE and TAB_WIDTH."
(make-local-variable 'indent-tabs-mode)
(make-local-variable 'tab-width)
(if (editorconfig-string-integer-p size)
(setq size (string-to-number size))
(when (not (equal size "tab")) (setq size nil)))
(setq tab-width (cond (tab_width (string-to-number tab_width))
((numberp size) size)
(t tab-width)))
(when (equal size "tab")
(setq size tab-width))
(cond ((equal style "space")
(setq indent-tabs-mode nil))
((equal style "tab")
(setq indent-tabs-mode t)))
(when size
(when (featurep 'evil)
(set (make-local-variable 'evil-shift-width) size))
(let ((parent major-mode)
entry)
;; Find the closet parent mode of `major-mode' in
;; `editorconfig-indentation-alist'.
(while (and (not (setq entry (assoc parent editorconfig-indentation-alist)))
(setq parent (get parent 'derived-mode-parent))))
(when entry
(let ((fn-or-list (cdr entry)))
(cond ((functionp fn-or-list) (funcall fn-or-list size))
((listp fn-or-list)
(dolist (elem fn-or-list)
(cond ((symbolp elem) (set (make-local-variable elem) size))
((consp elem)
(let ((spec (cdr elem)))
(set (make-local-variable (car elem))
(cond ((functionp spec) (funcall spec size))
((integerp spec) (* spec size))
(t spec))))))))))))))
(defun editorconfig-set-coding-system (end-of-line charset)
"Set buffer coding system by END-OF-LINE and CHARSET."
(let ((eol (cond
((equal end-of-line "lf") 'undecided-unix)
((equal end-of-line "cr") 'undecided-mac)
((equal end-of-line "crlf") 'undecided-dos)
(t 'undecided)))
(cs (cond
((equal charset "latin1") 'iso-latin-1)
((equal charset "utf-8") 'utf-8)
((equal charset "utf-8-bom") 'utf-8-with-signature)
((equal charset "utf-16be") 'utf-16be)
((equal charset "utf-16le") 'utf-16le)
(t 'undecided))))
(set-buffer-file-coding-system (merge-coding-systems
cs
eol)
nil t)))
(defun editorconfig-set-trailing-nl (final-newline)
"Set up requiring final newline by FINAL-NEWLINE."
(cond
((equal final-newline "true")
;; keep prefs around how/when the nl is added, if set - otherwise add on save
(set (make-local-variable 'require-final-newline) (or require-final-newline t))
(set (make-local-variable 'mode-require-final-newline) (or mode-require-final-newline t)))
((equal final-newline "false")
;; FIXME: Add functionality for actually REMOVING any trailing newlines here!
;; (rather than just making sure we don't automagically ADD a new one)
(set (make-local-variable 'require-final-newline) nil)
(set (make-local-variable 'mode-require-final-newline) nil))))
(defun editorconfig-set-trailing-ws (trim-trailing-ws)
"Set up trimming of trailing whitespace at end of lines by TRIM-TRAILING-WS."
(make-local-variable 'write-file-functions) ;; just current buffer
(when (equal trim-trailing-ws "true")
;; when true we push delete-trailing-whitespace (emacs > 21)
;; to write-file-functions
(add-to-list
'write-file-functions
'delete-trailing-whitespace))
(when (equal trim-trailing-ws "false")
;; when false we remove every delete-trailing-whitespace
;; from write-file-functions
(setq
write-file-functions
(delete
'delete-trailing-whitespace
write-file-functions))))
(defun editorconfig-set-line-length (length)
"Set the max line length (fill-column) to LENGTH."
(when (editorconfig-string-integer-p length)
(set-fill-column (string-to-number length))))
(defun editorconfig-call-editorconfig-exec ()
"Call EditorConfig core and return output."
(let ((filename (buffer-file-name)))
(with-temp-buffer
(setq default-directory "/")
(if (eq 0
(call-process editorconfig-exec-path nil t nil filename))
(buffer-string)
(error (buffer-string))))))
(defun editorconfig-parse-properties (props-string)
"Create properties hash table from PROPS-STRING."
(let (props-list properties)
(setq props-list (split-string props-string "\n")
properties (make-hash-table :test 'equal))
(dolist (prop props-list properties)
(let ((key-val (split-string prop " *= *")))
(when (> (length key-val) 1)
(let ((key (intern (car key-val)))
(val (mapconcat 'identity (cdr key-val) "")))
(puthash key val 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-call-editorconfig-exec))
(error "Unable to find editorconfig executable")))
(defun editorconfig-get-properties ()
"Get EditorConfig properties of current buffer.
It calls `editorconfig-get-properties-from-exec' if
`editorconfig-exec-path` is found, otherwise
`editorconfig-core-get-properties-hash'."
(if (executable-find editorconfig-exec-path)
(editorconfig-get-properties-from-exec)
(editorconfig-core-get-properties-hash)))
;;;###autoload
(defun editorconfig-apply ()
"Apply EditorConfig properties for current buffer."
(interactive)
(when buffer-file-name
(condition-case err
(progn
(unless (functionp editorconfig-get-properties-function)
(error "Invalid editorconfig-get-properties-function value"))
(let ((props (funcall editorconfig-get-properties-function)))
(progn
(editorconfig-set-indentation (gethash 'indent_style props)
(gethash 'indent_size props)
(gethash 'tab_width props))
(editorconfig-set-coding-system
(gethash 'end_of_line props)
(gethash 'charset props))
(editorconfig-set-trailing-nl (gethash 'insert_final_newline props))
(editorconfig-set-trailing-ws (gethash 'trim_trailing_whitespace props))
(editorconfig-set-line-length (gethash 'max_line_length props))
(run-hook-with-args 'editorconfig-custom-hooks props))))
(error
(display-warning 'editorconfig
(concat (error-message-string err)
". Styles will not be applied.")
:error)))))
;;;###autoload
(define-minor-mode editorconfig-mode
"Toggle EditorConfig feature."
:global t
:lighter ""
(dolist (hook (list
'find-file-hook
'after-change-major-mode-hook))
(if editorconfig-mode
(add-hook hook 'editorconfig-apply)
(remove-hook hook 'editorconfig-apply))))
;;;###autoload
(add-to-list 'auto-mode-alist '("/\\.editorconfig\\'" . conf-unix-mode))
(provide 'editorconfig)
;;; editorconfig.el ends here