-
Notifications
You must be signed in to change notification settings - Fork 21
/
web-beautify.el
195 lines (155 loc) · 6.87 KB
/
web-beautify.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
;;; web-beautify.el --- Format HTML, CSS and JavaScript/JSON
;; Copyright (C) 2013-2016 Yasuyuki Oka and web-beautify contributors
;; Author: Yasuyuki Oka <[email protected]>
;; Version: 0.3.2
;; URL: https://github.com/yasuyk/web-beautify
;; This program 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.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Add the following to your Emacs init file.
;; (require 'web-beautify) ;; Not necessary if using ELPA package
;; (eval-after-load 'js2-mode
;; '(define-key js2-mode-map (kbd "C-c b") 'web-beautify-js))
;; (eval-after-load 'json-mode
;; '(define-key json-mode-map (kbd "C-c b") 'web-beautify-js))
;; (eval-after-load 'sgml-mode
;; '(define-key html-mode-map (kbd "C-c b") 'web-beautify-html))
;; (eval-after-load 'css-mode
;; '(define-key css-mode-map (kbd "C-c b") 'web-beautify-css))
;; If you want to automatically format before saving a file,
;; add the following hook to your Emacs configuration:
;; (eval-after-load 'js2-mode
;; '(add-hook 'js2-mode-hook
;; (lambda ()
;; (add-hook 'before-save-hook 'web-beautify-js-buffer t t))))
;; (eval-after-load 'json-mode
;; '(add-hook 'json-mode-hook
;; (lambda ()
;; (add-hook 'before-save-hook 'web-beautify-js-buffer t t))))
;; (eval-after-load 'sgml-mode
;; '(add-hook 'html-mode-hook
;; (lambda ()
;; (add-hook 'before-save-hook 'web-beautify-html-buffer t t))))
;; (eval-after-load 'css-mode
;; '(add-hook 'css-mode-hook
;; (lambda ()
;; (add-hook 'before-save-hook 'web-beautify-css-buffer t t))))
;; For more information, See URL https://github.com/yasuyk/web-beautify.
;;; Code:
(defvar web-beautify-html-program "html-beautify"
"The executable to use for formatting HTML.")
(defvar web-beautify-css-program "css-beautify"
"The executable to use for formatting CSS.")
(defvar web-beautify-js-program "js-beautify"
"The executable to use for formatting JavaScript and JSON.")
(defconst web-beautify-args '("-f" "-"))
(defun web-beautify-command-not-found-message (program)
"Construct a message about PROGRAM not found."
(format
"%s not found. Install it with `npm -g install js-beautify`."
program))
(defun web-beautify-format-error-message (buffer-name)
"Construct a format error message with BUFFER-NAME."
(format
"Could not apply web-beautify. See %s to for details."
buffer-name))
(defun web-beautify-get-shell-command (program)
"Join PROGRAM with the constant js-beautify args."
(mapconcat 'identity (append (list program) web-beautify-args) " "))
(declare-function web-mode-reload "ext:web-mode")
(declare-function js2-mode "ext:js2-mode")
(defun web-beautify-reload ()
"Reload mode to activate faces."
(deactivate-mark)
(cond ((eq major-mode 'web-mode)
(web-mode-reload))
((eq major-mode 'js2-mode)
(js2-mode))))
(defun web-beautify-format-region (program beginning end)
"By PROGRAM, format each line in the BEGINNING .. END region."
;; Check that js-beautify is installed.
(if (executable-find program)
(let* ((output-buffer-name "*Web Beautify Errors*")
(output-buffer (get-buffer-create output-buffer-name))
;; Stash the previous point/window positions so they can be
;; reclaimed after the buffer is replaced. Otherwise there is a
;; disturbing "jump" to vertically-center point after being
;; momentarily bounced to the top of the file.
(previous-point (point))
(previous-window-start (window-start))
(shell-command (web-beautify-get-shell-command program)))
;; Run the command.
(if (zerop (shell-command-on-region beginning end shell-command (current-buffer) t output-buffer t))
(progn
;; Reclaim position for a smooth transition.
(goto-char previous-point)
(set-window-start nil previous-window-start)
(message "Applied web-beautify.")
(web-beautify-reload)
(kill-buffer output-buffer))
;; Unfortunately an error causes the buffer to be replaced with
;; emptiness... so undo that. Kind of an ugly hack. But a
;; properly-configured web-beautify shouldn't encounter this much, if
;; ever.
(undo)
(message (web-beautify-format-error-message output-buffer-name))))
(message (web-beautify-command-not-found-message program))))
(defun web-beautify-format-buffer (program)
"By PROGRAM, format current buffer with EXTENSTION."
(web-beautify-format-region program (point-min) (point-max)))
;;;###autoload
(defun web-beautify-html ()
"Format region if active, otherwise the current buffer.
Formatting is done according to the html-beautify command."
(interactive)
(if (use-region-p)
(web-beautify-format-region
web-beautify-html-program
(region-beginning) (region-end))
(web-beautify-html-buffer)))
;;;###autoload
(defun web-beautify-html-buffer ()
"Format the current buffer according to the html-beautify command."
(web-beautify-format-buffer web-beautify-html-program))
;;;###autoload
(defun web-beautify-css ()
"Format region if active, otherwise the current buffer.
Formatting is done according to the css-beautify command."
(interactive)
(if (use-region-p)
(web-beautify-format-region
web-beautify-css-program
(region-beginning) (region-end))
(web-beautify-css-buffer)))
;;;###autoload
(defun web-beautify-css-buffer ()
"Format the current buffer according to the css-beautify command."
(web-beautify-format-buffer web-beautify-css-program))
;;;###autoload
(defun web-beautify-js ()
"Format region if active, otherwise the current buffer.
Formatting is done according to the js-beautify command."
(interactive)
(if (use-region-p)
(web-beautify-format-region
web-beautify-js-program
(region-beginning) (region-end))
(web-beautify-js-buffer)))
;;;###autoload
(defun web-beautify-js-buffer ()
"Format the current buffer according to the js-beautify command."
(web-beautify-format-buffer web-beautify-js-program))
(provide 'web-beautify)
;; Local Variables:
;; coding: utf-8
;; eval: (checkdoc-minor-mode 1)
;; End:
;;; web-beautify.el ends here